Skip to content

Commit e03b4c9

Browse files
Calsignjason-rl
authored andcommitted
Merge pull request Calsign#41 from Calsign/calsignlabs/revup/main/test-only-modules
Handle discovery of test-only modules correctly
1 parent b72d724 commit e03b4c9

20 files changed

Lines changed: 232 additions & 70 deletions

File tree

gazelle_rust_parser/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "gazelle_rust_parser"
33
version = "0.1.0"
4-
edition = "2021"
4+
edition = "2024"
55
license = "Apache-2.0"
66
description = "Parse and extract imports from rust sources, used by the gazelle_rust build file generator"
77
homepage = "https://github.com/Calsign/gazelle_rust/tree/main/gazelle_rust_parser"

gazelle_rust_parser/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct RustImports {
1717
pub imports: Vec<String>,
1818
pub test_imports: Vec<String>,
1919
pub extern_mods: Vec<String>,
20+
pub test_extern_mods: Vec<String>,
2021
pub compile_data: Vec<String>,
2122
}
2223

@@ -75,6 +76,7 @@ pub fn parse_imports_from_str(
7576
imports: filter_imports(root_scope.imports),
7677
test_imports: filter_imports(root_scope.test_imports),
7778
extern_mods: visitor.extern_mods.into_iter().collect(),
79+
test_extern_mods: visitor.test_extern_mods.into_iter().collect(),
7880
compile_data: visitor.compile_data.into_iter().collect(),
7981
})
8082
}
@@ -205,6 +207,8 @@ struct AstVisitor<'ast> {
205207
hints: Hints,
206208
/// bare mods defined in external files
207209
extern_mods: HashSet<String>,
210+
/// bare mods defined in external files that are declared behind `#[cfg(test)]`
211+
test_extern_mods: HashSet<String>,
208212
/// mods that are disallowed from being added to the current scope; this is currently only used
209213
/// for a hack, see below
210214
mod_denylist: HashSet<Ident<'ast>>,
@@ -229,6 +233,7 @@ impl AstVisitor<'_> {
229233
scope_mods: HashSet::default(),
230234
hints: Hints::default(),
231235
extern_mods: HashSet::new(),
236+
test_extern_mods: HashSet::new(),
232237
mod_denylist: HashSet::new(),
233238
enabled_features: enabled_features.iter().cloned().collect(),
234239
compile_data: HashSet::new(),
@@ -562,6 +567,7 @@ impl<'ast> AstVisitor<'ast> {
562567
self.mod_denylist.insert(id_copy);
563568
}
564569
self.extern_mods = other.extern_mods;
570+
self.test_extern_mods = other.test_extern_mods;
565571
self.compile_data = other.compile_data;
566572
self.enabled_features = other.enabled_features;
567573
self.hints.has_main = other.hints.has_main;
@@ -725,8 +731,14 @@ impl<'ast> Visit<'ast> for AstVisitor<'ast> {
725731
}
726732

727733
if self.is_root_scope() && node.content.is_none() {
734+
let extern_mod = node.ident.unraw().to_string();
735+
728736
// this mod is defined in a different file
729-
self.extern_mods.insert(node.ident.unraw().to_string());
737+
if is_test_only {
738+
self.test_extern_mods.insert(extern_mod);
739+
} else {
740+
self.extern_mods.insert(extern_mod);
741+
}
730742
}
731743

732744
self.add_mod(&node.ident);

gazelle_rust_parser/test_data/test_only.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ fn foobar_test(arg: f::X) {
1313
use a;
1414
}
1515

16+
#[cfg(test)]
17+
mod test_extern_mod;
18+
1619
#[cfg(test)]
1720
mod tests {
1821
use c;

gazelle_rust_parser/tests/parse_test.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct TestCase {
88
expected_imports: Vec<&'static str>,
99
expected_test_imports: Vec<&'static str>,
1010
expected_extern_mods: Vec<&'static str>,
11+
expected_test_extern_mods: Vec<&'static str>,
1112
expected_compile_data: Vec<&'static str>,
1213
}
1314

@@ -46,6 +47,7 @@ lazy_static::lazy_static! {
4647
],
4748
expected_test_imports: vec![],
4849
expected_extern_mods: vec!["extern_mod"],
50+
expected_test_extern_mods: vec![],
4951
expected_compile_data: vec![
5052
"file1.txt",
5153
"file2.txt",
@@ -66,6 +68,7 @@ lazy_static::lazy_static! {
6668
"f",
6769
],
6870
expected_extern_mods: vec![],
71+
expected_test_extern_mods: vec!["test_extern_mod"],
6972
expected_compile_data: vec![],
7073
},
7174
TestCase {
@@ -74,6 +77,7 @@ lazy_static::lazy_static! {
7477
expected_imports: vec!["ee"],
7578
expected_test_imports: vec![],
7679
expected_extern_mods: vec![],
80+
expected_test_extern_mods: vec![],
7781
expected_compile_data: vec![],
7882
},
7983
TestCase {
@@ -90,6 +94,7 @@ lazy_static::lazy_static! {
9094
expected_extern_mods: vec![
9195
"extern_mod_2",
9296
],
97+
expected_test_extern_mods: vec![],
9398
expected_compile_data: vec![],
9499
},
95100
TestCase {
@@ -105,6 +110,7 @@ lazy_static::lazy_static! {
105110
],
106111
expected_test_imports: vec![],
107112
expected_extern_mods: vec![],
113+
expected_test_extern_mods: vec![],
108114
expected_compile_data: vec![
109115
"file1.txt",
110116
"file2.txt",
@@ -134,6 +140,7 @@ lazy_static::lazy_static! {
134140
"async_std_timeout_dep",
135141
],
136142
expected_extern_mods: vec![],
143+
expected_test_extern_mods: vec![],
137144
expected_compile_data: vec![],
138145
},
139146
TestCase {
@@ -142,6 +149,7 @@ lazy_static::lazy_static! {
142149
expected_imports: vec!["gazelle"],
143150
expected_test_imports: vec![],
144151
expected_extern_mods: vec![],
152+
expected_test_extern_mods: vec![],
145153
expected_compile_data: vec![],
146154
},
147155
];
@@ -224,6 +232,15 @@ fn parse_test() -> Result<(), Box<dyn Error>> {
224232
.collect::<Vec<_>>(),
225233
"extern_modes",
226234
);
235+
assert_eq_vecs(
236+
&rust_imports.test_extern_mods,
237+
&test_case
238+
.expected_test_extern_mods
239+
.iter()
240+
.map(|s| s.to_string())
241+
.collect::<Vec<_>>(),
242+
"test_extern_mods",
243+
);
227244
assert_eq_vecs(
228245
&rust_imports.compile_data,
229246
&test_case
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:rust_mode generate_from_cargo
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:rust_mode generate_from_cargo
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[workspace]
2+
3+
members = [
4+
"helper",
5+
"subject",
6+
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
Test that a module declared #[cfg(test)] contributes its imports to the crate test target
3+
rather than to the library.

generation_tests/cargo/test_only_modules/WORKSPACE

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_library")
2+
3+
rust_library(
4+
name = "helper",
5+
srcs = ["src/lib.rs"],
6+
compile_data = ["Cargo.toml"],
7+
edition = "2021",
8+
visibility = ["//visibility:public"],
9+
)

0 commit comments

Comments
 (0)