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);
}
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);
}
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();
}
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);
}
};
}
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");
}
Aggregations