Skip to content

Commit 3cdf1ab

Browse files
committed
Auto merge of #11190 - epage:test, r=weihanglo
fix(test): Distinguish 'testname' from escaped arguments When working on clap v4, it appeared that `last` and `trailing_var_arg` are mutually exclusive, so I called that out in the debug asserts in #4187. Unfortunately, I didn't document my research on this as my focus was elsewhere. When updating cargo to use clap v4 in #11159, I found `last` and `trailing_var_arg` were used together. I figured this was what I was trying to catch with above and I went off of my intuitive sense of these commands and assumed `trailing_var_arg` was intended, not knowing about the `testname` positional that is mostly just a forward to `libtest`, there for documentation purposes, except for a small optimization. So it looks like we just need the `last` call and not the `trailing_var_arg` call. This restores us to behavior from 531ce13 which is what I originally wrote the tests against. It looks like #11159 was merged after the last beta branch was made, so we shouldn't need to cherry-pick this into any other release. For reviewing this, I made the test updates in the first commit, showing the wrong behavior. The following commit fixes the behavior and updates the tests to expected behavior. Fixes #11191
2 parents aff3bb8 + 3dd8413 commit 3cdf1ab

File tree

3 files changed

+107
-7
lines changed

3 files changed

+107
-7
lines changed

src/bin/cargo/commands/bench.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn cli() -> Command {
1414
Arg::new("args")
1515
.help("Arguments for the bench binary")
1616
.num_args(0..)
17-
.trailing_var_arg(true),
17+
.last(true),
1818
)
1919
.arg_targets_all(
2020
"Benchmark only this package's library",

src/bin/cargo/commands/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn cli() -> Command {
1515
Arg::new("args")
1616
.help("Arguments for the test binary")
1717
.num_args(0..)
18-
.trailing_var_arg(true),
18+
.last(true),
1919
)
2020
.arg(
2121
flag(

tests/testsuite/test.rs

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -725,13 +725,101 @@ fn dont_run_examples() {
725725
}
726726

727727
#[cargo_test]
728-
fn pass_through_command_line() {
728+
fn pass_through_escaped() {
729729
let p = project()
730730
.file(
731731
"src/lib.rs",
732732
"
733-
#[test] fn foo() {}
734-
#[test] fn bar() {}
733+
/// ```rust
734+
/// assert!(foo::foo());
735+
/// ```
736+
pub fn foo() -> bool {
737+
true
738+
}
739+
740+
/// ```rust
741+
/// assert!(!foo::bar());
742+
/// ```
743+
pub fn bar() -> bool {
744+
false
745+
}
746+
747+
#[test] fn test_foo() {
748+
assert!(foo());
749+
}
750+
#[test] fn test_bar() {
751+
assert!(!bar());
752+
}
753+
",
754+
)
755+
.build();
756+
757+
p.cargo("test -- bar")
758+
.with_stderr(
759+
"\
760+
[COMPILING] foo v0.0.1 ([CWD])
761+
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
762+
[RUNNING] [..] (target/debug/deps/foo-[..][EXE])
763+
[DOCTEST] foo
764+
",
765+
)
766+
.with_stdout_contains("running 1 test")
767+
.with_stdout_contains("test test_bar ... ok")
768+
.run();
769+
770+
p.cargo("test -- foo")
771+
.with_stderr(
772+
"\
773+
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
774+
[RUNNING] [..] (target/debug/deps/foo-[..][EXE])
775+
[DOCTEST] foo
776+
",
777+
)
778+
.with_stdout_contains("running 1 test")
779+
.with_stdout_contains("test test_foo ... ok")
780+
.run();
781+
782+
p.cargo("test -- foo bar")
783+
.with_stderr(
784+
"\
785+
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
786+
[RUNNING] [..] (target/debug/deps/foo-[..][EXE])
787+
[DOCTEST] foo
788+
",
789+
)
790+
.with_stdout_contains("running 2 tests")
791+
.with_stdout_contains("test test_foo ... ok")
792+
.with_stdout_contains("test test_bar ... ok")
793+
.run();
794+
}
795+
796+
// Unlike `pass_through_escaped`, doctests won't run when using `testname` as an optimization
797+
#[cargo_test]
798+
fn pass_through_testname() {
799+
let p = project()
800+
.file(
801+
"src/lib.rs",
802+
"
803+
/// ```rust
804+
/// assert!(foo::foo());
805+
/// ```
806+
pub fn foo() -> bool {
807+
true
808+
}
809+
810+
/// ```rust
811+
/// assert!(!foo::bar());
812+
/// ```
813+
pub fn bar() -> bool {
814+
false
815+
}
816+
817+
#[test] fn test_foo() {
818+
assert!(foo());
819+
}
820+
#[test] fn test_bar() {
821+
assert!(!bar());
822+
}
735823
",
736824
)
737825
.build();
@@ -745,7 +833,7 @@ fn pass_through_command_line() {
745833
",
746834
)
747835
.with_stdout_contains("running 1 test")
748-
.with_stdout_contains("test bar ... ok")
836+
.with_stdout_contains("test test_bar ... ok")
749837
.run();
750838

751839
p.cargo("test foo")
@@ -756,7 +844,19 @@ fn pass_through_command_line() {
756844
",
757845
)
758846
.with_stdout_contains("running 1 test")
759-
.with_stdout_contains("test foo ... ok")
847+
.with_stdout_contains("test test_foo ... ok")
848+
.run();
849+
850+
p.cargo("test foo -- bar")
851+
.with_stderr(
852+
"\
853+
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
854+
[RUNNING] [..] (target/debug/deps/foo-[..][EXE])
855+
",
856+
)
857+
.with_stdout_contains("running 2 tests")
858+
.with_stdout_contains("test test_foo ... ok")
859+
.with_stdout_contains("test test_bar ... ok")
760860
.run();
761861
}
762862

0 commit comments

Comments
 (0)