Skip to content

Commit 56f2ec7

Browse files
committed
Auto merge of #5075 - ehuss:report-test-ws, r=matklad
Fix test error hint in a workspace. This adds "-p NAME" to the hint on how to re-run a test when it fails if it is inside a workspace. Fixes #5053
2 parents ed8dfce + c61efd8 commit 56f2ec7

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

src/bin/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ pub fn execute(mut options: Options, config: &mut Config) -> CliResult {
186186
None => Ok(()),
187187
Some(err) => {
188188
Err(match err.exit.as_ref().and_then(|e| e.code()) {
189-
Some(i) => CliError::new(format_err!("{}", err.hint()), i),
189+
Some(i) => CliError::new(format_err!("{}", err.hint(&ws)), i),
190190
None => CliError::new(err.into(), 101),
191191
})
192192
}

src/cargo/ops/cargo_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn run_unit_tests(options: &TestOptions,
107107
match result {
108108
Err(e) => {
109109
let e = e.downcast::<ProcessError>()?;
110-
errors.push((kind.clone(), test.clone(), e));
110+
errors.push((kind.clone(), test.clone(), pkg.name().to_string(), e));
111111
if !options.no_fail_fast {
112112
break;
113113
}
@@ -117,10 +117,10 @@ fn run_unit_tests(options: &TestOptions,
117117
}
118118

119119
if errors.len() == 1 {
120-
let (kind, test, e) = errors.pop().unwrap();
121-
Ok((Test::UnitTest(kind, test), vec![e]))
120+
let (kind, name, pkg_name, e) = errors.pop().unwrap();
121+
Ok((Test::UnitTest{kind, name, pkg_name}, vec![e]))
122122
} else {
123-
Ok((Test::Multiple, errors.into_iter().map(|(_, _, e)| e).collect()))
123+
Ok((Test::Multiple, errors.into_iter().map(|(_, _, _, e)| e).collect()))
124124
}
125125
}
126126

src/cargo/util/errors.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fmt;
44
use std::process::{Output, ExitStatus};
55
use std::str;
66

7-
use core::TargetKind;
7+
use core::{TargetKind, Workspace};
88
use failure::{Context, Error, Fail};
99

1010
pub use failure::Error as CargoError;
@@ -92,7 +92,7 @@ pub struct CargoTestError {
9292
pub enum Test {
9393
Multiple,
9494
Doc,
95-
UnitTest(TargetKind, String)
95+
UnitTest{kind: TargetKind, name: String, pkg_name: String}
9696
}
9797

9898
impl CargoTestError {
@@ -111,16 +111,26 @@ impl CargoTestError {
111111
}
112112
}
113113

114-
pub fn hint(&self) -> String {
114+
pub fn hint(&self, ws: &Workspace) -> String {
115115
match self.test {
116-
Test::UnitTest(ref kind, ref name) => {
116+
Test::UnitTest{ref kind, ref name, ref pkg_name} => {
117+
let pkg_info = if ws.members().count() > 1 && ws.is_virtual() {
118+
format!("-p {} ", pkg_name)
119+
} else {
120+
String::new()
121+
};
122+
117123
match *kind {
118-
TargetKind::Bench => format!("test failed, to rerun pass '--bench {}'", name),
119-
TargetKind::Bin => format!("test failed, to rerun pass '--bin {}'", name),
120-
TargetKind::Lib(_) => "test failed, to rerun pass '--lib'".into(),
121-
TargetKind::Test => format!("test failed, to rerun pass '--test {}'", name),
124+
TargetKind::Bench =>
125+
format!("test failed, to rerun pass '{}--bench {}'", pkg_info, name),
126+
TargetKind::Bin =>
127+
format!("test failed, to rerun pass '{}--bin {}'", pkg_info, name),
128+
TargetKind::Lib(_) =>
129+
format!("test failed, to rerun pass '{}--lib'", pkg_info),
130+
TargetKind::Test =>
131+
format!("test failed, to rerun pass '{}--test {}'", pkg_info, name),
122132
TargetKind::ExampleBin | TargetKind::ExampleLib(_) =>
123-
format!("test failed, to rerun pass '--example {}", name),
133+
format!("test failed, to rerun pass '{}--example {}", pkg_info, name),
124134
_ => "test failed.".into()
125135
}
126136
},

tests/testsuite/test.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,3 +2936,36 @@ fn test_hint_not_masked_by_doctest() {
29362936
.with_stderr_contains("[ERROR] test failed, to rerun pass \
29372937
'--test integ'"));
29382938
}
2939+
2940+
#[test]
2941+
fn test_hint_workspace() {
2942+
let workspace = project("workspace")
2943+
.file("Cargo.toml", r#"
2944+
[workspace]
2945+
members = ["a", "b"]
2946+
"#)
2947+
.file("a/Cargo.toml", r#"
2948+
[project]
2949+
name = "a"
2950+
version = "0.1.0"
2951+
"#)
2952+
.file("a/src/lib.rs", r#"
2953+
#[test]
2954+
fn t1() {}
2955+
"#)
2956+
.file("b/Cargo.toml", r#"
2957+
[project]
2958+
name = "b"
2959+
version = "0.1.0"
2960+
"#)
2961+
.file("b/src/lib.rs", r#"
2962+
#[test]
2963+
fn t1() {assert!(false)}
2964+
"#)
2965+
.build();
2966+
2967+
assert_that(workspace.cargo("test"),
2968+
execs().with_stderr_contains(
2969+
"[ERROR] test failed, to rerun pass '-p b --lib'")
2970+
.with_status(101));
2971+
}

0 commit comments

Comments
 (0)