Search in sources :

Example 6 with Revision

use of com.google.copybara.revision.Revision 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) 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) 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) Revision(com.google.copybara.revision.Revision) 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 7 with Revision

use of com.google.copybara.revision.Revision in project copybara by google.

the class ReadConfigFromChangeWorkflowTest method testWriterStateMaintained.

/**
 * A test that check that we can mutate the glob in iterative mode
 */
@SuppressWarnings("unchecked")
@Test
public void testWriterStateMaintained() throws Exception {
    options.workflowOptions.lastRevision = "0";
    String configCode = mutatingWorkflow("*");
    Config cfg = skylark.loadConfig(configCode);
    ConfigLoader constantConfigLoader = new ConfigLoader(skylark.createModuleSet(), skylark.createConfigFile("copy.bara.sky", configCode), options.general.getStarlarkMode()) {

        @Override
        protected Config doLoadForRevision(Console console, Revision revision) throws ValidationException {
            try {
                return skylark.loadConfig(mutatingWorkflow(revision.asString()));
            } catch (IOException e) {
                throw new AssertionError("Should not fail", e);
            }
        }
    };
    ReadConfigFromChangeWorkflow<?, ?> wf = new ReadConfigFromChangeWorkflow<>((Workflow) cfg.getMigration("default"), options.build(), constantConfigLoader, new ConfigValidator() {

        @Override
        public ValidationResult validate(Config config, String migrationName) {
            return ValidationResult.EMPTY;
        }
    });
    origin.singleFileChange(0, "base", "fileB", "b");
    origin.singleFileChange(1, "one", "file1", "b");
    origin.singleFileChange(2, "two", "file2", "b");
    origin.singleFileChange(3, "three", "file3", "b");
    wf.run(Files.createTempDirectory("workdir"), ImmutableList.of("3"));
    assertThat(destination.processed).hasSize(3);
    assertThat(destination.processed.get(0).getDestinationFiles().toString()).contains("file1");
    assertThat(destination.processed.get(0).getWorkdir()).containsExactly("file1", "b");
    assertThat(destination.processed.get(1).getDestinationFiles().toString()).contains("file2");
    assertThat(destination.processed.get(1).getWorkdir()).containsExactly("file2", "b");
    assertThat(destination.processed.get(2).getDestinationFiles().toString()).contains("file3");
    assertThat(destination.processed.get(2).getWorkdir()).containsExactly("file3", "b");
}
Also used : ConfigValidator(com.google.copybara.config.ConfigValidator) Revision(com.google.copybara.revision.Revision) Config(com.google.copybara.config.Config) Console(com.google.copybara.util.console.Console) IOException(java.io.IOException) ValidationResult(com.google.copybara.config.ValidationResult) Test(org.junit.Test)

Example 8 with Revision

use of com.google.copybara.revision.Revision in project copybara by google.

the class WorkflowTest method givenLastRevFlagInfoCommandUsesIt.

@Test
@SuppressWarnings("unchecked")
public void givenLastRevFlagInfoCommandUsesIt() throws Exception {
    Path originPath = Files.createTempDirectory("origin");
    Path destinationPath = Files.createTempDirectory("destination");
    GitRepository origin = GitRepository.newRepo(/*verbose*/
    true, originPath, getGitEnv()).init();
    GitRepository destination = GitRepository.newRepo(/*verbose*/
    true, destinationPath, getGitEnv()).init();
    String primaryBranch = origin.getPrimaryBranch();
    String config = "core.workflow(" + "    name = '" + "default" + "'," + String.format("    origin = git.origin( url = 'file://%s', ref = '%s'),\n", origin.getWorkTree(), primaryBranch) + "    destination = git.destination(" + "        url = 'file://" + destination.getWorkTree() + "',\n" + "    ),\n" + "    authoring = " + authoring + "," + "    mode = '" + WorkflowMode.ITERATIVE + "'," + ")\n";
    Files.write(originPath.resolve("foo.txt"), "not important".getBytes(UTF_8));
    origin.add().files("foo.txt").run();
    origin.commit("Foo <foo@bara.com>", ZonedDateTime.now(ZoneId.systemDefault()), "not important");
    String firstCommit = origin.parseRef("HEAD");
    Files.write(destinationPath.resolve("foo.txt"), "not important".getBytes(UTF_8));
    destination.add().files("foo.txt").run();
    destination.commit("Foo <foo@bara.com>", ZonedDateTime.now(ZoneId.systemDefault()), "not important");
    Files.write(originPath.resolve("foo.txt"), "foo".getBytes(UTF_8));
    origin.add().files("foo.txt").run();
    origin.commit("Foo <foo@bara.com>", ZonedDateTime.now(ZoneId.systemDefault()), "change1");
    options.setWorkdirToRealTempDir();
    // Pass custom HOME directory so that we run an hermetic test and we
    // can add custom configuration to $HOME/.gitconfig.
    options.setEnvironment(GitTestUtil.getGitEnv().getEnvironment());
    options.setHomeDir(Files.createTempDirectory("home").toString());
    options.gitDestination.committerName = "Foo";
    options.gitDestination.committerEmail = "foo@foo.com";
    options.workflowOptions.checkLastRevState = true;
    options.setLastRevision(firstCommit);
    Info<Revision> info = (Info<Revision>) loadConfig(config).getMigration("default").getInfo();
    verifyInfo(info, "change1\n");
    assertThat(info.originDescription().get("url")).containsExactly("file://" + origin.getWorkTree());
    assertThat(info.destinationDescription().get("url")).containsExactly("file://" + destination.getWorkTree());
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) GitRepository(com.google.copybara.git.GitRepository) GitRevision(com.google.copybara.git.GitRevision) DummyRevision(com.google.copybara.testing.DummyRevision) Revision(com.google.copybara.revision.Revision) Test(org.junit.Test)

Example 9 with Revision

use of com.google.copybara.revision.Revision 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 10 with Revision

use of com.google.copybara.revision.Revision 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)

Aggregations

Revision (com.google.copybara.revision.Revision)14 Test (org.junit.Test)10 Workflow (com.google.copybara.Workflow)6 RecordsProcessCallDestination (com.google.copybara.testing.RecordsProcessCallDestination)6 Config (com.google.copybara.config.Config)5 Migration (com.google.copybara.config.Migration)5 ValidationException (com.google.copybara.exception.ValidationException)5 Console (com.google.copybara.util.console.Console)5 IOException (java.io.IOException)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 MigrationReference (com.google.copybara.Info.MigrationReference)4 Author (com.google.copybara.authoring.Author)4 Change (com.google.copybara.revision.Change)4 DummyRevision (com.google.copybara.testing.DummyRevision)4 ProcessedChange (com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange)4 Path (java.nio.file.Path)4 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)3 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)3