Debian Codegraph @

tests/by-util/test_dirname.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 uutests::new_ucmd;
7
8 #[test]
9 fn test_invalid_arg() {
10 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
11 }
12
13 #[test]
14 fn test_missing_operand() {
15 new_ucmd!().fails_with_code(1);
16 }
17
18 #[test]
19 fn test_path_with_trailing_slashes() {
20 new_ucmd!()
21 .arg("/root/alpha/beta/gamma/delta/epsilon/omega//")
22 .succeeds()
23 .stdout_is("/root/alpha/beta/gamma/delta/epsilon\n");
24 }
25
26 #[test]
27 fn test_path_without_trailing_slashes() {
28 new_ucmd!()
29 .arg("/root/alpha/beta/gamma/delta/epsilon/omega")
30 .succeeds()
31 .stdout_is("/root/alpha/beta/gamma/delta/epsilon\n");
32 }
33
34 #[test]
35 fn test_path_without_trailing_slashes_and_zero() {
36 new_ucmd!()
37 .arg("-z")
38 .arg("/root/alpha/beta/gamma/delta/epsilon/omega")
39 .succeeds()
40 .stdout_is("/root/alpha/beta/gamma/delta/epsilon\u{0}");
41
42 new_ucmd!()
43 .arg("--zero")
44 .arg("/root/alpha/beta/gamma/delta/epsilon/omega")
45 .succeeds()
46 .stdout_is("/root/alpha/beta/gamma/delta/epsilon\u{0}");
47 }
48
49 #[test]
50 fn test_repeated_zero() {
51 new_ucmd!()
52 .arg("--zero")
53 .arg("--zero")
54 .arg("foo/bar")
55 .succeeds()
56 .stdout_only("foo\u{0}");
57 }
58
59 #[test]
60 fn test_root() {
61 new_ucmd!().arg("/").succeeds().stdout_is("/\n");
62 }
63
64 #[test]
65 fn test_pwd() {
66 new_ucmd!().arg(".").succeeds().stdout_is(".\n");
67 }
68
69 #[test]
70 fn test_empty() {
71 new_ucmd!().arg("").succeeds().stdout_is(".\n");
72 }
73
74 #[test]
75 #[cfg(unix)]
76 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
77 fn test_dirname_non_utf8_paths() {
78 use std::ffi::OsStr;
79 use std::os::unix::ffi::OsStrExt;
80
81 let non_utf8_bytes = b"test_\xFF\xFE/file.txt";
82 let non_utf8_name = OsStr::from_bytes(non_utf8_bytes);
83
84 let result = new_ucmd!().arg(non_utf8_name).succeeds();
85
86 let output = result.stdout_str_lossy();
87 assert!(!output.is_empty());
88 assert!(output.contains("test_"));
89 }
90
91 #[test]
92 fn test_emoji_handling() {
93 new_ucmd!()
94 .arg("/🌍/path/to/🦀.txt")
95 .succeeds()
96 .stdout_is("/🌍/path/to\n");
97
98 new_ucmd!()
99 .arg("/🎉/path/to/🚀/")
100 .succeeds()
101 .stdout_is("/🎉/path/to\n");
102
103 new_ucmd!()
104 .args(&["-z", "/🌟/emoji/path/🦋.file"])
105 .succeeds()
106 .stdout_is("/🌟/emoji/path\u{0}");
107 }
108
109 #[test]
110 fn test_trailing_dot() {
111 new_ucmd!()
112 .arg("/home/dos/.")
113 .succeeds()
114 .stdout_is("/home/dos\n");
115
116 // Root with dot
117 new_ucmd!().arg("/.").succeeds().stdout_is("/\n");
118
119 // Relative path with /.
120 new_ucmd!().arg("hello/.").succeeds().stdout_is("hello\n");
121
122 // Deeper path with /.
123 new_ucmd!()
124 .arg("/foo/bar/baz/.")
125 .succeeds()
126 .stdout_is("/foo/bar/baz\n");
127 }
128
129 #[test]
130 fn test_trailing_dot_with_zero_flag() {
131 // Test that -z flag works correctly with /. paths
132 new_ucmd!()
133 .arg("-z")
134 .arg("/home/dos/.")
135 .succeeds()
136 .stdout_is("/home/dos\u{0}");
137
138 new_ucmd!()
139 .arg("--zero")
140 .arg("/.")
141 .succeeds()
142 .stdout_is("/\u{0}");
143 }
144
145 #[test]
146 fn test_trailing_dot_multiple_paths() {
147 // Test multiple paths, some with /. suffix
148 new_ucmd!()
149 .args(&["/home/dos/.", "/var/log", "/tmp/."])
150 .succeeds()
151 .stdout_is("/home/dos\n/var\n/tmp\n");
152 }
153
154 #[test]
155 fn test_trailing_dot_edge_cases() {
156 // Double slash before dot (should still work)
157 new_ucmd!()
158 .arg("/home/dos//.")
159 .succeeds()
160 .stdout_is("/home/dos\n");
161
162 // Path with . in middle (should use normal logic)
163 new_ucmd!()
164 .arg("/path/./to/file")
165 .succeeds()
166 .stdout_is("/path/./to\n");
167 }
168
169 #[test]
170 fn test_trailing_dot_emoji() {
171 // Emoji paths with /. suffix
172 new_ucmd!()
173 .arg("/🌍/path/.")
174 .succeeds()
175 .stdout_is("/🌍/path\n");
176
177 new_ucmd!().arg("/🎉/🚀/.").succeeds().stdout_is("/🎉/🚀\n");
178 }
179
180 #[test]
181 #[cfg(unix)]
182 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
183 fn test_trailing_dot_non_utf8() {
184 use std::ffi::OsStr;
185 use std::os::unix::ffi::OsStrExt;
186
187 let non_utf8_bytes = b"/test_\xFF\xFE/.";
188 let non_utf8_path = OsStr::from_bytes(non_utf8_bytes);
189
190 let result = new_ucmd!().arg(non_utf8_path).succeeds();
191
192 let output = result.stdout_str_lossy();
193 assert!(!output.is_empty());
194 assert!(output.contains("test_"));
195 assert!(!output.trim().ends_with('.'));
196 }
197
198 #[test]
199 fn test_existing_behavior_preserved() {
200 // Normal paths without /. should work as before
201 new_ucmd!().arg("/home/dos").succeeds().stdout_is("/home\n");
202
203 new_ucmd!()
204 .arg("/home/dos/")
205 .succeeds()
206 .stdout_is("/home\n");
207
208 // Parent directory references
209 new_ucmd!()
210 .arg("/home/dos/..")
211 .succeeds()
212 .stdout_is("/home/dos\n");
213 }
214
215 #[test]
216 fn test_multiple_paths_comprehensive() {
217 // Comprehensive test for multiple paths in single invocation
218 new_ucmd!()
219 .args(&[
220 "/home/dos/.",
221 "/var/log",
222 ".",
223 "/tmp/.",
224 "",
225 "/",
226 "relative/path",
227 ])
228 .succeeds()
229 .stdout_is("/home/dos\n/var\n.\n/tmp\n.\n/\nrelative\n");
230 }
231
232 #[test]
233 fn test_all_dot_slash_variations() {
234 new_ucmd!().arg("foo//.").succeeds().stdout_is("foo\n");
235
236 new_ucmd!().arg("foo///.").succeeds().stdout_is("foo\n");
237
238 new_ucmd!().arg("foo/./").succeeds().stdout_is("foo\n");
239
240 new_ucmd!()
241 .arg("foo/bar/./")
242 .succeeds()
243 .stdout_is("foo/bar\n");
244
245 new_ucmd!().arg("foo/./bar").succeeds().stdout_is("foo/.\n");
246 }
247
248 #[test]
249 fn test_dot_slash_component_preservation() {
250 new_ucmd!().arg("a/./b").succeeds().stdout_is("a/.\n");
251
252 new_ucmd!()
253 .arg("a/./b/./c")
254 .succeeds()
255 .stdout_is("a/./b/.\n");
256
257 new_ucmd!()
258 .arg("foo/./bar/baz")
259 .succeeds()
260 .stdout_is("foo/./bar\n");
261
262 new_ucmd!()
263 .arg("/path/./to/file")
264 .succeeds()
265 .stdout_is("/path/./to\n");
266 }
267
268 #[test]
269 #[cfg(windows)]
270 fn test_win_backslash_as_separator() {
271 for (input, expected) in [
272 // Relative paths
273 (r"foo\bar", "foo"),
274 (r"foo\bar\baz", r"foo\bar"),
275 // Mixed separators - whichever separator is "last" is removed
276 (r"C:\Users/foo", r"C:\Users"),
277 (r"C:/Users\foo", r"C:/Users"),
278 // Trailing backslashes stripped
279 (r"C:\Users\foo\", r"C:\Users"),
280 (r"\foo\bar\\", r"\foo"),
281 // Trailing `\.` pattern
282 (r"C:\foo\bar\.", r"C:\foo\bar"),
283 (r"\foo\.", r"\foo"),
284 // Repeated backslashes
285 (r"C:\foo\\bar", r"C:\foo"),
286 ] {
287 new_ucmd!()
288 .arg(input)
289 .succeeds()
290 .stdout_is(format!("{expected}\n"));
291 }
292
293 // -z flag
294 new_ucmd!()
295 .args(&["-z", r"C:\Users\foo"])
296 .succeeds()
297 .stdout_is("C:\\Users\u{0}");
298 }
299
300 #[test]
301 #[cfg(windows)]
302 fn test_win_path_prefixes() {
303 for (input, expected) in [
304 // Disk: C:\foo → C:\ ; C:\ → C:\ ; C:foo → C: ; C: → C:
305 (r"C:\foo", "C:\\"),
306 (r"C:\", "C:\\"),
307 (r"C:foo", "C:"),
308 ("C:", "C:"),
309 // UNC: \\server\share\foo → \\server\share\ ; \\server\share → as-is
310 (r"\\server\share\foo", r"\\server\share\"),
311 (r"\\server\share", r"\\server\share"),
312 // VerbatimDisk: \\?\C:\foo → \\?\C:\ ; \\?\C:\ → \\?\C:\
313 (r"\\?\C:\foo", r"\\?\C:\"),
314 (r"\\?\C:\", r"\\?\C:\"),
315 // VerbatimUNC: \\?\UNC\s\sh\foo → \\?\UNC\s\sh\
316 (r"\\?\UNC\server\share\foo", r"\\?\UNC\server\share\"),
317 (r"\\?\UNC\server\share\", r"\\?\UNC\server\share\"),
318 // DeviceNS: \\.\dev\foo → \\.\dev\ ; \\.\dev → as-is
319 (r"\\.\PhysicalDrive0\foo", r"\\.\PhysicalDrive0\"),
320 (r"\\.\PhysicalDrive0", r"\\.\PhysicalDrive0"),
321 // Root-only backslash (no prefix, just separator)
322 (r"\foo", "\\"),
323 ("\\", "\\"),
324 ] {
325 new_ucmd!()
326 .arg(input)
327 .succeeds()
328 .stdout_is(format!("{expected}\n"));
329 }
330 }