Skip to content

Commit e5971b9

Browse files
committed
Slightly improve ergonomics of writing Cargo tests
1 parent 8cc8f3b commit e5971b9

File tree

6 files changed

+20
-94
lines changed

6 files changed

+20
-94
lines changed

tests/testsuite/build.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3705,16 +3705,7 @@ fn build_multiple_packages() {
37053705
.file("d2/src/main.rs", "fn main() { println!(\"d2\"); }")
37063706
.build();
37073707

3708-
assert_that(
3709-
p.cargo("build")
3710-
.arg("-p")
3711-
.arg("d1")
3712-
.arg("-p")
3713-
.arg("d2")
3714-
.arg("-p")
3715-
.arg("foo"),
3716-
execs().with_status(0),
3717-
);
3708+
assert_that(p.cargo("build -p d1 -p d2 -p foo"), execs().with_status(0));
37183709

37193710
assert_that(&p.bin("foo"), existing_file());
37203711
assert_that(

tests/testsuite/cargotest/support/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,12 @@ impl Project {
233233

234234
pub fn cargo(&self, cmd: &str) -> ProcessBuilder {
235235
let mut p = self.process(&cargo_exe());
236-
p.arg(cmd);
236+
for arg in cmd.split_whitespace() {
237+
if arg.contains('"') || arg.contains('\'') {
238+
panic!("shell-style argument parsing is not supported")
239+
}
240+
p.arg(arg);
241+
}
237242
return p;
238243
}
239244

tests/testsuite/clean.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,7 @@ fn clean_multiple_packages() {
8585
.file("d2/src/main.rs", "fn main() { println!(\"d2\"); }")
8686
.build();
8787

88-
assert_that(
89-
p.cargo("build")
90-
.arg("-p")
91-
.arg("d1")
92-
.arg("-p")
93-
.arg("d2")
94-
.arg("-p")
95-
.arg("foo"),
96-
execs().with_status(0),
97-
);
88+
assert_that(p.cargo("build -p d1 -p d2 -p foo"), execs().with_status(0));
9889

9990
let d1_path = &p.build_dir()
10091
.join("debug")
@@ -108,12 +99,7 @@ fn clean_multiple_packages() {
10899
assert_that(d2_path, existing_file());
109100

110101
assert_that(
111-
p.cargo("clean")
112-
.arg("-p")
113-
.arg("d1")
114-
.arg("-p")
115-
.arg("d2")
116-
.cwd(&p.root().join("src")),
102+
p.cargo("clean -p d1 -p d2").cwd(&p.root().join("src")),
117103
execs().with_status(0).with_stdout(""),
118104
);
119105
assert_that(&p.bin("foo"), existing_file());

tests/testsuite/run.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ fn simple_quiet() {
5959
.build();
6060

6161
assert_that(
62-
p.cargo("run").arg("-q"),
62+
p.cargo("run -q"),
6363
execs().with_status(0).with_stdout("hello"),
6464
);
6565

6666
assert_that(
67-
p.cargo("run").arg("--quiet"),
67+
p.cargo("run --quiet"),
6868
execs().with_status(0).with_stdout("hello"),
6969
);
7070
}
@@ -1133,12 +1133,5 @@ fn explicit_bin_with_args() {
11331133
)
11341134
.build();
11351135

1136-
assert_that(
1137-
p.cargo("run")
1138-
.arg("--bin")
1139-
.arg("foo")
1140-
.arg("hello")
1141-
.arg("world"),
1142-
execs().with_status(0),
1143-
);
1136+
assert_that(p.cargo("run --bin foo hello world"), execs().with_status(0));
11441137
}

tests/testsuite/rustc.rs

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,7 @@ fn build_main_and_allow_unstable_options() {
111111
.build();
112112

113113
assert_that(
114-
p.cargo("rustc")
115-
.arg("-v")
116-
.arg("--bin")
117-
.arg("foo")
118-
.arg("--")
119-
.arg("-C")
120-
.arg("debug-assertions"),
114+
p.cargo("rustc -v --bin foo -- -C debug-assertions"),
121115
execs().with_status(0).with_stderr(&format!(
122116
"\
123117
[COMPILING] {name} v{version} ({url})
@@ -208,13 +202,7 @@ fn build_with_args_to_one_of_multiple_binaries() {
208202
.build();
209203

210204
assert_that(
211-
p.cargo("rustc")
212-
.arg("-v")
213-
.arg("--bin")
214-
.arg("bar")
215-
.arg("--")
216-
.arg("-C")
217-
.arg("debug-assertions"),
205+
p.cargo("rustc -v --bin bar -- -C debug-assertions"),
218206
execs().with_status(0).with_stderr(format!(
219207
"\
220208
[COMPILING] foo v0.0.1 ({url})
@@ -292,13 +280,7 @@ fn build_with_args_to_one_of_multiple_tests() {
292280
.build();
293281

294282
assert_that(
295-
p.cargo("rustc")
296-
.arg("-v")
297-
.arg("--test")
298-
.arg("bar")
299-
.arg("--")
300-
.arg("-C")
301-
.arg("debug-assertions"),
283+
p.cargo("rustc -v --test bar -- -C debug-assertions"),
302284
execs().with_status(0).with_stderr(format!(
303285
"\
304286
[COMPILING] foo v0.0.1 ({url})
@@ -420,13 +402,7 @@ fn build_only_bar_dependency() {
420402
.build();
421403

422404
assert_that(
423-
foo.cargo("rustc")
424-
.arg("-v")
425-
.arg("-p")
426-
.arg("bar")
427-
.arg("--")
428-
.arg("-C")
429-
.arg("debug-assertions"),
405+
foo.cargo("rustc -v -p bar -- -C debug-assertions"),
430406
execs().with_status(0).with_stderr(
431407
"\
432408
[COMPILING] bar v0.1.0 ([..])
@@ -504,12 +480,7 @@ fn fail_with_multiple_packages() {
504480
.build();
505481

506482
assert_that(
507-
foo.cargo("rustc")
508-
.arg("-v")
509-
.arg("-p")
510-
.arg("bar")
511-
.arg("-p")
512-
.arg("baz"),
483+
foo.cargo("rustc -v -p bar -p baz"),
513484
execs().with_status(1).with_stderr_contains(
514485
"\
515486
error: The argument '--package <SPEC>' was provided more than once, \

tests/testsuite/test.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,14 +2833,7 @@ fn selective_test_wonky_profile() {
28332833
let p = p.build();
28342834

28352835
assert_that(
2836-
p.cargo("test")
2837-
.arg("-v")
2838-
.arg("--no-run")
2839-
.arg("--release")
2840-
.arg("-p")
2841-
.arg("foo")
2842-
.arg("-p")
2843-
.arg("a"),
2836+
p.cargo("test -v --no-run --release -p foo -p a"),
28442837
execs().with_status(0),
28452838
);
28462839
}
@@ -3051,13 +3044,7 @@ fn panic_abort_multiple() {
30513044
.file("a/src/lib.rs", "")
30523045
.build();
30533046
assert_that(
3054-
p.cargo("test")
3055-
.arg("--release")
3056-
.arg("-v")
3057-
.arg("-p")
3058-
.arg("foo")
3059-
.arg("-p")
3060-
.arg("a"),
3047+
p.cargo("test --release -v -p foo -p a"),
30613048
execs().with_status(0),
30623049
);
30633050
}
@@ -3246,14 +3233,7 @@ fn test_many_with_features() {
32463233
.build();
32473234

32483235
assert_that(
3249-
p.cargo("test")
3250-
.arg("-v")
3251-
.arg("-p")
3252-
.arg("a")
3253-
.arg("-p")
3254-
.arg("foo")
3255-
.arg("--features")
3256-
.arg("foo"),
3236+
p.cargo("test -v -p a -p foo --features foo"),
32573237
execs().with_status(0),
32583238
);
32593239
}

0 commit comments

Comments
 (0)