aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleContext.java
diff options
context:
space:
mode:
authorGravatar Jon Brandvein <brandjon@google.com>2016-10-06 13:49:44 +0000
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2016-10-07 08:05:04 +0000
commitf341bc4f6e918b6a41c1536c111bbf24f14f967b (patch)
tree2925e0d71f723b0a4e29e0c4f80beea9fb95c70c /src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleContext.java
parent7cde82ec1c1a3b0a6c931417f81e4d6ed8e08455 (diff)
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 function should call the helper and then inspect the result of get_actions(). -- MOS_MIGRATED_REVID=135353411
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleContext.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleContext.java23
1 files changed, 21 insertions, 2 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleContext.java b/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleContext.java
index 665a15067a..594ace937e 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleContext.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleContext.java
@@ -22,6 +22,7 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.Root;
+import com.google.devtools.build.lib.analysis.ActionsProvider;
import com.google.devtools.build.lib.analysis.AnalysisUtils;
import com.google.devtools.build.lib.analysis.ConfigurationMakeVariableContext;
import com.google.devtools.build.lib.analysis.FilesToRunProvider;
@@ -164,8 +165,8 @@ public final class SkylarkRuleContext {
public SkylarkRuleContext(RuleContext ruleContext, Kind kind)
throws EvalException, InterruptedException {
this.ruleContext = Preconditions.checkNotNull(ruleContext);
- fragments = new FragmentCollection(ruleContext, ConfigurationTransition.NONE);
- hostFragments = new FragmentCollection(ruleContext, ConfigurationTransition.HOST);
+ this.fragments = new FragmentCollection(ruleContext, ConfigurationTransition.NONE);
+ this.hostFragments = new FragmentCollection(ruleContext, ConfigurationTransition.HOST);
if (kind == Kind.RULE) {
Collection<Attribute> attributes = ruleContext.getRule().getAttributes();
@@ -419,6 +420,24 @@ public final class SkylarkRuleContext {
return ruleContext;
}
+ @SkylarkCallable(name = "created_actions",
+ doc = "For rules marked <code>_skylark_testable=True</code>, this returns an "
+ + "<a href=\"ActionsSkylarkApiProvider.html\">actions</a> provider representing all "
+ + "actions created so far for the current rule. For all other rules, returns None. "
+ + "Note that the provider is not updated when subsequent actions are created, so you "
+ + "will have to call this function again if you wish to inspect them. "
+ + ""
+ + "<p>This is intended to help test rule-implementation helper functions that take in a "
+ + "<a href=\"ctx.html\">ctx</a> object and create actions for it.")
+ public Object createdActions() {
+ if (ruleContext.getRule().getRuleClassObject().isSkylarkTestable()) {
+ return ActionsProvider.create(
+ ruleContext.getAnalysisEnvironment().getRegisteredActions());
+ } else {
+ return Runtime.NONE;
+ }
+ }
+
@SkylarkCallable(name = "attr", structField = true, doc = ATTR_DOC)
public SkylarkClassObject getAttr() {
return attributesCollection.getAttr();