Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rule_test: apply "tags" to all rules in the macro #8784

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/test/shell/bazel/rule_test_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,51 @@ EOF
bazel build //:turtle_rule_test &> $TEST_log || fail "turtle_rule_test failed"
}

# Regression test for https://github.com/bazelbuild/bazel/issues/8723
#
# rule_test() is a macro that expands to a sh_test and _rule_test_rule.
# Expect that rule_tags(..., tags) is applied to both rules, but rule_tags(..., visibility) is only
# applied to the sh_test because _rule_test_rule should be private.
function test_kwargs_with_macro_rules() {
create_new_workspace
cat > BUILD <<'EOF'
load("@bazel_tools//tools/build_rules:test_rules.bzl", "rule_test")

genrule(
name = "x",
srcs = ["@does_not_exist//:bad"],
outs = ["x.out"],
cmd = "touch $@",
tags = ["dont_build_me"],
)

rule_test(
name = "x_test",
rule = "//:x",
generates = ["x.out"],
visibility = ["//foo:__pkg__"],
tags = ["dont_build_me"],
)
EOF

bazel build //:all >& "$TEST_log" && fail "should have failed" || true

bazel build --build_tag_filters=-dont_build_me //:all >& "$TEST_log" || fail "build failed"

bazel query --output=label 'attr(tags, dont_build_me, //:all)' >& "$TEST_log" || fail "query failed"
expect_log '//:x_test_impl'
expect_log '//:x_test\b'
expect_log '//:x\b'

bazel query --output=label 'attr(visibility, private, //:all)' >& "$TEST_log" || fail "query failed"
expect_log '//:x_test_impl'
expect_not_log '//:x_test\b'
expect_not_log '//:x\b'

bazel query --output=label 'attr(visibility, foo, //:all)' >& "$TEST_log" || fail "query failed"
expect_log '//:x_test\b'
expect_not_log '//:x_test_impl'
expect_not_log '//:x\b'
}

run_suite "rule_test tests"
77 changes: 51 additions & 26 deletions tools/build_rules/test_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def _make_sh_test(name, **kwargs):
**kwargs
)

def _merge_dicts(d1, d2):
r = {}
r.update(d1)
r.update(d2)
return r

### First, trivial tests that either always pass, always fail,
### or sometimes pass depending on a trivial computation.

Expand Down Expand Up @@ -75,11 +81,16 @@ _successful_rule = rule(

def successful_test(name, msg, **kwargs):
_successful_rule(
name = name + "_impl",
msg = msg,
out = name + "_impl.sh",
testonly = 1,
visibility = ["//visibility:private"],
**_merge_dicts(
kwargs,
dict(
name = name + "_impl",
msg = msg,
out = name + "_impl.sh",
testonly = 1,
visibility = ["//visibility:private"],
),
)
)

_make_sh_test(name, **kwargs)
Expand Down Expand Up @@ -121,11 +132,16 @@ _failed_rule = rule(

def failed_test(name, msg, **kwargs):
_failed_rule(
name = name + "_impl",
msg = msg,
out = name + "_impl.sh",
testonly = 1,
visibility = ["//visibility:private"],
**_merge_dicts(
kwargs,
dict(
name = name + "_impl",
msg = msg,
out = name + "_impl.sh",
testonly = 1,
visibility = ["//visibility:private"],
),
)
)

_make_sh_test(name, **kwargs)
Expand Down Expand Up @@ -306,13 +322,18 @@ _rule_test_rule = rule(

def rule_test(name, rule, generates = None, provides = None, **kwargs):
_rule_test_rule(
name = name + "_impl",
rule = rule,
generates = generates,
provides = provides,
out = name + ".sh",
testonly = 1,
visibility = ["//visibility:private"],
**_merge_dicts(
kwargs,
dict(
name = name + "_impl",
rule = rule,
generates = generates,
provides = provides,
out = name + ".sh",
testonly = 1,
visibility = ["//visibility:private"],
),
)
)

_make_sh_test(name, **kwargs)
Expand Down Expand Up @@ -372,14 +393,18 @@ _file_test_rule = rule(

def file_test(name, file, content = None, regexp = None, matches = None, **kwargs):
_file_test_rule(
name = name + "_impl",
file = file,
content = content or "",
regexp = regexp or "",
matches = matches if (matches != None) else -1,
out = name + "_impl.sh",
testonly = 1,
visibility = ["//visibility:private"],
**_merge_dicts(
kwargs,
dict(
name = name + "_impl",
file = file,
content = content or "",
regexp = regexp or "",
matches = matches if (matches != None) else -1,
out = name + "_impl.sh",
testonly = 1,
visibility = ["//visibility:private"],
),
)
)

_make_sh_test(name, **kwargs)