Skip to content

Commit

Permalink
Update tests to avoid implicit depset iteration
Browse files Browse the repository at this point in the history
Progress towards #5816

RELNOTES: None.
PiperOrigin-RevId: 249341642
  • Loading branch information
laurentlb authored and Copybara-Service committed May 21, 2019
1 parent 1fc6fea commit 32282e5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public void aspectWithOutputGroupsAsList() throws Exception {
"test/aspect.bzl",
"def _impl(target, ctx):",
" g = target.output_group('_hidden_top_level" + INTERNAL_SUFFIX + "')",
" return struct(output_groups = { 'my_result' : [ f for f in g] })",
" return struct(output_groups = { 'my_result' : g.to_list() })",
"",
"MyAspect = aspect(",
" implementation=_impl,",
Expand Down Expand Up @@ -526,7 +526,7 @@ public void aspectWithOutputGroupsAsListDeclaredProvider() throws Exception {
"test/aspect.bzl",
"def _impl(target, ctx):",
" g = target[OutputGroupInfo]._hidden_top_level" + INTERNAL_SUFFIX,
" return [OutputGroupInfo(my_result= [ f for f in g])]",
" return [OutputGroupInfo(my_result=g.to_list())]",
"",
"MyAspect = aspect(",
" implementation=_impl,",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public void testOutputGroupsWithList() throws Exception {
"load('//myinfo:myinfo.bzl', 'MyInfo')",
"def _impl(ctx):",
" f = ctx.attr.dep.output_group('_hidden_top_level" + INTERNAL_SUFFIX + "')",
" g = list(f)",
" g = f.to_list()",
" return [MyInfo(result = f),",
" OutputGroupInfo(my_group = g, my_empty_group = [])]",
"my_rule = rule(implementation = _impl,",
Expand Down Expand Up @@ -361,7 +361,7 @@ public void testOutputGroupsDeclaredProviderWithList() throws Exception {
"load('//myinfo:myinfo.bzl', 'MyInfo')",
"def _impl(ctx):",
" f = ctx.attr.dep[OutputGroupInfo]._hidden_top_level" + INTERNAL_SUFFIX,
" g = list(f)",
" g = f.to_list()",
" return [MyInfo(result = f),",
" OutputGroupInfo(my_group = g, my_empty_group = [])]",
"my_rule = rule(implementation = _impl,",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1530,21 +1530,22 @@ public void testAccessingRunfiles() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//test:foo");
Object filenames =
evalRuleContextCode(
ruleContext, "[f.short_path for f in ruleContext.attr.dep.default_runfiles.files]");
ruleContext,
"[f.short_path for f in ruleContext.attr.dep.default_runfiles.files.to_list()]");
assertThat(filenames).isInstanceOf(SkylarkList.class);
SkylarkList filenamesList = (SkylarkList) filenames;
assertThat(filenamesList).containsAtLeast("test/lib.py", "test/lib2.py");
Object emptyFilenames =
evalRuleContextCode(
ruleContext, "list(ruleContext.attr.dep.default_runfiles.empty_filenames)");
ruleContext, "ruleContext.attr.dep.default_runfiles.empty_filenames.to_list()");
assertThat(emptyFilenames).isInstanceOf(SkylarkList.class);
SkylarkList emptyFilenamesList = (SkylarkList) emptyFilenames;
assertThat(emptyFilenamesList).containsExactly("test/__init__.py");

SkylarkRuleContext ruleWithInitContext = createRuleContext("//test:foo_with_init");
Object noEmptyFilenames =
evalRuleContextCode(
ruleWithInitContext, "list(ruleContext.attr.dep.default_runfiles.empty_filenames)");
ruleWithInitContext, "ruleContext.attr.dep.default_runfiles.empty_filenames.to_list()");
assertThat(noEmptyFilenames).isInstanceOf(SkylarkList.class);
SkylarkList noEmptyFilenamesList = (SkylarkList) noEmptyFilenames;
assertThat(noEmptyFilenamesList).isEmpty();
Expand Down Expand Up @@ -1587,15 +1588,15 @@ public void testAccessingRunfilesSymlinks_legacy() throws Exception {
evalRuleContextCode(
ruleWithSymlinkContext,
"[s.path for s in",
"ruleContext.attr.data[0].data_runfiles.symlinks]");
"ruleContext.attr.data[0].data_runfiles.symlinks.to_list()]");
assertThat(symlinkPaths).isInstanceOf(SkylarkList.class);
SkylarkList<String> symlinkPathsList = (SkylarkList<String>) symlinkPaths;
assertThat(symlinkPathsList).containsExactly("symlink_test/a.py").inOrder();
Object symlinkFilenames =
evalRuleContextCode(
ruleWithSymlinkContext,
"[s.target_file.short_path for s in",
"ruleContext.attr.data[0].data_runfiles.symlinks]");
"ruleContext.attr.data[0].data_runfiles.symlinks.to_list()]");
assertThat(symlinkFilenames).isInstanceOf(SkylarkList.class);
SkylarkList<String> symlinkFilenamesList = (SkylarkList<String>) symlinkFilenames;
assertThat(symlinkFilenamesList).containsExactly("test/a.py").inOrder();
Expand Down Expand Up @@ -1638,15 +1639,15 @@ public void testAccessingRunfilesSymlinks() throws Exception {
evalRuleContextCode(
ruleWithSymlinkContext,
"[s.path for s in",
"ruleContext.attr.data[0].data_runfiles.symlinks]");
"ruleContext.attr.data[0].data_runfiles.symlinks.to_list()]");
assertThat(symlinkPaths).isInstanceOf(SkylarkList.class);
SkylarkList<String> symlinkPathsList = (SkylarkList<String>) symlinkPaths;
assertThat(symlinkPathsList).containsExactly("symlink_test/a.py").inOrder();
Object symlinkFilenames =
evalRuleContextCode(
ruleWithSymlinkContext,
"[s.target_file.short_path for s in",
"ruleContext.attr.data[0].data_runfiles.symlinks]");
"ruleContext.attr.data[0].data_runfiles.symlinks.to_list()]");
assertThat(symlinkFilenames).isInstanceOf(SkylarkList.class);
SkylarkList<String> symlinkFilenamesList = (SkylarkList<String>) symlinkFilenames;
assertThat(symlinkFilenamesList).containsExactly("test/a.py").inOrder();
Expand Down Expand Up @@ -1690,15 +1691,15 @@ public void testAccessingRunfilesRootSymlinks_legacy() throws Exception {
evalRuleContextCode(
ruleWithRootSymlinkContext,
"[s.path for s in",
"ruleContext.attr.data[0].data_runfiles.root_symlinks]");
"ruleContext.attr.data[0].data_runfiles.root_symlinks.to_list()]");
assertThat(rootSymlinkPaths).isInstanceOf(SkylarkList.class);
SkylarkList<String> rootSymlinkPathsList = (SkylarkList<String>) rootSymlinkPaths;
assertThat(rootSymlinkPathsList).containsExactly("root_symlink_test/a.py").inOrder();
Object rootSymlinkFilenames =
evalRuleContextCode(
ruleWithRootSymlinkContext,
"[s.target_file.short_path for s in",
"ruleContext.attr.data[0].data_runfiles.root_symlinks]");
"ruleContext.attr.data[0].data_runfiles.root_symlinks.to_list()]");
assertThat(rootSymlinkFilenames).isInstanceOf(SkylarkList.class);
SkylarkList<String> rootSymlinkFilenamesList = (SkylarkList<String>) rootSymlinkFilenames;
assertThat(rootSymlinkFilenamesList).containsExactly("test/a.py").inOrder();
Expand Down Expand Up @@ -1742,15 +1743,15 @@ public void testAccessingRunfilesRootSymlinks() throws Exception {
evalRuleContextCode(
ruleWithRootSymlinkContext,
"[s.path for s in",
"ruleContext.attr.data[0].data_runfiles.root_symlinks]");
"ruleContext.attr.data[0].data_runfiles.root_symlinks.to_list()]");
assertThat(rootSymlinkPaths).isInstanceOf(SkylarkList.class);
SkylarkList<String> rootSymlinkPathsList = (SkylarkList<String>) rootSymlinkPaths;
assertThat(rootSymlinkPathsList).containsExactly("root_symlink_test/a.py").inOrder();
Object rootSymlinkFilenames =
evalRuleContextCode(
ruleWithRootSymlinkContext,
"[s.target_file.short_path for s in",
"ruleContext.attr.data[0].data_runfiles.root_symlinks]");
"ruleContext.attr.data[0].data_runfiles.root_symlinks.to_list()]");
assertThat(rootSymlinkFilenames).isInstanceOf(SkylarkList.class);
SkylarkList<String> rootSymlinkFilenamesList = (SkylarkList<String>) rootSymlinkFilenames;
assertThat(rootSymlinkFilenamesList).containsExactly("test/a.py").inOrder();
Expand Down Expand Up @@ -1851,7 +1852,7 @@ public void testDependencyActionsProvider() throws Exception {
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]"));
update("file", eval("ruleContext.attr.dep.files.to_list()[0]"));
Object actionUnchecked = eval("actions.by_file[file]");
assertThat(actionUnchecked).isInstanceOf(ActionAnalysisMetadata.class);
}
Expand Down Expand Up @@ -1913,10 +1914,10 @@ public void testAbstractActionInterface() throws Exception {
assertThat(eval("action2.content")).isEqualTo(Runtime.NONE);
assertThat(eval("action1.substitutions")).isEqualTo(Runtime.NONE);

assertThat(eval("list(action1.inputs)")).isEqualTo(eval("[]"));
assertThat(eval("list(action1.outputs)")).isEqualTo(eval("[file1]"));
assertThat(eval("list(action2.inputs)")).isEqualTo(eval("[file1]"));
assertThat(eval("list(action2.outputs)")).isEqualTo(eval("[file2]"));
assertThat(eval("action1.inputs.to_list()")).isEqualTo(eval("[]"));
assertThat(eval("action1.outputs.to_list()")).isEqualTo(eval("[file1]"));
assertThat(eval("action2.inputs.to_list()")).isEqualTo(eval("[file1]"));
assertThat(eval("action2.outputs.to_list()")).isEqualTo(eval("[file2]"));
}

// For created_actions() tests, the "undertest" rule represents both the code under test and the
Expand Down Expand Up @@ -1991,7 +1992,7 @@ public void testSpawnActionInterface() throws Exception {
simpleBuildDefinition);
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
update("ruleContext", ruleContext);
update("file", eval("list(ruleContext.attr.dep.files)[0]"));
update("file", eval("ruleContext.attr.dep.files.to_list()[0]"));
update("action", eval("ruleContext.attr.dep[Actions].by_file[file]"));

assertThat(eval("type(action)")).isEqualTo("Action");
Expand Down Expand Up @@ -2102,7 +2103,7 @@ public void testFileWriteActionInterface() throws Exception {
simpleBuildDefinition);
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
update("ruleContext", ruleContext);
update("file", eval("list(ruleContext.attr.dep.files)[0]"));
update("file", eval("ruleContext.attr.dep.files.to_list()[0]"));
update("action", eval("ruleContext.attr.dep[Actions].by_file[file]"));

assertThat(eval("type(action)")).isEqualTo("Action");
Expand Down Expand Up @@ -2141,7 +2142,7 @@ public void testTemplateExpansionActionInterface() throws Exception {
")");
SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
update("ruleContext", ruleContext);
update("file", eval("list(ruleContext.attr.dep.files)[0]"));
update("file", eval("ruleContext.attr.dep.files.to_list()[0]"));
update("action", eval("ruleContext.attr.dep[Actions].by_file[file]"));

assertThat(eval("type(action)")).isEqualTo("Action");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import com.google.devtools.build.lib.syntax.SkylarkList;
import com.google.devtools.build.lib.syntax.SkylarkList.MutableList;
import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
import com.google.devtools.build.lib.syntax.StarlarkSemantics;
import com.google.devtools.build.lib.testutil.MoreAsserts;
import com.google.devtools.build.lib.util.Fingerprint;
import com.google.devtools.build.lib.util.OsUtils;
Expand Down Expand Up @@ -248,6 +249,13 @@ public void testSkylarkFunctionAmbiguousArguments() throws Exception {
@SuppressWarnings("unchecked")
@Test
public void testListComprehensionsWithNestedSet() throws Exception {
ev =
createEvaluationTestCase(
StarlarkSemantics.DEFAULT_SEMANTICS.toBuilder()
.incompatibleDepsetIsNotIterable(false)
.build());
ev.initialize();

Object result = eval("[x + x for x in depset([1, 2, 3])]");
assertThat((Iterable<Object>) result).containsExactly(2, 4, 6).inOrder();
}
Expand Down Expand Up @@ -706,8 +714,8 @@ public void testResolveCommandExpandLocations() throws Exception {
" label_dict = {}",
" all = []",
" for dep in ruleContext.attr.srcs + ruleContext.attr.tools:",
" all.extend(list(dep.files))",
" label_dict[dep.label] = list(dep.files)",
" all.extend(dep.files.to_list())",
" label_dict[dep.label] = dep.files.to_list()",
" return ruleContext.resolve_command(",
" command='A$(locations //foo:mytool) B$(location //foo:file3.dat)',",
" attribute='cmd', expand_locations=True, label_dict=label_dict)",
Expand Down Expand Up @@ -3185,4 +3193,3 @@ public String getMessage() {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void testStackTraceWithIf() throws Exception {
+ LINE_SEPARATOR
+ "\t\ts[0]",
"def foo():",
" s = depset()",
" s = []",
" if s[0] == 1:",
" x = 1",
"foo()");
Expand Down Expand Up @@ -249,7 +249,6 @@ public void testListSort() throws Exception {
.testEval("sorted([True, False, True])", "[False, True, True]")
.testEval("sorted(['a','x','b','z'])", "[\"a\", \"b\", \"x\", \"z\"]")
.testEval("sorted({1: True, 5: True, 4: False})", "[1, 4, 5]")
.testEval("sorted(depset([1, 5, 4]))", "[1, 4, 5]")
.testIfExactError("Cannot compare function with function", "sorted([sorted, sorted])");
}

Expand Down Expand Up @@ -637,8 +636,7 @@ public void testZipFunction() throws Exception {
.testStatement("str(zip([1], {2: 'a'}))", "[(1, 2)]")
.testStatement("str(zip([1], []))", "[]")
.testIfErrorContains("type 'int' is not iterable", "zip(123)")
.testIfErrorContains("type 'int' is not iterable", "zip([1], 1)")
.testStatement("str(zip([1], depset([2])))", "[(1, 2)]");
.testIfErrorContains("type 'int' is not iterable", "zip([1], 1)");
}

/**
Expand Down Expand Up @@ -701,7 +699,6 @@ public void testFail() throws Exception {
public void testTupleCoercion() throws Exception {
new BothModesTest()
.testStatement("tuple([1, 2]) == (1, 2)", true)
.testStatement("tuple(depset([1, 2])) == (1, 2)", true)
// Depends on current implementation of dict
.testStatement("tuple({1: 'foo', 2: 'bar'}) == (1, 2)", true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1487,8 +1487,8 @@ public void testClassObjectAccess() throws Exception {
}

@Test
public void testInSet() throws Exception {
new SkylarkTest()
public void testInSetDeprecated() throws Exception {
new SkylarkTest("--incompatible_depset_is_not_iterable=false")
.testStatement("'b' in depset(['a', 'b'])", Boolean.TRUE)
.testStatement("'c' in depset(['a', 'b'])", Boolean.FALSE)
.testStatement("1 in depset(['a', 'b'])", Boolean.FALSE);
Expand Down

0 comments on commit 32282e5

Please sign in to comment.