Skip to content

fix(test): Distinguish 'testname' from escaped arguments #11190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn cli() -> Command {
Arg::new("args")
.help("Arguments for the bench binary")
.num_args(0..)
.trailing_var_arg(true),
.last(true),
)
.arg_targets_all(
"Benchmark only this package's library",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn cli() -> Command {
Arg::new("args")
.help("Arguments for the test binary")
.num_args(0..)
.trailing_var_arg(true),
.last(true),
)
.arg(
flag(
Expand Down
110 changes: 105 additions & 5 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,13 +725,101 @@ fn dont_run_examples() {
}

#[cargo_test]
fn pass_through_command_line() {
fn pass_through_escaped() {
let p = project()
.file(
"src/lib.rs",
"
#[test] fn foo() {}
#[test] fn bar() {}
/// ```rust
/// assert!(foo::foo());
/// ```
pub fn foo() -> bool {
true
}

/// ```rust
/// assert!(!foo::bar());
/// ```
pub fn bar() -> bool {
false
}

#[test] fn test_foo() {
assert!(foo());
}
#[test] fn test_bar() {
assert!(!bar());
}
",
)
.build();

p.cargo("test -- bar")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[RUNNING] [..] (target/debug/deps/foo-[..][EXE])
[DOCTEST] foo
",
)
.with_stdout_contains("running 1 test")
.with_stdout_contains("test test_bar ... ok")
.run();

p.cargo("test -- foo")
.with_stderr(
"\
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[RUNNING] [..] (target/debug/deps/foo-[..][EXE])
[DOCTEST] foo
",
)
.with_stdout_contains("running 1 test")
.with_stdout_contains("test test_foo ... ok")
.run();

p.cargo("test -- foo bar")
.with_stderr(
"\
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[RUNNING] [..] (target/debug/deps/foo-[..][EXE])
[DOCTEST] foo
",
)
.with_stdout_contains("running 2 tests")
.with_stdout_contains("test test_foo ... ok")
.with_stdout_contains("test test_bar ... ok")
.run();
}

// Unlike `pass_through_escaped`, doctests won't run when using `testname` as an optimization
#[cargo_test]
fn pass_through_testname() {
let p = project()
.file(
"src/lib.rs",
"
/// ```rust
/// assert!(foo::foo());
/// ```
pub fn foo() -> bool {
true
}

/// ```rust
/// assert!(!foo::bar());
/// ```
pub fn bar() -> bool {
false
}

#[test] fn test_foo() {
assert!(foo());
}
#[test] fn test_bar() {
assert!(!bar());
}
",
)
.build();
Expand All @@ -745,7 +833,7 @@ fn pass_through_command_line() {
",
)
.with_stdout_contains("running 1 test")
.with_stdout_contains("test bar ... ok")
.with_stdout_contains("test test_bar ... ok")
.run();

p.cargo("test foo")
Expand All @@ -756,7 +844,19 @@ fn pass_through_command_line() {
",
)
.with_stdout_contains("running 1 test")
.with_stdout_contains("test foo ... ok")
.with_stdout_contains("test test_foo ... ok")
.run();

p.cargo("test foo -- bar")
.with_stderr(
"\
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[RUNNING] [..] (target/debug/deps/foo-[..][EXE])
",
)
.with_stdout_contains("running 2 tests")
.with_stdout_contains("test test_foo ... ok")
.with_stdout_contains("test test_bar ... ok")
.run();
}

Expand Down