From f341bc4f6e918b6a41c1536c111bbf24f14f967b Mon Sep 17 00:00:00 2001 From: Jon Brandvein Date: Thu, 6 Oct 2016 13:49:44 +0000 Subject: 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 --- .../build/lib/rules/SkylarkRuleContext.java | 23 ++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleContext.java') 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 attributes = ruleContext.getRule().getAttributes(); @@ -419,6 +420,24 @@ public final class SkylarkRuleContext { return ruleContext; } + @SkylarkCallable(name = "created_actions", + doc = "For rules marked _skylark_testable=True, this returns an " + + "actions 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. " + + "" + + "

This is intended to help test rule-implementation helper functions that take in a " + + "ctx 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(); -- cgit v1.2.3