|
1 | 1 | """Unittest to verify proc-macro targets"""
|
2 | 2 |
|
3 | 3 | load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
|
4 |
| -load("//rust:defs.bzl", "rust_proc_macro", "rust_test") |
| 4 | +load("//rust:defs.bzl", "rust_library", "rust_proc_macro", "rust_test") |
5 | 5 |
|
6 | 6 | def _proc_macro_does_not_leak_deps_impl(ctx):
|
7 | 7 | env = analysistest.begin(ctx)
|
@@ -30,8 +30,6 @@ def _proc_macro_does_not_leak_deps_impl(ctx):
|
30 | 30 |
|
31 | 31 | return analysistest.end(env)
|
32 | 32 |
|
33 |
| -proc_macro_does_not_leak_deps_test = analysistest.make(_proc_macro_does_not_leak_deps_impl) |
34 |
| - |
35 | 33 | def _proc_macro_does_not_leak_deps_test():
|
36 | 34 | rust_proc_macro(
|
37 | 35 | name = "proc_macro_definition",
|
@@ -68,17 +66,83 @@ def _proc_macro_does_not_leak_deps_test():
|
68 | 66 | target_under_test = ":deps_not_leaked",
|
69 | 67 | )
|
70 | 68 |
|
| 69 | +proc_macro_does_not_leak_deps_test = analysistest.make(_proc_macro_does_not_leak_deps_impl) |
| 70 | + |
| 71 | +# Tests that a lib_a -> proc_macro -> lib_b does not propagate lib_b to the inputs of lib_a |
| 72 | +def _proc_macro_does_not_leak_lib_deps_impl(ctx): |
| 73 | + env = analysistest.begin(ctx) |
| 74 | + actions = analysistest.target_under_test(env).actions |
| 75 | + rustc_actions = [] |
| 76 | + for action in actions: |
| 77 | + if action.mnemonic == "Rustc" or action.mnemonic == "RustcMetadata": |
| 78 | + rustc_actions.append(action) |
| 79 | + |
| 80 | + # We should have a RustcMetadata and a Rustc action. |
| 81 | + asserts.true(env, len(rustc_actions) == 2, "expected 2 actions, got %d" % len(rustc_actions)) |
| 82 | + |
| 83 | + for rustc_action in rustc_actions: |
| 84 | + # lib :a has a dependency on :my_macro via a rust_proc_macro target. |
| 85 | + # lib :b (which is a dependency of :my_macro) should not appear in the inputs of :a |
| 86 | + b_inputs = [i for i in rustc_action.inputs.to_list() if "libb" in i.path] |
| 87 | + b_args = [arg for arg in rustc_action.argv if "libb" in arg] |
| 88 | + |
| 89 | + asserts.equals(env, 0, len(b_inputs)) |
| 90 | + asserts.equals(env, 0, len(b_args)) |
| 91 | + |
| 92 | + return analysistest.end(env) |
| 93 | + |
| 94 | +def _proc_macro_does_not_leak_lib_deps_test(): |
| 95 | + rust_library( |
| 96 | + name = "b", |
| 97 | + srcs = ["leaks_deps/lib/b.rs"], |
| 98 | + edition = "2018", |
| 99 | + ) |
| 100 | + |
| 101 | + rust_proc_macro( |
| 102 | + name = "my_macro", |
| 103 | + srcs = ["leaks_deps/lib/my_macro.rs"], |
| 104 | + edition = "2018", |
| 105 | + deps = [ |
| 106 | + ":b", |
| 107 | + ], |
| 108 | + ) |
| 109 | + |
| 110 | + rust_library( |
| 111 | + name = "a", |
| 112 | + srcs = ["leaks_deps/lib/a.rs"], |
| 113 | + edition = "2018", |
| 114 | + proc_macro_deps = [ |
| 115 | + ":my_macro", |
| 116 | + ], |
| 117 | + ) |
| 118 | + |
| 119 | + NOT_WINDOWS = select({ |
| 120 | + "@platforms//os:linux": [], |
| 121 | + "@platforms//os:macos": [], |
| 122 | + "//conditions:default": ["@platforms//:incompatible"], |
| 123 | + }) |
| 124 | + |
| 125 | + proc_macro_does_not_leak_lib_deps_test( |
| 126 | + name = "proc_macro_does_not_leak_lib_deps_test", |
| 127 | + target_under_test = ":a", |
| 128 | + target_compatible_with = NOT_WINDOWS, |
| 129 | + ) |
| 130 | + |
| 131 | +proc_macro_does_not_leak_lib_deps_test = analysistest.make(_proc_macro_does_not_leak_lib_deps_impl, config_settings = {"@//rust/settings:pipelined_compilation": True}) |
| 132 | + |
71 | 133 | def proc_macro_does_not_leak_deps_test_suite(name):
|
72 | 134 | """Entry-point macro called from the BUILD file.
|
73 | 135 |
|
74 | 136 | Args:
|
75 | 137 | name: Name of the macro.
|
76 | 138 | """
|
77 | 139 | _proc_macro_does_not_leak_deps_test()
|
| 140 | + _proc_macro_does_not_leak_lib_deps_test() |
78 | 141 |
|
79 | 142 | native.test_suite(
|
80 | 143 | name = name,
|
81 | 144 | tests = [
|
82 | 145 | ":proc_macro_does_not_leak_deps_test",
|
| 146 | + ":proc_macro_does_not_leak_lib_deps_test", |
83 | 147 | ],
|
84 | 148 | )
|
0 commit comments