Skip to content

Commit 1fb9a13

Browse files
committed
Implement leak test for libs
1 parent bb7e9b0 commit 1fb9a13

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use my_macro::greet;
2+
3+
pub fn use_macro() -> &'static str {
4+
greet!()
5+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn hello() -> &'static str {
2+
"hello"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use b::hello;
2+
use proc_macro::{Literal, TokenStream, TokenTree};
3+
4+
#[proc_macro]
5+
pub fn greet(_item: TokenStream) -> TokenStream {
6+
TokenTree::Literal(Literal::string(hello())).into()
7+
}

test/unit/proc_macro/leaks_deps/proc_macro_does_not_leak_deps.bzl

+67-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Unittest to verify proc-macro targets"""
22

33
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")
55

66
def _proc_macro_does_not_leak_deps_impl(ctx):
77
env = analysistest.begin(ctx)
@@ -30,8 +30,6 @@ def _proc_macro_does_not_leak_deps_impl(ctx):
3030

3131
return analysistest.end(env)
3232

33-
proc_macro_does_not_leak_deps_test = analysistest.make(_proc_macro_does_not_leak_deps_impl)
34-
3533
def _proc_macro_does_not_leak_deps_test():
3634
rust_proc_macro(
3735
name = "proc_macro_definition",
@@ -68,17 +66,83 @@ def _proc_macro_does_not_leak_deps_test():
6866
target_under_test = ":deps_not_leaked",
6967
)
7068

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+
71133
def proc_macro_does_not_leak_deps_test_suite(name):
72134
"""Entry-point macro called from the BUILD file.
73135
74136
Args:
75137
name: Name of the macro.
76138
"""
77139
_proc_macro_does_not_leak_deps_test()
140+
_proc_macro_does_not_leak_lib_deps_test()
78141

79142
native.test_suite(
80143
name = name,
81144
tests = [
82145
":proc_macro_does_not_leak_deps_test",
146+
":proc_macro_does_not_leak_lib_deps_test",
83147
],
84148
)

0 commit comments

Comments
 (0)