Search in sources :

Example 11 with GitRepository

use of com.google.copybara.git.GitRepository in project copybara by google.

the class WorkflowTest method testToFolderFlag.

@Test
public void testToFolderFlag() 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"), "change".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");
    options.workflowOptions.toFolder = true;
    options.general.squash = true;
    Path localFolder = Files.createTempDirectory("local_folder");
    options.folderDestination.localFolder = localFolder.toString();
    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;
    loadConfig(config).getMigration("default").run(Files.createTempDirectory("checkout"), ImmutableList.of());
    FileSubjects.assertThatPath(localFolder).containsFile("foo.txt", "change").containsNoMoreFiles();
    assertThrows(CannotResolveRevisionException.class, () -> destination.resolveReference("HEAD"));
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) GitRepository(com.google.copybara.git.GitRepository) Test(org.junit.Test)

Example 12 with GitRepository

use of com.google.copybara.git.GitRepository 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 13 with GitRepository

use of com.google.copybara.git.GitRepository 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) Test(org.junit.Test)

Example 14 with GitRepository

use of com.google.copybara.git.GitRepository in project copybara by google.

the class WorkflowTest method testTestWorkflowWithDiffInOriginAndRespondNo.

@Test
public void testTestWorkflowWithDiffInOriginAndRespondNo() throws Exception {
    GitRepository remote = GitRepository.newBareRepo(Files.createTempDirectory("gitdir"), getGitEnv(), /*verbose=*/
    true, DEFAULT_TIMEOUT, /*noVerify=*/
    false).withWorkTree(workdir);
    remote.init();
    String primaryBranch = remote.getPrimaryBranch();
    Files.write(workdir.resolve("foo.txt"), new byte[] {});
    remote.add().files("foo.txt").run();
    remote.simpleCommand("commit", "foo.txt", "-m", "message_a");
    GitRevision lastRev = remote.resolveReference(primaryBranch);
    Files.write(workdir.resolve("bar.txt"), "change content".getBytes(UTF_8));
    remote.add().files("bar.txt").run();
    remote.simpleCommand("commit", "bar.txt", "-m", "message_s");
    TestingConsole testingConsole = new TestingConsole().respondNo();
    options.workflowOptions.lastRevision = lastRev.getSha1();
    options.setWorkdirToRealTempDir().setConsole(testingConsole).setHomeDir(StandardSystemProperty.USER_HOME.value());
    Workflow<?, ?> workflow = (Workflow<?, ?>) new SkylarkTestExecutor(options).loadConfig("core.workflow(\n" + "    name = 'foo',\n" + "    origin = git.origin(url='" + remote.getGitDir() + "'),\n" + "    destination = folder.destination(),\n" + "    mode = 'ITERATIVE',\n" + "    authoring = " + authoring + ",\n" + "    transformations = [metadata.replace_message(''),],\n" + ")\n").getMigration("foo");
    workflow.getWorkflowOptions().diffInOrigin = true;
    ChangeRejectedException e = assertThrows(ChangeRejectedException.class, () -> workflow.run(workdir, ImmutableList.of(primaryBranch)));
    assertThat(e.getMessage()).contains("User aborted execution: did not confirm diff in origin changes.");
}
Also used : GitRepository(com.google.copybara.git.GitRepository) ChangeRejectedException(com.google.copybara.exception.ChangeRejectedException) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) GitRevision(com.google.copybara.git.GitRevision) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Test(org.junit.Test)

Example 15 with GitRepository

use of com.google.copybara.git.GitRepository in project copybara by google.

the class WorkflowTest method testWorkflowWithEmptyDiffInOrigin.

@Test
public void testWorkflowWithEmptyDiffInOrigin() throws Exception {
    GitRepository remote = GitRepository.newBareRepo(Files.createTempDirectory("gitdir"), getGitEnv(), /*verbose=*/
    true, DEFAULT_TIMEOUT, /*noVerify=*/
    false).withWorkTree(workdir);
    remote.init();
    String primaryBranch = remote.getPrimaryBranch();
    Files.write(workdir.resolve("foo.txt"), new byte[] {});
    remote.add().files("foo.txt").run();
    remote.simpleCommand("commit", "foo.txt", "-m", "message_a");
    GitRevision lastRev = remote.resolveReference(primaryBranch);
    Files.write(workdir.resolve("foo.txt"), "change content".getBytes(UTF_8));
    remote.add().files("foo.txt").run();
    remote.simpleCommand("commit", "foo.txt", "-m", "message_a");
    Files.write(workdir.resolve("foo.txt"), new byte[] {});
    remote.add().files("foo.txt").run();
    remote.simpleCommand("commit", "foo.txt", "-m", "message_a");
    TestingConsole testingConsole = new TestingConsole().respondYes();
    options.workflowOptions.lastRevision = lastRev.getSha1();
    options.general.force = false;
    options.setWorkdirToRealTempDir().setConsole(testingConsole).setHomeDir(StandardSystemProperty.USER_HOME.value());
    Workflow<?, ?> workflow = (Workflow<?, ?>) new SkylarkTestExecutor(options).loadConfig("core.workflow(\n" + "    name = 'foo',\n" + String.format("    origin = git.origin(url='%s', ref='%s'),\n", remote.getGitDir(), primaryBranch) + "    destination = folder.destination(),\n" + "    mode = 'ITERATIVE',\n" + "    authoring = " + authoring + ",\n" + "    transformations = [metadata.replace_message(''),],\n" + ")\n").getMigration("foo");
    workflow.getWorkflowOptions().diffInOrigin = true;
    workflow.run(workdir, ImmutableList.of(primaryBranch));
    testingConsole.assertThat().onceInLog(MessageType.WARNING, ".*No difference at diff_in_origin.*");
}
Also used : GitRepository(com.google.copybara.git.GitRepository) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) GitRevision(com.google.copybara.git.GitRevision) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Test(org.junit.Test)

Aggregations

GitRepository (com.google.copybara.git.GitRepository)17 Test (org.junit.Test)12 FileSubjects.assertThatPath (com.google.copybara.testing.FileSubjects.assertThatPath)10 Path (java.nio.file.Path)10 Migration (com.google.copybara.config.Migration)6 GitRevision (com.google.copybara.git.GitRevision)5 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)5 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)3 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)3 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)3 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)3 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)3 ValidationException (com.google.copybara.exception.ValidationException)3 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)3 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)2 IOException (java.io.IOException)2 Predicate (java.util.function.Predicate)2 ChangeRejectedException (com.google.copybara.exception.ChangeRejectedException)1 GerritOptions (com.google.copybara.git.GerritOptions)1 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)1