fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
01cc76ba
patching
1"""Unittest to verify proc-macro targets""" 2 3load ( "@bazel_skylib//lib:unittest.bzl" , "analysistest" ) 4load ( "//rust:defs.bzl" , "rust_proc_macro" , "rust_test" ) 5load ( 6"//test/unit:common.bzl" , 7"assert_action_mnemonic" , 8"assert_argv_contains" , 9"assert_list_contains_adjacent_elements" , 10"assert_list_contains_adjacent_elements_not" , 11) 12 13def _proc_macro_test_targets ( edition ): 14"""Define a set of `rust_proc_macro` targets for testing 15 16Args: 17edition (str): The rust edition to use for the new targets 18""" 19rust_proc_macro ( 20name = "proc_macro_{}" . format ( edition ), 21srcs = [ 22"proc_macro_{}.rs" . format ( edition ), 23], 24edition = edition , 25visibility = [ "//test:__subpackages__" ], 26) 27 28rust_test ( 29name = "wrapper_rule_for_macro_{}" . format ( edition ), 30crate = ":proc_macro_{}" . format ( edition ), 31edition = edition , 32) 33 34def _unit_test_impl ( ctx , edition , is_wrapper ): 35env = analysistest . begin ( ctx ) 36actions = analysistest . target_under_test ( env ). actions 37action = actions [ 0 ] 38assert_action_mnemonic ( env , action , "Rustc" ) 39 40if edition == "2015" : 41# Edition 2015 does not use `--extern proc_macro` instead this 42# must be explicitly set in Rust code. 43assert_list_contains_adjacent_elements_not ( env , action . argv , [ "--extern" , "proc_macro" ]) 44elif edition == "2018" : 45# `--extern proc_macro` is required to resolve build proc-macro 46assert_list_contains_adjacent_elements ( env , action . argv , [ "--extern" , "proc_macro" ]) 47if not is_wrapper : 48assert_argv_contains ( env , action , "--edition=2018" ) 49else : 50fail ( "Unexpected edition" ) 51 52return analysistest . end ( env ) 53 54def _extern_flag_not_passed_when_compiling_macro_2015_impl ( ctx ): 55return _unit_test_impl ( ctx , "2015" , False ) 56 57def _extern_flag_passed_when_compiling_macro_2018_impl ( ctx ): 58return _unit_test_impl ( ctx , "2018" , False ) 59 60def _extern_flag_not_passed_when_compiling_macro_wrapper_rule_2015_impl ( ctx ): 61return _unit_test_impl ( ctx , "2015" , True ) 62 63def _extern_flag_passed_when_compiling_macro_wrapper_rule_2018_impl ( ctx ): 64return _unit_test_impl ( ctx , "2018" , True ) 65 66extern_flag_not_passed_when_compiling_macro_2015_test = analysistest . make ( _extern_flag_not_passed_when_compiling_macro_2015_impl ) 67extern_flag_passed_when_compiling_macro_2018_test = analysistest . make ( _extern_flag_passed_when_compiling_macro_2018_impl ) 68extern_flag_not_passed_when_compiling_macro_wrapper_rule_2015_test = analysistest . make ( _extern_flag_not_passed_when_compiling_macro_wrapper_rule_2015_impl ) 69extern_flag_passed_when_compiling_macro_wrapper_rule_2018_test = analysistest . make ( _extern_flag_passed_when_compiling_macro_wrapper_rule_2018_impl ) 70 71def _proc_macro_test (): 72"""Generate targets and tests""" 73 74_proc_macro_test_targets ( "2015" ) 75_proc_macro_test_targets ( "2018" ) 76 77extern_flag_not_passed_when_compiling_macro_2015_test ( 78name = "extern_flag_not_passed_when_compiling_macro_2015" , 79target_under_test = ":proc_macro_2015" , 80) 81extern_flag_passed_when_compiling_macro_2018_test ( 82name = "extern_flag_passed_when_compiling_macro_2018" , 83target_under_test = ":proc_macro_2018" , 84) 85extern_flag_not_passed_when_compiling_macro_wrapper_rule_2015_test ( 86name = "extern_flag_not_passed_when_compiling_macro_wrapper_rule_2015" , 87target_under_test = ":wrapper_rule_for_macro_2015" , 88) 89extern_flag_passed_when_compiling_macro_wrapper_rule_2018_test ( 90name = "extern_flag_passed_when_compiling_macro_wrapper_rule_2018" , 91target_under_test = ":wrapper_rule_for_macro_2018" , 92) 93 94def proc_macro_test_suite ( name ): 95"""Entry-point macro called from the BUILD file. 96 97Args: 98name: Name of the macro. 99""" 100_proc_macro_test () 101 102native . test_suite ( 103name = name , 104tests = [ 105":extern_flag_not_passed_when_compiling_macro_2015" , 106":extern_flag_passed_when_compiling_macro_2018" , 107":extern_flag_not_passed_when_compiling_macro_wrapper_rule_2015" , 108":extern_flag_passed_when_compiling_macro_wrapper_rule_2018" , 109], 110)