Search in sources :

Example 1 with ExplicitReversal

use of com.google.copybara.transform.ExplicitReversal in project copybara by google.

the class CoreTransformTest method testSecondLayerTransformWithInnerAndOuterNoop.

@Test
public void testSecondLayerTransformWithInnerAndOuterNoop() throws ValidationException, IOException {
    String secondLayerTransform = "core.transform([\n" + "    core.replace(\n" + "        before = 'not found',\n" + "        after = 'not important',\n" + "    ),\n" + "], ignore_noop=False),\n";
    ExplicitReversal t = skylark.eval("x", "x=" + "core.transform([\n" + "    core.replace(\n" + "        before = 'foo',\n" + "        after = 'bar',\n" + "     )," + secondLayerTransform + "    core.replace(\n" + "        before = 'bar',\n" + "        after = 'baz',\n" + "    )\n" + "], ignore_noop=True)");
    Files.write(checkoutDir.resolve("file.txt"), "foo".getBytes(UTF_8));
    VoidOperationException e = assertThrows(VoidOperationException.class, () -> t.transform(TransformWorks.of(checkoutDir, "msg", console)));
    assertThat(e).hasMessageThat().containsMatch(".*was a no-op because it didn't " + "change any of the matching files.*");
}
Also used : VoidOperationException(com.google.copybara.exception.VoidOperationException) ExplicitReversal(com.google.copybara.transform.ExplicitReversal) Test(org.junit.Test)

Example 2 with ExplicitReversal

use of com.google.copybara.transform.ExplicitReversal in project copybara by google.

the class Core method transform.

@SuppressWarnings("unused")
@StarlarkMethod(name = "transform", doc = "Groups some transformations in a transformation that can contain a particular," + " manually-specified, reversal, where the forward version and reversed version" + " of the transform are represented as lists of transforms. The is useful if a" + " transformation does not automatically reverse, or if the automatic reversal" + " does not work for some reason." + "<br>" + "If reversal is not provided, the transform will try to compute the reverse of" + " the transformations list.", parameters = { @Param(name = "transformations", allowedTypes = { @ParamType(type = net.starlark.java.eval.Sequence.class, generic1 = Transformation.class) }, named = true, doc = "The list of transformations to run as a result of running this transformation."), @Param(name = "reversal", allowedTypes = { @ParamType(type = net.starlark.java.eval.Sequence.class, generic1 = Transformation.class), @ParamType(type = NoneType.class) }, doc = "The list of transformations to run as a result of running this" + " transformation in reverse.", named = true, positional = false, defaultValue = "None"), @Param(name = "ignore_noop", allowedTypes = { @ParamType(type = Boolean.class), @ParamType(type = NoneType.class) }, doc = "WARNING: Deprecated. Use `noop_behavior` instead.\nIn case a noop error happens in" + " the group of transformations (Both forward and reverse), it will be" + " ignored, but the rest of the transformations in the group will still be" + " executed. If ignore_noop is not set, we will apply the closest parent's" + " ignore_noop.", named = true, positional = false, defaultValue = "None"), @Param(name = "noop_behavior", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, doc = "How to handle no-op transformations:<br><ul> <li><b>'IGNORE_NOOP'</b>: Any no-ops" + " among the wrapped transformations are ignored.</li>" + " <li><b>'NOOP_IF_ANY_NOOP'</b>: Throws an exception as soon as a single" + " wrapped transformation is a no-op.</li> <li><b>'NOOP_IF_ALL_NOOP'</b>:" + " Ignores no-ops from the wrapped transformations unless they all no-op, in" + " which case an exception is thrown.</li></ul>", named = true, positional = false, defaultValue = "None") })
@DocDefault(field = "reversal", value = "The reverse of 'transformations'")
@DocDefault(field = "noop_behavior", value = "NOOP_IF_ANY_NOOP")
public Transformation transform(// <Transformation>
net.starlark.java.eval.Sequence<?> transformations, Object reversal, Object ignoreNoop, Object noopBehaviorString) throws EvalException, ValidationException {
    checkCondition(Starlark.isNullOrNone(ignoreNoop) || Starlark.isNullOrNone(noopBehaviorString), "The deprecated param 'ignore_noop' cannot be set simultaneously with 'noop_behavior'." + " Prefer using 'noop_behavior'.");
    Sequence.NoopBehavior noopBehavior = stringToEnum("noop_behavior", convertFromNoneable(noopBehaviorString, "NOOP_IF_ANY_NOOP"), Sequence.NoopBehavior.class);
    if (Boolean.TRUE.equals(ignoreNoop)) {
        noopBehavior = Sequence.NoopBehavior.IGNORE_NOOP;
    } else if (Boolean.FALSE.equals(ignoreNoop)) {
        noopBehavior = Sequence.NoopBehavior.FAIL_IF_ANY_NOOP;
    }
    Sequence forward = Sequence.fromConfig(generalOptions.profiler(), workflowOptions, transformations, "transformations", printHandler, debugOptions::transformWrapper, noopBehavior);
    net.starlark.java.eval.Sequence<Transformation> reverseList = convertFromNoneable(reversal, null);
    if (reverseList == null) {
        try {
            reverseList = StarlarkList.immutableCopyOf(ImmutableList.of(forward.reverse()));
        } catch (NonReversibleValidationException e) {
            throw Starlark.errorf("transformations are not automatically reversible." + " Use 'reversal' field to explicitly configure the reversal of the transform");
        }
    }
    Sequence reverse = Sequence.fromConfig(generalOptions.profiler(), workflowOptions, reverseList, "reversal", printHandler, debugOptions::transformWrapper, noopBehavior);
    return new ExplicitReversal(forward, reverse);
}
Also used : NonReversibleValidationException(com.google.copybara.exception.NonReversibleValidationException) SkylarkTransformation(com.google.copybara.transform.SkylarkTransformation) Transformations.toTransformation(com.google.copybara.transform.Transformations.toTransformation) Sequence(com.google.copybara.transform.Sequence) ExplicitReversal(com.google.copybara.transform.ExplicitReversal) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) DocDefault(com.google.copybara.doc.annotations.DocDefault)

Example 3 with ExplicitReversal

use of com.google.copybara.transform.ExplicitReversal in project copybara by google.

the class TransformWorkTest method testGetHiddenLabel.

@Test
public void testGetHiddenLabel() throws Exception {
    TransformWork work = create("Foo\n\nSOME=TEST\n");
    ExplicitReversal t = skylark.eval("t", "" + "def user_transform(ctx):\n" + "    " + "ctx.add_label('FOO','ONE', hidden = True)" + "\n" + "    " + "ctx.add_label('FOO','TWO', hidden = True)" + "\n" + "t = core.transform([user_transform])");
    t.transform(work);
    assertThat(work.getLabel("FOO")).isEqualTo("TWO");
    assertThat(work.getAllLabels("FOO").getImmutableList()).isEqualTo(ImmutableList.of("ONE", "TWO"));
}
Also used : ExplicitReversal(com.google.copybara.transform.ExplicitReversal) Test(org.junit.Test)

Example 4 with ExplicitReversal

use of com.google.copybara.transform.ExplicitReversal in project copybara by google.

the class TransformWorkTest method checkLabelWithSkylark.

private void checkLabelWithSkylark(String originalMsg, String transform, String expectedOutputMsg) throws Exception {
    TransformWork work = create(originalMsg);
    ExplicitReversal t = skylark.eval("t", "" + "def user_transform(ctx):\n" + "    " + transform + "\n" + "t = core.transform([user_transform])");
    t.transform(work);
    assertThat(work.getMessage()).isEqualTo(expectedOutputMsg);
}
Also used : ExplicitReversal(com.google.copybara.transform.ExplicitReversal)

Example 5 with ExplicitReversal

use of com.google.copybara.transform.ExplicitReversal in project copybara by google.

the class TransformWorkTest method testAddTwoDifferentHiddenLabels.

@Test
public void testAddTwoDifferentHiddenLabels() throws Exception {
    TransformWork work = create("Foo\n\nSOME=TEST\n");
    ExplicitReversal t = skylark.eval("t", "" + "def user_transform(ctx):\n" + "    " + "ctx.add_label('ONE','val2', hidden = True)" + "\n" + "    " + "ctx.add_label('ONE','val2', hidden = True)" + "\n" + "    " + "ctx.add_label('ONE','val1', hidden = True)" + "\n" + "    " + "ctx.add_label('TWO','val2', hidden = True)" + "\n" + "    " + "ctx.add_label('TWO','val2', hidden = True)" + "\n" + "    " + "ctx.add_label('TWO','val1', hidden = True)" + "\n" + "t = core.transform([user_transform])");
    t.transform(work);
    assertThat(work.getAllLabels("ONE").getImmutableList()).isEqualTo(ImmutableList.of("val2", "val1"));
    assertThat(work.getAllLabels("TWO").getImmutableList()).isEqualTo(ImmutableList.of("val2", "val1"));
}
Also used : ExplicitReversal(com.google.copybara.transform.ExplicitReversal) Test(org.junit.Test)

Aggregations

ExplicitReversal (com.google.copybara.transform.ExplicitReversal)16 Test (org.junit.Test)14 VoidOperationException (com.google.copybara.exception.VoidOperationException)2 DocDefault (com.google.copybara.doc.annotations.DocDefault)1 NonReversibleValidationException (com.google.copybara.exception.NonReversibleValidationException)1 Sequence (com.google.copybara.transform.Sequence)1 SkylarkTransformation (com.google.copybara.transform.SkylarkTransformation)1 Transformations.toTransformation (com.google.copybara.transform.Transformations.toTransformation)1 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)1 StarlarkMethod (net.starlark.java.annot.StarlarkMethod)1