Search in sources :

Example 31 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class FolderDestinationTest method testRelativePaths.

@Test
public void testRelativePaths() throws Exception {
    Path dest = Files.createDirectories(Files.createTempDirectory("workdir").resolve("one"));
    workdir = Files.createTempDirectory("local_folder");
    Files.write(Files.createDirectories(dest.resolve("folder")).resolve("file1.txt"), "foo".getBytes(UTF_8));
    Files.write(Files.createDirectories(workdir.resolve("folder")).resolve("file1.txt"), "bar".getBytes(UTF_8));
    options.setWorkdirToRealTempDir();
    options.folderDestination.localFolder = dest.resolve("../one").toString();
    WriterContext writerContext = new WriterContext("not_important", "not_important", false, new DummyRevision("not_important"), Glob.ALL_FILES.roots());
    skylark.<FolderDestination>eval("dest", "dest = folder.destination()").newWriter(writerContext).write(TransformResults.of(workdir, new DummyRevision("not_important")), Glob.createGlob(ImmutableList.of("folder/file1.txt"), ImmutableList.of()), options.general.console());
    assertThatPath(dest).containsFile("folder/file1.txt", "bar").containsNoMoreFiles();
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 32 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class GerritDestinationTest method process.

private void process(DummyRevision originRef, GerritDestination destination) throws ValidationException, RepoException, IOException {
    WriterContext writerContext = new WriterContext("GerritDestination", "TEST", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
    Writer<GitRevision> gitRevisionWriter = destination.newWriter(writerContext);
    // This is largely unused, except for the label finder.
    TransformWork work = new TransformWork(workdir, new Metadata("Desc", new Author("foo", "foo@foo.com"), ImmutableSetMultimap.of()), Changes.EMPTY, console, new MigrationInfo(DummyOrigin.LABEL_NAME, null), originRef, console -> new DummyEndpoint(), console -> gitRevisionWriter.getFeedbackEndPoint(console), () -> gitRevisionWriter.getDestinationReader(console, null, workdir));
    ImmutableList<DestinationEffect> result = gitRevisionWriter.write(TransformResults.of(workdir, originRef).withLabelFinder(work::getAllLabels).withIdentity(originRef.asString()), Glob.createGlob(ImmutableList.of("**"), excludedDestinationPaths), console);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
    assertThat(result.get(0).getType()).isEqualTo(Type.CREATED);
    assertThat(result.get(0).getDestinationRef().getType()).isEqualTo("commit");
    assertThat(result.get(0).getDestinationRef().getId()).matches("[0-9a-f]{40}");
}
Also used : DummyEndpoint(com.google.copybara.testing.DummyEndpoint) WriterContext(com.google.copybara.WriterContext) MigrationInfo(com.google.copybara.MigrationInfo) DestinationEffect(com.google.copybara.DestinationEffect) TransformWork(com.google.copybara.TransformWork) DummyRevision(com.google.copybara.testing.DummyRevision) Metadata(com.google.copybara.Metadata) Author(com.google.copybara.authoring.Author)

Example 33 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class GerritDestinationTest method testEmptyReviewersField.

@Test
public void testEmptyReviewersField() 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()), glob, console);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getErrors()).isEmpty();
    assertPushRef("refs/for/master%topic=testTopic");
}
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 34 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class GerritDestinationTest method gerritSubmit_plusTwoRestricted.

@Test
public void gerritSubmit_plusTwoRestricted() 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"), mockResponseWithStatus("Applying label \\\"Code-Review\\\": 2 is restricted\\n\\n", 401));
    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());
    ValidationException validationException = assertThrows(ValidationException.class, () -> destination.newWriter(writerContext).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()), glob, console));
    assertThat(validationException).hasMessageThat().contains("2 is restricted");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WriterContext(com.google.copybara.WriterContext) MockRequestAssertion(com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion) ValidationException(com.google.copybara.exception.ValidationException) DummyRevision(com.google.copybara.testing.DummyRevision) Glob(com.google.copybara.util.Glob) Test(org.junit.Test)

Example 35 with WriterContext

use of com.google.copybara.WriterContext in project copybara by google.

the class GerritDestinationTest method gerritSubmit_fail.

@Test
public void gerritSubmit_fail() 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 : \"12345\"," + "  status : \"NEW\"" + "}]"));
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/revisions/.*/review"), mockResponse("{\"labels\": { \"Code-Review\": 2}}"));
    gitUtil.mockApi(eq("POST"), matches(BASE_URL + "/changes/.*/submit"), mockResponseWithStatus("Submit failed.", 403));
    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());
    GerritApiException gerritApiException = assertThrows(GerritApiException.class, () -> destination.newWriter(writerContext).write(TransformResults.of(workdir, originRef).withSummary("Test message").withIdentity(originRef.asString()), glob, console));
    assertThat(gerritApiException).hasMessageThat().contains("Submit failed");
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) Glob(com.google.copybara.util.Glob) GerritApiException(com.google.copybara.git.gerritapi.GerritApiException) Test(org.junit.Test)

Aggregations

WriterContext (com.google.copybara.WriterContext)58 DummyRevision (com.google.copybara.testing.DummyRevision)58 Test (org.junit.Test)46 DestinationEffect (com.google.copybara.DestinationEffect)21 Glob (com.google.copybara.util.Glob)17 Path (java.nio.file.Path)15 Author (com.google.copybara.authoring.Author)14 Changes (com.google.copybara.Changes)13 ValidationException (com.google.copybara.exception.ValidationException)12 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)11 CheckerException (com.google.copybara.checks.CheckerException)10 DummyChecker (com.google.copybara.testing.DummyChecker)10 MockRequestAssertion (com.google.copybara.testing.git.GitTestUtil.MockRequestAssertion)10 CommandOutput (com.google.copybara.util.CommandOutput)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 ImmutableList (com.google.common.collect.ImmutableList)8 ImmutableSet (com.google.common.collect.ImmutableSet)8 Truth.assertThat (com.google.common.truth.Truth.assertThat)8 ChangeMessage (com.google.copybara.ChangeMessage)8 Writer (com.google.copybara.Destination.Writer)8