aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java')
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java69
1 files changed, 66 insertions, 3 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java
index 7e5a9ee3e0..82a6acb1d8 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java
@@ -15,6 +15,10 @@
package com.google.devtools.build.lib.skylark;
import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -41,6 +45,11 @@ import com.google.devtools.build.lib.syntax.SkylarkSignature;
import com.google.devtools.build.lib.syntax.SkylarkSignature.Param;
import com.google.devtools.build.lib.testutil.MoreAsserts;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
@@ -49,6 +58,7 @@ import java.util.regex.Pattern;
/**
* Tests for SkylarkRuleImplementationFunctions.
*/
+@RunWith(JUnit4.class)
public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
@SkylarkSignature(
@@ -68,9 +78,8 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
@SkylarkSignature(name = "throw", documented = false)
BuiltinFunction throwFunction;
- @Override
- public void setUp() throws Exception {
- super.setUp();
+ @Before
+ public final void createBuildFile() throws Exception {
scratch.file(
"foo/BUILD",
"genrule(name = 'foo',",
@@ -141,6 +150,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
}
}
+ @Test
public void testSkylarkFunctionPosArgs() throws Exception {
setupSkylarkFunction("a = mock('a', 'b', mandatory_key='c')");
Map<?, ?> params = (Map<?, ?>) lookup("a");
@@ -150,6 +160,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertEquals("x", params.get("optional_key"));
}
+ @Test
public void testSkylarkFunctionKwArgs() throws Exception {
setupSkylarkFunction("a = mock(optional='b', mandatory='a', mandatory_key='c')");
Map<?, ?> params = (Map<?, ?>) lookup("a");
@@ -159,6 +170,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertEquals("x", params.get("optional_key"));
}
+ @Test
public void testSkylarkFunctionTooFewArguments() throws Exception {
checkSkylarkFunctionError(
"insufficient arguments received by mock("
@@ -167,6 +179,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
"mock()");
}
+ @Test
public void testSkylarkFunctionTooManyArguments() throws Exception {
checkSkylarkFunctionError(
"too many (3) positional arguments in call to "
@@ -174,6 +187,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
"mock('a', 'b', 'c')");
}
+ @Test
public void testSkylarkFunctionAmbiguousArguments() throws Exception {
checkSkylarkFunctionError(
"argument 'mandatory' passed both by position and by name "
@@ -182,11 +196,13 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
}
@SuppressWarnings("unchecked")
+ @Test
public void testListComprehensionsWithNestedSet() throws Exception {
Object result = eval("[x + x for x in set([1, 2, 3])]");
assertThat((Iterable<Object>) result).containsExactly(2, 4, 6).inOrder();
}
+ @Test
public void testNestedSetGetsConvertedToSkylarkNestedSet() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
Object result =
@@ -198,6 +214,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertEquals(Artifact.class, nset.getContentType().getType());
}
+ @Test
public void testCreateSpawnActionCreatesSpawnAction() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
createTestSpawnAction(ruleContext);
@@ -207,6 +224,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertThat(action).isInstanceOf(SpawnAction.class);
}
+ @Test
public void testCreateSpawnActionArgumentsWithCommand() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
createTestSpawnAction(ruleContext);
@@ -222,6 +240,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertEquals(targetConfig.getDefaultShellEnvironment(), action.getEnvironment());
}
+ @Test
public void testCreateSpawnActionArgumentsWithExecutable() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
evalRuleContextCode(
@@ -240,6 +259,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
MoreAsserts.assertContainsSublist(action.getArguments(), "foo/t.exe", "--a", "--b");
}
+ @Test
public void testCreateSpawnActionArgumentsBadExecutable() throws Exception {
checkErrorContains(
createRuleContext("//foo:foo"),
@@ -251,6 +271,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
" executable = 'xyz.exe')");
}
+ @Test
public void testCreateSpawnActionShellCommandList() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
evalRuleContextCode(
@@ -270,6 +291,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
.inOrder();
}
+ @Test
public void testCreateSpawnActionEnvAndExecInfo() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
evalRuleContextCode(
@@ -291,6 +313,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertEquals(ImmutableMap.of("a", "b"), action.getExecutionInfo());
}
+ @Test
public void testCreateSpawnActionUnknownParam() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
checkErrorContains(
@@ -299,6 +322,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
"ruleContext.action(outputs=[], bad_param = 'some text')");
}
+ @Test
public void testCreateSpawnActionNoExecutable() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
checkErrorContains(
@@ -320,6 +344,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
" use_default_shell_env = True)");
}
+ @Test
public void testCreateSpawnActionBadGenericArg() throws Exception {
checkErrorContains(
createRuleContext("//foo:foo"),
@@ -330,6 +355,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
" command = 'dummy_command')");
}
+ @Test
public void testCreateSpawnActionCommandsListTooShort() throws Exception {
checkErrorContains(
createRuleContext("//foo:foo"),
@@ -339,6 +365,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
" command = ['dummy_command', '--arg'])");
}
+ @Test
public void testCreateFileAction() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
FileWriteAction action =
@@ -354,6 +381,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertFalse(action.makeExecutable());
}
+ @Test
public void testEmptyAction() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
@@ -373,6 +401,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
.isEqualTo(Runtime.NONE);
}
+ @Test
public void testEmptyActionWithExtraAction() throws Exception {
scratch.file(
"test/empty.bzl",
@@ -405,6 +434,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
getPseudoActionViaExtraAction("//test:my_empty_action", "//test:listener");
}
+ @Test
public void testExpandLocation() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:bar");
@@ -463,6 +493,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
Pattern.matches(expectedPattern, computedValue));
}
+ @Test
public void testResolveCommandMakeVariables() throws Exception {
evalRuleContextCode(
createRuleContext("//foo:resolve_me"),
@@ -477,6 +508,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertThat(argv.get(2)).isEqualTo("I got the World on a string");
}
+ @Test
public void testResolveCommandInputs() throws Exception {
evalRuleContextCode(
createRuleContext("//foo:resolve_me"),
@@ -489,6 +521,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertThat(manifests).hasSize(1);
}
+ @Test
public void testResolveCommandExpandLocations() throws Exception {
evalRuleContextCode(
createRuleContext("//foo:resolve_me"),
@@ -510,6 +543,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertMatches("argv[2]", "A.*/mytool .*/mytool.sh B.*file3.dat", argv.get(2));
}
+ @Test
public void testResolveCommandScript() throws Exception {
evalRuleContextCode(
createRuleContext("//foo:resolve_me"),
@@ -526,6 +560,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertMatches("argv[1]", "^.*/resolve_me[.]script[.]sh$", argv.get(1));
}
+ @Test
public void testBadParamTypeErrorMessage() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
checkErrorContains(
@@ -538,6 +573,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
" executable = False)");
}
+ @Test
public void testCreateTemplateAction() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
TemplateExpansionAction action =
@@ -562,6 +598,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
* Once {@link
* com.google.devtools.build.lib.syntax.ParserInputSource#create(com.google.devtools.build.lib.vfs.Path)} parses files using UTF-8, this test will fail.
*/
+ @Test
public void testCreateTemplateActionWithWrongEncoding() throws Exception {
String value = "Š©±½";
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
@@ -587,6 +624,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
return new String(input.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
+ @Test
public void testGetProviderNotTransitiveInfoCollection() throws Exception {
checkErrorContains(
createRuleContext("//foo:foo"),
@@ -595,6 +633,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
"provider('some string', 'FileProvider')");
}
+ @Test
public void testGetProviderNonExistingClassType() throws Exception {
checkErrorContains(
createRuleContext("//foo:foo"),
@@ -605,6 +644,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
"func()");
}
+ @Test
public void testGetProviderNotTransitiveInfoProviderClassType() throws Exception {
checkErrorContains(
createRuleContext("//foo:foo"),
@@ -615,6 +655,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
"func()");
}
+ @Test
public void testRunfilesAddFromDependencies() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:bar");
Object result =
@@ -623,6 +664,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
.contains("libjl.jar");
}
+ @Test
public void testRunfilesStatelessWorksAsOnlyPosArg() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:bar");
Object result =
@@ -631,12 +673,14 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
.contains("libjl.jar");
}
+ @Test
public void testRunfilesBadListGenericType() throws Exception {
checkErrorContains(
"Illegal argument: expected type File for 'files' element but got type string instead",
"ruleContext.runfiles(files = ['some string'])");
}
+ @Test
public void testRunfilesBadSetGenericType() throws Exception {
checkErrorContains(
"expected set of Files or NoneType for 'transitive_files' while calling runfiles "
@@ -644,6 +688,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
"ruleContext.runfiles(transitive_files=set([1, 2, 3]))");
}
+ @Test
public void testRunfilesArtifactsFromArtifact() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
Object result =
@@ -654,6 +699,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertThat(ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result))).contains("t.exe");
}
+ @Test
public void testRunfilesArtifactsFromIterableArtifacts() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
Object result =
@@ -666,6 +712,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
ImmutableList.of("a.txt", "b.img"));
}
+ @Test
public void testRunfilesArtifactsFromNestedSetArtifacts() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
Object result =
@@ -678,6 +725,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
ImmutableList.of("a.txt", "b.img"));
}
+ @Test
public void testRunfilesArtifactsFromDefaultAndFiles() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:bar");
Object result =
@@ -697,6 +745,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
return ((Runfiles) runfiles).getAllArtifacts();
}
+ @Test
public void testRunfilesBadKeywordArguments() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
checkErrorContains(
@@ -705,11 +754,13 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
"ruleContext.runfiles(bad_keyword = '')");
}
+ @Test
public void testNsetContainsList() throws Exception {
checkErrorContains(
"sets cannot contain items of type 'list'", "set() + [ruleContext.files.srcs]");
}
+ @Test
public void testCmdJoinPaths() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
Object result =
@@ -718,6 +769,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertEquals("foo/a.txt:foo/b.img", result);
}
+ @Test
public void testStructPlusArtifactErrorMessage() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
checkErrorContains(
@@ -726,6 +778,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
"ruleContext.files.tools[0] + struct(a = 1)");
}
+ @Test
public void testNoSuchProviderErrorMessage() throws Exception {
checkErrorContains(
createRuleContext("//foo:bar"),
@@ -733,6 +786,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
"ruleContext.attr.srcs[0].my_provider");
}
+ @Test
public void testFilesForRuleConfiguredTarget() throws Exception {
Object result =
evalRuleContextCode(createRuleContext("//foo:foo"), "ruleContext.attr.srcs[0].files");
@@ -740,6 +794,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
"a.txt", ActionsTestUtil.baseNamesOf(((SkylarkNestedSet) result).getSet(Artifact.class)));
}
+ @Test
public void testFilesForFileConfiguredTarget() throws Exception {
Object result =
evalRuleContextCode(createRuleContext("//foo:bar"), "ruleContext.attr.srcs[0].files");
@@ -748,6 +803,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
ActionsTestUtil.baseNamesOf(((SkylarkNestedSet) result).getSet(Artifact.class)));
}
+ @Test
public void testCtxStructFieldsCustomErrorMessages() throws Exception {
checkErrorContains("No attribute 'foo' in attr.", "ruleContext.attr.foo");
checkErrorContains("No attribute 'foo' in outputs.", "ruleContext.outputs.foo");
@@ -756,18 +812,21 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
checkErrorContains("No attribute 'foo' in executable.", "ruleContext.executable.foo");
}
+ @Test
public void testBinDirPath() throws Exception {
SkylarkRuleContext ctx = createRuleContext("//foo:bar");
Object result = evalRuleContextCode(ctx, "ruleContext.configuration.bin_dir.path");
assertEquals(ctx.getConfiguration().getBinFragment().getPathString(), result);
}
+ @Test
public void testEmptyLabelListTypeAttrInCtx() throws Exception {
SkylarkRuleContext ctx = createRuleContext("//foo:baz");
Object result = evalRuleContextCode(ctx, "ruleContext.attr.srcs");
assertEquals(MutableList.EMPTY, result);
}
+ @Test
public void testDefinedMakeVariable() throws Exception {
SkylarkRuleContext ctx = createRuleContext("//foo:baz");
String javac = (String) evalRuleContextCode(ctx, "ruleContext.var['JAVAC']");
@@ -776,6 +835,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
assertEquals("/javac", javac);
}
+ @Test
public void testCodeCoverageConfigurationAccess() throws Exception {
SkylarkRuleContext ctx = createRuleContext("//foo:baz");
boolean coverage =
@@ -832,6 +892,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
}
}
+ @Test
public void testStackTraceWithoutOriginalMessage() throws Exception {
setupThrowFunction(
new BuiltinFunction("throw") {
@@ -848,6 +909,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
"throw()");
}
+ @Test
public void testNoStackTraceOnInterrupt() throws Exception {
setupThrowFunction(
new BuiltinFunction("throw") {
@@ -864,6 +926,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
}
}
+ @Test
public void testGlobInImplicitOutputs() throws Exception {
scratch.file("test/glob.bzl",
"def _impl(ctx):",