Search in sources :

Example 1 with GitTestUtil

use of com.google.copybara.testing.git.GitTestUtil 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();
    gitUtil = new GitTestUtil(options);
    Path credentialsFile = Files.createTempFile("credentials", "test");
    Files.write(credentialsFile, BASE_URL.getBytes(UTF_8));
    GitRepository repo = newBareRepo(Files.createTempDirectory("test_repo"), getGitEnv(), /*verbose=*/
    true, DEFAULT_TIMEOUT, /*noVerify=*/
    false).init().withCredentialHelper("store --file=" + credentialsFile);
    gitUtil.mockRemoteGitRepos(new Validator(), repo);
    options.gitDestination = new GitDestinationOptions(options.general, options.git);
    options.gitDestination.committerEmail = "commiter@email";
    options.gitDestination.committerName = "Bara Kopi";
    excludedDestinationPaths = ImmutableList.of();
    repoGitDir = gitUtil.mockRemoteRepo("localhost:33333/foo/bar").getGitDir();
    url = "https://localhost:33333/foo/bar";
    repo().init();
    skylark = new SkylarkTestExecutor(options);
    when(gitUtil.httpTransport().buildRequest(eq("GET"), startsWith("https://localhost:33333/changes/"))).then((Answer<LowLevelHttpRequest>) invocation -> {
        String change = changeIdFromRequest((String) invocation.getArguments()[1]);
        return mockResponse("[" + "{" + "  change_id : \"" + change + "\"," + "  status : \"NEW\"" + "}]");
    });
    gitUtil.mockApi(eq("GET"), eq("https://localhost:33333/changes/?q=" + "hashtag:%22copybara_id_origin_ref_commiter@email%22%20AND" + "%20project:foo/bar%20AND%20status:NEW"), mockResponse("[]"));
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) GitTestUtil(com.google.copybara.testing.git.GitTestUtil) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) Validator(com.google.copybara.testing.git.GitTestUtil.Validator) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ZonedDateTime(java.time.ZonedDateTime) DestinationEffect(com.google.copybara.DestinationEffect) WriterContext(com.google.copybara.WriterContext) TransformWork(com.google.copybara.TransformWork) Author(com.google.copybara.authoring.Author) GitTestUtil.getGitEnv(com.google.copybara.testing.git.GitTestUtil.getGitEnv) ChangeIdPolicy(com.google.copybara.git.GerritDestination.ChangeIdPolicy) GitTesting(com.google.copybara.git.testing.GitTesting) Path(java.nio.file.Path) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) DEFAULT_TIMEOUT(com.google.copybara.util.CommandRunner.DEFAULT_TIMEOUT) Type(com.google.copybara.DestinationEffect.Type) ImmutableSet(com.google.common.collect.ImmutableSet) ArgumentMatchers.startsWith(org.mockito.ArgumentMatchers.startsWith) DummyEndpoint(com.google.copybara.testing.DummyEndpoint) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ZoneId(java.time.ZoneId) ALWAYS_TRUE(com.google.copybara.testing.git.GitTestUtil.ALWAYS_TRUE) CheckerException(com.google.copybara.checks.CheckerException) List(java.util.List) GitTestUtil.mockResponseAndValidateRequest(com.google.copybara.testing.git.GitTestUtil.mockResponseAndValidateRequest) ArgumentMatchers.matches(org.mockito.ArgumentMatchers.matches) LabelFinder(com.google.copybara.LabelFinder) GitRepository.newBareRepo(com.google.copybara.git.GitRepository.newBareRepo) RedundantChangeException(com.google.copybara.exception.RedundantChangeException) Pattern(java.util.regex.Pattern) GitTestUtil.mockResponse(com.google.copybara.testing.git.GitTestUtil.mockResponse) Joiner(com.google.common.base.Joiner) Writer(com.google.copybara.Destination.Writer) Iterables(com.google.common.collect.Iterables) DummyRevision(com.google.copybara.testing.DummyRevision) GitTestUtil.mockResponseWithStatus(com.google.copybara.testing.git.GitTestUtil.mockResponseWithStatus) Assert.assertThrows(org.junit.Assert.assertThrows) DummyOrigin(com.google.copybara.testing.DummyOrigin) RunWith(org.junit.runner.RunWith) RepoException(com.google.copybara.exception.RepoException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) MockRequestAssertion(com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion) MessageType(com.google.copybara.util.console.Message.MessageType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Hashing(com.google.common.hash.Hashing) Changes(com.google.copybara.Changes) ChangeMessage(com.google.copybara.ChangeMessage) Strings(com.google.common.base.Strings) Answer(org.mockito.stubbing.Answer) ImmutableList(com.google.common.collect.ImmutableList) Metadata(com.google.copybara.Metadata) TransformResults(com.google.copybara.testing.TransformResults) GerritApiException(com.google.copybara.git.gerritapi.GerritApiException) MigrationInfo(com.google.copybara.MigrationInfo) PathSubject(com.google.copybara.testing.FileSubjects.PathSubject) Before(org.junit.Before) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) DummyChecker(com.google.copybara.testing.DummyChecker) ValidationException(com.google.copybara.exception.ValidationException) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) GerritWriteHook(com.google.copybara.git.GerritDestination.GerritWriteHook) Glob(com.google.copybara.util.Glob) Message(com.google.copybara.util.console.Message) GitTestUtil.writeFile(com.google.copybara.testing.git.GitTestUtil.writeFile) GerritMessageInfo(com.google.copybara.git.GerritDestination.GerritMessageInfo) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) GitTestUtil(com.google.copybara.testing.git.GitTestUtil) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) Validator(com.google.copybara.testing.git.GitTestUtil.Validator) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Before(org.junit.Before)

Example 2 with GitTestUtil

use of com.google.copybara.testing.git.GitTestUtil in project copybara by google.

the class GerritOriginTest method setup.

@Before
public void setup() throws Exception {
    options = new OptionsBuilder();
    console = new TestingConsole();
    options = new OptionsBuilder().setConsole(console).setOutputRootToTmpDir();
    skylark = new SkylarkTestExecutor(options);
    // Pass custom HOME directory so that we run an hermetic test and we
    // can add custom configuration to $HOME/.gitconfig.
    Path userHomeForTest = Files.createTempDirectory("home");
    options.setEnvironment(GitTestUtil.getGitEnv().getEnvironment());
    options.setHomeDir(userHomeForTest.toString());
    gitUtil = new GitTestUtil(options);
    gitUtil.mockRemoteGitRepos();
    createTestRepo();
    String url = "https://" + REPO_URL;
    origin = skylark.eval("result", String.format("result = " + "git.gerrit_origin(" + "    url = '%s'," + ")", url));
    Files.write(remote.resolve("base.txt"), new byte[0]);
    repo.add().files("base.txt").run();
    git("commit", "-m", "baseline", "--date", commitTime);
    baseline = repo.parseRef("HEAD");
    Files.write(remote.resolve("test.txt"), "some content".getBytes(UTF_8));
    repo.add().files("test.txt").run();
    git("commit", "-m", "first change", "--date", commitTime);
    firstRevision = new GitRevision(repo, repo.parseRef("HEAD"), GerritChange.gerritPatchSetAsReviewReference(1), "12345", ImmutableListMultimap.<String, String>builder().put(GerritChange.GERRIT_CHANGE_NUMBER_LABEL, "12345").put(GerritChange.GERRIT_CHANGE_ID_LABEL, CHANGE_ID).put(GerritChange.GERRIT_COMPLETE_CHANGE_ID_LABEL, "my_branch-12345").put(GerritChange.GERRIT_CHANGE_BRANCH, "my_branch").put(GERRIT_OWNER_EMAIL_LABEL, "the_owner@example.com").put("GERRIT_REVIEWER_EMAIL", "foo@example.com").put("GERRIT_REVIEWER_EMAIL", "bar@example.com").put("GERRIT_CC_EMAIL", "baz@example.com").put(GerritChange.GERRIT_CHANGE_DESCRIPTION_LABEL, CHANGE_DESCRIPTION).put(DEFAULT_INTEGRATE_LABEL, "gerrit " + url + " 12345 Patch Set 1 " + CHANGE_ID).put(GitRepository.GIT_DESCRIBE_REQUESTED_VERSION, repo.parseRef("HEAD").substring(0, 7)).put(GitRepository.GIT_DESCRIBE_FIRST_PARENT, repo.parseRef("HEAD").substring(0, 7)).put(GitRepository.GIT_DESCRIBE_ABBREV, "").build(), url);
    git("update-ref", "refs/changes/45/12345/1", firstRevision.getSha1());
    git("commit", "-m", "second change", "--date", commitTime, "--amend");
    Files.write(remote.resolve("foo.md"), "some content".getBytes(UTF_8));
    repo.add().files("foo.md").run();
    secondRevision = new GitRevision(repo, repo.parseRef("HEAD"), GerritChange.gerritPatchSetAsReviewReference(2), "12345", ImmutableListMultimap.<String, String>builder().put(GerritChange.GERRIT_CHANGE_NUMBER_LABEL, "12345").put(GerritChange.GERRIT_CHANGE_ID_LABEL, CHANGE_ID).put(GerritChange.GERRIT_COMPLETE_CHANGE_ID_LABEL, "my_branch-12345").put(GerritChange.GERRIT_CHANGE_BRANCH, "my_branch").put(GERRIT_OWNER_EMAIL_LABEL, "the_owner@example.com").put("GERRIT_REVIEWER_EMAIL", "foo@example.com").put("GERRIT_REVIEWER_EMAIL", "bar@example.com").put("GERRIT_CC_EMAIL", "baz@example.com").put(GerritChange.GERRIT_CHANGE_DESCRIPTION_LABEL, CHANGE_DESCRIPTION).put(GitRepository.GIT_DESCRIBE_REQUESTED_VERSION, repo.parseRef("HEAD").substring(0, 7)).put(GitRepository.GIT_DESCRIBE_FIRST_PARENT, repo.parseRef("HEAD").substring(0, 7)).put(GitRepository.GIT_DESCRIBE_ABBREV, "").put(DEFAULT_INTEGRATE_LABEL, "gerrit " + url + " 12345 Patch Set 2 " + CHANGE_ID).build(), url);
    git("update-ref", "refs/changes/45/12345/2", secondRevision.getSha1());
    git("commit", "-m", "third change", "--date", commitTime, "--amend");
    thirdRevision = new GitRevision(repo, repo.parseRef("HEAD"), GerritChange.gerritPatchSetAsReviewReference(3), "12345", ImmutableListMultimap.<String, String>builder().put(GerritChange.GERRIT_CHANGE_NUMBER_LABEL, "12345").put(GerritChange.GERRIT_COMPLETE_CHANGE_ID_LABEL, "my_branch-12345").put(GerritChange.GERRIT_CHANGE_BRANCH, "my_branch").put(GERRIT_OWNER_EMAIL_LABEL, "the_owner@example.com").put("GERRIT_REVIEWER_EMAIL", "foo@example.com").put("GERRIT_REVIEWER_EMAIL", "bar@example.com").put("GERRIT_CC_EMAIL", "baz@example.com").put(GerritChange.GERRIT_CHANGE_ID_LABEL, CHANGE_ID).put(GerritChange.GERRIT_CHANGE_DESCRIPTION_LABEL, CHANGE_DESCRIPTION).put(GitRepository.GIT_DESCRIBE_REQUESTED_VERSION, repo.parseRef("HEAD").substring(0, 7)).put(GitRepository.GIT_DESCRIBE_FIRST_PARENT, repo.parseRef("HEAD").substring(0, 7)).put(GitRepository.GIT_DESCRIBE_ABBREV, "").put(DEFAULT_INTEGRATE_LABEL, "gerrit " + url + " 12345 Patch Set 3 " + CHANGE_ID).build(), url);
    git("update-ref", "refs/changes/45/12345/3", thirdRevision.getSha1());
    GitTestUtil.createFakeGerritNodeDbMeta(repo, 12345, CHANGE_ID);
}
Also used : Path(java.nio.file.Path) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) GitTestUtil(com.google.copybara.testing.git.GitTestUtil) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Before(org.junit.Before)

Example 3 with GitTestUtil

use of com.google.copybara.testing.git.GitTestUtil in project copybara by google.

the class GitDestinationIntegrateTest method setup.

@Before
public void setup() throws Exception {
    repoGitDir = Files.createTempDirectory("GitDestinationTest-repoGitDir");
    workdir = Files.createTempDirectory("workdir");
    git("init", "--bare", repoGitDir.toString());
    console = new TestingConsole();
    options = new OptionsBuilder().setConsole(console).setOutputRootToTmpDir();
    gitUtil = new GitTestUtil(options);
    gitUtil.mockRemoteGitRepos();
    options.gitDestination = new GitDestinationOptions(options.general, options.git);
    options.gitDestination.committerEmail = "commiter@email";
    options.gitDestination.committerName = "Bara Kopi";
    destinationFiles = Glob.createGlob(ImmutableList.of("**"), ImmutableList.of("ignore*"));
    options.setForce(true);
    url = "file://" + repoGitDir;
    skylark = new SkylarkTestExecutor(options);
    primaryBranch = repo().getPrimaryBranch();
}
Also used : TestingConsole(com.google.copybara.util.console.testing.TestingConsole) GitTestUtil(com.google.copybara.testing.git.GitTestUtil) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Before(org.junit.Before)

Example 4 with GitTestUtil

use of com.google.copybara.testing.git.GitTestUtil 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");
    git("init", "--bare", repoGitDir.toString());
    console = new TestingConsole();
    options = new OptionsBuilder().setConsole(console).setForce(true).setOutputRootToTmpDir();
    gitUtil = new GitTestUtil(options);
    gitUtil.mockRemoteGitRepos(new CompleteRefValidator());
    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);
    primaryBranchMigration = "False";
}
Also used : Path(java.nio.file.Path) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) GitTestUtil(com.google.copybara.testing.git.GitTestUtil) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) CompleteRefValidator(com.google.copybara.testing.git.GitTestUtil.CompleteRefValidator) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Before(org.junit.Before)

Example 5 with GitTestUtil

use of com.google.copybara.testing.git.GitTestUtil 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

SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)9 GitTestUtil (com.google.copybara.testing.git.GitTestUtil)9 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)8 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)8 Before (org.junit.Before)8 Path (java.nio.file.Path)7 DummyChecker (com.google.copybara.testing.DummyChecker)4 WriterContext (com.google.copybara.WriterContext)2 DummyRevision (com.google.copybara.testing.DummyRevision)2 CompleteRefValidator (com.google.copybara.testing.git.GitTestUtil.CompleteRefValidator)2 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)1 Joiner (com.google.common.base.Joiner)1 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)1 Iterables (com.google.common.collect.Iterables)1 Hashing (com.google.common.hash.Hashing)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)1