use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.
the class SkylarkRuleImplementationFunctionsTest method testRunfilesArtifactsFromArtifact.
@Test
public void testRunfilesArtifactsFromArtifact() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
Object result = evalRuleContextCode(ruleContext, "artifacts = ruleContext.files.tools", "ruleContext.runfiles(files = artifacts)");
assertThat(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result))).contains("t.exe");
}
use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.
the class SkylarkRuleContextTest method testCoverageInstrumentedFalseForSourceFileLabel.
@Test
public void testCoverageInstrumentedFalseForSourceFileLabel() throws Exception {
setUpCoverageInstrumentedTest();
useConfiguration("--collect_code_coverage", "--instrumentation_filter=.");
SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
Object result = evalRuleContextCode(ruleContext, "ruleContext.coverage_instrumented(ruleContext.attr.srcs[0])");
assertThat((Boolean) result).isFalse();
}
use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.
the class SkylarkRuleContextTest method testGetRule.
@Test
public void testGetRule() throws Exception {
scratch.file("test/skylark/BUILD");
scratch.file("test/skylark/rulestr.bzl", "def rule_dict(name):", " return native.existing_rule(name)", "def rules_dict():", " return native.existing_rules()", "def nop(ctx):", " pass", "nop_rule = rule(attrs = {'x': attr.label()}, implementation = nop)", "consume_rule = rule(attrs = {'s': attr.string_list()}, implementation = nop)");
scratch.file("test/getrule/BUILD", "load('/test/skylark/rulestr', 'rules_dict', 'rule_dict', 'nop_rule', 'consume_rule')", "genrule(name = 'a', outs = ['a.txt'], ", " licenses = ['notice'],", " output_to_bindir = False,", " tools = [ '//test:bla' ], cmd = 'touch $@')", "nop_rule(name = 'c', x = ':a')", "rlist= rules_dict()", "consume_rule(name = 'all_str', s = [rlist['a']['kind'], rlist['a']['name'], ", " rlist['c']['kind'], rlist['c']['name']])", "adict = rule_dict('a')", "cdict = rule_dict('c')", "consume_rule(name = 'a_str', ", " s = [adict['kind'], adict['name'], adict['outs'][0], adict['tools'][0]])", "consume_rule(name = 'genrule_attr', ", " s = adict.keys())", "consume_rule(name = 'c_str', s = [cdict['kind'], cdict['name'], cdict['x']])");
SkylarkRuleContext allContext = createRuleContext("//test/getrule:all_str");
List<String> result = (List<String>) evalRuleContextCode(allContext, "ruleContext.attr.s");
assertThat(result).containsExactly("genrule", "a", "nop_rule", "c");
result = (List<String>) evalRuleContextCode(createRuleContext("//test/getrule:a_str"), "ruleContext.attr.s");
assertThat(result).containsExactly("genrule", "a", ":a.txt", "//test:bla");
result = (List<String>) evalRuleContextCode(createRuleContext("//test/getrule:c_str"), "ruleContext.attr.s");
assertThat(result).containsExactly("nop_rule", "c", ":a");
result = (List<String>) evalRuleContextCode(createRuleContext("//test/getrule:genrule_attr"), "ruleContext.attr.s");
assertThat(result).containsExactly("name", "visibility", "tags", "generator_name", "generator_function", "generator_location", "features", "compatible_with", "restricted_to", "srcs", "tools", "toolchains", "outs", "cmd", "output_to_bindir", "local", "message", "executable", "stamp", "heuristic_label_expansion", "kind");
}
use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.
the class SkylarkRuleContextTest method testCallerRelativeLabelInExternalRepository.
@Test
public void testCallerRelativeLabelInExternalRepository() throws Exception {
scratch.file("BUILD");
scratch.file("external_rule.bzl", "def _impl(ctx):", " return", "external_rule = rule(", " implementation = _impl,", " attrs = {", " 'internal_dep': attr.label(", " default = Label('//:dep', relative_to_caller_repository = True)", " )", " }", ")");
scratch.file("/r/BUILD", "filegroup(name='dep')");
scratch.file("/r/a/BUILD", "load('/external_rule', 'external_rule')", "external_rule(name='r')");
scratch.overwriteFile("WORKSPACE", new ImmutableList.Builder<String>().addAll(analysisMock.getWorkspaceContents(mockToolsConfig)).add("local_repository(name='r', path='/r')").build());
// Repository shuffling messes with toolchain labels.
invalidatePackages(/*alsoConfigs=*/
false);
SkylarkRuleContext context = createRuleContext("@r//a:r");
Label depLabel = (Label) evalRuleContextCode(context, "ruleContext.attr.internal_dep.label");
assertThat(depLabel).isEqualTo(Label.parseAbsolute("@r//:dep"));
}
use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.
the class SkylarkRuleContextTest method testAccessingRunfiles.
@Test
public void testAccessingRunfiles() throws Exception {
scratch.file("test/a.py");
scratch.file("test/b.py");
scratch.file("test/__init__.py");
scratch.file("test/rule.bzl", "def _impl(ctx):", " return", "skylark_rule = rule(", " implementation = _impl,", " attrs = {", " 'dep': attr.label(),", " },", ")");
scratch.file("test/BUILD", "load('/test/rule', 'skylark_rule')", "py_library(name = 'lib', srcs = ['a.py', 'b.py'])", "skylark_rule(name = 'foo', dep = ':lib')", "py_library(name = 'lib_with_init', srcs = ['a.py', 'b.py', '__init__.py'])", "skylark_rule(name = 'foo_with_init', dep = ':lib_with_init')");
SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
Object filenames = evalRuleContextCode(ruleContext, "[f.short_path for f in ruleContext.attr.dep.default_runfiles.files]");
assertThat(filenames).isInstanceOf(SkylarkList.class);
SkylarkList filenamesList = (SkylarkList) filenames;
assertThat(filenamesList).containsExactly("test/a.py", "test/b.py").inOrder();
Object emptyFilenames = evalRuleContextCode(ruleContext, "list(ruleContext.attr.dep.default_runfiles.empty_filenames)");
assertThat(emptyFilenames).isInstanceOf(SkylarkList.class);
SkylarkList emptyFilenamesList = (SkylarkList) emptyFilenames;
assertThat(emptyFilenamesList).containsExactly("test/__init__.py").inOrder();
SkylarkRuleContext ruleWithInitContext = createRuleContext("//test:foo_with_init");
Object noEmptyFilenames = evalRuleContextCode(ruleWithInitContext, "list(ruleContext.attr.dep.default_runfiles.empty_filenames)");
assertThat(noEmptyFilenames).isInstanceOf(SkylarkList.class);
SkylarkList noEmptyFilenamesList = (SkylarkList) noEmptyFilenames;
assertThat(noEmptyFilenamesList).containsExactly().inOrder();
}
Aggregations