Skip to content

Commit 5b5fb05

Browse files
authored
Added unit tests for rust_clippy_aspect (#851)
* Added unit tests for clippy_aspect * Updated test
1 parent ce8005c commit 5b5fb05

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

test/clippy/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ load(
66
"rust_test",
77
)
88

9+
package(default_visibility = ["//test:__subpackages__"])
10+
911
# Declaration of passing targets.
1012

1113
rust_binary(

test/unit/clippy/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
load(":clippy_test.bzl", "clippy_test_suite")
2+
3+
clippy_test_suite(
4+
name = "clippy_test_suite",
5+
)

test/unit/clippy/clippy_test.bzl

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""Unittest to verify properties of clippy rules"""
2+
3+
load("@bazel_skylib//lib:unittest.bzl", "analysistest")
4+
load("//rust:defs.bzl", "rust_clippy_aspect")
5+
load("//test/unit:common.bzl", "assert_argv_contains", "assert_list_contains_adjacent_elements")
6+
7+
def _find_clippy_action(actions):
8+
for action in actions:
9+
if action.mnemonic == "Clippy":
10+
return action
11+
fail("Failed to find Clippy action")
12+
13+
def _clippy_aspect_action_has_flag_impl(ctx, flags):
14+
env = analysistest.begin(ctx)
15+
16+
# TODO: Replace with `analysistest.target_under_test(env)` upstream fix
17+
# https://github.com/bazelbuild/bazel-skylib/pull/299
18+
target = env.ctx.attr.target_under_test_with_aspect
19+
20+
clippy_action = _find_clippy_action(target.actions)
21+
22+
# Ensure each flag is present in the clippy action
23+
for flag in flags:
24+
assert_argv_contains(
25+
env,
26+
clippy_action,
27+
flag,
28+
)
29+
30+
clippy_checks = target[OutputGroupInfo].clippy_checks.to_list()
31+
if len(clippy_checks) != 1:
32+
fail("clippy_checks is only expected to contain 1 file")
33+
34+
# Ensure the arguments to generate the marker file are present in
35+
# the clippy action
36+
assert_list_contains_adjacent_elements(
37+
env,
38+
clippy_action.argv,
39+
[
40+
"--touch-file",
41+
clippy_checks[0].path,
42+
],
43+
)
44+
45+
return analysistest.end(env)
46+
47+
def _binary_clippy_aspect_action_has_warnings_flag_test_impl(ctx):
48+
return _clippy_aspect_action_has_flag_impl(
49+
ctx,
50+
["-Dwarnings"],
51+
)
52+
53+
def _library_clippy_aspect_action_has_warnings_flag_test_impl(ctx):
54+
return _clippy_aspect_action_has_flag_impl(
55+
ctx,
56+
["-Dwarnings"],
57+
)
58+
59+
def _test_clippy_aspect_action_has_warnings_flag_test_impl(ctx):
60+
return _clippy_aspect_action_has_flag_impl(
61+
ctx,
62+
[
63+
"-Dwarnings",
64+
"--test",
65+
],
66+
)
67+
68+
def make_clippy_aspect_unittest(impl):
69+
return analysistest.make(
70+
impl,
71+
attrs = {
72+
# We can't just use target_under_test because we need to add our own aspect to the attribute.
73+
# See https://github.com/bazelbuild/bazel-skylib/pull/299
74+
"target_under_test_with_aspect": attr.label(aspects = [rust_clippy_aspect], mandatory = True),
75+
},
76+
)
77+
78+
binary_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest(_binary_clippy_aspect_action_has_warnings_flag_test_impl)
79+
library_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest(_library_clippy_aspect_action_has_warnings_flag_test_impl)
80+
test_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest(_test_clippy_aspect_action_has_warnings_flag_test_impl)
81+
82+
def clippy_test_suite(name):
83+
"""Entry-point macro called from the BUILD file.
84+
85+
Args:
86+
name (str): Name of the macro.
87+
"""
88+
89+
binary_clippy_aspect_action_has_warnings_flag_test(
90+
name = "binary_clippy_aspect_action_has_warnings_flag_test",
91+
target_under_test = Label("//test/clippy:ok_binary"),
92+
target_under_test_with_aspect = Label("//test/clippy:ok_binary"),
93+
)
94+
library_clippy_aspect_action_has_warnings_flag_test(
95+
name = "library_clippy_aspect_action_has_warnings_flag_test",
96+
target_under_test = Label("//test/clippy:ok_library"),
97+
target_under_test_with_aspect = Label("//test/clippy:ok_library"),
98+
)
99+
test_clippy_aspect_action_has_warnings_flag_test(
100+
name = "test_clippy_aspect_action_has_warnings_flag_test",
101+
target_under_test = Label("//test/clippy:ok_test"),
102+
target_under_test_with_aspect = Label("//test/clippy:ok_test"),
103+
)
104+
105+
native.test_suite(
106+
name = name,
107+
tests = [
108+
":binary_clippy_aspect_action_has_warnings_flag_test",
109+
":library_clippy_aspect_action_has_warnings_flag_test",
110+
":test_clippy_aspect_action_has_warnings_flag_test",
111+
],
112+
)

0 commit comments

Comments
 (0)