fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
81590f4b
0.8.1
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 71def _clippy_aspect_with_explicit_flags_test_impl ( ctx ): 72return _clippy_aspect_action_has_flag_impl ( 73ctx , 74_CLIPPY_EXPLICIT_FLAGS , 75) 76 77def make_clippy_aspect_unittest ( impl , ** kwargs ): 78return analysistest . make ( 79impl , 80extra_target_under_test_aspects = [ rust_clippy_aspect ], 81** kwargs 82) 83 84binary_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest ( _binary_clippy_aspect_action_has_warnings_flag_test_impl ) 85library_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest ( _library_clippy_aspect_action_has_warnings_flag_test_impl ) 86test_clippy_aspect_action_has_warnings_flag_test = make_clippy_aspect_unittest ( _test_clippy_aspect_action_has_warnings_flag_test_impl ) 87clippy_aspect_with_explicit_flags_test = make_clippy_aspect_unittest ( _clippy_aspect_with_explicit_flags_test_impl , config_settings = { "@//:clippy_flags" : _CLIPPY_EXPLICIT_FLAGS }) 88 89def clippy_test_suite ( name ): 90"""Entry-point macro called from the BUILD file. 91 92Args: 93name (str): Name of the macro. 94""" 95 96binary_clippy_aspect_action_has_warnings_flag_test ( 97name = "binary_clippy_aspect_action_has_warnings_flag_test" , 98target_under_test = Label ( "//test/clippy:ok_binary" ), 99) 100library_clippy_aspect_action_has_warnings_flag_test ( 101name = "library_clippy_aspect_action_has_warnings_flag_test" , 102target_under_test = Label ( "//test/clippy:ok_library" ), 103) 104test_clippy_aspect_action_has_warnings_flag_test ( 105name = "test_clippy_aspect_action_has_warnings_flag_test" , 106target_under_test = Label ( "//test/clippy:ok_test" ), 107) 108clippy_aspect_with_explicit_flags_test ( 109name = "binary_clippy_aspect_with_explicit_flags_test" , 110target_under_test = Label ( "//test/clippy:ok_binary" ), 111) 112clippy_aspect_with_explicit_flags_test ( 113name = "library_clippy_aspect_with_explicit_flags_test" , 114target_under_test = Label ( "//test/clippy:ok_library" ), 115) 116clippy_aspect_with_explicit_flags_test ( 117name = "test_clippy_aspect_with_explicit_flags_test" , 118target_under_test = Label ( "//test/clippy:ok_test" ), 119) 120 121native . test_suite ( 122name = name , 123tests = [ 124":binary_clippy_aspect_action_has_warnings_flag_test" , 125":library_clippy_aspect_action_has_warnings_flag_test" , 126":test_clippy_aspect_action_has_warnings_flag_test" , 127":binary_clippy_aspect_with_explicit_flags_test" , 128":library_clippy_aspect_with_explicit_flags_test" , 129":test_clippy_aspect_with_explicit_flags_test" , 130], 131)