Debian Codegraph @

tests/uudoc/mod.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 use std::{env, path::PathBuf, process::Command, sync::OnceLock};
7
8 static UUDOC_BINARY_PATH: OnceLock<PathBuf> = OnceLock::new();
9
10 fn get_uudoc_command() -> Command {
11 let uudoc_binary = UUDOC_BINARY_PATH.get_or_init(|| {
12 let coreutils_binary = PathBuf::from(env!("CARGO_BIN_EXE_coreutils"));
13 coreutils_binary.parent().unwrap().join("uudoc")
14 });
15 Command::new(uudoc_binary)
16 }
17
18 #[test]
19 fn test_manpage_generation() {
20 let output = get_uudoc_command()
21 .arg("manpage")
22 .arg("ls")
23 .output()
24 .expect("Failed to execute command");
25
26 assert!(
27 output.status.success(),
28 "Command failed with status: {}",
29 output.status
30 );
31 // Note: tldr warning is now printed at build time (in build.rs), not at runtime
32 assert!(
33 output.stderr.is_empty(),
34 "stderr should be empty but got: {}",
35 String::from_utf8_lossy(&output.stderr)
36 );
37
38 let output_str = String::from_utf8_lossy(&output.stdout);
39 assert!(output_str.contains("\n.TH LS"), "{output_str}");
40 assert!(output_str.contains('1'), "{output_str}");
41 assert!(output_str.contains("\n.SH NAME\nls"), "{output_str}");
42 }
43
44 #[test]
45 fn test_manpage_coreutils() {
46 let output = get_uudoc_command()
47 .arg("manpage")
48 .arg("coreutils")
49 .output()
50 .expect("Failed to execute command");
51
52 assert!(
53 output.status.success(),
54 "Command failed with status: {}",
55 output.status
56 );
57 // Note: tldr warning is now printed at build time (in build.rs), not at runtime
58 assert!(
59 output.stderr.is_empty(),
60 "stderr should be empty but got: {}",
61 String::from_utf8_lossy(&output.stderr)
62 );
63
64 let output_str = String::from_utf8_lossy(&output.stdout);
65 assert!(output_str.contains("\n.TH COREUTILS"), "{output_str}");
66 assert!(output_str.contains("coreutils"), "{output_str}");
67 assert!(output_str.contains("\n.SH NAME\ncoreutils"), "{output_str}");
68 }
69
70 #[test]
71 fn test_manpage_invalid_utility() {
72 let output = get_uudoc_command()
73 .arg("manpage")
74 .arg("nonexistent_utility")
75 .output()
76 .expect("Failed to execute command");
77
78 // Should fail for invalid utility
79 assert!(!output.status.success(), "Command should have failed");
80 }
81
82 #[test]
83 fn test_completion_generation() {
84 let output = get_uudoc_command()
85 .arg("completion")
86 .arg("ls")
87 .arg("powershell")
88 .output()
89 .expect("Failed to execute command");
90
91 assert!(
92 output.status.success(),
93 "Command failed with status: {}",
94 output.status
95 );
96 assert!(
97 output.stderr.is_empty(),
98 "stderr should be empty but got: {}",
99 String::from_utf8_lossy(&output.stderr)
100 );
101
102 let output_str = String::from_utf8_lossy(&output.stdout);
103 assert!(
104 output_str.contains("using namespace System.Management.Automation"),
105 "{output_str}"
106 );
107 }
108
109 // Prevent regression to:
110 //
111 // ❯ uudoc manpage base64 | rg --fixed-strings -- 'base32'
112 // The data are encoded as described for the base32 alphabet in RFC 4648.
113 // to the bytes of the formal base32 alphabet. Use \-\-ignore\-garbage
114 // The data are encoded as described for the base32 alphabet in RFC 4648.
115 // to the bytes of the formal base32 alphabet. Use \-\-ignore\-garbage
116 #[test]
117 fn test_manpage_base64() {
118 let output = get_uudoc_command()
119 .arg("manpage")
120 .arg("base64")
121 .env("LANG", "C") // force locale to english to avoid issues with manpage output
122 .output()
123 .expect("Failed to execute command");
124
125 assert!(
126 output.status.success(),
127 "Command failed with status: {}",
128 output.status
129 );
130 // Note: tldr warning is now printed at build time (in build.rs), not at runtime
131 assert!(
132 output.stderr.is_empty(),
133 "stderr should be empty but got: {}",
134 String::from_utf8_lossy(&output.stderr)
135 );
136
137 let output_str = String::from_utf8_lossy(&output.stdout);
138 assert!(output_str.contains("base64 alphabet"));
139 assert!(!output_str.to_ascii_lowercase().contains("base32"));
140 }
141
142 // Test to ensure markdown headers are correctly formatted in generated markdown files
143 // Prevents regression of https://github.com/uutils/coreutils/issues/10003
144 #[test]
145 fn test_markdown_header_format() {
146 use std::fs;
147
148 // Read a sample markdown file from the documentation
149 // This assumes the docs have been generated (they should be in the repo)
150 let docs_path = "docs/src/utils/cat.md";
151
152 if fs::metadata(docs_path).is_ok() {
153 let content =
154 fs::read_to_string(docs_path).expect("Failed to read generated markdown file");
155
156 // Verify Options header is in markdown format (## Options)
157 assert!(
158 content.contains("## Options"),
159 "Generated markdown should contain '## Options' header"
160 );
161
162 // Verify no HTML h2 tags for Options (old format)
163 assert!(
164 !content.contains("<h2>Options</h2>"),
165 "Generated markdown should not contain '<h2>Options</h2>' (use markdown format instead)"
166 );
167
168 // Also verify Examples if it exists
169 if content.contains("## Examples") {
170 assert!(
171 content.contains("## Examples"),
172 "Generated markdown should contain '## Examples' header in markdown format"
173 );
174 }
175 }
176 }