fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
9a8e878d
0.8.0
1"""Unittest to verify workspace status stamping is applied to environment files""" 2 3load ( "@bazel_skylib//lib:unittest.bzl" , "analysistest" ) 4load ( "//rust:defs.bzl" , "rust_binary" , "rust_common" , "rust_library" , "rust_test" ) 5load ( 6"//test/unit:common.bzl" , 7"assert_action_mnemonic" , 8"assert_argv_contains" , 9"assert_argv_contains_not" , 10) 11 12_STAMP_ATTR_VALUES = ( 0 , 1 , - 1 ) 13_BUILD_FLAG_VALUES = ( "true" , "false" ) 14 15def _assert_stamped ( env , action ): 16assert_argv_contains ( env , action , "--volatile-status-file" ) 17assert_argv_contains ( env , action , "bazel-out/volatile-status.txt" ) 18 19assert_argv_contains ( env , action , "--stable-status-file" ) 20assert_argv_contains ( env , action , "bazel-out/stable-status.txt" ) 21 22def _assert_not_stamped ( env , action ): 23assert_argv_contains_not ( env , action , "--volatile-status-file" ) 24assert_argv_contains_not ( env , action , "bazel-out/volatile-status.txt" ) 25 26assert_argv_contains_not ( env , action , "--stable-status-file" ) 27assert_argv_contains_not ( env , action , "bazel-out/stable-status.txt" ) 28 29def _stamp_build_flag_test_impl ( ctx , flag_value ): 30env = analysistest . begin ( ctx ) 31target = analysistest . target_under_test ( env ) 32 33action = target . actions [ 0 ] 34assert_action_mnemonic ( env , action , "Rustc" ) 35 36is_test = target [ rust_common . crate_info ]. is_test 37is_bin = target [ rust_common . crate_info ]. type == "bin" 38 39# bazel build --stamp should lead to stamped rust binaries, but not 40# libraries and tests. 41if flag_value : 42if is_bin and not is_test : 43_assert_stamped ( env , action ) 44else : 45_assert_not_stamped ( env , action ) 46else : 47_assert_not_stamped ( env , action ) 48 49return analysistest . end ( env ) 50 51def _stamp_build_flag_is_true_impl ( ctx ): 52return _stamp_build_flag_test_impl ( ctx , True ) 53 54def _stamp_build_flag_is_false_impl ( ctx ): 55return _stamp_build_flag_test_impl ( ctx , False ) 56 57stamp_build_flag_is_true_test = analysistest . make ( 58_stamp_build_flag_is_true_impl , 59config_settings = { 60"//command_line_option:stamp" : True , 61}, 62) 63 64stamp_build_flag_is_false_test = analysistest . make ( 65_stamp_build_flag_is_false_impl , 66config_settings = { 67"//command_line_option:stamp" : False , 68}, 69) 70 71def _build_flag_tests (): 72tests = [] 73for stamp_value in _BUILD_FLAG_VALUES : 74if stamp_value == "true" : 75name = "default_with_build_flag_on" 76features = [ "always_stamp" ] 77build_flag_stamp_test = stamp_build_flag_is_true_test 78else : 79name = "default_with_build_flag_off" 80features = [ "never_stamp" ] 81build_flag_stamp_test = stamp_build_flag_is_false_test 82 83rust_library ( 84name = "{}_lib" . format ( name ), 85srcs = [ "stamp.rs" ], 86rustc_env_files = [ "stamp.env" ], 87edition = "2018" , 88# Building with --stamp should not affect rust libraries 89crate_features = [ "never_stamp" ], 90) 91 92rust_binary ( 93name = "{}_bin" . format ( name ), 94srcs = [ "stamp_main.rs" ], 95edition = "2018" , 96deps = [ "{}_lib" . format ( name )], 97rustc_env_files = [ "stamp.env" ], 98crate_features = features , 99) 100 101rust_test ( 102name = "{}_test" . format ( name ), 103crate = "{}_lib" . format ( name ), 104edition = "2018" , 105rustc_env_files = [ "stamp.env" ], 106# Building with --stamp should not affect tests 107crate_features = [ "never_stamp" ], 108) 109 110build_flag_stamp_test ( 111name = "lib_{}_test" . format ( name ), 112target_under_test = "{}_lib" . format ( name ), 113) 114build_flag_stamp_test ( 115name = "bin_{}_test" . format ( name ), 116target_under_test = "{}_bin" . format ( name ), 117) 118 119build_flag_stamp_test ( 120name = "test_{}_test" . format ( name ), 121target_under_test = "{}_test" . format ( name ), 122) 123 124tests . extend ([ 125"lib_{}_test" . format ( name ), 126"bin_{}_test" . format ( name ), 127"test_{}_test" . format ( name ), 128]) 129return tests 130 131def _attribute_stamp_test_impl ( ctx , attribute_value , build_flag_value ): 132env = analysistest . begin ( ctx ) 133target = analysistest . target_under_test ( env ) 134 135action = target . actions [ 0 ] 136assert_action_mnemonic ( env , action , "Rustc" ) 137 138if attribute_value == 1 : 139_assert_stamped ( env , action ) 140elif attribute_value == 0 : 141_assert_not_stamped ( env , action ) 142elif build_flag_value : 143_assert_stamped ( env , action ) 144else : 145_assert_not_stamped ( env , action ) 146 147return analysistest . end ( env ) 148 149def _always_stamp_build_flag_is_true_test_impl ( ctx ): 150return _attribute_stamp_test_impl ( ctx , attribute_value = 1 , build_flag_value = True ) 151 152def _always_stamp_build_flag_is_false_test_impl ( ctx ): 153return _attribute_stamp_test_impl ( ctx , attribute_value = 1 , build_flag_value = False ) 154 155def _never_stamp_build_flag_is_true_test_impl ( ctx ): 156return _attribute_stamp_test_impl ( ctx , attribute_value = 0 , build_flag_value = True ) 157 158def _never_stamp_build_flag_is_false_test_impl ( ctx ): 159return _attribute_stamp_test_impl ( ctx , attribute_value = 0 , build_flag_value = False ) 160 161def _consult_build_flag_value_is_true_test_impl ( ctx ): 162return _attribute_stamp_test_impl ( ctx , attribute_value = - 1 , build_flag_value = True ) 163 164def _consult_build_flag_value_is_false_test_impl ( ctx ): 165return _attribute_stamp_test_impl ( ctx , attribute_value = - 1 , build_flag_value = False ) 166 167always_stamp_test_build_flag_is_true_test = analysistest . make ( 168_always_stamp_build_flag_is_true_test_impl , 169config_settings = { 170"//command_line_option:stamp" : True , 171}, 172) 173 174always_stamp_test_build_flag_is_false_test = analysistest . make ( 175_always_stamp_build_flag_is_false_test_impl , 176config_settings = { 177"//command_line_option:stamp" : False , 178}, 179) 180 181never_stamp_test_build_flag_is_true_test = analysistest . make ( 182_never_stamp_build_flag_is_true_test_impl , 183config_settings = { 184"//command_line_option:stamp" : True , 185}, 186) 187 188never_stamp_test_build_flag_is_false_test = analysistest . make ( 189_never_stamp_build_flag_is_false_test_impl , 190config_settings = { 191"//command_line_option:stamp" : False , 192}, 193) 194 195consult_build_flag_value_is_true_test = analysistest . make ( 196_consult_build_flag_value_is_true_test_impl , 197config_settings = { 198"//command_line_option:stamp" : True , 199}, 200) 201 202consult_build_flag_value_is_false_test = analysistest . make ( 203_consult_build_flag_value_is_false_test_impl , 204config_settings = { 205"//command_line_option:stamp" : False , 206}, 207) 208 209def _stamp_attribute_tests (): 210tests = [] 211 212for stamp_value in _STAMP_ATTR_VALUES : 213for flag_value in _BUILD_FLAG_VALUES : 214if stamp_value == 1 : 215name = "always_stamp_build_flag_{}" . format ( flag_value ) 216features = [ "always_stamp_build_flag_{}" . format ( flag_value )] 217stamp_attr_test = always_stamp_test_build_flag_is_true_test if flag_value == "true" else always_stamp_test_build_flag_is_false_test 218elif stamp_value == 0 : 219name = "never_stamp_build_flag_{}" . format ( flag_value ) 220features = [ "never_stamp_build_flag_{}" . format ( flag_value )] 221stamp_attr_test = never_stamp_test_build_flag_is_true_test if flag_value == "true" else never_stamp_test_build_flag_is_false_test 222else : 223name = "consult_cmdline_value_is_{}" . format ( flag_value ) 224features = [ "consult_cmdline_value_is_{}" . format ( flag_value )] 225stamp_attr_test = consult_build_flag_value_is_true_test if flag_value == "true" else consult_build_flag_value_is_false_test 226 227rust_library ( 228name = "{}_lib" . format ( name ), 229srcs = [ "stamp.rs" ], 230edition = "2018" , 231rustc_env_files = [ ":stamp.env" ], 232stamp = stamp_value , 233crate_features = features , 234) 235 236rust_test ( 237name = "{}_unit_test" . format ( name ), 238crate = ":{}_lib" . format ( name ), 239edition = "2018" , 240rustc_env_files = [ ":stamp.env" ], 241stamp = stamp_value , 242crate_features = features , 243# We disable this test so that it doesn't try to run with bazel test //test/... 244# The reason for this is because then it is sensitive to the --stamp value which 245# we override in the unit test implementation. 246tags = [ "manual" ], 247) 248 249rust_binary ( 250name = "{}_bin" . format ( name ), 251srcs = [ "stamp_main.rs" ], 252edition = "2018" , 253deps = [ ":{}_lib" . format ( name )], 254rustc_env_files = [ ":stamp.env" ], 255stamp = stamp_value , 256crate_features = features , 257) 258 259stamp_attr_test ( 260name = "lib_{}_test" . format ( name ), 261target_under_test = "{}_lib" . format ( name ), 262) 263stamp_attr_test ( 264name = "bin_{}_test" . format ( name ), 265target_under_test = "{}_bin" . format ( name ), 266) 267 268stamp_attr_test ( 269name = "test_{}_test" . format ( name ), 270target_under_test = "{}_unit_test" . format ( name ), 271) 272 273tests . extend ([ 274"lib_{}_test" . format ( name ), 275"bin_{}_test" . format ( name ), 276"test_{}_test" . format ( name ), 277]) 278return tests 279 280def _process_wrapper_with_stamp_test_impl ( ctx ): 281env = analysistest . begin ( ctx ) 282target = analysistest . target_under_test ( env ) 283 284action = target . actions [ 0 ] 285assert_action_mnemonic ( env , action , "Rustc" ) 286 287_assert_not_stamped ( env , action ) 288 289return analysistest . end ( env ) 290 291process_wrapper_with_stamp_test = analysistest . make ( 292_process_wrapper_with_stamp_test_impl , 293config_settings = { 294"//command_line_option:stamp" : True , 295}, 296) 297 298def _process_wrapper_tests (): 299process_wrapper_with_stamp_test ( 300name = "test_process_wrapper_with_stamp_test" , 301target_under_test = "//util/process_wrapper:process_wrapper" , 302) 303 304return [ "test_process_wrapper_with_stamp_test" ] 305 306def stamp_test_suite ( name ): 307"""Entry-point macro called from the BUILD file. 308 309Args: 310name (str): Name of the macro. 311""" 312tests = _build_flag_tests () + _stamp_attribute_tests () + _process_wrapper_tests () 313 314native . test_suite ( 315name = name , 316tests = tests , 317)