fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

UebelAndreUpdated `bazel_skylib` to `1.1.1` (#957)fe5ddf34

0.5.0
3.3 KiB102 linesraw
1"""Unittest to verify properties of clippy rules"""
2
3load("@bazel_skylib//lib:unittest.bzl", "analysistest")
4load("//rust:defs.bzl", "rust_clippy_aspect")
5load("//test/unit:common.bzl", "assert_argv_contains", "assert_list_contains_adjacent_elements")
6
7def _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
13def _clippy_aspect_action_has_flag_impl(ctx, flags):
14    env = analysistest.begin(ctx)
15    target = analysistest.target_under_test(env)
16
17    clippy_action = _find_clippy_action(target.actions)
18
19    # Ensure each flag is present in the clippy action
20    for flag in flags:
21        assert_argv_contains(
22            env,
23            clippy_action,
24            flag,
25        )
26
27    clippy_checks = target[OutputGroupInfo].clippy_checks.to_list()
28    if len(clippy_checks) != 1:
29        fail("clippy_checks is only expected to contain 1 file")
30
31    # Ensure the arguments to generate the marker file are present in
32    # the clippy action
33    assert_list_contains_adjacent_elements(
34        env,
35        clippy_action.argv,
36        [
37            "--touch-file",
38            clippy_checks[0].path,
39        ],
40    )
41
42    return analysistest.end(env)
43
44def _binary_clippy_aspect_action_has_warnings_flag_test_impl(ctx):
45    return _clippy_aspect_action_has_flag_impl(
46        ctx,
47        ["-Dwarnings"],
48    )
49
50def _library_clippy_aspect_action_has_warnings_flag_test_impl(ctx):
51    return _clippy_aspect_action_has_flag_impl(
52        ctx,
53        ["-Dwarnings"],
54    )
55
56def _test_clippy_aspect_action_has_warnings_flag_test_impl(ctx):
57    return _clippy_aspect_action_has_flag_impl(
58        ctx,
59        [
60            "-Dwarnings",
61            "--test",
62        ],
63    )
64
65def make_clippy_aspect_unittest(impl):
66    return analysistest.make(
67        impl,
68        extra_target_under_test_aspects = [rust_clippy_aspect],
69    )
70
71binary_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest(_binary_clippy_aspect_action_has_warnings_flag_test_impl)
72library_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest(_library_clippy_aspect_action_has_warnings_flag_test_impl)
73test_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest(_test_clippy_aspect_action_has_warnings_flag_test_impl)
74
75def clippy_test_suite(name):
76    """Entry-point macro called from the BUILD file.
77
78    Args:
79        name (str): Name of the macro.
80    """
81
82    binary_clippy_aspect_action_has_warnings_flag_test(
83        name = "binary_clippy_aspect_action_has_warnings_flag_test",
84        target_under_test = Label("//test/clippy:ok_binary"),
85    )
86    library_clippy_aspect_action_has_warnings_flag_test(
87        name = "library_clippy_aspect_action_has_warnings_flag_test",
88        target_under_test = Label("//test/clippy:ok_library"),
89    )
90    test_clippy_aspect_action_has_warnings_flag_test(
91        name = "test_clippy_aspect_action_has_warnings_flag_test",
92        target_under_test = Label("//test/clippy:ok_test"),
93    )
94
95    native.test_suite(
96        name = name,
97        tests = [
98            ":binary_clippy_aspect_action_has_warnings_flag_test",
99            ":library_clippy_aspect_action_has_warnings_flag_test",
100            ":test_clippy_aspect_action_has_warnings_flag_test",
101        ],
102    )