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 bazelbuild/bazel#5816

    RELNOTES: None.
    PiperOrigin-RevId: 249341642
  • Loading branch information
Luca Di Grazia committed Sep 4, 2022
1 parent 72997c9 commit 3aa0954
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ public void duplicateOutputGroups() throws Exception {
"test/aspect.bzl",
"def _impl(target, ctx):",
" f = ctx.actions.declare_file('f.txt')",
" ctx.actions.write(f, 'f')",
" ctx.file_action(f, 'f')",
" return struct(output_groups = { 'duplicate' : depset([f]) })",
"",
"MyAspect = aspect(implementation=_impl)",
Expand Down Expand Up @@ -1820,47 +1820,6 @@ public void topLevelAspectsAndExtraActions() throws Exception {
.contains("file.xa");
}

/** Regression test for b/137960630. */
@Test
public void topLevelAspectsAndExtraActionsWithConflict() throws Exception {
scratch.file(
"test/aspect.bzl",
"def _aspect_impl(target, ctx):",
" f = ctx.actions.declare_file('dummy.txt')",
" ctx.actions.run_shell(outputs = [f], command='echo xxx > $(location f)',",
" mnemonic='AspectAction')",
" return struct()",
"my_aspect = aspect(implementation = _aspect_impl)");
scratch.file(
"test/BUILD",
"extra_action(",
" name = 'xa',",
" cmd = 'echo $(EXTRA_ACTION_FILE) > $(output file.xa)',",
" out_templates = ['file.xa'],",
")",
"action_listener(",
" name = 'al',",
" mnemonics = ['AspectAction'],",
" extra_actions = [':xa'],",
")",
"java_library(name = 'xxx')",
"java_library(name = 'yyy')");
useConfiguration("--experimental_action_listener=//test:al");
reporter.removeHandler(failFastHandler); // We expect an error.

if (keepGoing()) {
AnalysisResult result =
update(ImmutableList.of("test/aspect.bzl%my_aspect"), "//test:xxx", "//test:yyy");
assertThat(result.hasError()).isTrue();
} else {
assertThrows(
ViewCreationFailedException.class,
() -> update(ImmutableList.of("test/aspect.bzl%my_aspect"), "//test:xxx", "//test:yyy"));
}
assertContainsEvent(
"file 'extra_actions/test/xa/test/file.xa' is generated by these conflicting actions");
}

@Test
public void aspectsPropagatingToAllAttributes() throws Exception {
scratch.file(
Expand Down Expand Up @@ -2589,9 +2548,7 @@ public void aspectPropagatesOverOtherAspectAttributes() throws Exception {
// can be referenced at the top level using the --aspects flag. For ease of testing,
// apple_common.objc_proto_aspect is used as an example.
public void testTopLevelSkylarkObjcProtoAspect() throws Exception {
MockObjcSupport.setupObjcProtoLibrary(scratch);
scratch.file("test_skylark/BUILD");
scratch.file("x/data_filter.pbascii");
scratch.file(
"test_skylark/top_level_stub.bzl",
"top_level_aspect = apple_common.objc_proto_aspect",
Expand All @@ -2608,7 +2565,6 @@ public void testTopLevelSkylarkObjcProtoAspect() throws Exception {

scratch.file(
"x/BUILD",
"load('//objc_proto_library:objc_proto_library.bzl', 'objc_proto_library')",
"proto_library(",
" name = 'protos',",
" srcs = ['data.proto'],",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2977,4 +2977,28 @@ public void testRecursiveImport2() throws Exception {
+ "//test/skylark:ext3.bzl, //test/skylark:ext4.bzl]");
}
}

@Test
public void testOldOctalNotationIsForbidden() throws Exception {
setSkylarkSemanticsOptions("--incompatible_disallow_old_octal_notation=true");

scratch.file("test/extension.bzl", "y = 0246");

scratch.file("test/BUILD", "load('//test:extension.bzl', 'y')", "cc_library(name = 'r')");

reporter.removeHandler(failFastHandler);
getConfiguredTarget("//test:r");
assertContainsEvent("Invalid octal value `0246`, should be: `0o246`");
}

@Test
public void testOldOctalNotation() throws Exception {
setSkylarkSemanticsOptions("--incompatible_disallow_old_octal_notation=false");

scratch.file("test/extension.bzl", "y = 0246");

scratch.file("test/BUILD", "load('//test:extension.bzl', 'y')", "cc_library(name = 'r')");

getConfiguredTarget("//test:r");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.common.truth.Truth8.assertThat;
import static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;
import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
Expand All @@ -32,7 +31,7 @@
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.actions.FileWriteAction;
import com.google.devtools.build.lib.analysis.actions.StarlarkAction;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.analysis.configuredtargets.FileConfiguredTarget;
import com.google.devtools.build.lib.analysis.skylark.SkylarkRuleContext;
import com.google.devtools.build.lib.analysis.util.MockRule;
Expand Down Expand Up @@ -588,31 +587,30 @@ public void testGetRule() throws Exception {

result = (List<String>) evalRuleContextCode(
createRuleContext("//test/getrule:genrule_attr"), "ruleContext.attr.s");
assertThat(result)
.containsAtLeast(
"name",
"visibility",
"transitive_configs",
"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",
"exec_compatible_with");
assertThat(result).containsExactly(
"name",
"visibility",
"transitive_configs",
"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",
"exec_compatible_with");
}

@Test
Expand Down Expand Up @@ -681,56 +679,18 @@ public void testCreateSpawnActionArgumentsWithExecutableFilesToRunProvider() thr
SkylarkRuleContext ruleContext = createRuleContext("//foo:androidlib");
evalRuleContextCode(
ruleContext,
"ruleContext.actions.run(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
" arguments = ['--a','--b'],",
" executable = ruleContext.executable._idlclass)");
StarlarkAction action =
(StarlarkAction)
"ruleContext.actions.run(\n"
+ " inputs = ruleContext.files.srcs,\n"
+ " outputs = ruleContext.files.srcs,\n"
+ " arguments = ['--a','--b'],\n"
+ " executable = ruleContext.executable._idlclass)\n");
SpawnAction action =
(SpawnAction)
Iterables.getOnlyElement(
ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions());
assertThat(action.getCommandFilename()).matches("^.*/IdlClass(\\.exe){0,1}$");
}

@Test
public void testCreateStarlarkActionArgumentsWithUnusedInputsList() throws Exception {
setSkylarkSemanticsOptions("--experimental_starlark_unused_inputs_list=True");
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
evalRuleContextCode(
ruleContext,
"ruleContext.actions.run(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
" executable = 'executable',",
" unused_inputs_list = ruleContext.files.srcs[0])");
StarlarkAction action =
(StarlarkAction)
Iterables.getOnlyElement(
ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions());
assertThat(action.getUnusedInputsList()).isPresent();
assertThat(action.getUnusedInputsList().get().getFilename()).isEqualTo("a.txt");
assertThat(action.discoversInputs()).isTrue();
}

@Test
public void testCreateStarlarkActionArgumentsWithoutUnusedInputsList() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
evalRuleContextCode(
ruleContext,
"ruleContext.actions.run(",
" inputs = ruleContext.files.srcs,",
" outputs = ruleContext.files.srcs,",
" executable = 'executable',",
" unused_inputs_list = None)");
StarlarkAction action =
(StarlarkAction)
Iterables.getOnlyElement(
ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions());
assertThat(action.getUnusedInputsList()).isEmpty();
assertThat(action.discoversInputs()).isFalse();
}

@Test
public void testOutputs() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:bar");
Expand Down Expand Up @@ -873,7 +833,6 @@ public void testWorkspaceName() throws Exception {

@Test
public void testDeriveArtifactLegacy() throws Exception {
setSkylarkSemanticsOptions("--incompatible_new_actions_api=false");
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
Object result =
evalRuleContextCode(
Expand All @@ -886,7 +845,7 @@ public void testDeriveArtifactLegacy() throws Exception {
@Test
public void testDeriveArtifact() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
Object result = evalRuleContextCode(ruleContext, "ruleContext.actions.declare_file('a/b.txt')");
Object result = evalRuleContextCode(ruleContext, "ruleContext.new_file('a/b.txt')");
PathFragment fragment = ((Artifact) result).getRootRelativePath();
assertThat(fragment.getPathString()).isEqualTo("foo/a/b.txt");
}
Expand Down Expand Up @@ -930,7 +889,6 @@ public void testDeriveTreeArtifactNextToSibling() throws Exception {

@Test
public void testParamFileLegacy() throws Exception {
setSkylarkSemanticsOptions("--incompatible_new_actions_api=false");
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
Object result =
evalRuleContextCode(
Expand All @@ -943,7 +901,6 @@ public void testParamFileLegacy() throws Exception {

@Test
public void testParamFileSuffixLegacy() throws Exception {
setSkylarkSemanticsOptions("--incompatible_new_actions_api=false");
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
Object result =
evalRuleContextCode(
Expand Down Expand Up @@ -2317,7 +2274,6 @@ public void testCoverageInstrumentedMatchesFilterNonDefaultLabel() throws Except

@Test
public void testFrozenRuleContextHasInaccessibleAttributes() throws Exception {
setSkylarkSemanticsOptions("--incompatible_new_actions_api=false");
scratch.file("test/BUILD",
"load('//test:rules.bzl', 'main_rule', 'dep_rule')",
"dep_rule(name = 'dep')",
Expand Down Expand Up @@ -2360,7 +2316,6 @@ public void testFrozenRuleContextHasInaccessibleAttributes() throws Exception {

@Test
public void testFrozenRuleContextForAspectsHasInaccessibleAttributes() throws Exception {
setSkylarkSemanticsOptions("--incompatible_new_actions_api=false");
List<String> attributes = new ArrayList<>();
attributes.addAll(ctxAttributes);
attributes.addAll(ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@ public void testCreateSpawnActionUnknownParam() throws Exception {
"ruleContext.actions.run(outputs=[], bad_param = 'some text', executable = f)");
}

@Test
public void testCreateSpawnActionNoExecutable() throws Exception {
SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
checkErrorContains(
ruleContext,
"You must specify either 'command' or 'executable' argument",
"ruleContext.action(outputs=[])");
}

private Object createTestSpawnAction(SkylarkRuleContext ruleContext) throws Exception {
return evalRuleContextCode(
ruleContext,
Expand Down Expand Up @@ -1802,28 +1811,6 @@ public void testDeclaredProvidersInOperatorInvalidKey() throws Exception {
.contains("Type Target only supports querying by object constructors, got string instead");
}

@Test
public void testReturnNonExportedProvider() throws Exception {
scratch.file(
"test/my_rule.bzl",
"def _rule_impl(ctx):",
" foo_provider = provider()",
" foo = foo_provider()",
" return [foo]",
"",
"my_rule = rule(",
" implementation = _rule_impl,",
")");
scratch.file("test/BUILD", "load(':my_rule.bzl', 'my_rule')", "my_rule(name = 'my_rule')");

AssertionError expected =
assertThrows(AssertionError.class, () -> getConfiguredTarget("//test:my_rule"));
assertThat(expected)
.hasMessageThat()
.contains(
"cannot return a non-exported provider instance from a rule implementation function.");
}

@Test
public void testFilesForFileConfiguredTarget() throws Exception {
Object result =
Expand Down Expand Up @@ -2750,7 +2737,6 @@ public void testConfigurationField_NativeSplitTransitionProhibited() throws Exce
" '_attr': attr.label(",
" cfg = android_common.multi_cpu_configuration,",
" default = configuration_field(fragment='cpp', name = 'cc_toolchain'))})");
setSkylarkSemanticsOptions("--experimental_google_legacy_api");

scratch.file("test/BUILD", "load('//test:rule.bzl', 'foo')", "foo(name='foo')");

Expand Down
Loading

0 comments on commit 3aa0954

Please sign in to comment.