Debian Codegraph @

debian/rust-vendor/glob/tests/glob-std.rs

raw
1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-windows TempDir may cause IoError on windows: #10462
12
13 #![cfg_attr(test, deny(warnings))]
14
15 use glob::{glob, glob_with};
16 use std::env;
17 use std::fs;
18 use std::path::PathBuf;
19
20 #[test]
21 fn main() {
22 fn mk_file(path: &str, directory: bool) {
23 if directory {
24 fs::create_dir(path).unwrap();
25 } else {
26 fs::File::create(path).unwrap();
27 }
28 }
29
30 fn mk_symlink_file(original: &str, link: &str) {
31 #[cfg(unix)]
32 {
33 use std::os::unix::fs::symlink;
34 symlink(original, link).unwrap();
35 }
36 #[cfg(windows)]
37 {
38 use std::os::windows::fs::symlink_file;
39 symlink_file(original, link).unwrap();
40 }
41 }
42
43 fn mk_symlink_dir(original: &str, link: &str) {
44 #[cfg(unix)]
45 {
46 use std::os::unix::fs::symlink;
47 symlink(original, link).unwrap();
48 }
49 #[cfg(windows)]
50 {
51 use std::os::windows::fs::symlink_dir;
52 symlink_dir(original, link).unwrap();
53 }
54 }
55
56 fn glob_vec(pattern: &str) -> Vec<PathBuf> {
57 glob(pattern).unwrap().map(|r| r.unwrap()).collect()
58 }
59
60 fn glob_with_vec(pattern: &str, options: glob::MatchOptions) -> Vec<PathBuf> {
61 glob_with(pattern, options)
62 .unwrap()
63 .map(|r| r.unwrap())
64 .collect()
65 }
66
67 let root = tempfile::tempdir();
68 let root = root.expect("Should have created a temp directory");
69 assert!(env::set_current_dir(root.path()).is_ok());
70
71 mk_file("aaa", true);
72 mk_file("aaa/apple", true);
73 mk_file("aaa/orange", true);
74 mk_file("aaa/tomato", true);
75 mk_file("aaa/tomato/tomato.txt", false);
76 mk_file("aaa/tomato/tomoto.txt", false);
77 mk_file("bbb", true);
78 mk_file("bbb/specials", true);
79 mk_file("bbb/specials/!", false);
80 // a valid symlink
81 mk_symlink_file("aaa/apple", "aaa/green_apple");
82 // a broken symlink
83 mk_symlink_file("aaa/setsuna", "aaa/kazusa");
84
85 // windows does not allow `*` or `?` characters to exist in filenames
86 if env::consts::FAMILY != "windows" {
87 mk_file("bbb/specials/*", false);
88 mk_file("bbb/specials/?", false);
89 }
90
91 mk_file("bbb/specials/[", false);
92 mk_file("bbb/specials/]", false);
93 mk_file("ccc", true);
94 mk_file("xyz", true);
95 mk_file("xyz/x", false);
96 mk_file("xyz/y", false);
97 mk_file("xyz/z", false);
98
99 mk_file("r", true);
100 mk_file("r/current_dir.md", false);
101 mk_file("r/one", true);
102 mk_file("r/one/a.md", false);
103 mk_file("r/one/another", true);
104 mk_file("r/one/another/a.md", false);
105 mk_file("r/one/another/deep", true);
106 mk_file("r/one/another/deep/spelunking.md", false);
107 mk_file("r/another", true);
108 mk_file("r/another/a.md", false);
109 mk_file("r/two", true);
110 mk_file("r/two/b.md", false);
111 mk_file("r/three", true);
112 mk_file("r/three/c.md", false);
113
114 mk_file("dirsym", true);
115 mk_symlink_dir(root.path().join("r").to_str().unwrap(), "dirsym/link");
116
117 assert_eq!(
118 glob_vec("dirsym/**/*.md"),
119 vec!(
120 PathBuf::from("dirsym/link/another/a.md"),
121 PathBuf::from("dirsym/link/current_dir.md"),
122 PathBuf::from("dirsym/link/one/a.md"),
123 PathBuf::from("dirsym/link/one/another/a.md"),
124 PathBuf::from("dirsym/link/one/another/deep/spelunking.md"),
125 PathBuf::from("dirsym/link/three/c.md"),
126 PathBuf::from("dirsym/link/two/b.md")
127 )
128 );
129
130 // all recursive entities
131 assert_eq!(
132 glob_vec("r/**"),
133 vec!(
134 PathBuf::from("r/another"),
135 PathBuf::from("r/one"),
136 PathBuf::from("r/one/another"),
137 PathBuf::from("r/one/another/deep"),
138 PathBuf::from("r/three"),
139 PathBuf::from("r/two")
140 )
141 );
142
143 // std-canonicalized windows verbatim disk paths should work
144 if env::consts::FAMILY == "windows" {
145 let r_verbatim = PathBuf::from("r").canonicalize().unwrap();
146 assert_eq!(
147 glob_vec(&format!("{}\\**", r_verbatim.display().to_string()))
148 .into_iter()
149 .map(|p| p.strip_prefix(&r_verbatim).unwrap().to_owned())
150 .collect::<Vec<_>>(),
151 vec!(
152 PathBuf::from("another"),
153 PathBuf::from("one"),
154 PathBuf::from("one\\another"),
155 PathBuf::from("one\\another\\deep"),
156 PathBuf::from("three"),
157 PathBuf::from("two")
158 )
159 );
160 }
161
162 // collapse consecutive recursive patterns
163 assert_eq!(
164 glob_vec("r/**/**"),
165 vec!(
166 PathBuf::from("r/another"),
167 PathBuf::from("r/one"),
168 PathBuf::from("r/one/another"),
169 PathBuf::from("r/one/another/deep"),
170 PathBuf::from("r/three"),
171 PathBuf::from("r/two")
172 )
173 );
174
175 assert_eq!(
176 glob_vec("r/**/*"),
177 vec!(
178 PathBuf::from("r/another"),
179 PathBuf::from("r/another/a.md"),
180 PathBuf::from("r/current_dir.md"),
181 PathBuf::from("r/one"),
182 PathBuf::from("r/one/a.md"),
183 PathBuf::from("r/one/another"),
184 PathBuf::from("r/one/another/a.md"),
185 PathBuf::from("r/one/another/deep"),
186 PathBuf::from("r/one/another/deep/spelunking.md"),
187 PathBuf::from("r/three"),
188 PathBuf::from("r/three/c.md"),
189 PathBuf::from("r/two"),
190 PathBuf::from("r/two/b.md")
191 )
192 );
193
194 // followed by a wildcard
195 assert_eq!(
196 glob_vec("r/**/*.md"),
197 vec!(
198 PathBuf::from("r/another/a.md"),
199 PathBuf::from("r/current_dir.md"),
200 PathBuf::from("r/one/a.md"),
201 PathBuf::from("r/one/another/a.md"),
202 PathBuf::from("r/one/another/deep/spelunking.md"),
203 PathBuf::from("r/three/c.md"),
204 PathBuf::from("r/two/b.md")
205 )
206 );
207
208 // followed by a precise pattern
209 assert_eq!(
210 glob_vec("r/one/**/a.md"),
211 vec!(
212 PathBuf::from("r/one/a.md"),
213 PathBuf::from("r/one/another/a.md")
214 )
215 );
216
217 // followed by another recursive pattern
218 // collapses consecutive recursives into one
219 assert_eq!(
220 glob_vec("r/one/**/**/a.md"),
221 vec!(
222 PathBuf::from("r/one/a.md"),
223 PathBuf::from("r/one/another/a.md")
224 )
225 );
226
227 // followed by two precise patterns
228 assert_eq!(
229 glob_vec("r/**/another/a.md"),
230 vec!(
231 PathBuf::from("r/another/a.md"),
232 PathBuf::from("r/one/another/a.md")
233 )
234 );
235
236 assert_eq!(glob_vec(""), Vec::<PathBuf>::new());
237 assert_eq!(glob_vec("."), vec!(PathBuf::from(".")));
238 assert_eq!(glob_vec(".."), vec!(PathBuf::from("..")));
239
240 assert_eq!(glob_vec("aaa"), vec!(PathBuf::from("aaa")));
241 assert_eq!(glob_vec("aaa/"), vec!(PathBuf::from("aaa")));
242 assert_eq!(glob_vec("a"), Vec::<PathBuf>::new());
243 assert_eq!(glob_vec("aa"), Vec::<PathBuf>::new());
244 assert_eq!(glob_vec("aaaa"), Vec::<PathBuf>::new());
245
246 assert_eq!(glob_vec("aaa/apple"), vec!(PathBuf::from("aaa/apple")));
247 assert_eq!(glob_vec("aaa/apple/nope"), Vec::<PathBuf>::new());
248
249 // windows should support both / and \ as directory separators
250 if env::consts::FAMILY == "windows" {
251 assert_eq!(glob_vec("aaa\\apple"), vec!(PathBuf::from("aaa/apple")));
252 }
253
254 assert_eq!(
255 glob_vec("???/"),
256 vec!(
257 PathBuf::from("aaa"),
258 PathBuf::from("bbb"),
259 PathBuf::from("ccc"),
260 PathBuf::from("xyz")
261 )
262 );
263
264 assert_eq!(
265 glob_vec("aaa/tomato/tom?to.txt"),
266 vec!(
267 PathBuf::from("aaa/tomato/tomato.txt"),
268 PathBuf::from("aaa/tomato/tomoto.txt")
269 )
270 );
271
272 assert_eq!(
273 glob_vec("xyz/?"),
274 vec!(
275 PathBuf::from("xyz/x"),
276 PathBuf::from("xyz/y"),
277 PathBuf::from("xyz/z")
278 )
279 );
280
281 assert_eq!(glob_vec("a*"), vec!(PathBuf::from("aaa")));
282 assert_eq!(glob_vec("*a*"), vec!(PathBuf::from("aaa")));
283 assert_eq!(glob_vec("a*a"), vec!(PathBuf::from("aaa")));
284 assert_eq!(glob_vec("aaa*"), vec!(PathBuf::from("aaa")));
285 assert_eq!(glob_vec("*aaa"), vec!(PathBuf::from("aaa")));
286 assert_eq!(glob_vec("*aaa*"), vec!(PathBuf::from("aaa")));
287 assert_eq!(glob_vec("*a*a*a*"), vec!(PathBuf::from("aaa")));
288 assert_eq!(glob_vec("aaa*/"), vec!(PathBuf::from("aaa")));
289
290 assert_eq!(
291 glob_vec("aaa/*"),
292 vec!(
293 PathBuf::from("aaa/apple"),
294 PathBuf::from("aaa/green_apple"),
295 PathBuf::from("aaa/kazusa"),
296 PathBuf::from("aaa/orange"),
297 PathBuf::from("aaa/tomato"),
298 )
299 );
300
301 assert_eq!(
302 glob_vec("aaa/*a*"),
303 vec!(
304 PathBuf::from("aaa/apple"),
305 PathBuf::from("aaa/green_apple"),
306 PathBuf::from("aaa/kazusa"),
307 PathBuf::from("aaa/orange"),
308 PathBuf::from("aaa/tomato")
309 )
310 );
311
312 assert_eq!(
313 glob_vec("*/*/*.txt"),
314 vec!(
315 PathBuf::from("aaa/tomato/tomato.txt"),
316 PathBuf::from("aaa/tomato/tomoto.txt")
317 )
318 );
319
320 assert_eq!(
321 glob_vec("*/*/t[aob]m?to[.]t[!y]t"),
322 vec!(
323 PathBuf::from("aaa/tomato/tomato.txt"),
324 PathBuf::from("aaa/tomato/tomoto.txt")
325 )
326 );
327
328 assert_eq!(glob_vec("./aaa"), vec!(PathBuf::from("aaa")));
329 assert_eq!(glob_vec("./*"), glob_vec("*"));
330 assert_eq!(glob_vec("*/..").pop().unwrap(), PathBuf::from("xyz/.."));
331 assert_eq!(glob_vec("aaa/../bbb"), vec!(PathBuf::from("aaa/../bbb")));
332 assert_eq!(glob_vec("nonexistent/../bbb"), Vec::<PathBuf>::new());
333 assert_eq!(glob_vec("aaa/tomato/tomato.txt/.."), Vec::<PathBuf>::new());
334
335 assert_eq!(glob_vec("aaa/tomato/tomato.txt/"), Vec::<PathBuf>::new());
336
337 // Ensure to find a broken symlink.
338 assert_eq!(glob_vec("aaa/kazusa"), vec!(PathBuf::from("aaa/kazusa")));
339
340 assert_eq!(glob_vec("aa[a]"), vec!(PathBuf::from("aaa")));
341 assert_eq!(glob_vec("aa[abc]"), vec!(PathBuf::from("aaa")));
342 assert_eq!(glob_vec("a[bca]a"), vec!(PathBuf::from("aaa")));
343 assert_eq!(glob_vec("aa[b]"), Vec::<PathBuf>::new());
344 assert_eq!(glob_vec("aa[xyz]"), Vec::<PathBuf>::new());
345 assert_eq!(glob_vec("aa[]]"), Vec::<PathBuf>::new());
346
347 assert_eq!(glob_vec("aa[!b]"), vec!(PathBuf::from("aaa")));
348 assert_eq!(glob_vec("aa[!bcd]"), vec!(PathBuf::from("aaa")));
349 assert_eq!(glob_vec("a[!bcd]a"), vec!(PathBuf::from("aaa")));
350 assert_eq!(glob_vec("aa[!a]"), Vec::<PathBuf>::new());
351 assert_eq!(glob_vec("aa[!abc]"), Vec::<PathBuf>::new());
352
353 assert_eq!(
354 glob_vec("bbb/specials/[[]"),
355 vec!(PathBuf::from("bbb/specials/["))
356 );
357 assert_eq!(
358 glob_vec("bbb/specials/!"),
359 vec!(PathBuf::from("bbb/specials/!"))
360 );
361 assert_eq!(
362 glob_vec("bbb/specials/[]]"),
363 vec!(PathBuf::from("bbb/specials/]"))
364 );
365
366 mk_file("i", true);
367 mk_file("i/qwe", true);
368 mk_file("i/qwe/.aaa", false);
369 mk_file("i/qwe/.bbb", true);
370 mk_file("i/qwe/.bbb/ccc", false);
371 mk_file("i/qwe/.bbb/.ddd", false);
372 mk_file("i/qwe/eee", false);
373
374 let options = glob::MatchOptions {
375 case_sensitive: false,
376 require_literal_separator: true,
377 require_literal_leading_dot: true,
378 };
379 assert_eq!(glob_with_vec("i/**/*a*", options), Vec::<PathBuf>::new());
380 assert_eq!(glob_with_vec("i/**/*c*", options), Vec::<PathBuf>::new());
381 assert_eq!(glob_with_vec("i/**/*d*", options), Vec::<PathBuf>::new());
382 assert_eq!(
383 glob_with_vec("i/**/*e*", options),
384 vec!(PathBuf::from("i/qwe"), PathBuf::from("i/qwe/eee"))
385 );
386
387 if env::consts::FAMILY != "windows" {
388 assert_eq!(
389 glob_vec("bbb/specials/[*]"),
390 vec!(PathBuf::from("bbb/specials/*"))
391 );
392 assert_eq!(
393 glob_vec("bbb/specials/[?]"),
394 vec!(PathBuf::from("bbb/specials/?"))
395 );
396 }
397
398 if env::consts::FAMILY == "windows" {
399 assert_eq!(
400 glob_vec("bbb/specials/[![]"),
401 vec!(
402 PathBuf::from("bbb/specials/!"),
403 PathBuf::from("bbb/specials/]")
404 )
405 );
406
407 assert_eq!(
408 glob_vec("bbb/specials/[!]]"),
409 vec!(
410 PathBuf::from("bbb/specials/!"),
411 PathBuf::from("bbb/specials/[")
412 )
413 );
414
415 assert_eq!(
416 glob_vec("bbb/specials/[!!]"),
417 vec!(
418 PathBuf::from("bbb/specials/["),
419 PathBuf::from("bbb/specials/]")
420 )
421 );
422 } else {
423 assert_eq!(
424 glob_vec("bbb/specials/[![]"),
425 vec!(
426 PathBuf::from("bbb/specials/!"),
427 PathBuf::from("bbb/specials/*"),
428 PathBuf::from("bbb/specials/?"),
429 PathBuf::from("bbb/specials/]")
430 )
431 );
432
433 assert_eq!(
434 glob_vec("bbb/specials/[!]]"),
435 vec!(
436 PathBuf::from("bbb/specials/!"),
437 PathBuf::from("bbb/specials/*"),
438 PathBuf::from("bbb/specials/?"),
439 PathBuf::from("bbb/specials/[")
440 )
441 );
442
443 assert_eq!(
444 glob_vec("bbb/specials/[!!]"),
445 vec!(
446 PathBuf::from("bbb/specials/*"),
447 PathBuf::from("bbb/specials/?"),
448 PathBuf::from("bbb/specials/["),
449 PathBuf::from("bbb/specials/]")
450 )
451 );
452
453 assert_eq!(
454 glob_vec("bbb/specials/[!*]"),
455 vec!(
456 PathBuf::from("bbb/specials/!"),
457 PathBuf::from("bbb/specials/?"),
458 PathBuf::from("bbb/specials/["),
459 PathBuf::from("bbb/specials/]")
460 )
461 );
462
463 assert_eq!(
464 glob_vec("bbb/specials/[!?]"),
465 vec!(
466 PathBuf::from("bbb/specials/!"),
467 PathBuf::from("bbb/specials/*"),
468 PathBuf::from("bbb/specials/["),
469 PathBuf::from("bbb/specials/]")
470 )
471 );
472 }
473 }