-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now users can define Starlark transformations which can report NOOPs. As with the NOOPs reported by the build-in Java transformations, they will cause a workflow to fail unless ignored via `--ignore-noop` or wrapped in a `core.transform(..., noop_behavior='IGNORE_NOOP')`. BUG=194387788 PiperOrigin-RevId: 387420523 Change-Id: Ie9007975dfbc1b21833e5468216a2be9256c2b8d
- Loading branch information
1 parent
7af0df7
commit b37eda8
Showing
5 changed files
with
256 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
javatests/com/google/copybara/transform/SkylarkTransformationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright (C) 2016 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.copybara.transform; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.common.jimfs.Jimfs; | ||
import com.google.copybara.TransformWork; | ||
import com.google.copybara.Transformation; | ||
import com.google.copybara.TransformationStatus; | ||
import com.google.copybara.testing.OptionsBuilder; | ||
import com.google.copybara.testing.SkylarkTestExecutor; | ||
import com.google.copybara.testing.TransformWorks; | ||
import com.google.copybara.util.console.testing.TestingConsole; | ||
import java.io.IOException; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class SkylarkTransformationTest { | ||
|
||
private OptionsBuilder options; | ||
private Path checkoutDir; | ||
private TestingConsole console; | ||
private SkylarkTestExecutor skylark; | ||
private TransformWork transformWork; | ||
|
||
@Before | ||
public void setup() throws IOException { | ||
FileSystem fs = Jimfs.newFileSystem(); | ||
checkoutDir = fs.getPath("/test-checkoutDir"); | ||
Files.createDirectories(checkoutDir); | ||
console = new TestingConsole(); | ||
options = new OptionsBuilder().setConsole(console); | ||
skylark = new SkylarkTestExecutor(options); | ||
transformWork = TransformWorks.of(checkoutDir, "testmsg", console); | ||
} | ||
|
||
@Test | ||
public void testStarlarkTransform_returnsSuccess() throws Exception { | ||
Transformation t = | ||
skylark.eval( | ||
"t", | ||
"" | ||
+ "def foo(ctx):\n" | ||
+ " return ctx.success()\n" | ||
+ "\n" | ||
+ "t = core.dynamic_transform(foo)"); | ||
|
||
TransformationStatus status = t.transform(transformWork); | ||
|
||
assertThat(status.isSuccess()).isTrue(); | ||
} | ||
|
||
@Test | ||
public void testStarlarkTransform_returnsNoop() throws Exception { | ||
Transformation t = | ||
skylark.eval( | ||
"t", | ||
"" | ||
+ "def foo(ctx):\n" | ||
+ " return ctx.noop('Reason for noop.')\n" | ||
+ "\n" | ||
+ "t = core.dynamic_transform(foo)"); | ||
|
||
TransformationStatus status = t.transform(transformWork); | ||
|
||
assertThat(status.isNoop()).isTrue(); | ||
assertThat(status.getMessage()).isEqualTo("Reason for noop."); | ||
} | ||
|
||
@Test | ||
public void testStarlarkTransform_noReturnValue_isTreatedAsSuccess() throws Exception { | ||
Transformation t = | ||
skylark.eval( | ||
"t", | ||
"" | ||
+ "def foo(ctx):\n" | ||
+ " # Do nothing \n" | ||
+ " pass\n" | ||
+ "\n" | ||
+ "t = core.dynamic_transform(foo)"); | ||
|
||
TransformationStatus status = t.transform(transformWork); | ||
|
||
assertThat(status.isSuccess()).isTrue(); | ||
} | ||
|
||
@Test | ||
public void testStarlarkTransform_returnValueCanBeCasedOn() throws Exception { | ||
Transformation t = | ||
skylark.eval( | ||
"t", | ||
"" | ||
+ "def foo(ctx):\n" | ||
+ " return ctx.success()\n" | ||
+ "\n" | ||
+ "s = core.dynamic_transform(foo)" | ||
+ "\n" | ||
+ "def bar(ctx):\n" | ||
+ " status = ctx.run(s)\n" | ||
+ " if not status.is_success:\n" | ||
+ " core.fail_with_noop()\n" | ||
+ " if status.is_noop:\n" | ||
+ " core.fail_with_noop()\n" | ||
+ " return status" | ||
+ "\n" | ||
+ "t = core.dynamic_transform(foo)"); | ||
|
||
TransformationStatus status = t.transform(transformWork); | ||
|
||
assertThat(status.isSuccess()).isTrue(); | ||
} | ||
} |