aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test
diff options
context:
space:
mode:
authorGravatar Ulf Adams <ulfjack@google.com>2016-10-11 10:59:46 +0000
committerGravatar Yue Gan <yueg@google.com>2016-10-11 13:26:33 +0000
commit8d36a34ee02ad0fd07d713b8c8ee273ff30d2fb9 (patch)
tree5eb0a2c72932403d97cfc961f16c315c9a7e2ac3 /src/test
parent155bd6457c7426faa84b5acba991a1c80cdcf920 (diff)
*** 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 useful for unit testing analysis-time helper functions that take in ctx and have the side-effect of creating actions. In this use case, the testing rule should be marked _skylark_testable=True, and its implementation f... *** -- MOS_MIGRATED_REVID=135781162
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleContextTest.java81
1 files changed, 9 insertions, 72 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 674aa40bdb..6066c41a4c 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, boolean withSkylarkTestable) {
+ private String getSimpleUnderTestDefinition(String actionLine) {
return linesAsString(
"def _undertest_impl(ctx):",
" out = ctx.outputs.out",
@@ -1037,18 +1037,10 @@ public class SkylarkRuleContextTest extends SkylarkTestCase {
"undertest_rule = rule(",
" implementation = _undertest_impl,",
" outputs = {'out': '%{name}.txt'},",
- withSkylarkTestable ? " _skylark_testable = True," : "",
+ " _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):",
@@ -1097,8 +1089,13 @@ public class SkylarkRuleContextTest extends SkylarkTestCase {
public void testNoAccessToDependencyActionsWithoutSkylarkTest() throws Exception {
reporter.removeHandler(failFastHandler);
scratch.file("test/rules.bzl",
- getSimpleNontestableUnderTestDefinition(
- "ctx.action(outputs=[out], command='echo foo123 > ' + out.path)"),
+ "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'},",
+ ")",
testingRuleDefinition);
scratch.file("test/BUILD",
simpleBuildDefinition);
@@ -1151,66 +1148,6 @@ 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",