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 all 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
55 changes: 55 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,59 @@ 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:
# * test- and build-rule attributes (e.g. "tags") are applied to both rules,
# * test-only attributes are applied only to the sh_rule,
# * the build rule has its own visibility
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"],
args = ["x"],
flaky = False,
local = True,
shard_count = 2,
size = "small",
timeout = "short",
)
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"
84 changes: 58 additions & 26 deletions tools/build_rules/test_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ def _make_sh_test(name, **kwargs):
**kwargs
)

_TEST_ATTRS = {
"args": None,
"size": None,
"timeout": None,
"flaky": None,
"local": None,
"shard_count": None,
}

def _helper_rule_attrs(test_attrs, own_attrs):
r = {}
r.update({k: v for k, v in test_attrs.items() if k not in _TEST_ATTRS})
r.update(own_attrs)
r.update(
dict(
testonly = 1,
visibility = ["//visibility:private"],
),
)
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 +96,14 @@ _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"],
**_helper_rule_attrs(
kwargs,
dict(
name = name + "_impl",
msg = msg,
out = name + "_impl.sh",
),
)
)

_make_sh_test(name, **kwargs)
Expand Down Expand Up @@ -121,11 +145,14 @@ _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"],
**_helper_rule_attrs(
kwargs,
dict(
name = name + "_impl",
msg = msg,
out = name + "_impl.sh",
),
)
)

_make_sh_test(name, **kwargs)
Expand Down Expand Up @@ -306,13 +333,16 @@ _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"],
**_helper_rule_attrs(
kwargs,
dict(
name = name + "_impl",
rule = rule,
generates = generates,
provides = provides,
out = name + ".sh",
),
)
)

_make_sh_test(name, **kwargs)
Expand Down Expand Up @@ -372,14 +402,16 @@ _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"],
**_helper_rule_attrs(
kwargs,
dict(
name = name + "_impl",
file = file,
content = content or "",
regexp = regexp or "",
matches = matches if (matches != None) else -1,
out = name + "_impl.sh",
),
)
)

_make_sh_test(name, **kwargs)