Search in sources :

Example 31 with ValidationException

use of com.google.copybara.exception.ValidationException in project copybara by google.

the class WorkflowTest method nonReversibleButCheckReverseSet.

@Test
public void nonReversibleButCheckReverseSet() throws Exception {
    origin.singleFileChange(0, "one commit", "foo.txt", "1").singleFileChange(1, "one commit", "test.txt", "1\nTRANSFORMED42");
    Workflow<?, ?> workflow = changeRequestWorkflow("0");
    ValidationException e = assertThrows(ValidationException.class, () -> workflow.run(workdir, ImmutableList.of("1")));
    assertThat(e).hasMessageThat().isEqualTo("Workflow 'default' is not reversible");
    console().assertThat().onceInLog(MessageType.PROGRESS, "Checking that the transformations can be reverted");
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) Test(org.junit.Test)

Example 32 with ValidationException

use of com.google.copybara.exception.ValidationException in project copybara by google.

the class WorkflowTest method changeRequestSmartPrune_manualBaseline.

@Test
public void changeRequestSmartPrune_manualBaseline() throws Exception {
    smartPrune = true;
    // For now we don't support smart_prune with the flag since the flag refers to the destination
    // baseline and for smart_prune we need the origin revision. We might revisit this if it
    // if requested by users
    options.workflowOptions.changeBaseline = "42";
    ValidationException e = assertThrows(ValidationException.class, () -> checkChangeRequestSmartPrune());
    assertThat(e).hasMessageThat().contains("smart_prune is not compatible with --change-request-parent");
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) Test(org.junit.Test)

Example 33 with ValidationException

use of com.google.copybara.exception.ValidationException in project copybara by google.

the class WorkflowTest method reversibleCheckSymlinkError.

@Test
public void reversibleCheckSymlinkError() throws Exception {
    Path someRoot = Files.createTempDirectory("someRoot");
    Path originPath = someRoot.resolve("origin");
    Files.createDirectories(originPath);
    GitRepository origin = GitRepository.newRepo(/*verbose*/
    true, originPath, getGitEnv()).init();
    String primaryBranch = origin.getPrimaryBranch();
    String config = "core.workflow(\n" + "    name = 'default',\n" + String.format("    origin = git.origin( url = 'file://%s', ref = '%s'),\n", origin.getWorkTree(), primaryBranch) + "    destination = testing.destination(),\n" + "    authoring = " + authoring + ",\n" + "    origin_files = glob(['included/**']),\n" + "    reversible_check = True,\n" + "    mode = '" + WorkflowMode.SQUASH + "',\n" + ")\n";
    Migration workflow = loadConfig(config).getMigration("default");
    Path included = originPath.resolve("included");
    Files.createDirectory(included);
    Files.write(originPath.resolve("included/foo.txt"), "a".getBytes(UTF_8));
    Path fileOutsideCheckout = someRoot.resolve("file_outside_checkout");
    Files.write(fileOutsideCheckout, "THE CONTENT".getBytes(UTF_8));
    Files.createSymbolicLink(included.resolve("symlink"), included.relativize(fileOutsideCheckout));
    origin.add().files("included/foo.txt").run();
    origin.add().files("included/symlink").run();
    origin.commit("Foo <foo@bara.com>", ZonedDateTime.now(ZoneId.systemDefault()), "A commit");
    ValidationException expected = assertThrows(ValidationException.class, () -> workflow.run(workdir, ImmutableList.of()));
    assertThat(expected.getMessage()).matches("" + "Failed to perform reversible check of transformations due to symlink '.*' that " + "points outside the checkout dir. Consider removing this symlink from your " + "origin_files or, alternatively, set reversible_check = False in your " + "workflow.");
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) GitRepository(com.google.copybara.git.GitRepository) ValidationException(com.google.copybara.exception.ValidationException) Migration(com.google.copybara.config.Migration) Test(org.junit.Test)

Example 34 with ValidationException

use of com.google.copybara.exception.ValidationException in project copybara by google.

the class WorkflowTest method verifyHookForException.

private <T extends Exception> void verifyHookForException(Class<T> expectedExceptionType, Type expectedEffectType, String expectedErrorMsg) throws IOException, ValidationException, RepoException {
    origin.singleFileChange(0, "one commit", "foo.txt", "1");
    String config = "" + "def test(ctx):\n" + "  origin_refs = [ctx.origin.new_origin_ref('1111')]\n" + "  dest_ref = ctx.destination.new_destination_ref(ref = '9999', type = 'some_type')\n" + "  ctx.record_effect('New effect', origin_refs, dest_ref)\n" + "\n" + "core.workflow(\n" + "  name = 'default',\n" + "  origin = testing.origin(),\n" + "  destination = testing.destination(),\n" + "  transformations = [],\n" + "  authoring = " + authoring + ",\n" + "  after_migration = [test]" + ")\n";
    try {
        loadConfig(config).getMigration("default").run(workdir, ImmutableList.of());
        fail();
    } catch (Exception expected) {
        if (!expectedExceptionType.isInstance(expected)) {
            throw expected;
        }
        assertThat(expected).hasMessageThat().contains(expectedErrorMsg);
    }
    assertThat(eventMonitor.changeMigrationFinishedEventCount()).isEqualTo(1);
    ChangeMigrationFinishedEvent event = Iterables.getOnlyElement(eventMonitor.changeMigrationFinishedEvents);
    assertThat(event.getDestinationEffects()).hasSize(2);
    DestinationEffect firstEffect = event.getDestinationEffects().get(0);
    assertThat(firstEffect.getType()).isEqualTo(expectedEffectType);
    assertThat(firstEffect.getSummary()).isEqualTo("Errors happened during the migration");
    assertThat(firstEffect.getErrors()).contains(expectedErrorMsg);
    // Effect from the hook is also created
    DestinationEffect secondEffect = event.getDestinationEffects().get(1);
    assertThat(secondEffect.getSummary()).isEqualTo("New effect");
    assertThat(secondEffect.getType()).isEqualTo(Type.UPDATED);
    assertThat(secondEffect.getOriginRefs().get(0).getRef()).isEqualTo("1111");
    assertThat(secondEffect.getDestinationRef().getId()).isEqualTo("9999");
}
Also used : ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) ChangeRejectedException(com.google.copybara.exception.ChangeRejectedException) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) VoidOperationException(com.google.copybara.exception.VoidOperationException) NotADestinationFileException(com.google.copybara.exception.NotADestinationFileException) RepoException(com.google.copybara.exception.RepoException) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) ValidationException(com.google.copybara.exception.ValidationException) IOException(java.io.IOException)

Example 35 with ValidationException

use of com.google.copybara.exception.ValidationException in project copybara by google.

the class WorkflowTest method testNonReversibleInsideGit.

@Test
public void testNonReversibleInsideGit() throws IOException, ValidationException, RepoException {
    origin.singleFileChange(0, "one commit", "foo.txt", "foo\nbar\n");
    GitRepository.newRepo(/*verbose*/
    true, workdir, getGitEnv()).init();
    Path subdir = Files.createDirectory(workdir.resolve("subdir"));
    String config = "" + "core.workflow(\n" + "    name = 'default',\n" + "    origin = testing.origin(),\n" + "    destination = testing.destination(),\n" + "    transformations = [core.replace('foo', 'bar')],\n" + "    authoring = " + authoring + ",\n" + "    reversible_check = True,\n" + ")\n";
    Migration workflow = loadConfig(config).getMigration("default");
    ValidationException e = assertThrows(ValidationException.class, () -> workflow.run(subdir, ImmutableList.of()));
    assertThat(e).hasMessageThat().contains("Cannot use 'reversible_check = True' because Copybara temporary directory");
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) ValidationException(com.google.copybara.exception.ValidationException) Migration(com.google.copybara.config.Migration) Test(org.junit.Test)

Aggregations

ValidationException (com.google.copybara.exception.ValidationException)178 Test (org.junit.Test)125 Path (java.nio.file.Path)33 RepoException (com.google.copybara.exception.RepoException)29 NonReversibleValidationException (com.google.copybara.exception.NonReversibleValidationException)26 ImmutableList (com.google.common.collect.ImmutableList)21 IOException (java.io.IOException)19 Console (com.google.copybara.util.console.Console)16 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)14 DummyRevision (com.google.copybara.testing.DummyRevision)14 Glob (com.google.copybara.util.Glob)14 ProfilerTask (com.google.copybara.profiler.Profiler.ProfilerTask)13 Nullable (javax.annotation.Nullable)13 Migration (com.google.copybara.config.Migration)11 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)11 Iterables (com.google.common.collect.Iterables)10 Change (com.google.copybara.Change)10 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)10 Collectors (java.util.stream.Collectors)10 WriterContext (com.google.copybara.WriterContext)9