Search in sources :

Example 6 with GitApiMockHttpTransport

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

the class GithubPrOriginTest 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, () -> this.options.general, new Validator() {

        @Override
        public void validateFetch(String url, boolean prune, boolean force, Iterable<String> refspecs) {
            for (String refspec : refspecs) {
                // WARNING! This check is important. While using short names like
                // 'master' in git fetch works for local git invocations, other
                // implementations of GitRepository might have problems if we don't
                // pass the whole reference.
                assertThat(refspec).startsWith("refs/");
                assertThat(refspec).contains(":refs/");
            }
        }
    });
    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();
    skylark = new SkylarkTestExecutor(options, GitModule.class);
    skylarkParser = new SkylarkParser(ImmutableSet.of(Core.class, Authoring.Module.class, FolderModule.class, GitModule.class));
}
Also used : Path(java.nio.file.Path) SkylarkParser(com.google.copybara.config.SkylarkParser) 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) HttpTransport(com.google.api.client.http.HttpTransport) GitApiMockHttpTransport(com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport) Authoring(com.google.copybara.authoring.Authoring) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Validator(com.google.copybara.testing.git.GitTestUtil.Validator) Before(org.junit.Before)

Example 7 with GitApiMockHttpTransport

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

the class GerritDestinationTest method changeAlreadyMergedTooOften.

@Test
public void changeAlreadyMergedTooOften() throws Exception {
    fetch = "master";
    Files.write(workdir.resolve("file"), "some content".getBytes());
    options.setForce(true);
    String firstChangeId = "I" + Hashing.sha1().newHasher().putString("origin_ref", StandardCharsets.UTF_8).putString(options.gitDestination.committerEmail, StandardCharsets.UTF_8).putInt(0).hash();
    String secondChangeId = "I" + Hashing.sha1().newHasher().putString("origin_ref", Charsets.UTF_8).putString(options.gitDestination.committerEmail, StandardCharsets.UTF_8).putInt(1).hash();
    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 change = changeIdFromRequest(url);
                String result = "[" + "{" + "  change_id : \"" + change + "\"," + "  status : \"MERGED\"" + "}]";
                return result.getBytes(UTF_8);
            }
            throw new IllegalArgumentException(method + " " + url);
        }
    };
    try {
        process(new DummyRevision("origin_ref"));
        fail("Should have thrown RepoException");
    } catch (RepoException expected) {
        assertThat(expected.getMessage()).contains("Unable to find unmerged change for ");
    }
}
Also used : DummyRevision(com.google.copybara.testing.DummyRevision) GitApiMockHttpTransport(com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport) IOException(java.io.IOException) RepoException(com.google.copybara.exception.RepoException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 8 with GitApiMockHttpTransport

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

the class GerritDestinationTest method changeAlreadyMergedOnce.

@Test
public void changeAlreadyMergedOnce() throws Exception {
    fetch = "master";
    Files.write(workdir.resolve("file"), "some content".getBytes());
    options.setForce(true);
    String firstChangeId = "I" + Hashing.sha1().newHasher().putString("origin_ref", StandardCharsets.UTF_8).putString(options.gitDestination.committerEmail, StandardCharsets.UTF_8).putInt(0).hash();
    String secondChangeId = "I" + Hashing.sha1().newHasher().putString("origin_ref", StandardCharsets.UTF_8).putString(options.gitDestination.committerEmail, StandardCharsets.UTF_8).putInt(1).hash();
    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 change = changeIdFromRequest(url);
                String result = "[" + "{" + "  change_id : \"" + change + "\"," + "  status : \"" + (change.equals(firstChangeId) ? "MERGED" : "NEW") + "\"" + "}]";
                return result.getBytes(UTF_8);
            }
            throw new IllegalArgumentException(method + " " + url);
        }
    };
    process(new DummyRevision("origin_ref"));
    assertThat(lastCommitChangeIdLine("origin_ref", repo())).isEqualTo(GerritDestination.CHANGE_ID_LABEL + ": " + secondChangeId);
}
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)

Aggregations

GitApiMockHttpTransport (com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport)8 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)6 IOException (java.io.IOException)6 DummyRevision (com.google.copybara.testing.DummyRevision)5 Test (org.junit.Test)5 RepoException (com.google.copybara.exception.RepoException)3 Path (java.nio.file.Path)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 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)2 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)2 TestGitOptions (com.google.copybara.testing.git.GitTestUtil.TestGitOptions)2 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)2 Before (org.junit.Before)2 Authoring (com.google.copybara.authoring.Authoring)1 SkylarkParser (com.google.copybara.config.SkylarkParser)1 Validator (com.google.copybara.testing.git.GitTestUtil.Validator)1