Search in sources :

Example 41 with SkylarkRuleContext

use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.

the class SkylarkRuleImplementationFunctionsTest method testEmptyAction.

@Test
public void testEmptyAction() throws Exception {
    SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
    checkEmptyAction(ruleContext, "mnemonic = 'test'");
    checkEmptyAction(ruleContext, "mnemonic = 'test', inputs = ruleContext.files.srcs");
    checkErrorContains(ruleContext, "missing mandatory named-only argument 'mnemonic' while calling empty_action", "ruleContext.empty_action(inputs = ruleContext.files.srcs)");
}
Also used : SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) Test(org.junit.Test)

Example 42 with SkylarkRuleContext

use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.

the class SkylarkRuleImplementationFunctionsTest method testCreateSpawnActionArgumentsWithExecutable.

@Test
public void testCreateSpawnActionArgumentsWithExecutable() throws Exception {
    SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
    evalRuleContextCode(ruleContext, "ruleContext.action(", "  inputs = ruleContext.files.srcs,", "  outputs = ruleContext.files.srcs,", "  arguments = ['--a','--b'],", "  executable = ruleContext.files.tools[0])");
    SpawnAction action = (SpawnAction) Iterables.getOnlyElement(ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions());
    assertArtifactFilenames(action.getInputs(), "a.txt", "b.img", "t.exe");
    assertArtifactFilenames(action.getOutputs(), "a.txt", "b.img");
    MoreAsserts.assertContainsSublist(action.getArguments(), "foo/t.exe", "--a", "--b");
}
Also used : SpawnAction(com.google.devtools.build.lib.analysis.actions.SpawnAction) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) Test(org.junit.Test)

Example 43 with SkylarkRuleContext

use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.

the class SkylarkRuleContextTest method testExternalShortPath.

@Test
public void testExternalShortPath() throws Exception {
    scratch.file("/bar/WORKSPACE");
    scratch.file("/bar/bar.txt");
    scratch.file("/bar/BUILD", "exports_files(['bar.txt'])");
    FileSystemUtils.appendIsoLatin1(scratch.resolve("WORKSPACE"), "local_repository(name = 'foo', path = '/bar')");
    scratch.file("test/BUILD", "genrule(", "    name = 'lib',", "    srcs = ['@foo//:bar.txt'],", "    cmd = 'echo $(SRCS) $@',", "    outs = ['lib.out'],", "    executable = 1,", ")");
    invalidatePackages();
    SkylarkRuleContext ruleContext = createRuleContext("//test:lib");
    String filename = evalRuleContextCode(ruleContext, "ruleContext.files.srcs[0].short_path").toString();
    assertThat(filename).isEqualTo("../foo/bar.txt");
}
Also used : SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) Test(org.junit.Test)

Example 44 with SkylarkRuleContext

use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.

the class SkylarkRuleContextTest method testLabelAttributeDefault.

@Test
public void testLabelAttributeDefault() throws Exception {
    scratch.file("my_rule.bzl", "def _impl(ctx):", "  return", "my_rule = rule(", "  implementation = _impl,", "  attrs = {", "    'explicit_dep': attr.label(default = Label('//:dep')),", "    '_implicit_dep': attr.label(default = Label('//:dep')),", "    'explicit_dep_list': attr.label_list(default = [Label('//:dep')]),", "    '_implicit_dep_list': attr.label_list(default = [Label('//:dep')]),", "  }", ")");
    scratch.file("BUILD", "filegroup(name='dep')", "load('/my_rule', 'my_rule')", "my_rule(name='r')");
    invalidatePackages();
    SkylarkRuleContext context = createRuleContext("//:r");
    Label explicitDepLabel = (Label) evalRuleContextCode(context, "ruleContext.attr.explicit_dep.label");
    assertThat(explicitDepLabel).isEqualTo(Label.parseAbsolute("//:dep"));
    Label implicitDepLabel = (Label) evalRuleContextCode(context, "ruleContext.attr._implicit_dep.label");
    assertThat(implicitDepLabel).isEqualTo(Label.parseAbsolute("//:dep"));
    Label explicitDepListLabel = (Label) evalRuleContextCode(context, "ruleContext.attr.explicit_dep_list[0].label");
    assertThat(explicitDepListLabel).isEqualTo(Label.parseAbsolute("//:dep"));
    Label implicitDepListLabel = (Label) evalRuleContextCode(context, "ruleContext.attr._implicit_dep_list[0].label");
    assertThat(implicitDepListLabel).isEqualTo(Label.parseAbsolute("//:dep"));
}
Also used : Label(com.google.devtools.build.lib.cmdline.Label) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) Test(org.junit.Test)

Example 45 with SkylarkRuleContext

use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.

the class SkylarkRuleContextTest method testHostConfiguration.

@Test
public void testHostConfiguration() throws Exception {
    SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
    Object result = evalRuleContextCode(ruleContext, "ruleContext.host_configuration");
    assertSame(result, ruleContext.getRuleContext().getHostConfiguration());
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) Test(org.junit.Test)

Aggregations

SkylarkRuleContext (com.google.devtools.build.lib.rules.SkylarkRuleContext)81 Test (org.junit.Test)79 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)47 Label (com.google.devtools.build.lib.cmdline.Label)6 Artifact (com.google.devtools.build.lib.actions.Artifact)5 SpawnAction (com.google.devtools.build.lib.analysis.actions.SpawnAction)5 SkylarkList (com.google.devtools.build.lib.syntax.SkylarkList)4 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)4 ActionAnalysisMetadata (com.google.devtools.build.lib.actions.ActionAnalysisMetadata)2 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)2 TemplateExpansionAction (com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction)2 SkylarkDict (com.google.devtools.build.lib.syntax.SkylarkDict)2 ImmutableList (com.google.common.collect.ImmutableList)1 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 RuleContext (com.google.devtools.build.lib.analysis.RuleContext)1 FileWriteAction (com.google.devtools.build.lib.analysis.actions.FileWriteAction)1 Substitution (com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution)1 AspectDescriptor (com.google.devtools.build.lib.packages.AspectDescriptor)1 JavaSourceJarsProvider (com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider)1 SkylarkCallable (com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)1