Skip to content

Commit eb31218

Browse files
author
duncan
committed
rust-lang#17470 - run unit tests at the crate level not workspace
1 parent a467883 commit eb31218

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

crates/flycheck/src/test_runner.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,15 @@ impl CargoTestHandle {
6666
path: Option<&str>,
6767
options: CargoOptions,
6868
root: &AbsPath,
69+
is_workspace: bool,
6970
sender: Sender<CargoTestMessage>,
7071
) -> std::io::Result<Self> {
7172
let mut cmd = Command::new(Tool::Cargo.path());
7273
cmd.env("RUSTC_BOOTSTRAP", "1");
7374
cmd.arg("test");
74-
cmd.arg("--workspace");
75+
if is_workspace {
76+
cmd.arg("--workspace");
77+
}
7578
// --no-fail-fast is needed to ensure that all requested tests will run
7679
cmd.arg("--no-fail-fast");
7780
cmd.arg("--manifest-path");

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,20 +225,34 @@ pub(crate) fn handle_run_test(
225225
.unwrap_or_default(),
226226
None => "".to_owned(),
227227
};
228-
let test_path = if lca.is_empty() {
229-
None
230-
} else if let Some((_, path)) = lca.split_once("::") {
231-
Some(path)
228+
let (package_name, test_path) = if lca.is_empty() {
229+
(None, None)
230+
} else if let Some((package_name, path)) = lca.split_once("::") {
231+
(Some(package_name), Some(path))
232232
} else {
233-
None
233+
(None, None)
234234
};
235235
let mut handles = vec![];
236236
for ws in &*state.workspaces {
237237
if let ProjectWorkspaceKind::Cargo { cargo, .. } = &ws.kind {
238+
// If possible, scope the test to a specific package otherwise run at the workspace level
239+
let (is_workspace, test_root) = if let Some(package_name) = package_name {
240+
if let Some(package) =
241+
cargo.packages().find(|p| cargo[*p].name.replace('-', "_") == package_name)
242+
{
243+
(false, cargo[package].manifest.parent())
244+
} else {
245+
(true, cargo.workspace_root())
246+
}
247+
} else {
248+
(true, cargo.workspace_root())
249+
};
250+
238251
let handle = flycheck::CargoTestHandle::new(
239252
test_path,
240253
state.config.cargo_test_options(),
241-
cargo.workspace_root(),
254+
test_root,
255+
is_workspace,
242256
state.test_run_sender.clone(),
243257
)?;
244258
handles.push(handle);

0 commit comments

Comments
 (0)