Skip to content

Commit

Permalink
rule_test: apply "tags" to all rules in the macro
Browse files Browse the repository at this point in the history
RELNOTES: rule_test: fix Bazel 0.27 regression ("tags" attribute was ingored, bazelbuild#8723

Fixes bazelbuild#8723

Change-Id: I25ac338e4978084085b8c49d6d0a1c47d8dc4fd1

Closes bazelbuild#8784.

Change-Id: Ie53b8787ab8679ff4896820ef552c2696eac1d09
PiperOrigin-RevId: 256509917
  • Loading branch information
laszlocsomor authored and copybara-github committed Jul 4, 2019
1 parent 5812b3d commit 6d0b14b
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 26 deletions.
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)

0 comments on commit 6d0b14b

Please sign in to comment.