Search in sources :

Example 26 with ValidationException

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

the class ExamplesTest method checkExamples.

private ImmutableList<Result> checkExamples(SkylarkTestExecutor executor, Class<?> module, ImmutableList<Example> samples, String name) {
    ImmutableList.Builder<Result> result = ImmutableList.builder();
    for (Example example : samples) {
        Object val;
        String exampleRef = module.getName() + "#" + name + ": " + example.title();
        // Quilt requires an extra file.
        if (exampleRef.contains("com.google.copybara.transform.patch.PatchModule#quiltApply")) {
            executor.addConfigFile("patches/series", "");
        }
        try {
            val = Strings.isNullOrEmpty(example.testExistingVariable()) ? executor.eval("a", "a=" + example.code()) : executor.eval(example.testExistingVariable(), example.code());
            assertWithMessage(exampleRef).that(val).isNotNull();
            result.add(new Result(exampleRef, module, null));
        } catch (ValidationException | AssertionError e) {
            result.add(new Result(exampleRef, module, e));
        }
    }
    return result.build();
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) ImmutableList(com.google.common.collect.ImmutableList) Example(com.google.copybara.doc.annotations.Example)

Example 27 with ValidationException

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

the class InfoTest method setUp.

@Before
public void setUp() throws IOException {
    dummyOriginDescription = ImmutableMultimap.of("origin", "foo");
    dummyDestinationDescription = ImmutableMultimap.of("dest", "bar");
    console = new TestingConsole();
    temp = Files.createTempDirectory("temp");
    optionsBuilder = new OptionsBuilder();
    optionsBuilder.setConsole(console);
    optionsBuilder.setWorkdirToRealTempDir();
    skylark = new SkylarkTestExecutor(optionsBuilder);
    eventMonitor = new TestingEventMonitor();
    optionsBuilder.general.enableEventMonitor("just testing", eventMonitor);
    optionsBuilder.general.starlarkMode = StarlarkMode.STRICT.name();
    migration = mock(Migration.class);
    config = new Config(ImmutableMap.of("workflow", migration), temp.resolve("copy.bara.sky").toString(), ImmutableMap.of());
    configWithDeps = mock(ConfigWithDependencies.class);
    when(configWithDeps.getConfig()).thenAnswer(i -> config);
    configInfo = "" + "core.workflow(" + "    name = 'workflow'," + "    origin = git.origin(url = 'https://example.com/orig', ref = 'master')," + "    destination = git.destination(url = 'https://example.com/dest')," + "    authoring = authoring.overwrite('Foo <foo@example.com>')" + ")\n\n" + "";
    info = new InfoCmd((configPath, sourceRef) -> new ConfigLoader(skylark.createModuleSet(), skylark.createConfigFile("copy.bara.sky", configInfo), optionsBuilder.general.getStarlarkMode()) {

        @Override
        protected Config doLoadForRevision(Console console, Revision revision) throws ValidationException {
            try {
                return skylark.loadConfig(configPath);
            } catch (IOException e) {
                throw new AssertionError("Should not fail", e);
            }
        }
    }, getFakeContextProvider());
}
Also used : MigrationReference(com.google.copybara.Info.MigrationReference) DummyRevision(com.google.copybara.testing.DummyRevision) ZonedDateTime(java.time.ZonedDateTime) RunWith(org.junit.runner.RunWith) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) MessageType(com.google.copybara.util.console.Message.MessageType) ImmutableList(com.google.common.collect.ImmutableList) Author(com.google.copybara.authoring.Author) ExitCode(com.google.copybara.util.ExitCode) Config(com.google.copybara.config.Config) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Path(java.nio.file.Path) Before(org.junit.Before) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) ImmutableMap(com.google.common.collect.ImmutableMap) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Files(java.nio.file.Files) Migration(com.google.copybara.config.Migration) ValidationException(com.google.copybara.exception.ValidationException) ConfigWithDependencies(com.google.copybara.config.SkylarkParser.ConfigWithDependencies) Console(com.google.copybara.util.console.Console) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) ZoneId(java.time.ZoneId) Mockito(org.mockito.Mockito) TestingEventMonitor(com.google.copybara.testing.TestingEventMonitor) StarlarkMode(com.google.copybara.util.console.StarlarkMode) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) SUCCESS(com.google.copybara.util.ExitCode.SUCCESS) Mockito.mock(org.mockito.Mockito.mock) Migration(com.google.copybara.config.Migration) Config(com.google.copybara.config.Config) IOException(java.io.IOException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) TestingEventMonitor(com.google.copybara.testing.TestingEventMonitor) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) DummyRevision(com.google.copybara.testing.DummyRevision) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Console(com.google.copybara.util.console.Console) ConfigWithDependencies(com.google.copybara.config.SkylarkParser.ConfigWithDependencies) Before(org.junit.Before)

Example 28 with ValidationException

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

the class TransformWorkTest method testSymlinks_outside.

@Test
public void testSymlinks_outside() throws IOException, ValidationException {
    Path base = Files.createDirectories(workdir.resolve("foo"));
    Path tempFile = Files.createTempFile("foo", "bar");
    Files.write(tempFile, "THE CONTENT".getBytes(UTF_8));
    Files.createSymbolicLink(base.resolve("symlink"), tempFile);
    Transformation transformation = skylark.eval("transformation", "" + "def test(ctx):\n" + "    ctx.new_path('foo/symlink').read_symlink()\n" + "\n" + "transformation = core.transform([test])");
    ValidationException e = assertThrows(ValidationException.class, () -> transformation.transform(TransformWorks.of(workdir, "test", console)));
    assertThat(e).hasMessageThat().contains("points to a file outside the checkout dir");
}
Also used : Path(java.nio.file.Path) ValidationException(com.google.copybara.exception.ValidationException) Test(org.junit.Test)

Example 29 with ValidationException

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

the class FeedbackTest method testErrorResultThrowsValidationException.

@Test
public void testErrorResultThrowsValidationException() throws Exception {
    Feedback feedback = feedback("" + "def test_action(ctx):\n" + "    return ctx.error('This is an error')\n", "test_action");
    ValidationException expected = assertThrows(ValidationException.class, () -> feedback.run(workdir, ImmutableList.of()));
    assertThat(expected).hasMessageThat().contains("Feedback migration 'default' action 'test_action' returned error: " + "This is an error. Aborting execution.");
    console.assertThat().equalsNext(MessageType.ERROR, "Action 'test_action' returned error: This is an error");
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) Feedback(com.google.copybara.feedback.Feedback) Test(org.junit.Test)

Example 30 with ValidationException

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

the class FeedbackTest method testActionsMustReturnResult.

@Test
public void testActionsMustReturnResult() throws Exception {
    Feedback feedback = feedback("" + "def test_action(ctx):\n" + "    ctx.console.info('Bad action')\n" + "\n", "test_action");
    ValidationException expected = assertThrows(ValidationException.class, () -> feedback.run(workdir, ImmutableList.of()));
    assertThat(expected).hasMessageThat().contains("Actions must return a result via built-in functions: success(), " + "error(), noop() return, but 'test_action' returned: None");
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) Feedback(com.google.copybara.feedback.Feedback) 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