Search in sources :

Example 1 with TransformWork

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

the class SequenceTest method cachedTreeStateTranformWork.

private TransformWork cachedTreeStateTranformWork() throws IOException {
    TransformWork work = TransformWorks.of(checkoutDir, "foo", console);
    // Force a map based tree-state
    work.getTreeState().find(Glob.ALL_FILES.relativeTo(checkoutDir));
    work.getTreeState().notifyNoChange();
    work = work.withUpdatedTreeState();
    assertCachedTreeState(work).isTrue();
    return work;
}
Also used : TransformWork(com.google.copybara.TransformWork)

Example 2 with TransformWork

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

the class SequenceTest method testSequenceTreeStateIsNotCached_firstBad.

@Test
public void testSequenceTreeStateIsNotCached_firstBad() throws IOException, ValidationException {
    t1.useTreeState = false;
    t2.useTreeState = true;
    TransformWork work = cachedTreeStateTranformWork();
    sequence.transform(work);
    assertCachedTreeState(work.withUpdatedTreeState()).isFalse();
}
Also used : TransformWork(com.google.copybara.TransformWork) Test(org.junit.Test)

Example 3 with TransformWork

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

the class SequenceTest method testSequenceTreeStateIsNotCached_allGood.

/**
 * The TreeState passed to a Sequence shouldn't return a cached TreeState after invocation and
 * calling updateTreeState(). Since it could contain many non-cache-safe transforms and the
 * last one could be a safe one. But we shouldn't be able to reuse that cache.
 */
@Test
public void testSequenceTreeStateIsNotCached_allGood() throws IOException, ValidationException {
    t1.useTreeState = true;
    t2.useTreeState = true;
    TransformWork work = cachedTreeStateTranformWork();
    sequence.transform(work);
    assertCachedTreeState(work.withUpdatedTreeState()).isFalse();
}
Also used : TransformWork(com.google.copybara.TransformWork) Test(org.junit.Test)

Example 4 with TransformWork

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

the class SkylarkTransformation method transform.

@Override
public TransformationStatus transform(TransformWork work) throws IOException, ValidationException, RepoException {
    SkylarkConsole skylarkConsole = new SkylarkConsole(work.getConsole());
    TransformWork skylarkWork = work.withConsole(skylarkConsole).withParams(params);
    TransformationStatus status = TransformationStatus.success();
    try (Mutability mu = Mutability.create("dynamic_transform")) {
        StarlarkThread thread = new StarlarkThread(mu, StarlarkSemantics.DEFAULT);
        thread.setPrintHandler(printHandler);
        Object result = Starlark.call(thread, function, ImmutableList.of(skylarkWork), /*kwargs=*/
        ImmutableMap.of());
        result = result == Starlark.NONE ? TransformationStatus.success() : result;
        checkCondition(result instanceof TransformationStatus, "Dynamic transforms functions should return nothing or objects of type %s, but '%s'" + " returned: %s", TransformationStatus.STARLARK_TYPE_NAME, describe(), result);
        status = (TransformationStatus) result;
    } catch (EvalException e) {
        if (e.getCause() instanceof EmptyChangeException) {
            throw ((EmptyChangeException) e.getCause());
        }
        if (e.getCause() instanceof RepoException) {
            throw new RepoException(String.format("Error while executing the skylark transformation %s: %s", describe(), e.getMessageWithStack()), e);
        }
        throw new ValidationException(String.format("Error while executing the skylark transformation %s: %s", describe(), e.getMessageWithStack()), e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException("This should not happen.", e);
    } finally {
        work.updateFrom(skylarkWork);
    }
    checkCondition(skylarkConsole.getErrorCount() == 0, "%d error(s) while executing %s", skylarkConsole.getErrorCount(), describe());
    return status;
}
Also used : TransformationStatus(com.google.copybara.TransformationStatus) ValidationException(com.google.copybara.exception.ValidationException) NonReversibleValidationException(com.google.copybara.exception.NonReversibleValidationException) StarlarkThread(net.starlark.java.eval.StarlarkThread) TransformWork(com.google.copybara.TransformWork) Mutability(net.starlark.java.eval.Mutability) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) EvalException(net.starlark.java.eval.EvalException) RepoException(com.google.copybara.exception.RepoException)

Example 5 with TransformWork

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

the class GitOriginTest method testChanges.

@Test
public void testChanges() throws Exception {
    // Need to "round" it since git doesn't store the milliseconds
    ZonedDateTime beforeTime = ZonedDateTime.now(ZoneId.systemDefault()).minusSeconds(1);
    String author = "John Name <john@name.com>";
    singleFileCommit(author, "change2", "test.txt", "some content2");
    git("tag", "-m", "This is a tag", "0.1");
    singleFileCommit(author, "change3", "test.txt", "some content3");
    singleFileCommit(author, "change4", "test.txt", "some content4");
    ImmutableList<Change<GitRevision>> changes = newReader().changes(origin.resolve(firstCommitRef), origin.resolve("HEAD")).getChanges();
    assertThat(changes).hasSize(3);
    assertThat(changes.stream().map(c -> c.getRevision().getUrl()).allMatch(c -> c.startsWith("file://"))).isTrue();
    assertThat(changes.get(0).getMessage()).isEqualTo("change2\n");
    assertThat(changes.get(0).getRevision().associatedLabel("GIT_DESCRIBE_CHANGE_VERSION")).contains("0.1");
    assertThat(changes.get(1).getMessage()).isEqualTo("change3\n");
    assertThat(changes.get(1).getRevision().associatedLabel("GIT_DESCRIBE_CHANGE_VERSION")).contains("0.1-1-g" + changes.get(1).getRevision().asString().substring(0, 7));
    assertThat(changes.get(2).getMessage()).isEqualTo("change4\n");
    assertThat(changes.get(2).getRevision().associatedLabel("GIT_DESCRIBE_CHANGE_VERSION")).contains("0.1-2-g" + changes.get(2).getRevision().asString().substring(0, 7));
    TransformWork work = TransformWorks.of(Paths.get(""), "some msg", console).withChanges(new Changes(changes.reverse(), ImmutableList.of()));
    assertThat(work.getLabel("GIT_DESCRIBE_CHANGE_VERSION")).isEqualTo("0.1-2-g" + changes.get(2).getRevision().asString().substring(0, 7));
    assertThat(work.getAllLabels("GIT_DESCRIBE_CHANGE_VERSION").getImmutableList()).isEqualTo(ImmutableList.of("0.1-2-g" + changes.get(2).getRevision().asString().substring(0, 7), "0.1-1-g" + changes.get(1).getRevision().asString().substring(0, 7), "0.1"));
    for (Change<GitRevision> change : changes) {
        assertThat(change.getAuthor().getEmail()).isEqualTo("john@name.com");
        assertThat(change.getDateTime()).isAtLeast(beforeTime);
        assertThat(change.getDateTime()).isAtMost(ZonedDateTime.now(ZoneId.systemDefault()).plusSeconds(1));
    }
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) GitTestUtil(com.google.copybara.testing.git.GitTestUtil) AuthoringMappingMode(com.google.copybara.authoring.Authoring.AuthoringMappingMode) ZonedDateTime(java.time.ZonedDateTime) ProcessedChange(com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange) TransformWork(com.google.copybara.TransformWork) LabelsAwareModule(com.google.copybara.config.LabelsAwareModule) VisitResult(com.google.copybara.ChangeVisitable.VisitResult) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.Change) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Workflow(com.google.copybara.Workflow) Path(java.nio.file.Path) Reader(com.google.copybara.Origin.Reader) ImmutableSet(com.google.common.collect.ImmutableSet) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) Revision(com.google.copybara.Revision) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) List(java.util.List) Iterables(com.google.common.collect.Iterables) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) RepoException(com.google.copybara.exception.RepoException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) MessageType(com.google.copybara.util.console.Message.MessageType) Changes(com.google.copybara.Changes) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) ChangesResponse(com.google.copybara.Origin.Reader.ChangesResponse) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ModuleSet(com.google.copybara.ModuleSet) Before(org.junit.Before) Glob.createGlob(com.google.copybara.util.Glob.createGlob) EmptyReason(com.google.copybara.Origin.Reader.ChangesResponse.EmptyReason) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) ValidationException(com.google.copybara.exception.ValidationException) UserPassword(com.google.copybara.git.GitCredential.UserPassword) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Authoring(com.google.copybara.authoring.Authoring) Glob(com.google.copybara.util.Glob) Paths(java.nio.file.Paths) TransformWorks(com.google.copybara.testing.TransformWorks) GitTestUtil.writeFile(com.google.copybara.testing.git.GitTestUtil.writeFile) Changes(com.google.copybara.Changes) ZonedDateTime(java.time.ZonedDateTime) TransformWork(com.google.copybara.TransformWork) ProcessedChange(com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange) Change(com.google.copybara.Change) Test(org.junit.Test)

Aggregations

TransformWork (com.google.copybara.TransformWork)41 Test (org.junit.Test)33 Transformation (com.google.copybara.Transformation)23 TransformationStatus (com.google.copybara.TransformationStatus)9 DummyRevision (com.google.copybara.testing.DummyRevision)6 Changes (com.google.copybara.Changes)5 Author (com.google.copybara.authoring.Author)5 ValidationException (com.google.copybara.exception.ValidationException)4 Change (com.google.copybara.Change)3 ProcessedChange (com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange)3 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)2 NonReversibleValidationException (com.google.copybara.exception.NonReversibleValidationException)2 RepoException (com.google.copybara.exception.RepoException)2 TransformWorks.toChange (com.google.copybara.testing.TransformWorks.toChange)2 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1