Search in sources :

Example 56 with DummyRevision

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

the class GitHubDestinationTest method testCheckerOnPush.

@Test
public void testCheckerOnPush() throws Exception {
    options.workflowOptions.initHistory = true;
    addFiles(remote, primaryBranch, "first change", ImmutableMap.of("foo.txt", "hello"));
    options.testingOptions.checker = new DummyChecker(ImmutableSet.of("BAD"));
    GitDestination d = skylark.eval("r", "r = git.github_destination(" + "    url = '" + url + "', \n" + "    push = '" + primaryBranch + "',\n" + "    checker = testing.dummy_checker(),\n" + ")");
    WriterContext writerContext = new WriterContext("piper_to_github", "test", false, new DummyRevision("origin_ref1"), Glob.ALL_FILES.roots());
    writeFile(this.workdir, "test.txt", "BAD");
    Writer<GitRevision> writer = d.newWriter(writerContext);
    DummyRevision ref = new DummyRevision("origin_ref1");
    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());
    TransformResult result = TransformResults.of(workdir, ref).withChanges(changes);
    assertThat(assertThrows(CheckerException.class, () -> writer.write(result, destinationFiles, console))).hasMessageThat().contains("Bad word 'bad' found");
}
Also used : Changes(com.google.copybara.Changes) WriterContext(com.google.copybara.WriterContext) TransformResult(com.google.copybara.TransformResult) CheckerException(com.google.copybara.checks.CheckerException) DummyRevision(com.google.copybara.testing.DummyRevision) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.Change) DummyChecker(com.google.copybara.testing.DummyChecker) Test(org.junit.Test)

Example 57 with DummyRevision

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

the class GitHubDestinationTest method testDryRun.

@Test
public void testDryRun() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    addFiles(remote, primaryBranch, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "foo").buildOrThrow());
    remote.simpleCommand("branch", "other");
    WriterContext writerContext = new WriterContext("piper_to_github", "test", true, new DummyRevision("origin_ref1"), Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = destination().newWriter(writerContext);
    process(writer, new DummyRevision("origin_ref1"));
    GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("foo.txt", "foo").containsNoMoreFiles();
    Files.write(workdir.resolve("test.txt"), "some content".getBytes(UTF_8));
    // Run again without dry run
    writer = newWriter();
    process(writer, new DummyRevision("origin_ref1"));
    GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("test.txt", "some content").containsNoMoreFiles();
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 58 with DummyRevision

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

the class GitHubDestinationTest method testPrToUpdateWithLabel.

@Test
public void testPrToUpdateWithLabel() throws Exception {
    gitUtil.mockApi("GET", "https://api.github.com/repos/foo/git/refs/heads/other_12345", mockResponse("{\n" + "\"ref\" : \"refs/heads/test_existing_12345_pr\",\n" + "\"node_id\" : \"MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlQQ==\",\n" + "\"url\" :" + " \"https://api.github.com/repos/octocat/Hello-World/git/refs/heads/test_existing_12345_pr\",\n" + "\"object\" : {\n" + "         \"type\" : \"commit\",\n" + "         \"sha\" : \"aa218f56b14c9653891f9e74264a383fa43fefbd\",\n" + "         \"url\" :" + " \"https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd\"\n" + "       }\n" + "}"));
    gitUtil.mockApi("GET", "https://api.github.com/repos/foo/git/refs/heads/other_6789", mockResponse("{\n" + "\"ref\" : \"refs/heads/test_existing_6789_pr\",\n" + "\"node_id\" : \"MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlQQ==\",\n" + "\"url\" :" + " \"https://api.github.com/repos/octocat/Hello-World/git/refs/heads/test_existing_6789_pr\",\n" + "\"object\" : {\n" + "         \"type\" : \"commit\",\n" + "         \"sha\" : \"aa218f56b14c9653891f9e74264a383fa43fefbd\",\n" + "         \"url\" :" + " \"https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd\"\n" + "       }\n" + "}"));
    addFiles(remote, primaryBranch, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "foo").buildOrThrow());
    remote.simpleCommand("branch", "other_12345");
    remote.simpleCommand("branch", "other_6789");
    GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("foo.txt", "foo").containsNoMoreFiles();
    GitTesting.assertThatCheckout(remote, "other_12345").containsFile("foo.txt", "foo").containsNoMoreFiles();
    GitTesting.assertThatCheckout(remote, "other_6789").containsFile("foo.txt", "foo").containsNoMoreFiles();
    writeFile(this.workdir, "test.txt", "some content");
    WriterContext writerContext = new WriterContext("piper_to_github", "test", false, new DummyRevision("origin_ref1"), Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = destinationWithExistingPrBranch("other_${my_label}", /*deletePrBranch=*/
    "None").newWriter(writerContext);
    process(writer, new DummyRevision("origin_ref1"));
    GitTesting.assertThatCheckout(remote, primaryBranch).containsFile("test.txt", "some content").containsNoMoreFiles();
    GitTesting.assertThatCheckout(remote, "other_12345").containsFile("test.txt", "some content").containsNoMoreFiles();
    GitTesting.assertThatCheckout(remote, "other_6789").containsFile("test.txt", "some content").containsNoMoreFiles();
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 59 with DummyRevision

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

the class GitHubPrDestinationTest method testBranchNameFromUser.

private void testBranchNameFromUser(String branchNameFromUser, String expectedBranchName, String contextReference) throws ValidationException, IOException, RepoException {
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo'," + "    destination_ref = 'other'," + "    pr_branch = " + "\'" + branchNameFromUser + "\'," + ")");
    DummyRevision dummyRevision = new DummyRevision("dummyReference", contextReference);
    mockNoPullRequestsGet(expectedBranchName);
    gitUtil.mockApi("POST", "https://api.github.com/repos/foo/pulls", mockResponseAndValidateRequest("{\n" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"open\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"" + "}", MockRequestAssertion.equals("{\"base\":\"other\",\"body\":\"test summary\\n\",\"head\":\"" + expectedBranchName + "\",\"title\":\"test summary\"}")));
    WriterContext writerContext = new WriterContext("piper_to_github_pr", "TEST", false, dummyRevision, Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = d.newWriter(writerContext);
    GitRepository remote = gitUtil.mockRemoteRepo("github.com/foo");
    addFiles(remote, "main", "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").buildOrThrow());
    addFiles(remote, "other", "second change", ImmutableMap.<String, String>builder().put("foo.txt", "test").buildOrThrow());
    writeFile(this.workdir, "test.txt", "some content");
    writer.write(TransformResults.of(this.workdir, new DummyRevision("one")), Glob.ALL_FILES, console);
    assertThat(remote.refExists(expectedBranchName)).isTrue();
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision)

Example 60 with DummyRevision

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

the class GitHubPrDestinationTest method getWriterForTestEmptyDiff.

private Writer<GitRevision> getWriterForTestEmptyDiff() throws Exception {
    gitUtil = new GitTestUtil(options);
    gitUtil.mockRemoteGitRepos();
    options.gitDestination = new GitDestinationOptions(options.general, options.git);
    options.gitDestination.committerEmail = "commiter@email";
    options.gitDestination.committerName = "Bara Kopi";
    skylark = new SkylarkTestExecutor(options);
    options.githubDestination.destinationPrBranch = "test_feature";
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo', \n" + "    title = 'Title ${aaa}',\n" + "    body = 'Body ${aaa}',\n" + "    allow_empty_diff = False,\n" + "    destination_ref = 'main',\n" + "    pr_branch = 'test_${CONTEXT_REFERENCE}',\n" + "    primary_branch_migration = " + primaryBranchMigration + ",\n" + ")");
    WriterContext writerContext = new WriterContext("piper_to_github", "TEST", false, new DummyRevision("feature", "feature"), Glob.ALL_FILES.roots());
    return d.newWriter(writerContext);
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) GitTestUtil(com.google.copybara.testing.git.GitTestUtil) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor)

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