22
33load ("@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" )
5+ load ("//test/unit:common.bzl" , "assert_argv_contains" , "assert_argv_contains_prefix_suffix" , "assert_env_value" )
66
77def _find_clippy_action (actions ):
88 for action in actions :
@@ -93,6 +93,35 @@ def _clippy_aspect_with_explicit_flags_test_impl(ctx):
9393 _CLIPPY_EXPLICIT_FLAGS + _CLIPPY_INDIVIDUALLY_ADDED_EXPLICIT_FLAGS ,
9494 )
9595
96+ def _clippy_aspect_conf_dir_test_impl (ctx , expected_dir ):
97+ env = analysistest .begin (ctx )
98+ target = analysistest .target_under_test (env )
99+
100+ clippy_action = _find_clippy_action (target .actions )
101+ assert_env_value (
102+ env ,
103+ clippy_action ,
104+ "CLIPPY_CONF_DIR" ,
105+ "${{pwd}}/{}" .format (expected_dir ),
106+ )
107+
108+ # Exactly one clippy.toml should appear in the action inputs, and it
109+ # should come from `expected_dir`. Any other clippy.toml (e.g., the
110+ # unchosen global default) is a bug: the aspect would still rerun on
111+ # changes to a config it never reads.
112+ config_inputs = [
113+ f
114+ for f in clippy_action .inputs .to_list ()
115+ if f .basename in ("clippy.toml" , ".clippy.toml" )
116+ ]
117+ asserts .true (
118+ env ,
119+ len (config_inputs ) == 1 and config_inputs [0 ].dirname == expected_dir ,
120+ "expected exactly one clippy config from {} in action inputs, got {}" .format (expected_dir , config_inputs ),
121+ )
122+
123+ return analysistest .end (env )
124+
96125def make_clippy_aspect_unittest (impl , ** kwargs ):
97126 return analysistest .make (
98127 impl ,
@@ -146,6 +175,14 @@ clippy_aspect_with_output_diagnostics_test = make_clippy_aspect_unittest(
146175 },
147176)
148177
178+ clippy_aspect_uses_default_conf_dir_test = make_clippy_aspect_unittest (
179+ lambda ctx : _clippy_aspect_conf_dir_test_impl (ctx , "rust/settings" ),
180+ )
181+
182+ clippy_aspect_uses_target_conf_dir_test = make_clippy_aspect_unittest (
183+ lambda ctx : _clippy_aspect_conf_dir_test_impl (ctx , "test/clippy/target_config" ),
184+ )
185+
149186def clippy_test_suite (name ):
150187 """Entry-point macro called from the BUILD file.
151188
@@ -193,6 +230,23 @@ def clippy_test_suite(name):
193230 target_under_test = Label ("//test/clippy:ok_library" ),
194231 )
195232
233+ clippy_aspect_uses_default_conf_dir_test (
234+ name = "clippy_aspect_uses_default_conf_dir_test" ,
235+ target_under_test = Label ("//test/clippy:ok_library" ),
236+ )
237+ clippy_aspect_uses_target_conf_dir_test (
238+ name = "clippy_aspect_uses_target_conf_dir_test" ,
239+ target_under_test = Label ("//test/clippy:ok_library_with_clippy_config" ),
240+ )
241+ clippy_aspect_uses_default_conf_dir_test (
242+ name = "clippy_aspect_crate_test_ignores_library_conf_dir_test" ,
243+ target_under_test = Label ("//test/clippy:ok_crate_test_without_clippy_config" ),
244+ )
245+ clippy_aspect_uses_target_conf_dir_test (
246+ name = "clippy_aspect_crate_test_uses_own_conf_dir_test" ,
247+ target_under_test = Label ("//test/clippy:ok_crate_test_with_clippy_config" ),
248+ )
249+
196250 native .test_suite (
197251 name = name ,
198252 tests = [
@@ -205,5 +259,9 @@ def clippy_test_suite(name):
205259 ":clippy_aspect_without_clippy_error_format_test" ,
206260 ":clippy_aspect_with_clippy_error_format_test" ,
207261 ":clippy_aspect_with_output_diagnostics_test" ,
262+ ":clippy_aspect_uses_default_conf_dir_test" ,
263+ ":clippy_aspect_uses_target_conf_dir_test" ,
264+ ":clippy_aspect_crate_test_ignores_library_conf_dir_test" ,
265+ ":clippy_aspect_crate_test_uses_own_conf_dir_test" ,
208266 ],
209267 )
0 commit comments