UebelAndre | 5b5fb05 | 2021-07-22 11:32:39 -0700 | [diff] [blame] | 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) |
UebelAndre | fe5ddf3 | 2021-10-08 06:10:30 -0700 | [diff] [blame] | 15 | target = analysistest.target_under_test(env) |
UebelAndre | 5b5fb05 | 2021-07-22 11:32:39 -0700 | [diff] [blame] | 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 | |
| 44 | def _binary_clippy_aspect_action_has_warnings_flag_test_impl(ctx): |
| 45 | return _clippy_aspect_action_has_flag_impl( |
| 46 | ctx, |
| 47 | ["-Dwarnings"], |
| 48 | ) |
| 49 | |
| 50 | def _library_clippy_aspect_action_has_warnings_flag_test_impl(ctx): |
| 51 | return _clippy_aspect_action_has_flag_impl( |
| 52 | ctx, |
| 53 | ["-Dwarnings"], |
| 54 | ) |
| 55 | |
| 56 | def _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 | |
UebelAndre | 81590f4 | 2022-06-08 04:08:59 -0700 | [diff] [blame] | 65 | _CLIPPY_EXPLICIT_FLAGS = [ |
| 66 | "-Dwarnings", |
| 67 | "-A", |
| 68 | "clippy::needless_return", |
| 69 | ] |
| 70 | |
Brian Granaghan | 72d1d4a | 2024-04-23 02:56:23 -0700 | [diff] [blame] | 71 | _CLIPPY_INDIVIDUALLY_ADDED_EXPLICIT_FLAGS = [ |
| 72 | "-A", |
| 73 | "clippy::new_without_default", |
| 74 | "-A", |
| 75 | "clippy::needless_range_loop", |
| 76 | ] |
| 77 | |
UebelAndre | 81590f4 | 2022-06-08 04:08:59 -0700 | [diff] [blame] | 78 | def _clippy_aspect_with_explicit_flags_test_impl(ctx): |
| 79 | return _clippy_aspect_action_has_flag_impl( |
| 80 | ctx, |
Brian Granaghan | 72d1d4a | 2024-04-23 02:56:23 -0700 | [diff] [blame] | 81 | _CLIPPY_EXPLICIT_FLAGS + _CLIPPY_INDIVIDUALLY_ADDED_EXPLICIT_FLAGS, |
UebelAndre | 81590f4 | 2022-06-08 04:08:59 -0700 | [diff] [blame] | 82 | ) |
| 83 | |
| 84 | def make_clippy_aspect_unittest(impl, **kwargs): |
UebelAndre | 5b5fb05 | 2021-07-22 11:32:39 -0700 | [diff] [blame] | 85 | return analysistest.make( |
| 86 | impl, |
UebelAndre | fe5ddf3 | 2021-10-08 06:10:30 -0700 | [diff] [blame] | 87 | extra_target_under_test_aspects = [rust_clippy_aspect], |
UebelAndre | 81590f4 | 2022-06-08 04:08:59 -0700 | [diff] [blame] | 88 | **kwargs |
UebelAndre | 5b5fb05 | 2021-07-22 11:32:39 -0700 | [diff] [blame] | 89 | ) |
| 90 | |
| 91 | binary_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest(_binary_clippy_aspect_action_has_warnings_flag_test_impl) |
| 92 | library_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest(_library_clippy_aspect_action_has_warnings_flag_test_impl) |
| 93 | test_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest(_test_clippy_aspect_action_has_warnings_flag_test_impl) |
Nick Gooding | 35c92d0 | 2023-10-11 20:13:58 +0100 | [diff] [blame] | 94 | clippy_aspect_with_explicit_flags_test = make_clippy_aspect_unittest( |
| 95 | _clippy_aspect_with_explicit_flags_test_impl, |
| 96 | config_settings = { |
Brian Granaghan | 72d1d4a | 2024-04-23 02:56:23 -0700 | [diff] [blame] | 97 | str(Label("//:clippy_flag")): _CLIPPY_INDIVIDUALLY_ADDED_EXPLICIT_FLAGS, |
Nick Gooding | 35c92d0 | 2023-10-11 20:13:58 +0100 | [diff] [blame] | 98 | str(Label("//:clippy_flags")): _CLIPPY_EXPLICIT_FLAGS, |
| 99 | }, |
| 100 | ) |
UebelAndre | 5b5fb05 | 2021-07-22 11:32:39 -0700 | [diff] [blame] | 101 | |
| 102 | def clippy_test_suite(name): |
| 103 | """Entry-point macro called from the BUILD file. |
| 104 | |
| 105 | Args: |
| 106 | name (str): Name of the macro. |
| 107 | """ |
| 108 | |
| 109 | binary_clippy_aspect_action_has_warnings_flag_test( |
| 110 | name = "binary_clippy_aspect_action_has_warnings_flag_test", |
| 111 | target_under_test = Label("//test/clippy:ok_binary"), |
UebelAndre | 5b5fb05 | 2021-07-22 11:32:39 -0700 | [diff] [blame] | 112 | ) |
| 113 | library_clippy_aspect_action_has_warnings_flag_test( |
| 114 | name = "library_clippy_aspect_action_has_warnings_flag_test", |
| 115 | target_under_test = Label("//test/clippy:ok_library"), |
UebelAndre | 5b5fb05 | 2021-07-22 11:32:39 -0700 | [diff] [blame] | 116 | ) |
| 117 | test_clippy_aspect_action_has_warnings_flag_test( |
| 118 | name = "test_clippy_aspect_action_has_warnings_flag_test", |
| 119 | target_under_test = Label("//test/clippy:ok_test"), |
UebelAndre | 5b5fb05 | 2021-07-22 11:32:39 -0700 | [diff] [blame] | 120 | ) |
UebelAndre | 81590f4 | 2022-06-08 04:08:59 -0700 | [diff] [blame] | 121 | clippy_aspect_with_explicit_flags_test( |
| 122 | name = "binary_clippy_aspect_with_explicit_flags_test", |
| 123 | target_under_test = Label("//test/clippy:ok_binary"), |
| 124 | ) |
| 125 | clippy_aspect_with_explicit_flags_test( |
| 126 | name = "library_clippy_aspect_with_explicit_flags_test", |
| 127 | target_under_test = Label("//test/clippy:ok_library"), |
| 128 | ) |
| 129 | clippy_aspect_with_explicit_flags_test( |
| 130 | name = "test_clippy_aspect_with_explicit_flags_test", |
| 131 | target_under_test = Label("//test/clippy:ok_test"), |
| 132 | ) |
UebelAndre | 5b5fb05 | 2021-07-22 11:32:39 -0700 | [diff] [blame] | 133 | |
| 134 | native.test_suite( |
| 135 | name = name, |
| 136 | tests = [ |
| 137 | ":binary_clippy_aspect_action_has_warnings_flag_test", |
| 138 | ":library_clippy_aspect_action_has_warnings_flag_test", |
| 139 | ":test_clippy_aspect_action_has_warnings_flag_test", |
UebelAndre | 81590f4 | 2022-06-08 04:08:59 -0700 | [diff] [blame] | 140 | ":binary_clippy_aspect_with_explicit_flags_test", |
| 141 | ":library_clippy_aspect_with_explicit_flags_test", |
| 142 | ":test_clippy_aspect_with_explicit_flags_test", |
UebelAndre | 5b5fb05 | 2021-07-22 11:32:39 -0700 | [diff] [blame] | 143 | ], |
| 144 | ) |