Skip to content

Commit 73f124f

Browse files
committed
Fix compile_fail tests by moving to macros crate doctests
- Add compile_fail doctests in macros/src/lib.rs as crate documentation - Remove non-functional compile_fail doc comments from test_macros.rs - All invalid syntax examples now properly test compilation failures The compile_fail attribute only works in actual doctests (library/crate documentation), not in test function doc comments. Moving these tests to the macros crate where they belong allows them to properly verify that invalid macro syntax fails to compile as expected.
1 parent b901784 commit 73f124f

File tree

2 files changed

+104
-29
lines changed

2 files changed

+104
-29
lines changed

macros/src/lib.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,107 @@
1+
//! cmd_lib_macros - Procedural macros for cmd_lib
2+
//!
3+
//! ## Invalid syntax examples that should fail to compile
4+
//!
5+
//! This section contains documentation tests that demonstrate invalid macro syntax
6+
//! which should result in compilation errors. These serve as tests to ensure
7+
//! the macros properly reject invalid input.
8+
//!
9+
//! ### Invalid variable expansion syntax
10+
//!
11+
//! Variable names cannot start with numbers:
12+
//! ```compile_fail
13+
//! # use cmd_lib::*;
14+
//! run_cmd!(echo "${msg0}");
15+
//! ```
16+
//!
17+
//! Invalid spacing in variable expansion:
18+
//! ```compile_fail
19+
//! # use cmd_lib::*;
20+
//! run_fun!(echo "${ msg }");
21+
//! ```
22+
//!
23+
//! Unclosed variable expansion:
24+
//! ```compile_fail
25+
//! # use cmd_lib::*;
26+
//! run_fun!(echo "${");
27+
//! ```
28+
//!
29+
//! Unclosed variable name:
30+
//! ```compile_fail
31+
//! # use cmd_lib::*;
32+
//! run_fun!(echo "${msg");
33+
//! ```
34+
//!
35+
//! Variable names cannot be numbers:
36+
//! ```compile_fail
37+
//! # use cmd_lib::*;
38+
//! run_fun!(echo "${0}");
39+
//! ```
40+
//!
41+
//! Variable names cannot have spaces:
42+
//! ```compile_fail
43+
//! # use cmd_lib::*;
44+
//! run_fun!(echo "${ 0 }");
45+
//! ```
46+
//!
47+
//! Variable names cannot start with numbers:
48+
//! ```compile_fail
49+
//! # use cmd_lib::*;
50+
//! run_fun!(echo "${0msg}");
51+
//! ```
52+
//!
53+
//! Variable names cannot contain spaces:
54+
//! ```compile_fail
55+
//! # use cmd_lib::*;
56+
//! run_fun!(echo "${msg 0}");
57+
//! ```
58+
//!
59+
//! ### Invalid redirect syntax
60+
//!
61+
//! Invalid redirect operator spacing:
62+
//! ```compile_fail
63+
//! # use cmd_lib::*;
64+
//! run_cmd!(ls > >&1);
65+
//! ```
66+
//!
67+
//! Invalid redirect to stdout:
68+
//! ```compile_fail
69+
//! # use cmd_lib::*;
70+
//! run_cmd!(ls >>&1);
71+
//! ```
72+
//!
73+
//! Invalid redirect to stderr:
74+
//! ```compile_fail
75+
//! # use cmd_lib::*;
76+
//! run_cmd!(ls >>&2);
77+
//! ```
78+
//!
79+
//! ### Double redirect errors
80+
//!
81+
//! Triple redirect operator:
82+
//! ```compile_fail
83+
//! # use cmd_lib::*;
84+
//! run_cmd!(ls / /x &>>> /tmp/f);
85+
//! ```
86+
//!
87+
//! Double redirect with space:
88+
//! ```compile_fail
89+
//! # use cmd_lib::*;
90+
//! run_cmd!(ls / /x &> > /tmp/f);
91+
//! ```
92+
//!
93+
//! Double output redirect:
94+
//! ```compile_fail
95+
//! # use cmd_lib::*;
96+
//! run_cmd!(ls / /x > > /tmp/f);
97+
//! ```
98+
//!
99+
//! Append and output redirect:
100+
//! ```compile_fail
101+
//! # use cmd_lib::*;
102+
//! run_cmd!(ls / /x >> > /tmp/f);
103+
//! ```
104+
1105
use proc_macro2::{TokenStream, TokenTree};
2106
use proc_macro_error2::{abort, proc_macro_error};
3107
use quote::quote;

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]

0 commit comments

Comments
 (0)