Skip to content

Commit

Permalink
Make testLabelsOperator not depend on Python rule internals.
Browse files Browse the repository at this point in the history
The testLabelsOperator test was testing a case for implicit attributes by
using the "$py_toolchain_type" attribute, which is only present for this
test to pass. To fix, the test writes a simple Starlark rule with an
implicit attribute that it queries instead of using the Python rules.

PiperOrigin-RevId: 505187391
Change-Id: I8ac3f83320392f9f1a92e9f9559f41c7beaeda5a
  • Loading branch information
rickeylev authored and copybara-github committed Jan 27, 2023
1 parent 17e3fd1 commit d13c964
Showing 1 changed file with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1072,17 +1072,17 @@ private void runNodepDepsTest(boolean expectVisibilityDep, Setting... settings)

@Test
public void testNodepDeps_defaultIsTrue() throws Exception {
runNodepDepsTest(/*expectVisibilityDep=*/ true);
runNodepDepsTest(/* expectVisibilityDep= */ true);
}

@Test
public void testNodepDeps_false() throws Exception {
runNodepDepsTest(/*expectVisibilityDep=*/ false, Setting.NO_NODEP_DEPS);
runNodepDepsTest(/* expectVisibilityDep= */ false, Setting.NO_NODEP_DEPS);
}

@Test
public void testCycleInStarlark() throws Exception {
runCycleInStarlarkTest(/*checkFailureDetail=*/ true);
runCycleInStarlarkTest(/* checkFailureDetail= */ true);
}

protected void runCycleInStarlarkTest(boolean checkFailureDetail) throws Exception {
Expand All @@ -1103,8 +1103,7 @@ protected void runCycleInStarlarkTest(boolean checkFailureDetail) throws Excepti
public void testLabelsOperator() throws Exception {
writeBuildFiles3();
writeBuildFilesWithConfigurableAttributes();
writeFile("k/BUILD", "py_binary(name='k', srcs=['k.py'])");
analysisMock.pySupport().setup(mockToolsConfig);
writeBuildFilesWithImplicitAttribute();

// srcs:
assertThat(eval("labels(srcs, //a)")).isEqualTo(eval("//b + //c"));
Expand All @@ -1123,13 +1122,12 @@ public void testLabelsOperator() throws Exception {
assertThat(eval("labels(no_such_attr, //b)")).isEqualTo(EMPTY);

// singleton LABEL:
assertThat(eval("labels(srcs, //k)")).isEqualTo(eval("//k:k.py"));
assertThat(eval("labels(srcs, //k)")).isEqualTo(eval("//k:k.txt"));

// Works for implicit edges too. This is for consistency with --output
// xml, which exposes them too.
RepositoryName toolsRepository = helper.getToolsRepository();
assertThat(eval("labels(\"$py_toolchain_type\", //k)"))
.isEqualTo(eval(toolsRepository + "//tools/python:toolchain_type"));
// xml, which exposes them too. Note that, for whatever reason, the
// implicit attribute must be referenced using "$" instead of "_".
assertThat(eval("labels('$implicit', //k)")).isEqualTo(eval("//k:implicit"));

// Configurable deps:
if (testConfigurableAttributes()) {
Expand All @@ -1138,6 +1136,25 @@ public void testLabelsOperator() throws Exception {
}
}

private void writeBuildFilesWithImplicitAttribute() throws Exception {
writeFile(
"k/defs.bzl",
"def impl(ctx):",
" return [DefaultInfo()]",
"has_implicit_attr = rule(",
" implementation=impl,",
" attrs = {",
" 'srcs': attr.label_list(),",
" '_implicit': attr.label(default='//k:implicit')",
" },",
")");
writeFile(
"k/BUILD",
"load(':defs.bzl', 'has_implicit_attr')",
"has_implicit_attr(name='k', srcs=['k.txt'])",
"filegroup(name='implicit')");
}

/* tests(x) operator */

@Test
Expand Down Expand Up @@ -1886,7 +1903,7 @@ protected final void runBadRuleInDeps(Object code) throws Exception {
writeFile("foo/BUILD", "sh_library(name = 'foo', deps = ['//bar:bar'])");
writeFile("bar/BUILD", "sh_library(name = 'bar', srcs = 'bad_single_file')");
EvalThrowsResult evalThrowsResult =
evalThrows("deps(//foo:foo)", /*unconditionallyThrows=*/ false);
evalThrows("deps(//foo:foo)", /* unconditionallyThrows= */ false);
FailureDetail.Builder failureDetailBuilder = FailureDetail.newBuilder();
if (code instanceof FailureDetails.PackageLoading.Code) {
failureDetailBuilder.setPackageLoading(
Expand Down Expand Up @@ -1979,7 +1996,7 @@ public void boundedRdepsWithError() throws Exception {
"sh_library(name = 'foo', deps = [':dep'])",
"sh_library(name = 'dep', deps = ['//bar:missing'])");
assertThat(
evalThrows("rdeps(//foo:foo, //foo:dep, 1)", /*unconditionallyThrows=*/ false)
evalThrows("rdeps(//foo:foo, //foo:dep, 1)", /* unconditionallyThrows= */ false)
.getMessage())
.contains("preloading transitive closure failed: no such package 'bar':");
}
Expand Down

0 comments on commit d13c964

Please sign in to comment.