Skip to content

Commit fabc2b6

Browse files
committed
Fix compile_fail tests using trybuild
The previous approach using doc comments with ```compile_fail didn't work in regular test functions. Trybuild provides the correct way to test compilation failures with expected error messages.
1 parent b901784 commit fabc2b6

File tree

5 files changed

+123
-29
lines changed

5 files changed

+123
-29
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ rayon = "1.8.0"
3030
clap = { version = "4", features = ["derive"] }
3131
byte-unit = "4.0.19"
3232
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
33+
trybuild = "1.0"
3334

3435
[features]
3536
build-print = ["dep:build-print"]

tests/compile_fail.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[test]
2+
fn compile_fail_tests() {
3+
let t = trybuild::TestCases::new();
4+
t.compile_fail("tests/ui/compile_fail.rs");
5+
}

tests/test_macros.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,6 @@ fn test_vars_in_str3() {
108108
}
109109

110110
#[test]
111-
// FIXME: doctests have no effect here, and we need to split these into one test per error
112-
/// ```compile_fail
113-
/// run_cmd!(echo "${msg0}").unwrap();
114-
/// assert_eq!(run_fun!(echo "${ msg }").unwrap(), "${ msg }");
115-
/// assert_eq!(run_fun!(echo "${}").unwrap(), "${}");
116-
/// assert_eq!(run_fun!(echo "${").unwrap(), "${");
117-
/// assert_eq!(run_fun!(echo "${msg").unwrap(), "${msg");
118-
/// assert_eq!(run_fun!(echo "$}").unwrap(), "$}");
119-
/// assert_eq!(run_fun!(echo "${}").unwrap(), "${}");
120-
/// assert_eq!(run_fun!(echo "${").unwrap(), "${");
121-
/// assert_eq!(run_fun!(echo "${0}").unwrap(), "${0}");
122-
/// assert_eq!(run_fun!(echo "${ 0 }").unwrap(), "${ 0 }");
123-
/// assert_eq!(run_fun!(echo "${0msg}").unwrap(), "${0msg}");
124-
/// assert_eq!(run_fun!(echo "${msg 0}").unwrap(), "${msg 0}");
125-
/// assert_eq!(run_fun!(echo "${msg 0}").unwrap(), "${msg 0}");
126-
/// ```
127111
fn test_vars_in_str4() {}
128112

129113
#[test]
@@ -286,12 +270,6 @@ fn test_ignore_and_pipefail() {
286270
}
287271

288272
#[test]
289-
// FIXME: doctests have no effect here, and we need to split these into one test per error
290-
/// ```compile_fail
291-
/// run_cmd!(ls > >&1).unwrap();
292-
/// run_cmd!(ls >>&1).unwrap();
293-
/// run_cmd!(ls >>&2).unwrap();
294-
/// ```
295273
fn test_redirect() {
296274
let tmp_file = "/tmp/f";
297275
assert!(run_cmd!(echo xxxx > $tmp_file).is_ok());
@@ -358,13 +336,6 @@ fn test_current_dir() {
358336
}
359337

360338
#[test]
361-
// FIXME: doctests have no effect here, and we need to split these into one test per error
362-
/// ```compile_fail
363-
/// run_cmd!(ls / /x &>>> /tmp/f).unwrap();
364-
/// run_cmd!(ls / /x &> > /tmp/f).unwrap();
365-
/// run_cmd!(ls / /x > > /tmp/f).unwrap();
366-
/// run_cmd!(ls / /x >> > /tmp/f).unwrap();
367-
/// ```
368339
fn test_redirect_fail() {}
369340

370341
#[test]

tests/ui/compile_fail.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use cmd_lib::*;
2+
3+
fn main() {
4+
// Invalid variable expansions
5+
run_cmd!(echo "${msg0}").unwrap();
6+
run_fun!(echo "${ msg }").unwrap();
7+
run_fun!(echo "${}").unwrap();
8+
run_fun!(echo "${").unwrap();
9+
run_fun!(echo "${msg").unwrap();
10+
run_fun!(echo "$}").unwrap();
11+
run_fun!(echo "${0}").unwrap();
12+
run_fun!(echo "${ 0 }").unwrap();
13+
run_fun!(echo "${0msg}").unwrap();
14+
run_fun!(echo "${msg 0}").unwrap();
15+
16+
// Invalid redirect syntax
17+
run_cmd!(ls > >&1).unwrap();
18+
run_cmd!(ls >> &1).unwrap();
19+
run_cmd!(ls >> &2).unwrap();
20+
21+
// Double redirect errors
22+
run_cmd!(ls / /x &>>> /tmp/f).unwrap();
23+
run_cmd!(ls / /x &> > /tmp/f).unwrap();
24+
run_cmd!(ls / /x > > /tmp/f).unwrap();
25+
run_cmd!(ls / /x >> > /tmp/f).unwrap();
26+
}

tests/ui/compile_fail.stderr

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
error: bad substitution: expected '}'
2+
--> tests/ui/compile_fail.rs:6:19
3+
|
4+
6 | run_fun!(echo "${ msg }").unwrap();
5+
| ^^^^^^^^^^
6+
7+
error: bad substitution: expected '}'
8+
--> tests/ui/compile_fail.rs:8:19
9+
|
10+
8 | run_fun!(echo "${").unwrap();
11+
| ^^^^
12+
13+
error: bad substitution: expected '}'
14+
--> tests/ui/compile_fail.rs:9:19
15+
|
16+
9 | run_fun!(echo "${msg").unwrap();
17+
| ^^^^^^^
18+
19+
error: bad substitution: expected '}'
20+
--> tests/ui/compile_fail.rs:11:19
21+
|
22+
11 | run_fun!(echo "${0}").unwrap();
23+
| ^^^^^^
24+
25+
error: bad substitution: expected '}'
26+
--> tests/ui/compile_fail.rs:12:19
27+
|
28+
12 | run_fun!(echo "${ 0 }").unwrap();
29+
| ^^^^^^^^
30+
31+
error: bad substitution: expected '}'
32+
--> tests/ui/compile_fail.rs:13:19
33+
|
34+
13 | run_fun!(echo "${0msg}").unwrap();
35+
| ^^^^^^^^^
36+
37+
error: bad substitution: expected '}'
38+
--> tests/ui/compile_fail.rs:14:19
39+
|
40+
14 | run_fun!(echo "${msg 0}").unwrap();
41+
| ^^^^^^^^^^
42+
43+
error: wrong double redirection format
44+
--> tests/ui/compile_fail.rs:17:19
45+
|
46+
17 | run_cmd!(ls > >&1).unwrap();
47+
| ^
48+
49+
error: raw fd not allowed for append redirection
50+
--> tests/ui/compile_fail.rs:18:19
51+
|
52+
18 | run_cmd!(ls >>&1).unwrap();
53+
| ^
54+
55+
error: raw fd not allowed for append redirection
56+
--> tests/ui/compile_fail.rs:19:19
57+
|
58+
19 | run_cmd!(ls >>&2).unwrap();
59+
| ^
60+
61+
error: wrong double redirection format
62+
--> tests/ui/compile_fail.rs:22:25
63+
|
64+
22 | run_cmd!(ls / /x &>>> /tmp/f).unwrap();
65+
| ^
66+
67+
error: wrong double redirection format
68+
--> tests/ui/compile_fail.rs:23:25
69+
|
70+
23 | run_cmd!(ls / /x &> > /tmp/f).unwrap();
71+
| ^
72+
73+
error: wrong double redirection format
74+
--> tests/ui/compile_fail.rs:24:24
75+
|
76+
24 | run_cmd!(ls / /x > > /tmp/f).unwrap();
77+
| ^
78+
79+
error: wrong double redirection format
80+
--> tests/ui/compile_fail.rs:25:25
81+
|
82+
25 | run_cmd!(ls / /x >> > /tmp/f).unwrap();
83+
| ^
84+
85+
error[E0425]: cannot find value `msg0` in this scope
86+
--> tests/ui/compile_fail.rs:5:5
87+
|
88+
5 | run_cmd!(echo "${msg0}").unwrap();
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
90+
|
91+
= note: this error originates in the macro `run_cmd` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)