Skip to content

Commit 4d39d98

Browse files
author
duncan
committed
#17470 - run unit tests at the crate level not workspace
1 parent 232e555 commit 4d39d98

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

crates/flycheck/src/test_runner.rs

+4-1
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

+20-6
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,34 @@ pub(crate) fn handle_run_test(
218218
.unwrap_or_default(),
219219
None => "".to_owned(),
220220
};
221-
let test_path = if lca.is_empty() {
222-
None
223-
} else if let Some((_, path)) = lca.split_once("::") {
224-
Some(path)
221+
let (package_name, test_path) = if lca.is_empty() {
222+
(None, None)
223+
} else if let Some((package_name, path)) = lca.split_once("::") {
224+
(Some(package_name), Some(path))
225225
} else {
226-
None
226+
(None, None)
227227
};
228228
let mut handles = vec![];
229229
for ws in &*state.workspaces {
230230
if let ProjectWorkspaceKind::Cargo { cargo, .. } = &ws.kind {
231+
// If possible, scope the test to a specific package otherwise run at the workspace level
232+
let (is_workspace, test_root) = if let Some(package_name) = package_name {
233+
if let Some(package) =
234+
cargo.packages().find(|p| cargo[*p].name.replace('-', "_") == package_name)
235+
{
236+
(false, cargo[package].manifest.parent())
237+
} else {
238+
(true, cargo.workspace_root())
239+
}
240+
} else {
241+
(true, cargo.workspace_root())
242+
};
243+
231244
let handle = flycheck::CargoTestHandle::new(
232245
test_path,
233246
state.config.cargo_test_options(),
234-
cargo.workspace_root(),
247+
test_root,
248+
is_workspace,
235249
state.test_run_sender.clone(),
236250
)?;
237251
handles.push(handle);

0 commit comments

Comments
 (0)