aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test
diff options
context:
space:
mode:
authorGravatar Jon Brandvein <brandjon@google.com>2016-10-11 20:27:58 +0000
committerGravatar Yue Gan <yueg@google.com>2016-10-12 08:56:11 +0000
commita629005a0f7b5c702067b2f47e048f11700a5092 (patch)
treee878fc3978091fcc5c81e54a97b076bf3880fec8 /src/test
parent0c4a10adcfa8a6a07f6a1c56ea460ad614ec5c6d (diff)
Automated [] rollback of commit 8d36a34ee02ad0fd07d713b8c8ee273ff30d2fb9.
*** Reason for rollback *** Fixed depended-on broken CL *** Original change description *** Automated [] rollback of commit f341bc4f6e918b6a41c1536c111bbf24f14f967b. *** Reason for rollback *** Depends on c/135226123 which depends on commit 9c25afe750a937b2152c21a93effc8b9ba82c27b, which needs to be rolled back. *** Original change description *** Add ctx.get_actions(), for inspecting the actions created by the current rule. This returns an ActionsProvider. In the case where the rule does not emit any more actions afterwards, the provider is equivalent to the one that gets passed on to the rule's dependencies. This may be u... *** ROLLBACK_OF=135781162 -- MOS_MIGRATED_REVID=135833495
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java81
1 files changed, 72 insertions, 9 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java
index 6066c41a4c..674aa40bdb 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java
@@ -1029,7 +1029,7 @@ public class SkylarkRuleContextTest extends SkylarkTestCase {
// The common structure of the following actions tests is a rule under test depended upon by
// a testing rule, where the rule under test has one output and one caller-supplied action.
- private String getSimpleUnderTestDefinition(String actionLine) {
+ private String getSimpleUnderTestDefinition(String actionLine, boolean withSkylarkTestable) {
return linesAsString(
"def _undertest_impl(ctx):",
" out = ctx.outputs.out",
@@ -1037,10 +1037,18 @@ public class SkylarkRuleContextTest extends SkylarkTestCase {
"undertest_rule = rule(",
" implementation = _undertest_impl,",
" outputs = {'out': '%{name}.txt'},",
- " _skylark_testable = True,",
+ withSkylarkTestable ? " _skylark_testable = True," : "",
")");
}
+ private String getSimpleUnderTestDefinition(String actionLine) {
+ return getSimpleUnderTestDefinition(actionLine, true);
+ }
+
+ private String getSimpleNontestableUnderTestDefinition(String actionLine) {
+ return getSimpleUnderTestDefinition(actionLine, false);
+ }
+
private final String testingRuleDefinition =
linesAsString(
"def _testing_impl(ctx):",
@@ -1089,13 +1097,8 @@ public class SkylarkRuleContextTest extends SkylarkTestCase {
public void testNoAccessToDependencyActionsWithoutSkylarkTest() throws Exception {
reporter.removeHandler(failFastHandler);
scratch.file("test/rules.bzl",
- "def _undertest_impl(ctx):",
- " out = ctx.outputs.out",
- " ctx.action(outputs=[out], command='echo foo123 > ' + out.path)",
- "undertest_rule = rule(",
- " implementation = _undertest_impl,",
- " outputs = {'out': '%{name}.txt'},",
- ")",
+ getSimpleNontestableUnderTestDefinition(
+ "ctx.action(outputs=[out], command='echo foo123 > ' + out.path)"),
testingRuleDefinition);
scratch.file("test/BUILD",
simpleBuildDefinition);
@@ -1148,6 +1151,66 @@ public class SkylarkRuleContextTest extends SkylarkTestCase {
assertThat(eval("list(action2.outputs)")).isEqualTo(eval("[file2]"));
}
+ // For created_actions() tests, the "undertest" rule represents both the code under test and the
+ // Skylark user test code itself.
+
+ @Test
+ public void testCreatedActions() throws Exception {
+ // createRuleContext() gives us the context for a rule upon entry into its analysis function.
+ // But we need to inspect the result of calling created_actions() after the rule context has
+ // been modified by creating actions. So we'll call created_actions() from within the analysis
+ // function and pass it along as a provider.
+ scratch.file("test/rules.bzl",
+ "def _undertest_impl(ctx):",
+ " out1 = ctx.outputs.out1",
+ " out2 = ctx.outputs.out2",
+ " ctx.action(outputs=[out1], command='echo foo123 > ' + out1.path,",
+ " mnemonic='foo')",
+ " v = ctx.created_actions().by_file",
+ " ctx.action(outputs=[out2], command='echo bar123 > ' + out2.path)",
+ " return struct(v=v, out1=out1, out2=out2)",
+ "undertest_rule = rule(",
+ " implementation = _undertest_impl,",
+ " outputs = {'out1': '%{name}1.txt',",
+ " 'out2': '%{name}2.txt'},",
+ " _skylark_testable = True,",
+ ")",
+ testingRuleDefinition
+ );
+ scratch.file("test/BUILD",
+ simpleBuildDefinition);
+ SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
+
+ Object mapUnchecked = evalRuleContextCode(ruleContext, "ruleContext.attr.dep.v");
+ assertThat(mapUnchecked).isInstanceOf(SkylarkDict.class);
+ SkylarkDict<?, ?> map = (SkylarkDict<?, ?>) mapUnchecked;
+ // Should only have the first action because created_actions() was called
+ // before the second action was created.
+ Object file = eval("ruleContext.attr.dep.out1");
+ assertThat(map).hasSize(1);
+ assertThat(map).containsKey(file);
+ Object actionUnchecked = map.get(file);
+ assertThat(actionUnchecked).isInstanceOf(ActionAnalysisMetadata.class);
+ assertThat(((ActionAnalysisMetadata) actionUnchecked).getMnemonic()).isEqualTo("foo");
+ }
+
+ @Test
+ public void testNoAccessToCreatedActionsWithoutSkylarkTest() throws Exception {
+ scratch.file("test/rules.bzl",
+ getSimpleNontestableUnderTestDefinition(
+ "ctx.action(outputs=[out], command='echo foo123 > ' + out.path)")
+ );
+ scratch.file("test/BUILD",
+ "load(':rules.bzl', 'undertest_rule')",
+ "undertest_rule(",
+ " name = 'undertest',",
+ ")");
+ SkylarkRuleContext ruleContext = createRuleContext("//test:undertest");
+
+ Object result = evalRuleContextCode(ruleContext, "ruleContext.created_actions()");
+ assertThat(result).isEqualTo(Runtime.NONE);
+ }
+
@Test
public void testSpawnActionInterface() throws Exception {
scratch.file("test/rules.bzl",