Search in sources :

Example 26 with Console

use of com.google.copybara.util.console.Console in project copybara by google.

the class WorkflowTest method checkIterativeModeWithError.

@SuppressWarnings("unchecked")
private <T extends Exception> T checkIterativeModeWithError(T exception) throws IOException, ValidationException {
    for (int timestamp = 0; timestamp < 10; timestamp++) {
        origin.addSimpleChange(timestamp);
    }
    // Override destination with one that always throws EmptyChangeException.
    options.testingOptions.destination = new RecordsProcessCallDestination() {

        @Override
        public Writer<Revision> newWriter(WriterContext writerContext) {
            return new WriterImpl(writerContext.isDryRun()) {

                @Override
                public ImmutableList<DestinationEffect> write(TransformResult transformResult, Glob destinationFiles, Console console) throws ValidationException, RepoException {
                    assert exception != null;
                    Throwables.propagateIfPossible(exception, ValidationException.class, RepoException.class);
                    throw new RuntimeException(exception);
                }
            };
        }
    };
    Workflow<?, ?> workflow = iterativeWorkflow(/*previousRef=*/
    "1");
    try {
        workflow.run(workdir, ImmutableList.of("3"));
        fail();
    } catch (Exception expected) {
        assertThat(expected).isInstanceOf(expected.getClass());
        return (T) expected;
    }
    return exception;
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) ValidationException(com.google.copybara.exception.ValidationException) ImmutableList(com.google.common.collect.ImmutableList) RepoException(com.google.copybara.exception.RepoException) 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) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Console(com.google.copybara.util.console.Console) Glob(com.google.copybara.util.Glob)

Example 27 with Console

use of com.google.copybara.util.console.Console in project copybara by google.

the class WorkflowTest method testOnFinishHook_error.

// Validates that the hook is executed when the workflow throws ValidationException, and that
// the correct effect is populated
@Test
public void testOnFinishHook_error() throws Exception {
    options.testingOptions.destination = new RecordsProcessCallDestination() {

        @Override
        public Writer<Revision> newWriter(WriterContext writerContext) {
            return new RecordsProcessCallDestination.WriterImpl(false) {

                @Override
                public ImmutableList<DestinationEffect> write(TransformResult transformResult, Glob destinationFiles, Console console) throws ValidationException {
                    throw new ValidationException("Validation exception!");
                }
            };
        }
    };
    verifyHookForException(ValidationException.class, Type.ERROR, "Validation exception!");
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) ValidationException(com.google.copybara.exception.ValidationException) ImmutableList(com.google.common.collect.ImmutableList) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Console(com.google.copybara.util.console.Console) Glob(com.google.copybara.util.Glob) Test(org.junit.Test)

Example 28 with Console

use of com.google.copybara.util.console.Console in project copybara by google.

the class InfoTest method testInfoAvailableToMigrate.

@Test
public void testInfoAvailableToMigrate() throws Exception {
    info = new InfoCmd((configPath, sourceRef) -> new ConfigLoader(skylark.createModuleSet(), skylark.createConfigFile("copy.bara.sky", configInfo), optionsBuilder.general.getStarlarkMode()) {

        @Override
        public Config load(Console console) {
            return config;
        }

        @Override
        public ConfigWithDependencies loadWithDependencies(Console console) {
            return configWithDeps;
        }
    }, getFakeContextProvider());
    MigrationReference<DummyRevision> workflow = MigrationReference.create("workflow", new DummyRevision("1111"), ImmutableList.of(newChange("2222", "First change", ZonedDateTime.ofInstant(Instant.ofEpochSecond(1541631979), ZoneId.of("-08:00"))), newChange("3333", "Second change", ZonedDateTime.ofInstant(Instant.ofEpochSecond(1541639979), ZoneId.of("-08:00")))));
    Info<?> mockedInfo = Info.create(dummyOriginDescription, dummyDestinationDescription, ImmutableList.of(workflow));
    Mockito.<Info<? extends Revision>>when(migration.getInfo()).thenReturn(mockedInfo);
    // Copybara copybara = new Copybara(new ConfigValidator() {}, migration -> {});
    // copybara.info(optionsBuilder.build(), config, "workflow");
    info.run(new CommandEnv(temp, optionsBuilder.build(), ImmutableList.of("copy.bara.sky", "workflow")));
    assertThat(eventMonitor.infoFinishedEvent).isNotNull();
    assertThat(eventMonitor.infoFinishedEvent.getInfo()).isEqualTo(mockedInfo);
    console.assertThat().onceInLog(MessageType.INFO, ".*last_migrated 1111 - last_available 3333.*").onceInLog(MessageType.INFO, ".*Date.*Revision.*Description.*Author.*").onceInLog(MessageType.INFO, ".*2018-11-07 15:06:19.*2222.*First change.*Foo <Bar>.*").onceInLog(MessageType.INFO, ".*2018-11-07 17:19:39.*3333.*Second change.*Foo <Bar>.*");
}
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) Revision(com.google.copybara.revision.Revision) 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) Change(com.google.copybara.revision.Change) 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) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Console(com.google.copybara.util.console.Console) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 29 with Console

use of com.google.copybara.util.console.Console in project copybara by google.

the class InfoTest method testInfoUpToDate.

@Test
public void testInfoUpToDate() throws Exception {
    info = new InfoCmd((configPath, sourceRef) -> new ConfigLoader(skylark.createModuleSet(), skylark.createConfigFile("copy.bara.sky", configInfo), optionsBuilder.general.getStarlarkMode()) {

        @Override
        public Config load(Console console) {
            return config;
        }

        @Override
        public ConfigWithDependencies loadWithDependencies(Console console) {
            return configWithDeps;
        }
    }, getFakeContextProvider());
    MigrationReference<DummyRevision> workflow = MigrationReference.create("workflow", new DummyRevision("1111"), ImmutableList.of());
    Info<?> mockedInfo = Info.create(dummyOriginDescription, dummyDestinationDescription, ImmutableList.of(workflow));
    Mockito.<Info<? extends Revision>>when(migration.getInfo()).thenReturn(mockedInfo);
    info.run(new CommandEnv(temp, optionsBuilder.build(), ImmutableList.of("copy.bara.sky", "workflow")));
    assertThat(eventMonitor.infoFinishedEvent).isNotNull();
    assertThat(eventMonitor.infoFinishedEvent.getInfo()).isEqualTo(mockedInfo);
    console.assertThat().onceInLog(MessageType.INFO, ".*last_migrated 1111 - last_available None.*");
}
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) Revision(com.google.copybara.revision.Revision) 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) Change(com.google.copybara.revision.Change) 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) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Console(com.google.copybara.util.console.Console) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 30 with Console

use of com.google.copybara.util.console.Console in project copybara by google.

the class ApiCheckerTest method testCheckError.

@Test
public void testCheckError() {
    ApiChecker checker = new ApiChecker(new FakeChecker() {

        @Override
        public void doCheck(ImmutableMap<String, String> fields, Console console) throws CheckerException {
            throw new CheckerException("Check failed!");
        }
    }, new TestingConsole());
    ValidationException e = assertThrows(ValidationException.class, () -> checker.check("foo", new Object()));
    assertThat(e).hasMessageThat().isEqualTo("Check failed!");
    ValidationException failedCheck = assertThrows(ValidationException.class, () -> checker.check("foo", new Object(), "bar", new Object()));
    assertThat(failedCheck).hasMessageThat().isEqualTo("Check failed!");
    ValidationException failedCheckArgs = assertThrows(ValidationException.class, () -> checker.check("foo", new Object(), "bar", new Object(), "baz", new Object()));
    assertThat(failedCheckArgs).hasMessageThat().isEqualTo("Check failed!");
}
Also used : TestingConsole(com.google.copybara.util.console.testing.TestingConsole) ValidationException(com.google.copybara.exception.ValidationException) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Console(com.google.copybara.util.console.Console) Test(org.junit.Test)

Aggregations

Console (com.google.copybara.util.console.Console)38 Test (org.junit.Test)19 ValidationException (com.google.copybara.exception.ValidationException)14 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)11 IOException (java.io.IOException)11 ImmutableList (com.google.common.collect.ImmutableList)10 Config (com.google.copybara.config.Config)8 Path (java.nio.file.Path)7 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)6 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)6 Before (org.junit.Before)6 Truth.assertThat (com.google.common.truth.Truth.assertThat)5 Migration (com.google.copybara.config.Migration)5 Glob (com.google.copybara.util.Glob)5 RunWith (org.junit.runner.RunWith)5 JUnit4 (org.junit.runners.JUnit4)5 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)4 ConfigWithDependencies (com.google.copybara.config.SkylarkParser.ConfigWithDependencies)4 RepoException (com.google.copybara.exception.RepoException)4 Change (com.google.copybara.revision.Change)4