11"""Unittest to verify properties of clippy rules"""
22
3- load ("@bazel_skylib//lib:unittest.bzl" , "analysistest" )
3+ load ("@bazel_skylib//lib:unittest.bzl" , "analysistest" , "asserts" )
44load ("//rust:defs.bzl" , "rust_clippy_aspect" )
5- load ("//test/unit:common.bzl" , "assert_argv_contains" , "assert_argv_contains_prefix_suffix" , "assert_list_contains_adjacent_elements" )
5+ load ("//test/unit:common.bzl" , "assert_argv_contains" , "assert_argv_contains_prefix_suffix" , "assert_env_value" , " assert_list_contains_adjacent_elements" )
66
77def _find_clippy_action (actions ):
88 for action in actions :
@@ -83,6 +83,31 @@ def _clippy_aspect_with_explicit_flags_test_impl(ctx):
8383 _CLIPPY_EXPLICIT_FLAGS + _CLIPPY_INDIVIDUALLY_ADDED_EXPLICIT_FLAGS ,
8484 )
8585
86+ def _clippy_aspect_conf_dir_test_impl (ctx , expected_dir ):
87+ env = analysistest .begin (ctx )
88+ target = analysistest .target_under_test (env )
89+
90+ clippy_action = _find_clippy_action (target .actions )
91+ assert_env_value (
92+ env ,
93+ clippy_action ,
94+ "CLIPPY_CONF_DIR" ,
95+ "${{pwd}}/{}" .format (expected_dir ),
96+ )
97+
98+ config_inputs = [
99+ f
100+ for f in clippy_action .inputs .to_list ()
101+ if f .dirname == expected_dir and f .basename in ("clippy.toml" , ".clippy.toml" )
102+ ]
103+ asserts .true (
104+ env ,
105+ len (config_inputs ) == 1 ,
106+ "expected exactly one clippy config from {} in action inputs, got {}" .format (expected_dir , config_inputs ),
107+ )
108+
109+ return analysistest .end (env )
110+
86111def make_clippy_aspect_unittest (impl , ** kwargs ):
87112 return analysistest .make (
88113 impl ,
@@ -136,6 +161,14 @@ clippy_aspect_with_output_diagnostics_test = make_clippy_aspect_unittest(
136161 },
137162)
138163
164+ clippy_aspect_uses_default_conf_dir_test = make_clippy_aspect_unittest (
165+ lambda ctx : _clippy_aspect_conf_dir_test_impl (ctx , "rust/settings" ),
166+ )
167+
168+ clippy_aspect_uses_target_conf_dir_test = make_clippy_aspect_unittest (
169+ lambda ctx : _clippy_aspect_conf_dir_test_impl (ctx , "test/clippy/target_config" ),
170+ )
171+
139172def clippy_test_suite (name ):
140173 """Entry-point macro called from the BUILD file.
141174
@@ -183,6 +216,23 @@ def clippy_test_suite(name):
183216 target_under_test = Label ("//test/clippy:ok_library" ),
184217 )
185218
219+ clippy_aspect_uses_default_conf_dir_test (
220+ name = "clippy_aspect_uses_default_conf_dir_test" ,
221+ target_under_test = Label ("//test/clippy:ok_library" ),
222+ )
223+ clippy_aspect_uses_target_conf_dir_test (
224+ name = "clippy_aspect_uses_target_conf_dir_test" ,
225+ target_under_test = Label ("//test/clippy:ok_library_with_clippy_config" ),
226+ )
227+ clippy_aspect_uses_default_conf_dir_test (
228+ name = "clippy_aspect_crate_test_ignores_library_conf_dir_test" ,
229+ target_under_test = Label ("//test/clippy:ok_crate_test_without_clippy_config" ),
230+ )
231+ clippy_aspect_uses_target_conf_dir_test (
232+ name = "clippy_aspect_crate_test_uses_own_conf_dir_test" ,
233+ target_under_test = Label ("//test/clippy:ok_crate_test_with_clippy_config" ),
234+ )
235+
186236 native .test_suite (
187237 name = name ,
188238 tests = [
@@ -195,5 +245,9 @@ def clippy_test_suite(name):
195245 ":clippy_aspect_without_clippy_error_format_test" ,
196246 ":clippy_aspect_with_clippy_error_format_test" ,
197247 ":clippy_aspect_with_output_diagnostics_test" ,
248+ ":clippy_aspect_uses_default_conf_dir_test" ,
249+ ":clippy_aspect_uses_target_conf_dir_test" ,
250+ ":clippy_aspect_crate_test_ignores_library_conf_dir_test" ,
251+ ":clippy_aspect_crate_test_uses_own_conf_dir_test" ,
198252 ],
199253 )
0 commit comments