Search in sources :

Example 1 with GitApiMockHttpTransport

use of com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport in project copybara by google.

the class GithubPrDestinationTest method setup.

@Before
public void setup() throws Exception {
    repoGitDir = Files.createTempDirectory("GithubPrDestinationTest-repoGitDir");
    workdir = Files.createTempDirectory("workdir");
    localHub = Files.createTempDirectory("localHub");
    git("init", "--bare", repoGitDir.toString());
    console = new TestingConsole();
    options = new OptionsBuilder().setConsole(console).setOutputRootToTmpDir();
    options.git = new TestGitOptions(localHub, () -> GithubPrDestinationTest.this.options.general);
    options.github = new GithubOptions(() -> options.general, options.git) {

        @Override
        public GithubApi getApi(String project) throws RepoException {
            assertThat(project).isEqualTo(expectedProject);
            return super.getApi(project);
        }

        @Override
        protected HttpTransport getHttpTransport() {
            return gitApiMockHttpTransport;
        }
    };
    Path credentialsFile = Files.createTempFile("credentials", "test");
    Files.write(credentialsFile, "https://user:SECRET@github.com".getBytes(UTF_8));
    options.git.credentialHelperStorePath = credentialsFile.toString();
    options.gitDestination = new GitDestinationOptions(() -> options.general, options.git);
    options.gitDestination.committerEmail = "commiter@email";
    options.gitDestination.committerName = "Bara Kopi";
    skylark = new SkylarkTestExecutor(options, GitModule.class);
}
Also used : Path(java.nio.file.Path) TestGitOptions(com.google.copybara.testing.git.GitTestUtil.TestGitOptions) GithubApi(com.google.copybara.git.github.api.GithubApi) RepoException(com.google.copybara.exception.RepoException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) GitApiMockHttpTransport(com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Before(org.junit.Before)

Example 2 with GitApiMockHttpTransport

use of com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport in project copybara by google.

the class GithubPrDestinationTest method testCustomTitleAndBody.

@Test
public void testCustomTitleAndBody() throws ValidationException, IOException, RepoException {
    options.githubDestination.destinationPrBranch = "feature";
    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\":\"custom body\"," + "\"head\":\"feature\"," + "\"title\":\"custom title\"}");
                return ("{\n" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"open\",\n" + "  \"title\": \"custom title\",\n" + "  \"body\": \"custom body\"" + "}").getBytes();
            }
            fail(method + " " + url);
            throw new IllegalStateException();
        }
    };
    GithubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo', \n" + "    title = 'custom title',\n" + "    body = 'custom body',\n" + ")");
    Writer<GitRevision> writer = d.newWriter(Glob.ALL_FILES, /*dryRun=*/
    false, null, /*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);
}
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) Test(org.junit.Test)

Example 3 with GitApiMockHttpTransport

use of com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport in project copybara by google.

the class GerritDestinationTest method reuseChangeId.

@Test
public void reuseChangeId() throws Exception {
    fetch = "master";
    Files.write(workdir.resolve("file"), "some content".getBytes());
    options.setForce(true);
    options.gerrit.gerritChangeId = null;
    url = "https://localhost:33333/foo/bar";
    GitRepository repo = localGerritRepo("localhost:33333/foo/bar");
    gitApiMockHttpTransport = NO_CHANGE_FOUND_MOCK;
    process(new DummyRevision("origin_ref"));
    String changeId = lastCommitChangeIdLine("origin_ref", repo);
    assertThat(changeId).matches(GerritDestination.CHANGE_ID_LABEL + ": I[a-z0-9]+");
    LabelFinder labelFinder = new LabelFinder(changeId);
    Files.write(workdir.resolve("file"), "some different content".getBytes());
    gitApiMockHttpTransport = new GitApiMockHttpTransport() {

        @Override
        protected byte[] getContent(String method, String url, MockLowLevelHttpRequest request) throws IOException {
            String expected = "https://localhost:33333/changes/?q=change:%20" + labelFinder.getValue() + "%20AND%20project:foo/bar";
            if (method.equals("GET") && url.equals(expected)) {
                String result = "[" + "{" + "  change_id : \"" + labelFinder.getValue() + "\"," + "  status : \"NEW\"" + "}]";
                return result.getBytes(UTF_8);
            }
            throw new IllegalArgumentException(method + " " + url);
        }
    };
    // Allow to push again in a non-fastforward way.
    repo.simpleCommand("update-ref", "-d", "refs/for/master");
    process(new DummyRevision("origin_ref"));
    assertThat(lastCommitChangeIdLine("origin_ref", repo)).isEqualTo(changeId);
    GitTesting.assertThatCheckout(repo, "refs/for/master").containsFile("file", "some different content").containsNoMoreFiles();
}
Also used : DummyRevision(com.google.copybara.testing.DummyRevision) GitApiMockHttpTransport(com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport) LabelFinder(com.google.copybara.LabelFinder) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 4 with GitApiMockHttpTransport

use of com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport in project copybara by google.

the class GerritDestinationTest method setup.

@Before
public void setup() throws Exception {
    workdir = Files.createTempDirectory("workdir");
    console = new TestingConsole();
    options = new OptionsBuilder().setConsole(console).setOutputRootToTmpDir();
    urlMapper = Files.createTempDirectory("url_mapper");
    options.git = new TestGitOptions(urlMapper, () -> options.general);
    options.gerrit = new GerritOptions(() -> options.general, options.git) {

        @Override
        protected GerritApiTransportImpl getGerritApiTransport(URI uri) throws RepoException {
            return new GerritApiTransportImpl(repo(), uri, gitApiMockHttpTransport);
        }
    };
    options.gitDestination = new GitDestinationOptions(() -> options.general, options.git);
    options.gitDestination.committerEmail = "commiter@email";
    options.gitDestination.committerName = "Bara Kopi";
    excludedDestinationPaths = ImmutableList.of();
    repoGitDir = localGerritRepo("localhost:33333/foo/bar").getGitDir();
    url = "https://localhost:33333/foo/bar";
    repo().init();
    skylark = new SkylarkTestExecutor(options, GitModule.class);
    gitApiMockHttpTransport = new GitApiMockHttpTransport() {

        @Override
        protected byte[] getContent(String method, String url, MockLowLevelHttpRequest request) throws IOException {
            if (method.equals("GET") && url.startsWith("https://localhost:33333/changes/")) {
                String result = "[" + "{" + "  change_id : \"" + changeIdFromRequest(url) + "\"," + "  status : \"NEW\"" + "}]";
                return result.getBytes(UTF_8);
            }
            throw new IllegalArgumentException(method + " " + url);
        }
    };
}
Also used : TestGitOptions(com.google.copybara.testing.git.GitTestUtil.TestGitOptions) RepoException(com.google.copybara.exception.RepoException) IOException(java.io.IOException) URI(java.net.URI) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) GerritApiTransportImpl(com.google.copybara.git.gerritapi.GerritApiTransportImpl) GitApiMockHttpTransport(com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport) Before(org.junit.Before)

Example 5 with GitApiMockHttpTransport

use of com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport 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

GitApiMockHttpTransport (com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport)10 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)8 IOException (java.io.IOException)8 DummyRevision (com.google.copybara.testing.DummyRevision)6 Test (org.junit.Test)6 RepoException (com.google.copybara.exception.RepoException)4 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)3 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)3 TestGitOptions (com.google.copybara.testing.git.GitTestUtil.TestGitOptions)3 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)3 Path (java.nio.file.Path)3 Before (org.junit.Before)3 HttpTransport (com.google.api.client.http.HttpTransport)2 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)2 GithubApi (com.google.copybara.git.github.api.GithubApi)2 LabelFinder (com.google.copybara.LabelFinder)1 Authoring (com.google.copybara.authoring.Authoring)1 SkylarkParser (com.google.copybara.config.SkylarkParser)1 GerritApiTransportImpl (com.google.copybara.git.gerritapi.GerritApiTransportImpl)1 Validator (com.google.copybara.testing.git.GitTestUtil.Validator)1