Skip to content

Commit 44c535a

Browse files
committed
Improve error message to rerun a test in a workspace.
1 parent b08e4cb commit 44c535a

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

src/bin/cargo/commands/test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
150150
match err {
151151
None => Ok(()),
152152
Some(err) => Err(match err.exit.as_ref().and_then(|e| e.code()) {
153-
Some(i) => CliError::new(failure::format_err!("{}", err.hint(&ws)), i),
153+
Some(i) => CliError::new(
154+
failure::format_err!("{}", err.hint(&ws, &ops.compile_opts)),
155+
i,
156+
),
154157
None => CliError::new(err.into(), 101),
155158
}),
156159
}

src/cargo/ops/cargo_compile.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ impl Packages {
182182
};
183183
Ok(packages)
184184
}
185+
186+
/// Returns whether or not the user needs to pass a `-p` flag to target a
187+
/// specific package in the workspace.
188+
pub fn needs_spec_flag(&self, ws: &Workspace<'_>) -> bool {
189+
match self {
190+
Packages::Default => ws.default_members().count() > 1,
191+
Packages::All => ws.members().count() > 1,
192+
Packages::Packages(_) => true,
193+
Packages::OptOut(_) => true,
194+
}
195+
}
185196
}
186197

187198
#[derive(Debug, PartialEq, Eq)]

src/cargo/util/errors.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use failure::{Context, Error, Fail};
1010
use log::trace;
1111

1212
use crate::core::{TargetKind, Workspace};
13+
use crate::ops::CompileOptions;
1314

1415
pub type CargoResult<T> = failure::Fallible<T>; // Alex's body isn't quite ready to give up "Result"
1516

@@ -188,14 +189,14 @@ impl CargoTestError {
188189
}
189190
}
190191

191-
pub fn hint(&self, ws: &Workspace<'_>) -> String {
192+
pub fn hint(&self, ws: &Workspace<'_>, opts: &CompileOptions<'_>) -> String {
192193
match self.test {
193194
Test::UnitTest {
194195
ref kind,
195196
ref name,
196197
ref pkg_name,
197198
} => {
198-
let pkg_info = if ws.members().count() > 1 && ws.is_virtual() {
199+
let pkg_info = if opts.spec.needs_spec_flag(ws) {
199200
format!("-p {} ", pkg_name)
200201
} else {
201202
String::new()

tests/testsuite/test.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3270,7 +3270,7 @@ fn test_hint_not_masked_by_doctest() {
32703270
}
32713271

32723272
#[test]
3273-
fn test_hint_workspace() {
3273+
fn test_hint_workspace_virtual() {
32743274
let p = project()
32753275
.file(
32763276
"Cargo.toml",
@@ -3289,6 +3289,40 @@ fn test_hint_workspace() {
32893289
.with_stderr_contains("[ERROR] test failed, to rerun pass '-p b --lib'")
32903290
.with_status(101)
32913291
.run();
3292+
p.cargo("test")
3293+
.cwd("b")
3294+
.with_stderr_contains("[ERROR] test failed, to rerun pass '--lib'")
3295+
.with_status(101)
3296+
.run();
3297+
}
3298+
3299+
#[test]
3300+
fn test_hint_workspace_nonvirtual() {
3301+
let p = project()
3302+
.file(
3303+
"Cargo.toml",
3304+
r#"
3305+
[package]
3306+
name = "foo"
3307+
version = "0.1.0"
3308+
3309+
[workspace]
3310+
members = ["a"]
3311+
"#,
3312+
)
3313+
.file("src/lib.rs", "")
3314+
.file("a/Cargo.toml", &basic_manifest("a", "0.1.0"))
3315+
.file("a/src/lib.rs", "#[test] fn t1() {assert!(false)}")
3316+
.build();
3317+
3318+
p.cargo("test --all")
3319+
.with_stderr_contains("[ERROR] test failed, to rerun pass '-p a --lib'")
3320+
.with_status(101)
3321+
.run();
3322+
p.cargo("test -p a")
3323+
.with_stderr_contains("[ERROR] test failed, to rerun pass '-p a --lib'")
3324+
.with_status(101)
3325+
.run();
32923326
}
32933327

32943328
#[test]

0 commit comments

Comments
 (0)