Search in sources :

Example 21 with DummyRevision

use of com.google.copybara.testing.DummyRevision in project copybara by google.

the class GitDestinationIntegrateTest method testDestinationStatus.

/**
 * Checks that we can correctly read destination status (last fake merge commit created) after
 * an integration.
 *
 * <p>Don't change the file paths or contents of this test. It needs to be like this to generate
 * an empty diff with the feature branch (pure fake-merge).
 */
@Test
public void testDestinationStatus() throws ValidationException, IOException, RepoException {
    destinationFiles = Glob.createGlob(ImmutableList.of("foo/**"));
    Path gitDir = Files.createTempDirectory("gitdir");
    Path repoPath = Files.createTempDirectory("workdir");
    GitRepository repo = GitRepository.newBareRepo(gitDir, getGitEnv(), /*verbose=*/
    true, DEFAULT_TIMEOUT, /*noVerify=*/
    false).init().withWorkTree(repoPath);
    singleChange(repoPath, repo, "base.txt", "first change");
    repo.simpleCommand("branch", "feature1");
    repo.forceCheckout("feature1");
    GitRevision feature = singleChange(repoPath, repo, "foo/a", "Feature 1 change");
    repo.forceCheckout(primaryBranch);
    // Just so that the migration doesn't fail since the git repo is a non-bare repo.
    repo.forceCheckout("feature1");
    GitDestination destination = destination("url = 'file://" + repo.getGitDir() + "'", String.format("push='%s'", primaryBranch));
    migrateOriginChange(destination, "Test change\n" + "\n" + GitModule.DEFAULT_INTEGRATE_LABEL + "=file://" + repo.getGitDir().toString() + " feature1\n", "foo/a", "", "the_rev");
    GitLogEntry featureMerge = getLastMigratedChange(primaryBranch, repo);
    assertThat(featureMerge.getBody()).isEqualTo("Merge of " + feature.getSha1() + "\n" + "\n" + DummyOrigin.LABEL_NAME + ": the_rev\n");
    WriterContext writerContext = new WriterContext("piper_to_github", "TEST", false, new DummyRevision("feature"), Glob.ALL_FILES.roots());
    DestinationStatus destinationStatus = destination.newWriter(writerContext).getDestinationStatus(destinationFiles, DummyOrigin.LABEL_NAME);
    assertWithMessage(gitDir.toString()).that(destinationStatus.getBaseline()).isEqualTo("the_rev");
}
Also used : Path(java.nio.file.Path) WriterContext(com.google.copybara.WriterContext) DestinationStatus(com.google.copybara.Destination.DestinationStatus) DummyRevision(com.google.copybara.testing.DummyRevision) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Test(org.junit.Test)

Example 22 with DummyRevision

use of com.google.copybara.testing.DummyRevision in project copybara by google.

the class GitDestinationTest method canExcludeDestinationPathFromWorkflow.

@Test
public void canExcludeDestinationPathFromWorkflow() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    Path scratchTree = Files.createTempDirectory("GitDestinationTest-scratchTree");
    Files.write(scratchTree.resolve("excluded.txt"), "some content".getBytes(UTF_8));
    repo().withWorkTree(scratchTree).add().files("excluded.txt").run();
    repo().withWorkTree(scratchTree).simpleCommand("commit", "-m", "message");
    Files.write(workdir.resolve("normal_file.txt"), "some more content".getBytes(UTF_8));
    destinationFiles = Glob.createGlob(ImmutableList.of("**"), ImmutableList.of("excluded.txt"));
    process(newWriter(), new DummyRevision("ref"));
    assertThatCheckout(repo(), primaryBranch).containsFile("excluded.txt", "some content").containsFile("normal_file.txt", "some more content").containsNoMoreFiles();
}
Also used : Path(java.nio.file.Path) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 23 with DummyRevision

use of com.google.copybara.testing.DummyRevision in project copybara by google.

the class GitDestinationTest method testChangeDescriptionEmpty_empty_commit.

@Test
public void testChangeDescriptionEmpty_empty_commit() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    Path scratchTree = Files.createTempDirectory("GitDestinationTest-testLocalRepo");
    Files.write(scratchTree.resolve("foo"), "foo\n".getBytes(UTF_8));
    repo().withWorkTree(scratchTree).add().force().files("foo").run();
    repo().withWorkTree(scratchTree).simpleCommand("commit", "-a", "-m", "change");
    DummyRevision originRef = new DummyRevision("origin_ref");
    WriterContext writerContext = new WriterContext("GitDestinationTest", "test", true, new DummyRevision("origin_ref1"), Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = destination().newWriter(writerContext);
    ValidationException e = assertThrows(EmptyChangeException.class, () -> writer.write(TransformResults.of(workdir, originRef).withSummary("").withLabelFinder(s -> ImmutableList.of()).withSetRevId(false), Glob.createGlob(ImmutableList.of("nothing_to_be_found"), ImmutableList.of("test.txt")), console));
    assertThat(e).hasMessageThat().contains("Migration of the revision resulted in an empty change");
}
Also used : Path(java.nio.file.Path) TransformResult(com.google.copybara.TransformResult) ZonedDateTime(java.time.ZonedDateTime) DestinationEffect(com.google.copybara.DestinationEffect) WriterContext(com.google.copybara.WriterContext) VisitResult(com.google.copybara.ChangeVisitable.VisitResult) Author(com.google.copybara.authoring.Author) GitTestUtil.getGitEnv(com.google.copybara.testing.git.GitTestUtil.getGitEnv) Change(com.google.copybara.Change) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ZoneOffset(java.time.ZoneOffset) GitTesting(com.google.copybara.git.testing.GitTesting) Path(java.nio.file.Path) DEFAULT_TIMEOUT(com.google.copybara.util.CommandRunner.DEFAULT_TIMEOUT) Type(com.google.copybara.DestinationEffect.Type) ImmutableSet(com.google.common.collect.ImmutableSet) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) ImmutableMap(com.google.common.collect.ImmutableMap) GitTesting.assertThatCheckout(com.google.copybara.git.testing.GitTesting.assertThatCheckout) DestinationStatus(com.google.copybara.Destination.DestinationStatus) Instant(java.time.Instant) ZoneId(java.time.ZoneId) CheckerException(com.google.copybara.checks.CheckerException) List(java.util.List) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Writer(com.google.copybara.Destination.Writer) DummyRevision(com.google.copybara.testing.DummyRevision) Assert.assertThrows(org.junit.Assert.assertThrows) DummyOrigin(com.google.copybara.testing.DummyOrigin) RunWith(org.junit.runner.RunWith) RepoException(com.google.copybara.exception.RepoException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) MessageType(com.google.copybara.util.console.Message.MessageType) Changes(com.google.copybara.Changes) ChangeMessage(com.google.copybara.ChangeMessage) ArrayList(java.util.ArrayList) Truth(com.google.common.truth.Truth) ImmutableList(com.google.common.collect.ImmutableList) TransformResults(com.google.copybara.testing.TransformResults) CommandOutput(com.google.copybara.util.CommandOutput) Nullable(javax.annotation.Nullable) Before(org.junit.Before) Functions(com.google.common.base.Functions) ChangeMessage.parseMessage(com.google.copybara.ChangeMessage.parseMessage) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) DummyChecker(com.google.copybara.testing.DummyChecker) ValidationException(com.google.copybara.exception.ValidationException) UserPassword(com.google.copybara.git.GitCredential.UserPassword) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Maps(com.google.common.collect.Maps) Baseline(com.google.copybara.Origin.Baseline) Glob(com.google.copybara.util.Glob) TransformWorks(com.google.copybara.testing.TransformWorks) GitTestUtil.writeFile(com.google.copybara.testing.git.GitTestUtil.writeFile) WriterContext(com.google.copybara.WriterContext) ValidationException(com.google.copybara.exception.ValidationException) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 24 with DummyRevision

use of com.google.copybara.testing.DummyRevision in project copybara by google.

the class GitDestinationTest method processWithBaseline.

@Test
public void processWithBaseline() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    DummyRevision ref = new DummyRevision("origin_ref");
    Files.write(workdir.resolve("test.txt"), "some content".getBytes(UTF_8));
    Files.write(workdir.resolve("excluded"), "some content".getBytes(UTF_8));
    process(firstCommitWriter(), ref);
    String firstCommit = repo().parseRef("HEAD");
    Files.write(workdir.resolve("test.txt"), "new content".getBytes(UTF_8));
    process(newWriter(), ref);
    // Lets exclude now 'excluded' so that we check that the rebase correctly ignores
    // the missing file (IOW, it doesn't delete the file in the commit).
    destinationFiles = Glob.createGlob(ImmutableList.of("**"), ImmutableList.of("excluded"));
    Files.delete(workdir.resolve("excluded"));
    Files.write(workdir.resolve("test.txt"), "some content".getBytes(UTF_8));
    Files.write(workdir.resolve("other.txt"), "other file".getBytes(UTF_8));
    processWithBaseline(newWriter(), destinationFiles, ref, firstCommit);
    assertThatCheckout(repo(), primaryBranch).containsFile("test.txt", "new content").containsFile("other.txt", "other file").containsFile("excluded", "some content").containsNoMoreFiles();
}
Also used : DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 25 with DummyRevision

use of com.google.copybara.testing.DummyRevision in project copybara by google.

the class GitDestinationTest method processWithBaseline_noRebase.

@Test
public void processWithBaseline_noRebase() throws Exception {
    options.gitDestination.noRebase = true;
    options.setForce(true);
    fetch = primaryBranch;
    push = primaryBranch;
    DummyRevision ref = new DummyRevision("origin_ref");
    Files.write(workdir.resolve("test.txt"), "some content".getBytes(UTF_8));
    Files.write(workdir.resolve("excluded"), "some content".getBytes(UTF_8));
    process(firstCommitWriter(), ref);
    String firstCommit = repo().parseRef(primaryBranch);
    Files.write(workdir.resolve("test.txt"), "new content".getBytes(UTF_8));
    process(newWriter(), ref);
    // Lets exclude now 'excluded' so that we check that the rebase correctly ignores
    // the missing file (IOW, it doesn't delete the file in the commit).
    destinationFiles = Glob.createGlob(ImmutableList.of("**"), ImmutableList.of("excluded"));
    Files.delete(workdir.resolve("excluded"));
    Files.write(workdir.resolve("test.txt"), "some content".getBytes(UTF_8));
    Files.write(workdir.resolve("other.txt"), "other file".getBytes(UTF_8));
    push = "refs/heads/my_branch";
    processWithBaseline(newWriter(), destinationFiles, ref, firstCommit);
    assertThatCheckout(repo(), "refs/heads/my_branch").containsFile("test.txt", "some content").containsFile("other.txt", "other file").containsFile("excluded", "some content").containsNoMoreFiles();
}
Also used : DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Aggregations

DummyRevision (com.google.copybara.testing.DummyRevision)144 Test (org.junit.Test)124 WriterContext (com.google.copybara.WriterContext)58 Path (java.nio.file.Path)29 DestinationEffect (com.google.copybara.DestinationEffect)28 Author (com.google.copybara.authoring.Author)21 Changes (com.google.copybara.Changes)19 Glob (com.google.copybara.util.Glob)19 ValidationException (com.google.copybara.exception.ValidationException)18 IOException (java.io.IOException)16 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)14 TransformResult (com.google.copybara.TransformResult)13 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)13 ZonedDateTime (java.time.ZonedDateTime)13 TransformWork (com.google.copybara.TransformWork)12 RepoException (com.google.copybara.exception.RepoException)12 CheckerException (com.google.copybara.checks.CheckerException)11 DummyChecker (com.google.copybara.testing.DummyChecker)11 FileSubjects.assertThatPath (com.google.copybara.testing.FileSubjects.assertThatPath)11 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)11