1"""Unittest to verify per_crate_rustc_flag (attribute) propagation""" 2 3load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") 4load("//rust:defs.bzl", "rust_library") 5load("//test/unit:common.bzl", "assert_argv_contains", "assert_argv_contains_not") 6 7RUSTC_FLAG = "--codegen=linker-plugin-lto" 8MATCHING_LABEL_FLAG = "//test/unit/per_crate_rustc_flag:lib@--codegen=linker-plugin-lto" 9MATCHING_EXECUTION_PATH_FLAG = "test/unit/per_crate_rustc_flag/lib.rs@--codegen=linker-plugin-lto" 10NON_MATCHING_FLAG = "rust/private/rustc.bzl@--codegen=linker-plugin-lto" 11INVALID_FLAG = "--codegen=linker-plugin-lto" 12 13def target_action_contains_not_flag(env, target): 14 action = target.actions[0] 15 asserts.equals(env, "Rustc", action.mnemonic) 16 17 assert_argv_contains_not( 18 env = env, 19 action = action, 20 flag = RUSTC_FLAG, 21 ) 22 23def target_action_contains_flag(env, target): 24 action = target.actions[0] 25 asserts.equals(env, "Rustc", action.mnemonic) 26 27 assert_argv_contains( 28 env = env, 29 action = action, 30 flag = RUSTC_FLAG, 31 ) 32 33def _per_crate_rustc_flag_not_present_test(ctx): 34 env = analysistest.begin(ctx) 35 target = analysistest.target_under_test(env) 36 target_action_contains_not_flag(env, target) 37 38 return analysistest.end(env) 39 40def _per_crate_rustc_flag_present_test(ctx): 41 env = analysistest.begin(ctx) 42 target = analysistest.target_under_test(env) 43 target_action_contains_flag(env, target) 44 45 # Check the exec configuration target does NOT contain. 46 target = ctx.attr.lib_exec 47 target_action_contains_not_flag(env, target) 48 49 return analysistest.end(env) 50 51def _per_crate_rustc_flag_invalid_pattern_test(ctx): 52 env = analysistest.begin(ctx) 53 asserts.expect_failure(env, "does not follow the expected format: prefix_filter@flag") 54 55 return analysistest.end(env) 56 57per_crate_rustc_flag_not_present_test = analysistest.make(_per_crate_rustc_flag_not_present_test) 58 59per_crate_rustc_flag_not_present_non_matching_test = analysistest.make( 60 _per_crate_rustc_flag_not_present_test, 61 config_settings = { 62 str(Label("//:experimental_per_crate_rustc_flag")): [NON_MATCHING_FLAG], 63 }, 64) 65 66per_crate_rustc_flag_present_label_test = analysistest.make( 67 _per_crate_rustc_flag_present_test, 68 attrs = { 69 "lib_exec": attr.label( 70 mandatory = True, 71 cfg = "exec", 72 ), 73 }, 74 config_settings = { 75 str(Label("//:experimental_per_crate_rustc_flag")): [MATCHING_LABEL_FLAG], 76 }, 77) 78 79per_crate_rustc_flag_present_execution_path_test = analysistest.make( 80 _per_crate_rustc_flag_present_test, 81 attrs = { 82 "lib_exec": attr.label( 83 mandatory = True, 84 cfg = "exec", 85 ), 86 }, 87 config_settings = { 88 str(Label("//:experimental_per_crate_rustc_flag")): [MATCHING_EXECUTION_PATH_FLAG], 89 }, 90) 91 92per_crate_rustc_flag_invalid_pattern_test = analysistest.make( 93 _per_crate_rustc_flag_invalid_pattern_test, 94 expect_failure = True, 95 config_settings = { 96 str(Label("//:experimental_per_crate_rustc_flag")): [INVALID_FLAG], 97 }, 98) 99 100def _define_test_targets(): 101 rust_library( 102 name = "lib", 103 srcs = ["lib.rs"], 104 edition = "2018", 105 ) 106 107def per_crate_rustc_flag_test_suite(name): 108 """Entry-point macro called from the BUILD file. 109 110 Args: 111 name (str): Name of the macro. 112 """ 113 114 _define_test_targets() 115 116 per_crate_rustc_flag_not_present_test( 117 name = "per_crate_rustc_flag_not_present_test", 118 target_under_test = ":lib", 119 ) 120 121 per_crate_rustc_flag_not_present_non_matching_test( 122 name = "per_crate_rustc_flag_not_present_non_matching_test", 123 target_under_test = ":lib", 124 ) 125 126 per_crate_rustc_flag_present_label_test( 127 name = "per_crate_rustc_flag_present_label_test", 128 target_under_test = ":lib", 129 lib_exec = ":lib", 130 ) 131 132 per_crate_rustc_flag_present_execution_path_test( 133 name = "per_crate_rustc_flag_present_execution_path_test", 134 target_under_test = ":lib", 135 lib_exec = ":lib", 136 ) 137 138 per_crate_rustc_flag_invalid_pattern_test( 139 name = "per_crate_rustc_flag_invalid_pattern_test", 140 target_under_test = ":lib", 141 ) 142 143 native.test_suite( 144 name = name, 145 tests = [ 146 ":per_crate_rustc_flag_not_present_test", 147 ":per_crate_rustc_flag_not_present_non_matching_test", 148 ":per_crate_rustc_flag_present_label_test", 149 ":per_crate_rustc_flag_present_execution_path_test", 150 ":per_crate_rustc_flag_invalid_pattern_test", 151 ], 152 ) 153