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 16 Args: 17 edition (str): The rust edition to use for the new targets 18 """ 19 rust_proc_macro( 20 name = "proc_macro_{}".format(edition), 21 srcs = [ 22 "proc_macro_{}.rs".format(edition), 23 ], 24 edition = edition, 25 visibility = ["//test:__subpackages__"], 26 ) 27 28 rust_test( 29 name = "wrapper_rule_for_macro_{}".format(edition), 30 crate = ":proc_macro_{}".format(edition), 31 edition = edition, 32 ) 33 34def _unit_test_impl(ctx, edition, is_wrapper): 35 env = analysistest.begin(ctx) 36 actions = analysistest.target_under_test(env).actions 37 action = actions[0] 38 assert_action_mnemonic(env, action, "Rustc") 39 40 if edition == "2015": 41 # Edition 2015 does not use `--extern proc_macro` instead this 42 # must be explicitly set in Rust code. 43 assert_list_contains_adjacent_elements_not(env, action.argv, ["--extern", "proc_macro"]) 44 elif edition == "2018": 45 # `--extern proc_macro` is required to resolve build proc-macro 46 assert_list_contains_adjacent_elements(env, action.argv, ["--extern", "proc_macro"]) 47 if not is_wrapper: 48 assert_argv_contains(env, action, "--edition=2018") 49 else: 50 fail("Unexpected edition") 51 52 return analysistest.end(env) 53 54def _extern_flag_not_passed_when_compiling_macro_2015_impl(ctx): 55 return _unit_test_impl(ctx, "2015", False) 56 57def _extern_flag_passed_when_compiling_macro_2018_impl(ctx): 58 return _unit_test_impl(ctx, "2018", False) 59 60def _extern_flag_not_passed_when_compiling_macro_wrapper_rule_2015_impl(ctx): 61 return _unit_test_impl(ctx, "2015", True) 62 63def _extern_flag_passed_when_compiling_macro_wrapper_rule_2018_impl(ctx): 64 return _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 77 extern_flag_not_passed_when_compiling_macro_2015_test( 78 name = "extern_flag_not_passed_when_compiling_macro_2015", 79 target_under_test = ":proc_macro_2015", 80 ) 81 extern_flag_passed_when_compiling_macro_2018_test( 82 name = "extern_flag_passed_when_compiling_macro_2018", 83 target_under_test = ":proc_macro_2018", 84 ) 85 extern_flag_not_passed_when_compiling_macro_wrapper_rule_2015_test( 86 name = "extern_flag_not_passed_when_compiling_macro_wrapper_rule_2015", 87 target_under_test = ":wrapper_rule_for_macro_2015", 88 ) 89 extern_flag_passed_when_compiling_macro_wrapper_rule_2018_test( 90 name = "extern_flag_passed_when_compiling_macro_wrapper_rule_2018", 91 target_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 97 Args: 98 name: Name of the macro. 99 """ 100 _proc_macro_test() 101 102 native.test_suite( 103 name = name, 104 tests = [ 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 ) 111