Skip to content

Commit d51ecf8

Browse files
author
duncan
committed
exclude build script targets from testing
1 parent 6d50737 commit d51ecf8

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -195,46 +195,35 @@ pub(crate) fn handle_view_item_tree(
195195
}
196196

197197
// cargo test requires:
198+
// - the package is a member of the workspace
199+
// - the target in the package is not a build script (custom-build)
198200
// - the package name - the root of the test identifier supplied to this handler can be
199201
// a package or a target inside a package.
200202
// - the target name - if the test identifier is a target, it's needed in addition to the
201203
// package name to run the right test
202204
// - real names - the test identifier uses the namespace form where hyphens are replaced with
203205
// underscores. cargo test requires the real name.
204206
// - the target kind e.g. bin or lib
205-
fn find_test_target(namespace_root: &str, cargo: &CargoWorkspace) -> Option<TestTarget> {
206-
cargo.packages().filter(|p| cargo[*p].is_member).find_map(|p| {
207-
let package_name = &cargo[p].name;
208-
for target in cargo[p].targets.iter() {
209-
let target_name = &cargo[*target].name;
210-
if target_name.replace('-', "_") == namespace_root {
211-
return Some(TestTarget {
212-
package: package_name.clone(),
213-
target: target_name.clone(),
214-
kind: cargo[*target].kind,
215-
});
207+
fn all_test_targets(cargo: &CargoWorkspace) -> impl Iterator<Item = TestTarget> {
208+
cargo.packages().filter(|p| cargo[*p].is_member).flat_map(|p| {
209+
let package = &cargo[p];
210+
package.targets.iter().filter_map(|t| {
211+
let target = &cargo[*t];
212+
if target.kind == TargetKind::BuildScript {
213+
None
214+
} else {
215+
Some(TestTarget {
216+
package: package.name.clone(),
217+
target: target.name.clone(),
218+
kind: target.kind,
219+
})
216220
}
217-
}
218-
None
221+
})
219222
})
220223
}
221224

222-
fn get_all_targets(cargo: &CargoWorkspace) -> Vec<TestTarget> {
223-
cargo
224-
.packages()
225-
.filter(|p| cargo[*p].is_member)
226-
.flat_map(|p| {
227-
let package_name = &cargo[p].name;
228-
cargo[p].targets.iter().map(|target| {
229-
let target_name = &cargo[*target].name;
230-
TestTarget {
231-
package: package_name.clone(),
232-
target: target_name.clone(),
233-
kind: cargo[*target].kind,
234-
}
235-
})
236-
})
237-
.collect()
225+
fn find_test_target(namespace_root: &str, cargo: &CargoWorkspace) -> Option<TestTarget> {
226+
all_test_targets(cargo).find(|t| namespace_root == t.target.replace('-', "_"))
238227
}
239228

240229
pub(crate) fn handle_run_test(
@@ -266,7 +255,7 @@ pub(crate) fn handle_run_test(
266255
}
267256
})
268257
.collect_vec(),
269-
None => get_all_targets(cargo).into_iter().map(|target| (target, None)).collect(),
258+
None => all_test_targets(cargo).into_iter().map(|target| (target, None)).collect(),
270259
};
271260

272261
for (target, path) in tests {

0 commit comments

Comments
 (0)