Skip to content

Commit 1b921c8

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 1b921c8

File tree

2 files changed

+104
-35
lines changed

2 files changed

+104
-35
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 & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,6 @@ fn test_vars_in_str3() {
107107
assert_eq!(run_fun!(echo "$ msg").unwrap(), "$ msg");
108108
}
109109

110-
#[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-
/// ```
127-
fn test_vars_in_str4() {}
128-
129110
#[test]
130111
fn test_tls_set() {
131112
tls_init!(V, Vec<String>, vec![]);
@@ -286,12 +267,6 @@ fn test_ignore_and_pipefail() {
286267
}
287268

288269
#[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-
/// ```
295270
fn test_redirect() {
296271
let tmp_file = "/tmp/f";
297272
assert!(run_cmd!(echo xxxx > $tmp_file).is_ok());
@@ -357,16 +332,6 @@ fn test_current_dir() {
357332
);
358333
}
359334

360-
#[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-
/// ```
368-
fn test_redirect_fail() {}
369-
370335
#[test]
371336
fn test_buitin_stdout_redirect() {
372337
let f = "/tmp/builtin";

0 commit comments

Comments
 (0)