Search in sources :

Example 71 with DummyRevision

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

the class MetadataModuleTest method testCurrentRevForSquashEmptyChanges.

@Test
public void testCurrentRevForSquashEmptyChanges() throws Exception {
    options.setForce(true);
    passThruAuthoring();
    options.setLastRevision(origin.resolve("HEAD").asString());
    origin.singleFileChange(42, "test", "excluded", "");
    DummyRevision expectedRev = origin.resolve("HEAD");
    Config config = loadConfig("" + "core.workflow(\n" + "    name = 'default',\n" + "    origin =  testing.origin(),\n" + "    origin_files =  glob(['**'], exclude = ['excluded']),\n" + "    authoring = " + authoring + "\n," + "    destination = testing.destination(),\n" + "    mode = '" + WorkflowMode.SQUASH + "',\n" + "    transformations = [" + "        metadata.replace_message('${COPYBARA_CURRENT_REV}')," + "    ]\n" + ")\n");
    config.getMigration("default").run(workdir, ImmutableList.of());
    assertThat(Iterables.getLast(destination.processed).getChangesSummary()).isEqualTo(expectedRev.asString());
}
Also used : Config(com.google.copybara.config.Config) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 72 with DummyRevision

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

the class MetadataModuleTest method testSquashNotesWithMerge.

@Test
public void testSquashNotesWithMerge() throws Exception {
    Changes changes = new Changes(ImmutableList.of(new Change<>(new DummyRevision("3"), ORIGINAL_AUTHOR, "merge", fakeDate(), ImmutableListMultimap.of(), /*changeFiles=*/
    null, /*merge=*/
    true, ImmutableList.of(new DummyRevision("10"), new DummyRevision("20"))), new Change<>(new DummyRevision("2"), ORIGINAL_AUTHOR, "change2", fakeDate(), ImmutableListMultimap.of()), new Change<>(new DummyRevision("1"), ORIGINAL_AUTHOR, "change1", fakeDate(), ImmutableListMultimap.of())), ImmutableList.of());
    TransformWork work = TransformWorks.of(workdir, "the message", testingConsole).withChanges(changes);
    // The default is to use merges, since git.origin does --first-parent by default
    skylark.<MetadataSquashNotes>eval("s", "s = metadata.squash_notes()").transform(work);
    assertThat(work.getMessage()).isEqualTo("Copybara import of the project:\n" + "\n" + "  - 3 merge by Foo Bar <foo@bar.com>\n" + "  - 2 change2 by Foo Bar <foo@bar.com>\n" + "  - 1 change1 by Foo Bar <foo@bar.com>\n");
    work = TransformWorks.of(workdir, "the message", testingConsole).withChanges(changes);
    skylark.<MetadataSquashNotes>eval("s", "s = metadata.squash_notes(use_merge = False)").transform(work);
    assertThat(work.getMessage()).isEqualTo("Copybara import of the project:\n" + "\n" + "  - 2 change2 by Foo Bar <foo@bar.com>\n" + "  - 1 change1 by Foo Bar <foo@bar.com>\n");
}
Also used : Changes(com.google.copybara.Changes) TransformWork(com.google.copybara.TransformWork) DummyRevision(com.google.copybara.testing.DummyRevision) TransformWorks.toChange(com.google.copybara.testing.TransformWorks.toChange) ProcessedChange(com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange) Change(com.google.copybara.Change) Test(org.junit.Test)

Example 73 with DummyRevision

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

the class MetadataModuleTest method testExposeLabelAll.

@Test
public void testExposeLabelAll() throws Exception {
    TransformWork tw = TransformWorks.of(workdir, "some message\n\n" + "LABEL=aaa", testingConsole).withChanges(new Changes(ImmutableList.of(toChange(new DummyRevision("1").withLabels(ImmutableListMultimap.of("LABEL", "bbb")), ORIGINAL_AUTHOR), toChange(new DummyRevision("2").withLabels(ImmutableListMultimap.of("LABEL", "bbb")), ORIGINAL_AUTHOR), toChange(new DummyRevision("2").withLabels(ImmutableListMultimap.of("LABEL", "ccc")), ORIGINAL_AUTHOR)), ImmutableList.of())).withResolvedReference(new DummyRevision("123").withLabels(ImmutableListMultimap.of("LABEL", "ddd")));
    Transformation t = skylark.eval("t", "t = " + "metadata.expose_label('LABEL', 'NEW_VALUE', all = True)");
    t.transform(tw);
    assertThat(tw.getMessage()).isEqualTo("some message\n" + "\n" + "LABEL=aaa\n" + "NEW_VALUE=aaa\n" + "NEW_VALUE=bbb\n" + "NEW_VALUE=ccc\n" + "NEW_VALUE=ddd\n");
}
Also used : Changes(com.google.copybara.Changes) Transformation(com.google.copybara.Transformation) TransformWork(com.google.copybara.TransformWork) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 74 with DummyRevision

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

the class MetadataModuleTest method checkExposeLabel.

private void checkExposeLabel(String msg, String transform, String expectedOutput) throws ValidationException, IOException {
    TransformWork tw = TransformWorks.of(workdir, msg, testingConsole).withChanges(EMPTY_CHANGES).withResolvedReference(new DummyRevision("123").withLabels(ImmutableMap.of("SOME", "value")));
    Transformation t = skylarkExecutor.eval("t", "t = " + transform);
    t.transform(tw);
    assertThat(tw.getMessage()).isEqualTo(expectedOutput);
}
Also used : Transformation(com.google.copybara.Transformation) TransformWork(com.google.copybara.TransformWork) DummyRevision(com.google.copybara.testing.DummyRevision)

Example 75 with DummyRevision

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

the class GithubPrDestinationTest method checkWrite.

private void checkWrite(String groupId) throws ValidationException, RepoException, IOException {
    gitApiMockHttpTransport = new GitApiMockHttpTransport() {

        @Override
        protected byte[] getContent(String method, String url, MockLowLevelHttpRequest request) throws IOException {
            boolean isPulls = "https://api.github.com/repos/foo/pulls".equals(url);
            if ("GET".equals(method) && isPulls) {
                return "[]".getBytes(UTF_8);
            } else if ("POST".equals(method) && isPulls) {
                assertThat(request.getContentAsString()).isEqualTo("{\"base\":\"master\",\"body\":\"test summary\",\"head\":\"feature\",\"title\":\"test summary\"}");
                return ("{\n" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"open\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"" + "}").getBytes();
            }
            fail(method + " " + url);
            throw new IllegalStateException();
        }
    };
    GithubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo'" + ")");
    Writer<GitRevision> writer = d.newWriter(Glob.ALL_FILES, /*dryRun=*/
    false, groupId, /*oldWriter=*/
    null);
    GitRepository remote = localHubRepo("foo");
    addFiles(remote, null, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").build());
    Files.write(this.workdir.resolve("test.txt"), "some content".getBytes());
    writer.write(TransformResults.of(this.workdir, new DummyRevision("one")), console);
    Files.write(this.workdir.resolve("test.txt"), "other content".getBytes());
    writer.write(TransformResults.of(this.workdir, new DummyRevision("two")), console);
    // Use a new writer that shares the old state
    writer = d.newWriter(Glob.ALL_FILES, /*dryRun=*/
    false, groupId, /*oldWriter=*/
    writer);
    Files.write(this.workdir.resolve("test.txt"), "and content".getBytes());
    writer.write(TransformResults.of(this.workdir, new DummyRevision("three")), console);
    console.assertThat().timesInLog(1, MessageType.INFO, "Pull Request https://github.com/foo/pull/12345 created using branch 'feature'.");
    assertThat(remote.refExists("feature")).isTrue();
    assertThat(Iterables.transform(remote.log("feature").run(), GitLogEntry::getBody)).containsExactly("first change\n", "test summary\n" + "\n" + "DummyOrigin-RevId: one\n", "test summary\n" + "\n" + "DummyOrigin-RevId: two\n", "test summary\n" + "\n" + "DummyOrigin-RevId: three\n");
    // If we don't keep writer state (same as a new migration). We do a rebase of
    // all the changes.
    writer = d.newWriter(Glob.ALL_FILES, /*dryRun=*/
    false, groupId, /*oldWriter=*/
    null);
    Files.write(this.workdir.resolve("test.txt"), "and content".getBytes());
    writer.write(TransformResults.of(this.workdir, new DummyRevision("four")), console);
    assertThat(Iterables.transform(remote.log("feature").run(), GitLogEntry::getBody)).containsExactly("first change\n", "test summary\n" + "\n" + "DummyOrigin-RevId: four\n");
}
Also used : DummyRevision(com.google.copybara.testing.DummyRevision) GitApiMockHttpTransport(com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry)

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