fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
c3d71edd
main
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 ): 8for action in actions : 9if action . mnemonic == "Clippy" : 10return action 11fail ( "Failed to find Clippy action" ) 12 13def _clippy_aspect_action_has_flag_impl ( ctx , flags ): 14env = analysistest . begin ( ctx ) 15target = analysistest . target_under_test ( env ) 16 17clippy_action = _find_clippy_action ( target . actions ) 18 19# Ensure each flag is present in the clippy action 20for flag in flags : 21assert_argv_contains ( 22env , 23clippy_action , 24flag , 25) 26 27clippy_checks = target [ OutputGroupInfo ]. clippy_checks . to_list () 28if len ( clippy_checks ) != 1 : 29fail ( "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 33assert_list_contains_adjacent_elements ( 34env , 35clippy_action . argv , 36[ 37"--touch-file" , 38clippy_checks [ 0 ]. path , 39], 40) 41 42return analysistest . end ( env ) 43 44def _binary_clippy_aspect_action_has_warnings_flag_test_impl ( ctx ): 45return _clippy_aspect_action_has_flag_impl ( 46ctx , 47[ "-Dwarnings" ], 48) 49 50def _library_clippy_aspect_action_has_warnings_flag_test_impl ( ctx ): 51return _clippy_aspect_action_has_flag_impl ( 52ctx , 53[ "-Dwarnings" ], 54) 55 56def _test_clippy_aspect_action_has_warnings_flag_test_impl ( ctx ): 57return _clippy_aspect_action_has_flag_impl ( 58ctx , 59[ 60"-Dwarnings" , 61"--test" , 62], 63) 64 65_CLIPPY_EXPLICIT_FLAGS = [ 66"-Dwarnings" , 67"-A" , 68"clippy::needless_return" , 69] 70 71_CLIPPY_INDIVIDUALLY_ADDED_EXPLICIT_FLAGS = [ 72"-A" , 73"clippy::new_without_default" , 74"-A" , 75"clippy::needless_range_loop" , 76] 77 78def _clippy_aspect_with_explicit_flags_test_impl ( ctx ): 79return _clippy_aspect_action_has_flag_impl ( 80ctx , 81_CLIPPY_EXPLICIT_FLAGS + _CLIPPY_INDIVIDUALLY_ADDED_EXPLICIT_FLAGS , 82) 83 84def make_clippy_aspect_unittest ( impl , ** kwargs ): 85return analysistest . make ( 86impl , 87extra_target_under_test_aspects = [ rust_clippy_aspect ], 88** kwargs 89) 90 91binary_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest ( _binary_clippy_aspect_action_has_warnings_flag_test_impl ) 92library_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest ( _library_clippy_aspect_action_has_warnings_flag_test_impl ) 93test_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest ( _test_clippy_aspect_action_has_warnings_flag_test_impl ) 94clippy_aspect_with_explicit_flags_test = make_clippy_aspect_unittest ( 95_clippy_aspect_with_explicit_flags_test_impl , 96config_settings = { 97str ( Label ( "//rust/settings:clippy_flag" )): _CLIPPY_INDIVIDUALLY_ADDED_EXPLICIT_FLAGS , 98str ( Label ( "//rust/settings:clippy_flags" )): _CLIPPY_EXPLICIT_FLAGS , 99}, 100) 101 102def clippy_test_suite ( name ): 103"""Entry-point macro called from the BUILD file. 104 105Args: 106name (str): Name of the macro. 107""" 108 109binary_clippy_aspect_action_has_warnings_flag_test ( 110name = "binary_clippy_aspect_action_has_warnings_flag_test" , 111target_under_test = Label ( "//test/clippy:ok_binary" ), 112) 113library_clippy_aspect_action_has_warnings_flag_test ( 114name = "library_clippy_aspect_action_has_warnings_flag_test" , 115target_under_test = Label ( "//test/clippy:ok_library" ), 116) 117test_clippy_aspect_action_has_warnings_flag_test ( 118name = "test_clippy_aspect_action_has_warnings_flag_test" , 119target_under_test = Label ( "//test/clippy:ok_test" ), 120) 121clippy_aspect_with_explicit_flags_test ( 122name = "binary_clippy_aspect_with_explicit_flags_test" , 123target_under_test = Label ( "//test/clippy:ok_binary" ), 124) 125clippy_aspect_with_explicit_flags_test ( 126name = "library_clippy_aspect_with_explicit_flags_test" , 127target_under_test = Label ( "//test/clippy:ok_library" ), 128) 129clippy_aspect_with_explicit_flags_test ( 130name = "test_clippy_aspect_with_explicit_flags_test" , 131target_under_test = Label ( "//test/clippy:ok_test" ), 132) 133 134native . test_suite ( 135name = name , 136tests = [ 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" , 140":binary_clippy_aspect_with_explicit_flags_test" , 141":library_clippy_aspect_with_explicit_flags_test" , 142":test_clippy_aspect_with_explicit_flags_test" , 143], 144)