Search in sources :

Example 21 with Migration

use of com.google.copybara.config.Migration in project copybara by google.

the class WorkflowTest method changeRequestEmptyChanges.

@Test
public void changeRequestEmptyChanges() throws Exception {
    Path originPath = Files.createTempDirectory("origin");
    GitRepository origin = GitRepository.newRepo(/*verbose*/
    true, originPath, getGitEnv()).init();
    String primaryBranch = origin.getPrimaryBranch();
    String config = "def after_all(ctx):\n" + "  ctx.destination.message('after_all '" + " + str([e.type + ' '+ e.summary for e in ctx.effects]))\n" + "\n" + "core.workflow(" + "    name = 'default'," + String.format("    origin = git.origin( url = 'file://%s', ref = '%s'),\n", origin.getWorkTree(), primaryBranch) + "    destination = testing.destination(),\n" + "    authoring = " + authoring + "," + "    origin_files = glob(['included/**'])," + "    mode = '" + WorkflowMode.CHANGE_REQUEST + "'," + "    after_workflow = [after_all]" + ")\n";
    Migration workflow = loadConfig(config).getMigration("default");
    Files.createDirectory(originPath.resolve("included"));
    Files.write(originPath.resolve("included/foo.txt"), "a".getBytes(UTF_8));
    origin.add().files("included/foo.txt").run();
    origin.commit("Foo <foo@bara.com>", ZonedDateTime.now(ZoneId.systemDefault()), "the baseline\n\n" + destination.getLabelNameWhenOrigin() + "=42");
    Files.createDirectory(originPath.resolve("excluded"));
    Files.write(originPath.resolve("excluded/foo.txt"), "a".getBytes(UTF_8));
    origin.add().files("excluded/foo.txt").run();
    origin.commit("Foo <foo@bara.com>", ZonedDateTime.now(ZoneId.systemDefault()), "head change");
    EmptyChangeException thrown = assertThrows(EmptyChangeException.class, () -> workflow.run(workdir, ImmutableList.of()));
    assertThat(thrown).hasMessageThat().contains("doesn't include any change for origin_files = glob(include = [\"included/**\"])");
    assertThat(destination.getEndpoint().messages).hasSize(1);
    assertThat(destination.getEndpoint().messages.get(0)).containsMatch(".*didn't affect any destination file.*");
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) GitRepository(com.google.copybara.git.GitRepository) Migration(com.google.copybara.config.Migration) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Test(org.junit.Test)

Example 22 with Migration

use of com.google.copybara.config.Migration in project copybara by google.

the class WorkflowTest method reversibleCheckFiles.

@Test
public void reversibleCheckFiles() 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" + "    reversible_check = True,\n" + "    reversible_check_ignore_files = glob([\"to_ignore/**\"]," + "                                          exclude = [\"to_ignore/exclude\"]),\n" + "    mode = '" + WorkflowMode.SQUASH + "',\n" + "    transformations = [" + "      core.replace(before = 'aa', after = 'bb')" + "    ]" + ")\n";
    Migration workflow = loadConfig(config).getMigration("default");
    GitTestUtil.writeFile(originPath, "test", "aabb");
    GitTestUtil.writeFile(originPath, "to_ignore/test", "aabb");
    GitTestUtil.writeFile(originPath, "to_ignore/exclude", "aabb");
    origin.add().all().run();
    origin.simpleCommand("commit", "-m", "change");
    try {
        workflow.run(Files.createDirectory(someRoot.resolve("run1")), ImmutableList.of());
        fail();
    } catch (ValidationException e) {
        assertThat(e).hasMessageThat().contains("is not reversible");
        String msg = console().getMessages().stream().filter(m -> m.getType() == MessageType.ERROR && m.getText().contains("Non reversible")).findFirst().get().getText();
        assertThat(msg).contains("--- a/origin/test");
        assertThat(msg).doesNotContain("--- a/origin/to_ignore/test");
        assertThat(msg).contains("--- a/origin/to_ignore/exclude");
    }
    // Now lets fix the only file that we check in reversible check:
    GitTestUtil.writeFile(originPath, "test", "aa");
    GitTestUtil.writeFile(originPath, "to_ignore/exclude", "aa");
    origin.add().all().run();
    origin.simpleCommand("commit", "-m", "change 2");
    workflow.run(Files.createDirectory(someRoot.resolve("run2")), ImmutableList.of());
}
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 23 with Migration

use of com.google.copybara.config.Migration in project copybara by google.

the class WorkflowTest method runGitDescribeVersionSemanticsForFilteredChanges.

private void runGitDescribeVersionSemanticsForFilteredChanges(String mode) throws IOException, RepoException, ValidationException {
    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" + "    mode = '" + mode + "',\n" + "    transformations = [" + "metadata.add_header('Resolved revision is ${GIT_DESCRIBE_REQUESTED_VERSION}" + " and change revision is ${GIT_DESCRIBE_CHANGE_VERSION}')]" + ")\n";
    GitTestUtil.writeFile(originPath, "initial.txt", "initial");
    origin.add().files("initial.txt").run();
    origin.commit("Foo <foo@bara.com>", ZonedDateTime.now(ZoneId.systemDefault()), "Initial");
    origin.simpleCommand("tag", "-m", "this is a tag!", "0.1");
    options.setLastRevision(origin.parseRef("HEAD"));
    Migration workflow = loadConfig(config).getMigration("default");
    GitTestUtil.writeFile(originPath, "included/foo.txt", "a");
    origin.add().files("included/foo.txt").run();
    origin.commit("Foo <foo@bara.com>", ZonedDateTime.now(ZoneId.systemDefault()), "one");
    GitTestUtil.writeFile(originPath, "included/foo.txt", "b");
    origin.add().files("included/foo.txt").run();
    origin.commit("Foo <foo@bara.com>", ZonedDateTime.now(ZoneId.systemDefault()), "two");
    GitTestUtil.writeFile(originPath, "excluded/foo.txt", "c");
    origin.add().files("excluded/foo.txt").run();
    origin.commit("Foo <foo@bara.com>", ZonedDateTime.now(ZoneId.systemDefault()), "three");
    workflow.run(workdir, ImmutableList.of());
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) GitRepository(com.google.copybara.git.GitRepository) Migration(com.google.copybara.config.Migration)

Example 24 with Migration

use of com.google.copybara.config.Migration in project copybara by google.

the class GitMirrorTest method mergeInit.

private Migration mergeInit() throws IOException, ValidationException, RepoException {
    String cfg = "" + ("def _merger(ctx):\n" + "     ctx.console.info('Hello this is mirror!')\n" + "     branch = ctx.params['branch']\n" + "     oss_branch = ctx.params['oss_branch']\n" + "     ctx.origin_fetch(refspec = [branch +" + " ':refs/heads/copybara/origin_fetch'])\n" + "     exist = ctx.destination_fetch(refspec = [branch +" + " ':refs/heads/copybara/destination_fetch'])\n" + "     if exist:\n" + "         result = ctx.merge(branch = 'copybara/destination_fetch',           " + "              commits = ['refs/heads/copybara/origin_fetch'])\n" + "         if result.error:\n" + "             return ctx.error('Conflict merging ' + branch + ' into" + " destination: ' + result.error_msg)\n" + "         ctx.destination_push(refspec =" + " ['refs/heads/copybara/destination_fetch:' + branch])\n" + "     else:\n" + "         ctx.destination_push(refspec = ['refs/heads/copybara/origin_fetch:'" + " + branch])\n" + "     if oss_branch:\n" + "         ctx.destination_push(refspec = ['refs/heads/copybara/origin_fetch:'" + " + oss_branch])\n" + "     return ctx.success()\n" + "\n" + "def merger(branch, oss_branch = None):\n" + "    return core.action(impl = _merger,         params = {'branch': branch," + " 'oss_branch' : oss_branch})\n" + "\n") + "git.mirror(" + "    name = 'default'," + "    origin = 'file://" + originRepo.getGitDir().toAbsolutePath() + "'," + "    destination = 'file://" + destRepo.getGitDir().toAbsolutePath() + "'," + "    refspecs = [" + String.format("" + "       'refs/heads/%s:refs/heads/%s'," + "       'refs/heads/%s:refs/heads/oss'", primaryBranch, primaryBranch, primaryBranch) + "    ]," + "    actions = [merger(" + String.format("'refs/heads/%s'", primaryBranch) + ", oss_branch = 'refs/heads/oss')]," + ")";
    Migration mirror = loadMigration(cfg, "default");
    mirror.run(workdir, ImmutableList.of());
    String origPrimary = originRepo.git(originRepo.getGitDir(), "show-ref", primaryBranch, "-s").getStdout();
    assertThat(destRepo.git(destRepo.getGitDir(), "show-ref", primaryBranch, "-s").getStdout()).isEqualTo(origPrimary);
    assertThat(destRepo.git(destRepo.getGitDir(), "show-ref", "oss", "-s").getStdout()).isEqualTo(origPrimary);
    return mirror;
}
Also used : Migration(com.google.copybara.config.Migration)

Example 25 with Migration

use of com.google.copybara.config.Migration in project copybara by google.

the class GitMirrorTest method testMirror.

@Test
public void testMirror() throws Exception {
    RecordingListener recordingCallback = new RecordingListener();
    Profiler profiler = new Profiler(new FakeTicker());
    profiler.init(ImmutableList.of(recordingCallback));
    options.general.withProfiler(profiler);
    Migration mirror = createMirrorObj();
    mirror.run(workdir, ImmutableList.of());
    String orig = originRepo.git(originRepo.getGitDir(), "show-ref").getStdout();
    String dest = destRepo.git(destRepo.getGitDir(), "show-ref").getStdout();
    assertThat(dest).isEqualTo(orig);
    recordingCallback.assertMatchesNext(EventType.START, "//copybara").assertMatchesNext(EventType.START, "//copybara/run/default").assertMatchesNext(EventType.START, "//copybara/run/default/fetch").assertMatchesNext(EventType.END, "//copybara/run/default/fetch").assertMatchesNext(EventType.START, "//copybara/run/default/push").assertMatchesNext(EventType.END, "//copybara/run/default/push").assertMatchesNext(EventType.END, "//copybara/run/default");
}
Also used : Profiler(com.google.copybara.profiler.Profiler) RecordingListener(com.google.copybara.testing.profiler.RecordingListener) Migration(com.google.copybara.config.Migration) FakeTicker(com.google.common.testing.FakeTicker) Test(org.junit.Test)

Aggregations

Migration (com.google.copybara.config.Migration)30 Test (org.junit.Test)22 ValidationException (com.google.copybara.exception.ValidationException)9 Path (java.nio.file.Path)9 FileSubjects.assertThatPath (com.google.copybara.testing.FileSubjects.assertThatPath)7 GitRepository (com.google.copybara.git.GitRepository)6 Config (com.google.copybara.config.Config)5 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)3 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)3 TestingEventMonitor (com.google.copybara.testing.TestingEventMonitor)3 Console (com.google.copybara.util.console.Console)3 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)3 Before (org.junit.Before)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 MigrationReference (com.google.copybara.Info.MigrationReference)2 Author (com.google.copybara.authoring.Author)2