fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
6c389346
0.10.0
1"""Unittests for rust rules.""" 2 3load ( "@bazel_skylib//lib:unittest.bzl" , "analysistest" , "asserts" ) 4load ( "//rust:defs.bzl" , "rust_binary" , "rust_library" , "rust_proc_macro" ) 5load ( "//test/unit:common.bzl" , "assert_argv_contains" , "assert_list_contains_adjacent_elements" , "assert_list_contains_adjacent_elements_not" ) 6load ( ":wrap.bzl" , "wrap" ) 7 8NOT_WINDOWS = select ({ 9"@platforms//os:linux" : [], 10"@platforms//os:macos" : [], 11"//conditions:default" : [ "@platforms//:incompatible" ], 12}) 13 14ENABLE_PIPELINING = { 15"@//rust/settings:pipelined_compilation" : True , 16} 17 18def _second_lib_test_impl ( ctx ): 19env = analysistest . begin ( ctx ) 20tut = analysistest . target_under_test ( env ) 21rlib_action = [ act for act in tut . actions if act . mnemonic == "Rustc" ][ 0 ] 22metadata_action = [ act for act in tut . actions if act . mnemonic == "RustcMetadata" ][ 0 ] 23 24# Both actions should use the same --emit= 25assert_argv_contains ( env , rlib_action , "--emit=dep-info,link,metadata" ) 26assert_argv_contains ( env , metadata_action , "--emit=dep-info,link,metadata" ) 27 28# The metadata action should have a .rmeta as output and the rlib action a .rlib 29path = rlib_action . outputs . to_list ()[ 0 ]. path 30asserts . true ( 31env , 32path . endswith ( ".rlib" ), 33"expected Rustc to output .rlib, got " + path , 34) 35path = metadata_action . outputs . to_list ()[ 0 ]. path 36asserts . true ( 37env , 38path . endswith ( ".rmeta" ), 39"expected RustcMetadata to output .rmeta, got " + path , 40) 41 42# Only the action building metadata should contain --rustc-quit-on-rmeta 43assert_list_contains_adjacent_elements_not ( env , rlib_action . argv , [ "--rustc-quit-on-rmeta" , "true" ]) 44assert_list_contains_adjacent_elements ( env , metadata_action . argv , [ "--rustc-quit-on-rmeta" , "true" ]) 45 46# Check that both actions refer to the metadata of :first, not the rlib 47extern_metadata = [ arg for arg in metadata_action . argv if arg . startswith ( "--extern=first=" ) and "libfirst" in arg and arg . endswith ( ".rmeta" )] 48asserts . true ( 49env , 50len ( extern_metadata ) == 1 , 51"did not find a --extern=first=*.rmeta but expected one" , 52) 53extern_rlib = [ arg for arg in rlib_action . argv if arg . startswith ( "--extern=first=" ) and "libfirst" in arg and arg . endswith ( ".rmeta" )] 54asserts . true ( 55env , 56len ( extern_rlib ) == 1 , 57"did not find a --extern=first=*.rlib but expected one" , 58) 59 60# Check that the input to both actions is the metadata of :first 61input_metadata = [ i for i in metadata_action . inputs . to_list () if i . basename . startswith ( "libfirst" )] 62asserts . true ( env , len ( input_metadata ) == 1 , "expected only one libfirst input, found " + str ([ i . path for i in input_metadata ])) 63asserts . true ( env , input_metadata [ 0 ]. extension == "rmeta" , "expected libfirst dependency to be rmeta, found " + input_metadata [ 0 ]. path ) 64input_rlib = [ i for i in rlib_action . inputs . to_list () if i . basename . startswith ( "libfirst" )] 65asserts . true ( env , len ( input_rlib ) == 1 , "expected only one libfirst input, found " + str ([ i . path for i in input_rlib ])) 66asserts . true ( env , input_rlib [ 0 ]. extension == "rmeta" , "expected libfirst dependency to be rmeta, found " + input_rlib [ 0 ]. path ) 67 68return analysistest . end ( env ) 69 70def _bin_test_impl ( ctx ): 71env = analysistest . begin ( ctx ) 72tut = analysistest . target_under_test ( env ) 73bin_action = [ act for act in tut . actions if act . mnemonic == "Rustc" ][ 0 ] 74 75# Check that no inputs to this binary are .rmeta files. 76metadata_inputs = [ i . path for i in bin_action . inputs . to_list () if i . path . endswith ( ".rmeta" )] 77asserts . false ( env , metadata_inputs , "expected no metadata inputs, found " + str ( metadata_inputs )) 78 79return analysistest . end ( env ) 80 81bin_test = analysistest . make ( _bin_test_impl , config_settings = ENABLE_PIPELINING ) 82second_lib_test = analysistest . make ( _second_lib_test_impl , config_settings = ENABLE_PIPELINING ) 83 84def _pipelined_compilation_test (): 85rust_proc_macro ( 86name = "my_macro" , 87edition = "2021" , 88srcs = [ "my_macro.rs" ], 89) 90 91rust_library ( 92name = "first" , 93edition = "2021" , 94srcs = [ "first.rs" ], 95) 96 97rust_library ( 98name = "second" , 99edition = "2021" , 100srcs = [ "second.rs" ], 101deps = [ ":first" ], 102proc_macro_deps = [ ":my_macro" ], 103) 104 105rust_binary ( 106name = "bin" , 107edition = "2021" , 108srcs = [ "bin.rs" ], 109deps = [ ":second" ], 110) 111 112second_lib_test ( name = "second_lib_test" , target_under_test = ":second" , target_compatible_with = NOT_WINDOWS ) 113bin_test ( name = "bin_test" , target_under_test = ":bin" , target_compatible_with = NOT_WINDOWS ) 114 115def _rmeta_is_propagated_through_custom_rule_test_impl ( ctx ): 116env = analysistest . begin ( ctx ) 117tut = analysistest . target_under_test ( env ) 118 119# This is the metadata-generating action. It should depend on metadata for the library and, if generate_metadata is set 120# also depend on metadata for 'wrapper'. 121rust_action = [ act for act in tut . actions if act . mnemonic == "RustcMetadata" ][ 0 ] 122 123metadata_inputs = [ i for i in rust_action . inputs . to_list () if i . path . endswith ( ".rmeta" )] 124rlib_inputs = [ i for i in rust_action . inputs . to_list () if i . path . endswith ( ".rlib" )] 125 126seen_wrapper_metadata = False 127seen_to_wrap_metadata = False 128for mi in metadata_inputs : 129if "libwrapper" in mi . path : 130seen_wrapper_metadata = True 131if "libto_wrap" in mi . path : 132seen_to_wrap_metadata = True 133 134seen_wrapper_rlib = False 135seen_to_wrap_rlib = False 136for ri in rlib_inputs : 137if "libwrapper" in ri . path : 138seen_wrapper_rlib = True 139if "libto_wrap" in ri . path : 140seen_to_wrap_rlib = True 141 142if ctx . attr . generate_metadata : 143asserts . true ( env , seen_wrapper_metadata , "expected dependency on metadata for 'wrapper' but not found" ) 144asserts . false ( env , seen_wrapper_rlib , "expected no dependency on object for 'wrapper' but it was found" ) 145else : 146asserts . true ( env , seen_wrapper_rlib , "expected dependency on object for 'wrapper' but not found" ) 147asserts . false ( env , seen_wrapper_metadata , "expected no dependency on metadata for 'wrapper' but it was found" ) 148 149asserts . true ( env , seen_to_wrap_metadata , "expected dependency on metadata for 'to_wrap' but not found" ) 150asserts . false ( env , seen_to_wrap_rlib , "expected no dependency on object for 'to_wrap' but it was found" ) 151 152return analysistest . end ( env ) 153 154def _rmeta_is_used_when_building_custom_rule_test_impl ( ctx ): 155env = analysistest . begin ( ctx ) 156tut = analysistest . target_under_test ( env ) 157 158# This is the custom rule invocation of rustc. 159rust_action = [ act for act in tut . actions if act . mnemonic == "Rustc" ][ 0 ] 160 161# We want to check that the action depends on metadata, regardless of ctx.attr.generate_metadata 162seen_to_wrap_rlib = False 163seen_to_wrap_rmeta = False 164for act in rust_action . inputs . to_list (): 165if "libto_wrap" in act . path and act . path . endswith ( ".rlib" ): 166seen_to_wrap_rlib = True 167elif "libto_wrap" in act . path and act . path . endswith ( ".rmeta" ): 168seen_to_wrap_rmeta = True 169 170asserts . true ( env , seen_to_wrap_rmeta , "expected dependency on metadata for 'to_wrap' but not found" ) 171asserts . false ( env , seen_to_wrap_rlib , "expected no dependency on object for 'to_wrap' but it was found" ) 172 173return analysistest . end ( env ) 174 175rmeta_is_propagated_through_custom_rule_test = analysistest . make ( _rmeta_is_propagated_through_custom_rule_test_impl , attrs = { "generate_metadata" : attr . bool ()}, config_settings = ENABLE_PIPELINING ) 176rmeta_is_used_when_building_custom_rule_test = analysistest . make ( _rmeta_is_used_when_building_custom_rule_test_impl , config_settings = ENABLE_PIPELINING ) 177 178def _rmeta_not_produced_if_pipelining_disabled_test_impl ( ctx ): 179env = analysistest . begin ( ctx ) 180tut = analysistest . target_under_test ( env ) 181 182rust_action = [ act for act in tut . actions if act . mnemonic == "RustcMetadata" ] 183asserts . true ( env , len ( rust_action ) == 0 , "expected no metadata to be produced, but found a metadata action" ) 184 185return analysistest . end ( env ) 186 187rmeta_not_produced_if_pipelining_disabled_test = analysistest . make ( _rmeta_not_produced_if_pipelining_disabled_test_impl , config_settings = ENABLE_PIPELINING ) 188 189def _disable_pipelining_test (): 190rust_library ( 191name = "lib" , 192srcs = [ "custom_rule_test/to_wrap.rs" ], 193edition = "2021" , 194disable_pipelining = True , 195) 196rmeta_not_produced_if_pipelining_disabled_test ( 197name = "rmeta_not_produced_if_pipelining_disabled_test" , 198target_compatible_with = NOT_WINDOWS , 199target_under_test = ":lib" , 200) 201 202def _custom_rule_test ( generate_metadata , suffix ): 203rust_library ( 204name = "to_wrap" + suffix , 205crate_name = "to_wrap" , 206srcs = [ "custom_rule_test/to_wrap.rs" ], 207edition = "2021" , 208) 209wrap ( 210name = "wrapper" + suffix , 211crate_name = "wrapper" , 212target = ":to_wrap" + suffix , 213generate_metadata = generate_metadata , 214) 215rust_library ( 216name = "uses_wrapper" + suffix , 217srcs = [ "custom_rule_test/uses_wrapper.rs" ], 218deps = [ ":wrapper" + suffix ], 219edition = "2021" , 220) 221 222rmeta_is_propagated_through_custom_rule_test ( 223name = "rmeta_is_propagated_through_custom_rule_test" + suffix , 224generate_metadata = generate_metadata , 225target_compatible_with = NOT_WINDOWS , 226target_under_test = ":uses_wrapper" + suffix , 227) 228 229rmeta_is_used_when_building_custom_rule_test ( 230name = "rmeta_is_used_when_building_custom_rule_test" + suffix , 231target_compatible_with = NOT_WINDOWS , 232target_under_test = ":wrapper" + suffix , 233) 234 235def pipelined_compilation_test_suite ( name ): 236"""Entry-point macro called from the BUILD file. 237 238Args: 239name: Name of the macro. 240""" 241_pipelined_compilation_test () 242_disable_pipelining_test () 243_custom_rule_test ( generate_metadata = True , suffix = "_with_metadata" ) 244_custom_rule_test ( generate_metadata = False , suffix = "_without_metadata" ) 245 246native . test_suite ( 247name = name , 248tests = [ 249":bin_test" , 250":second_lib_test" , 251":rmeta_is_propagated_through_custom_rule_test_with_metadata" , 252":rmeta_is_propagated_through_custom_rule_test_without_metadata" , 253":rmeta_is_used_when_building_custom_rule_test_with_metadata" , 254":rmeta_is_used_when_building_custom_rule_test_without_metadata" , 255":rmeta_not_produced_if_pipelining_disabled_test" , 256], 257)