Debian Codegraph @

tests/by-util/test_basename.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 // spell-checker:ignore (words) reallylongexecutable nbaz
7
8 use uutests::new_ucmd;
9
10 #[test]
11 fn test_help() {
12 for help_flg in ["-h", "--help"] {
13 new_ucmd!()
14 .arg(help_flg)
15 .succeeds()
16 .no_stderr()
17 .stdout_contains("Usage:");
18 }
19 }
20
21 #[test]
22 fn test_version() {
23 for version_flg in ["-V", "--version"] {
24 assert!(
25 new_ucmd!()
26 .arg(version_flg)
27 .succeeds()
28 .no_stderr()
29 .stdout_str()
30 .starts_with("basename")
31 );
32 }
33 }
34
35 #[test]
36 fn test_directory() {
37 new_ucmd!()
38 .args(&["/root/alpha/beta/gamma/delta/epsilon/omega/"])
39 .succeeds()
40 .stdout_only("omega\n");
41 }
42
43 #[test]
44 fn test_file() {
45 new_ucmd!()
46 .args(&["/etc/passwd"])
47 .succeeds()
48 .stdout_only("passwd\n");
49 }
50
51 #[test]
52 fn test_trailing_separators() {
53 for (path, expected) in [
54 ("foo/bar", "bar\n"),
55 ("foo/bar/", "bar\n"),
56 ("foo/bar///", "bar\n"),
57 ("foo/./", ".\n"),
58 ("foo/.//", ".\n"),
59 ] {
60 new_ucmd!().arg(path).succeeds().stdout_only(expected);
61 }
62
63 let separator = std::path::MAIN_SEPARATOR;
64 let native_dot_path = format!("foo{separator}.{separator}{separator}");
65 new_ucmd!()
66 .arg(native_dot_path)
67 .succeeds()
68 .stdout_only(".\n");
69
70 let root = if cfg!(windows) { "\\\n" } else { "/\n" };
71 for path in ["/", "//", "///"] {
72 new_ucmd!().arg(path).succeeds().stdout_only(root);
73 }
74 }
75
76 #[test]
77 fn test_remove_suffix() {
78 new_ucmd!()
79 .args(&["/usr/local/bin/reallylongexecutable.exe", ".exe"])
80 .succeeds()
81 .stdout_only("reallylongexecutable\n");
82 }
83
84 #[test]
85 fn test_do_not_remove_suffix() {
86 new_ucmd!()
87 .args(&["/foo/bar/baz", "baz"])
88 .succeeds()
89 .stdout_only("baz\n");
90 }
91
92 #[test]
93 fn test_multiple_param() {
94 for multiple_param in ["-a", "--multiple", "--mul"] {
95 let path = "/foo/bar/baz";
96 new_ucmd!()
97 .args(&[multiple_param, path, path])
98 .succeeds()
99 .stdout_only("baz\nbaz\n");
100 }
101 }
102
103 #[test]
104 fn test_suffix_param() {
105 for suffix_param in ["-s", "--suffix", "--suf"] {
106 let path = "/foo/bar/baz.exe";
107 new_ucmd!()
108 .args(&[suffix_param, ".exe", path, path])
109 .succeeds()
110 .stdout_only("baz\nbaz\n");
111 }
112 }
113
114 #[test]
115 fn test_zero_param() {
116 for zero_param in ["-z", "--zero", "--ze"] {
117 let path = "/foo/bar/baz";
118 new_ucmd!()
119 .args(&[zero_param, "-a", path, path])
120 .succeeds()
121 .stdout_only("baz\0baz\0");
122 }
123 }
124
125 fn expect_error(input: &[&str]) {
126 assert!(
127 !new_ucmd!()
128 .args(input)
129 .fails()
130 .no_stdout()
131 .stderr_str()
132 .is_empty()
133 );
134 }
135
136 #[test]
137 fn test_invalid_option() {
138 let path = "/foo/bar/baz";
139 expect_error(&["-q", path]);
140 }
141
142 #[test]
143 fn test_no_args() {
144 expect_error(&[]);
145 }
146
147 #[test]
148 fn test_no_args_output() {
149 new_ucmd!().fails().usage_error("missing operand");
150 }
151
152 #[test]
153 fn test_too_many_args() {
154 expect_error(&["a", "b", "c"]);
155 }
156
157 #[test]
158 fn test_too_many_args_output() {
159 new_ucmd!()
160 .args(&["a", "b", "c"])
161 .fails()
162 .usage_error("extra operand 'c'");
163 }
164
165 #[cfg(any(unix, target_os = "redox"))]
166 #[test]
167 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
168 fn test_invalid_utf8_args() {
169 let param = uucore::os_str_from_bytes(b"/tmp/some-\xc0-file.k\xf3")
170 .expect("Only unix platforms can test non-unicode names");
171
172 new_ucmd!()
173 .arg(param)
174 .succeeds()
175 .stdout_is_bytes(b"some-\xc0-file.k\xf3\n");
176
177 let suffix = uucore::os_str_from_bytes(b".k\xf3")
178 .expect("Only unix platforms can test non-unicode names");
179
180 new_ucmd!()
181 .arg(param)
182 .arg(suffix)
183 .succeeds()
184 .stdout_is_bytes(b"some-\xc0-file\n");
185 }
186
187 #[test]
188 fn test_root() {
189 let expected = if cfg!(windows) { "\\\n" } else { "/\n" };
190 new_ucmd!().arg("/").succeeds().stdout_is(expected);
191 }
192
193 #[test]
194 fn test_double_slash() {
195 // TODO The GNU tests seem to suggest that some systems treat "//"
196 // as the same directory as "/" directory but not all systems. We
197 // should extend this test to account for that possibility.
198 let expected = if cfg!(windows) { "\\\n" } else { "/\n" };
199 new_ucmd!().arg("//").succeeds().stdout_is(expected);
200 new_ucmd!()
201 .args(&["//", "/"])
202 .succeeds()
203 .stdout_is(expected);
204 new_ucmd!()
205 .args(&["//", "//"])
206 .succeeds()
207 .stdout_is(expected);
208 }
209
210 #[test]
211 fn test_triple_slash() {
212 let expected = if cfg!(windows) { "\\\n" } else { "/\n" };
213 new_ucmd!().arg("///").succeeds().stdout_is(expected);
214 }
215
216 #[test]
217 fn test_trailing_dot() {
218 new_ucmd!().arg("/.").succeeds().stdout_is(".\n");
219 new_ucmd!().arg("hello/.").succeeds().stdout_is(".\n");
220 new_ucmd!().arg("/foo/bar/.").succeeds().stdout_is(".\n");
221 }
222
223 #[test]
224 fn test_simple_format() {
225 new_ucmd!().args(&["a-a", "-a"]).succeeds().stdout_is("a\n");
226 new_ucmd!()
227 .args(&["a--help", "--help"])
228 .succeeds()
229 .stdout_is("a\n");
230 new_ucmd!().args(&["a-h", "-h"]).succeeds().stdout_is("a\n");
231 new_ucmd!().args(&["f.s", ".s"]).succeeds().stdout_is("f\n");
232 new_ucmd!().args(&["a-s", "-s"]).succeeds().stdout_is("a\n");
233 new_ucmd!().args(&["a-z", "-z"]).succeeds().stdout_is("a\n");
234 new_ucmd!()
235 .args(&["a", "b", "c"])
236 .fails_with_code(1)
237 .stderr_contains("extra operand 'c'");
238 }
239
240 #[test]
241 fn test_invalid_arg() {
242 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
243 }
244
245 #[test]
246 fn test_repeated_multiple() {
247 new_ucmd!()
248 .args(&["-aa", "-a", "foo"])
249 .succeeds()
250 .stdout_is("foo\n");
251 }
252
253 #[test]
254 fn test_repeated_multiple_many() {
255 new_ucmd!()
256 .args(&["-aa", "-a", "1/foo", "q/bar", "x/y/baz"])
257 .succeeds()
258 .stdout_is("foo\nbar\nbaz\n");
259 }
260
261 #[test]
262 fn test_repeated_suffix_last() {
263 new_ucmd!()
264 .args(&["-s", ".h", "-s", ".c", "foo.c"])
265 .succeeds()
266 .stdout_is("foo\n");
267 }
268
269 #[test]
270 fn test_repeated_suffix_not_first() {
271 new_ucmd!()
272 .args(&["-s", ".h", "-s", ".c", "foo.h"])
273 .succeeds()
274 .stdout_is("foo.h\n");
275 }
276
277 #[test]
278 fn test_repeated_suffix_multiple() {
279 new_ucmd!()
280 .args(&["-as", ".h", "-a", "-s", ".c", "foo.c", "bar.c", "bar.h"])
281 .succeeds()
282 .stdout_is("foo\nbar\nbar.h\n");
283 }
284
285 #[test]
286 fn test_repeated_zero() {
287 new_ucmd!()
288 .args(&["-zz", "-z", "foo/bar"])
289 .succeeds()
290 .stdout_is("bar\0");
291 }
292
293 #[test]
294 fn test_zero_does_not_imply_multiple() {
295 new_ucmd!()
296 .args(&["-z", "foo.c", "c"])
297 .succeeds()
298 .stdout_is("foo.\0");
299 }
300
301 #[test]
302 fn test_suffix_implies_multiple() {
303 new_ucmd!()
304 .args(&["-s", ".c", "foo.c", "o.c"])
305 .succeeds()
306 .stdout_is("foo\no\n");
307 }
308
309 #[test]
310 fn test_emoji_handling() {
311 new_ucmd!()
312 .arg("/path/to/πŸ¦€.txt")
313 .succeeds()
314 .stdout_only("πŸ¦€.txt\n");
315
316 new_ucmd!()
317 .arg("/🌍/path/to/πŸš€.exe")
318 .succeeds()
319 .stdout_only("πŸš€.exe\n");
320
321 new_ucmd!()
322 .args(&["/path/to/file🎯.emoji", ".emoji"])
323 .succeeds()
324 .stdout_only("file🎯\n");
325 }