Search in sources :

Example 6 with Glob

use of com.google.copybara.util.Glob in project copybara by google.

the class GerritDestinationTest method testReviewerFieldWithTopic.

@Test
public void testReviewerFieldWithTopic() throws Exception {
    pushToRefsFor = "master";
    writeFile(workdir, "file", "some content");
    fetch = "master";
    options.gerrit.gerritTopic = "testTopic";
    options.setForce(true);
    url = "https://localhost:33333/foo/bar";
    mockNoChangesFound();
    DummyRevision originRef = new DummyRevision("origin_ref");
    GerritDestination destination = destination("submit = False", "reviewers = [\"${SOME_REVIEWER}\"]");
    Glob glob = Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths);
    WriterContext writerContext = new WriterContext("GerritDestination", "TEST", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
    List<DestinationEffect> result = destination.newWriter(writerContext).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()).withLabelFinder(e -> e.equals("SOME_REVIEWER") ? ImmutableList.of("foo@example.com") : ImmutableList.of()), glob, console);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
    assertPushRef("refs/for/master%topic=testTopic,hashtag=copybara_id_origin_ref_commiter@email," + "r=foo@example.com");
}
Also used : 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) WriterContext(com.google.copybara.WriterContext) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Glob(com.google.copybara.util.Glob) Test(org.junit.Test)

Example 7 with Glob

use of com.google.copybara.util.Glob in project copybara by google.

the class GerritDestinationTest method gerritSubmit_success.

@Test
public void gerritSubmit_success() throws Exception {
    options.gerrit.gerritChangeId = null;
    fetch = "master";
    writeFile(workdir, "file", "some content");
    url = BASE_URL + "/foo/bar";
    repoGitDir = gitUtil.mockRemoteRepo("user:SECRET@copybara-not-real.com/foo/bar").getGitDir();
    gitUtil.mockApi(eq("GET"), startsWith(BASE_URL + "/changes/"), mockResponse("[" + "{" + "  change_id : \"Iaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd\"," + "  status : \"NEW\"" + "}]"));
    AtomicBoolean submitCalled = new AtomicBoolean(false);
    AtomicBoolean reviewCalled = new AtomicBoolean(false);
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/revisions/.*/review"), mockResponseAndValidateRequest("{\"labels\": { \"Code-Review\": 2}}", new MockRequestAssertion("Always true with side-effect", s -> {
        reviewCalled.set(true);
        return true;
    })));
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/submit"), mockResponseAndValidateRequest("{" + "  change_id : \"Iaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd\"," + "  status : \"submitted\"" + "}", new MockRequestAssertion("Always true with side-effect", s -> {
        submitCalled.set(true);
        return true;
    })));
    options.setForce(true);
    DummyRevision originRef = new DummyRevision("origin_ref");
    GerritDestination destination = destination("submit = True", "gerrit_submit = True");
    Glob glob = Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths);
    WriterContext writerContext = new WriterContext("GerritDestinationTest", "test", false, originRef, Glob.ALL_FILES.roots());
    List<DestinationEffect> result = destination.newWriter(writerContext).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()), glob, console);
    assertThat(reviewCalled.get()).isTrue();
    assertThat(submitCalled.get()).isTrue();
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WriterContext(com.google.copybara.WriterContext) MockRequestAssertion(com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Glob(com.google.copybara.util.Glob) Test(org.junit.Test)

Example 8 with Glob

use of com.google.copybara.util.Glob in project copybara by google.

the class GerritDestinationTest method testGerritSubmit.

@Test
public void testGerritSubmit() throws Exception {
    options.gerrit.gerritChangeId = null;
    fetch = "master";
    pushToRefsFor = "master";
    writeFile(workdir, "file", "some content");
    options.setForce(true);
    url = "https://localhost:33333/foo/bar";
    GitRepository repo = gitUtil.mockRemoteRepo("localhost:33333/foo/bar");
    mockNoChangesFound();
    DummyRevision originRef = new DummyRevision("origin_ref");
    GerritDestination destination = destination("submit = True");
    Glob glob = Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths);
    WriterContext writerContext = new WriterContext("GerritDestinationTest", "test", false, originRef, Glob.ALL_FILES.roots());
    List<DestinationEffect> result = destination.newWriter(writerContext).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()), glob, console);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
    String changeId = lastCommitChangeIdLineForRef("origin_ref", repo, "refs/heads/master").replace("Change-Id: ", "").trim();
    assertThat(changeId).isNotNull();
    assertThat(destination.getType()).isEqualTo("git.destination");
    assertThat(destination.describe(glob).get("fetch")).isEqualTo(ImmutableSet.of("master"));
    assertThat(destination.describe(glob).get("push")).isEqualTo(ImmutableSet.of("master"));
}
Also used : WriterContext(com.google.copybara.WriterContext) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Glob(com.google.copybara.util.Glob) Test(org.junit.Test)

Example 9 with Glob

use of com.google.copybara.util.Glob in project copybara by google.

the class GerritDestinationTest method gerritSubmit_alreadySubmittable.

@Test
public void gerritSubmit_alreadySubmittable() throws Exception {
    options.gerrit.gerritChangeId = null;
    fetch = "master";
    writeFile(workdir, "file", "some content");
    url = BASE_URL + "/foo/bar";
    repoGitDir = gitUtil.mockRemoteRepo("user:SECRET@copybara-not-real.com/foo/bar").getGitDir();
    gitUtil.mockApi(eq("GET"), startsWith(BASE_URL + "/changes/"), mockResponse("[" + "{" + "  change_id : \"Iaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd\"," + "  status : \"NEW\"," + "  submittable : true" + "}]"));
    AtomicBoolean submitCalled = new AtomicBoolean(false);
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/revisions/.*/review"), mockResponseWithStatus("Change should not be voted on", 500));
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/submit"), mockResponseAndValidateRequest("{" + "  change_id : \"Iaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd\"," + "  status : \"submitted\"" + "}", new MockRequestAssertion("Always true with side-effect", s -> {
        submitCalled.set(true);
        return true;
    })));
    options.setForce(true);
    DummyRevision originRef = new DummyRevision("origin_ref");
    GerritDestination destination = destination("submit = True", "gerrit_submit = True");
    Glob glob = Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths);
    WriterContext writerContext = new WriterContext("GerritDestinationTest", "test", false, originRef, Glob.ALL_FILES.roots());
    List<DestinationEffect> result = destination.newWriter(writerContext).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()), glob, console);
    assertThat(submitCalled.get()).isTrue();
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WriterContext(com.google.copybara.WriterContext) MockRequestAssertion(com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion) DestinationEffect(com.google.copybara.DestinationEffect) DummyRevision(com.google.copybara.testing.DummyRevision) Glob(com.google.copybara.util.Glob) Test(org.junit.Test)

Example 10 with Glob

use of com.google.copybara.util.Glob in project copybara by google.

the class GitDestinationTest method multipleMigrationsToOneDestination_separateRoots.

/**
 * Verify that multiple (exclusive) migrations can write to the same Git repository. This is,
 * migrations from different origins writing to a different subpaths in the Git repo and excluding
 * the other ones.
 */
@Test
public void multipleMigrationsToOneDestination_separateRoots() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    Files.createDirectories(workdir.resolve("foo"));
    Files.createDirectories(workdir.resolve("bar"));
    Files.createDirectories(workdir.resolve("baz"));
    Files.write(workdir.resolve("foo/one"), "First version".getBytes(UTF_8));
    Files.write(workdir.resolve("bar/one"), "First version".getBytes(UTF_8));
    Files.write(workdir.resolve("baz/one"), "First version".getBytes(UTF_8));
    repo().withWorkTree(workdir).add().files("foo/one").files("bar/one").files("baz/one").run();
    repo().withWorkTree(workdir).simpleCommand("commit", "-m", "Initial commit");
    Glob repoAglob = Glob.createGlob(ImmutableList.of("foo/**"), ImmutableList.of("bar/**"));
    Glob repoBglob = Glob.createGlob(ImmutableList.of("bar/**"), ImmutableList.of("foo/**"));
    // Change on repo A
    Files.write(workdir.resolve("foo/one"), "Second version".getBytes(UTF_8));
    Writer<GitRevision> writer1 = newWriter();
    DummyRevision repoAfirstRev = new DummyRevision("Foo first");
    process(writer1, repoAglob, repoAfirstRev);
    assertThatCheckout(repo(), primaryBranch).containsFile("foo/one", "Second version").containsFile("bar/one", "First version").containsFile("baz/one", "First version").containsNoMoreFiles();
    verifyDestinationStatus(repoAglob, repoAfirstRev);
    // Change on repo B, does not affect repo A paths
    Files.write(workdir.resolve("bar/one"), "Second version".getBytes(UTF_8));
    Writer<GitRevision> writer2 = newWriter();
    DummyRevision repoBfirstRev = new DummyRevision("Bar first");
    process(writer2, repoBglob, repoBfirstRev);
    assertThatCheckout(repo(), primaryBranch).containsFile("foo/one", "Second version").containsFile("bar/one", "Second version").containsFile("baz/one", "First version").containsNoMoreFiles();
    verifyDestinationStatus(repoAglob, repoAfirstRev);
    verifyDestinationStatus(repoBglob, repoBfirstRev);
    // Change on repo A does not affect repo B paths
    Files.write(workdir.resolve("foo/one"), "Third version".getBytes(UTF_8));
    Writer<GitRevision> writer3 = newWriter();
    DummyRevision repoASecondRev = new DummyRevision("Foo second");
    process(writer3, repoAglob, repoASecondRev);
    assertThatCheckout(repo(), primaryBranch).containsFile("foo/one", "Third version").containsFile("bar/one", "Second version").containsFile("baz/one", "First version").containsNoMoreFiles();
    verifyDestinationStatus(repoAglob, repoASecondRev);
    verifyDestinationStatus(repoBglob, repoBfirstRev);
}
Also used : DummyRevision(com.google.copybara.testing.DummyRevision) Glob(com.google.copybara.util.Glob) Test(org.junit.Test)

Aggregations

Glob (com.google.copybara.util.Glob)24 Test (org.junit.Test)20 DummyRevision (com.google.copybara.testing.DummyRevision)17 WriterContext (com.google.copybara.WriterContext)15 ImmutableList (com.google.common.collect.ImmutableList)12 DestinationEffect (com.google.copybara.DestinationEffect)12 ValidationException (com.google.copybara.exception.ValidationException)11 RepoException (com.google.copybara.exception.RepoException)10 MockRequestAssertion (com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)9 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)8 Iterables (com.google.common.collect.Iterables)8 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)8 Path (java.nio.file.Path)8 Joiner (com.google.common.base.Joiner)7 ImmutableSet (com.google.common.collect.ImmutableSet)7 ChangeMessage (com.google.copybara.ChangeMessage)7 CheckerException (com.google.copybara.checks.CheckerException)7 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)6