Search in sources :

Example 16 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class GitDestinationTest method testTagWithLabel.

@Test
public void testTagWithLabel() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    tagName = "tag_${my_tag}";
    tagMsg = "msg_${my_msg}";
    Files.write(workdir.resolve("test.txt"), "some content".getBytes(UTF_8));
    options.setForce(true);
    WriterContext writerContext = new WriterContext("piper_to_github", "TEST", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
    evalDestination().newWriter(writerContext).write(TransformResults.of(workdir, new DummyRevision("ref1")), destinationFiles, console);
    options.setForce(false);
    Files.write(workdir.resolve("test.txt"), "some content 2".getBytes(UTF_8));
    evalDestinationWithTag(tagMsg).newWriter(writerContext).write(TransformResults.of(workdir, new DummyRevision("ref2")).withLabelFinder(Functions.forMap(ImmutableMap.of("my_tag", ImmutableList.of("12345"), "my_msg", ImmutableList.of("2345")))), destinationFiles, console);
    CommandOutput commandOutput = repo().simpleCommand("tag", "-n9");
    assertThat(commandOutput.getStdout()).matches(".*tag_12345.*msg_2345\n");
}
Also used : WriterContext(com.google.copybara.WriterContext) CommandOutput(com.google.copybara.util.CommandOutput) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 17 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class GitDestinationTest method getDestinationStatusForFakeMergeAndNonEmptyRoots.

/**
 * regression to ensure we don't do:
 *
 *     git log -- some_path
 *
 *  This doesn't work for fake merges as the merge is not shown when a path is passed even
 *  with -m.
 */
@Test
public void getDestinationStatusForFakeMergeAndNonEmptyRoots() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    Files.createDirectories(workdir.resolve("dir"));
    Files.write(workdir.resolve("dir/file"), "".getBytes(UTF_8));
    GitRepository repo = repo().withWorkTree(workdir);
    repo.add().files("dir/file").run();
    repo.simpleCommand("commit", "-m", "first commit");
    repo.simpleCommand("branch", "foo");
    Files.write(workdir.resolve("dir/file"), "other".getBytes(UTF_8));
    repo.add().files("dir/file").run();
    repo.simpleCommand("commit", "-m", "first commit");
    repo.forceCheckout("foo");
    Files.write(workdir.resolve("dir/file"), "feature".getBytes(UTF_8));
    repo.add().files("dir/file").run();
    repo.simpleCommand("commit", "-m", "first commit");
    repo.forceCheckout(primaryBranch);
    // Fake merge
    repo.simpleCommand("merge", "-Xours", "foo", "-m", "A fake merge\n\n" + DummyOrigin.LABEL_NAME + ": foo");
    destinationFiles = Glob.createGlob(ImmutableList.of("dir/**"));
    WriterContext writerContext = new WriterContext("piper_to_github", "TEST", false, new DummyRevision("feature"), Glob.ALL_FILES.roots());
    DestinationStatus status = destination().newWriter(writerContext).getDestinationStatus(destinationFiles, DummyOrigin.LABEL_NAME);
    assertThat(status).isNotNull();
    assertThat(status.getBaseline()).isEqualTo("foo");
}
Also used : WriterContext(com.google.copybara.WriterContext) DestinationStatus(com.google.copybara.Destination.DestinationStatus) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 18 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class GitDestinationTest method testChangeDescriptionEmpty.

@Test
public void testChangeDescriptionEmpty() 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(ValidationException.class, () -> writer.write(TransformResults.of(workdir, originRef).withSummary(" "), Glob.createGlob(ImmutableList.of("**"), ImmutableList.of("test.txt")), console));
    assertThat(e).hasMessageThat().contains("Change description is empty");
}
Also used : Path(java.nio.file.Path) WriterContext(com.google.copybara.WriterContext) ValidationException(com.google.copybara.exception.ValidationException) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 19 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class GitDestinationTest method setUpForTestingTag.

private WriterContext setUpForTestingTag() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    Files.write(workdir.resolve("test.txt"), "some content".getBytes(UTF_8));
    options.setForce(true);
    WriterContext writerContext = new WriterContext("piper_to_github", "TEST", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
    evalDestination().newWriter(writerContext).write(TransformResults.of(workdir, new DummyRevision("ref1")), destinationFiles, console);
    Files.write(workdir.resolve("test.txt"), "some content 2".getBytes(UTF_8));
    // push tag without tagMsg
    evalDestinationWithTag(null).newWriter(writerContext).write(TransformResults.of(workdir, new DummyRevision("ref2")), destinationFiles, console);
    Files.write(workdir.resolve("test.txt"), "some content 3".getBytes(UTF_8));
    return writerContext;
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision)

Example 20 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class GitHubDestinationTest method testPrToUpdateIngoredForInitHistory.

@Test
public void testPrToUpdateIngoredForInitHistory() throws Exception {
    options.workflowOptions.initHistory = true;
    addFiles(remote, primaryBranch, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "foo").buildOrThrow());
    WriterContext writerContext = new WriterContext("piper_to_github", "test", false, new DummyRevision("origin_ref1"), Glob.ALL_FILES.roots());
    writeFile(this.workdir, "test.txt", "some content");
    Writer<GitRevision> writer = destinationWithExistingPrBranch("other", "True").newWriter(writerContext);
    DummyRevision ref = new DummyRevision("origin_ref1");
    TransformResult result = TransformResults.of(workdir, ref);
    Changes changes = new Changes(ImmutableList.of(new Change<>(ref, new Author("foo", "foo@foo.com"), "message", ZonedDateTime.now(ZoneOffset.UTC), ImmutableListMultimap.of("my_label", "12345")), new Change<>(ref, new Author("foo", "foo@foo.com"), "message", ZonedDateTime.now(ZoneOffset.UTC), ImmutableListMultimap.of("my_label", "6789"))), ImmutableList.of());
    result = result.withChanges(changes);
    ImmutableList<DestinationEffect> destinationResult = writer.write(result, destinationFiles, console);
    assertThat(destinationResult).hasSize(1);
    assertThat(destinationResult.get(0).getErrors()).isEmpty();
    assertThat(destinationResult.get(0).getType()).isEqualTo(Type.CREATED);
    assertThat(destinationResult.get(0).getDestinationRef().getType()).isEqualTo("commit");
    assertThat(destinationResult.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
    // This is a migration of two changes (use the same ref because mocks)
    verifyNoInteractions(gitUtil.httpTransport());
    GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("test.txt", "some content").containsNoMoreFiles();
    assertThat(remote.simpleCommand("show-ref").getStdout()).doesNotContain("other");
}
Also used : Changes(com.google.copybara.Changes) WriterContext(com.google.copybara.WriterContext) TransformResult(com.google.copybara.TransformResult) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.Change) Test(org.junit.Test)

Aggregations

WriterContext (com.google.copybara.WriterContext)58 DummyRevision (com.google.copybara.testing.DummyRevision)58 Test (org.junit.Test)46 DestinationEffect (com.google.copybara.DestinationEffect)21 Glob (com.google.copybara.util.Glob)17 Path (java.nio.file.Path)15 Author (com.google.copybara.authoring.Author)14 Changes (com.google.copybara.Changes)13 ValidationException (com.google.copybara.exception.ValidationException)12 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)11 CheckerException (com.google.copybara.checks.CheckerException)10 DummyChecker (com.google.copybara.testing.DummyChecker)10 MockRequestAssertion (com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion)10 CommandOutput (com.google.copybara.util.CommandOutput)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 ImmutableList (com.google.common.collect.ImmutableList)8 ImmutableSet (com.google.common.collect.ImmutableSet)8 Truth.assertThat (com.google.common.truth.Truth.assertThat)8 ChangeMessage (com.google.copybara.ChangeMessage)8 Writer (com.google.copybara.Destination.Writer)8