use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.
the class SkylarkRuleContextTest method testSpawnActionInterface.
@Test
public void testSpawnActionInterface() throws Exception {
scratch.file("test/rules.bzl", getSimpleUnderTestDefinition("ctx.action(outputs=[out], command='echo foo123 > ' + out.path)"), testingRuleDefinition);
scratch.file("test/BUILD", simpleBuildDefinition);
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
update("ruleContext", ruleContext);
update("file", eval("list(ruleContext.attr.dep.files)[0]"));
update("action", eval("ruleContext.attr.dep[Actions].by_file[file]"));
assertThat(eval("type(action)")).isEqualTo("Action");
Object argvUnchecked = eval("action.argv");
assertThat(argvUnchecked).isInstanceOf(SkylarkList.MutableList.class);
SkylarkList.MutableList<?> argv = (SkylarkList.MutableList<?>) argvUnchecked;
assertThat(argv).hasSize(3);
assertThat(argv.isImmutable()).isTrue();
Object result = eval("action.argv[2].startswith('echo foo123')");
assertThat((Boolean) result).isTrue();
}
use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.
the class SkylarkRuleContextTest method shouldGetPrerequisite.
@Test
public void shouldGetPrerequisite() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:asr");
Object result = evalRuleContextCode(ruleContext, "ruleContext.attr.srcjar");
TransitiveInfoCollection tic = (TransitiveInfoCollection) result;
assertThat(tic).isInstanceOf(FileConfiguredTarget.class);
assertEquals("asr-src.jar", tic.getLabel().getName());
}
use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.
the class SkylarkRuleContextTest method testDependencyActionsProvider.
@Test
public void testDependencyActionsProvider() throws Exception {
scratch.file("test/rules.bzl", getSimpleUnderTestDefinition("ctx.action(outputs=[out], command='echo foo123 > ' + out.path)"), testingRuleDefinition);
scratch.file("test/BUILD", simpleBuildDefinition);
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
Object provider = evalRuleContextCode(ruleContext, "ruleContext.attr.dep[Actions]");
assertThat(provider).isInstanceOf(SkylarkClassObject.class);
assertThat(((SkylarkClassObject) provider).getConstructor()).isEqualTo(ActionsProvider.SKYLARK_CONSTRUCTOR);
update("actions", provider);
Object mapping = eval("actions.by_file");
assertThat(mapping).isInstanceOf(SkylarkDict.class);
assertThat((SkylarkDict<?, ?>) mapping).hasSize(1);
update("file", eval("list(ruleContext.attr.dep.files)[0]"));
Object actionUnchecked = eval("actions.by_file[file]");
assertThat(actionUnchecked).isInstanceOf(ActionAnalysisMetadata.class);
}
use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.
the class SkylarkRuleContextTest method testGetLabel.
@Test
public void testGetLabel() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
Object result = evalRuleContextCode(ruleContext, "ruleContext.label");
assertEquals("//foo:foo", ((Label) result).toString());
}
use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.
the class SkylarkRuleContextTest method testLabelKeyedStringDictAcceptsDefaultValues.
@Test
public void testLabelKeyedStringDictAcceptsDefaultValues() throws Exception {
scratch.file("my_rule.bzl", "def _impl(ctx):", " return", "my_rule = rule(", " implementation = _impl,", " attrs = {", " 'label_dict': attr.label_keyed_string_dict(default={Label('//:default'): 'defs'}),", " }", ")");
scratch.file("BUILD", "filegroup(name='default')", "load('//:my_rule.bzl', 'my_rule')", "my_rule(name='r')");
invalidatePackages();
SkylarkRuleContext context = createRuleContext("//:r");
Label keyLabel = (Label) evalRuleContextCode(context, "ruleContext.attr.label_dict.keys()[0].label");
assertThat(keyLabel).isEqualTo(Label.parseAbsolute("//:default"));
String valueString = (String) evalRuleContextCode(context, "ruleContext.attr.label_dict.values()[0]");
assertThat(valueString).isEqualTo("defs");
}
Aggregations