Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_path.d

raw
1 @safe pure nothrow @nogc unittest
2 {
3 import std.path;
4
5 version (Windows)
6 {
7 assert( '/'.isDirSeparator);
8 assert( '\\'.isDirSeparator);
9 }
10 else
11 {
12 assert( '/'.isDirSeparator);
13 assert(!'\\'.isDirSeparator);
14 }
15 }
16
17 @safe unittest
18 {
19 import std.path;
20
21 assert(baseName!(CaseSensitive.no)("dir/file.EXT", ".ext") == "file");
22 assert(baseName!(CaseSensitive.yes)("dir/file.EXT", ".ext") != "file");
23
24 version (Posix)
25 assert(relativePath!(CaseSensitive.no)("/FOO/bar", "/foo/baz") == "../bar");
26 else
27 assert(relativePath!(CaseSensitive.no)(`c:\FOO\bar`, `c:\foo\baz`) == `..\bar`);
28 }
29
30 @safe unittest
31 {
32 import std.path;
33
34 assert(baseName("dir/file.ext") == "file.ext");
35 assert(baseName("dir/file.ext", ".ext") == "file");
36 assert(baseName("dir/file.ext", ".xyz") == "file.ext");
37 assert(baseName("dir/filename", "name") == "file");
38 assert(baseName("dir/subdir/") == "subdir");
39
40 version (Windows)
41 {
42 assert(baseName(`d:file.ext`) == "file.ext");
43 assert(baseName(`d:\dir\file.ext`) == "file.ext");
44 }
45 }
46
47 @safe unittest
48 {
49 import std.path;
50
51 assert(dirName("") == ".");
52 assert(dirName("file"w) == ".");
53 assert(dirName("dir/"d) == ".");
54 assert(dirName("dir///") == ".");
55 assert(dirName("dir/file"w.dup) == "dir");
56 assert(dirName("dir///file"d.dup) == "dir");
57 assert(dirName("dir/subdir/") == "dir");
58 assert(dirName("/dir/file"w) == "/dir");
59 assert(dirName("/file"d) == "/");
60 assert(dirName("/") == "/");
61 assert(dirName("///") == "/");
62
63 version (Windows)
64 {
65 assert(dirName(`dir\`) == `.`);
66 assert(dirName(`dir\\\`) == `.`);
67 assert(dirName(`dir\file`) == `dir`);
68 assert(dirName(`dir\\\file`) == `dir`);
69 assert(dirName(`dir\subdir\`) == `dir`);
70 assert(dirName(`\dir\file`) == `\dir`);
71 assert(dirName(`\file`) == `\`);
72 assert(dirName(`\`) == `\`);
73 assert(dirName(`\\\`) == `\`);
74 assert(dirName(`d:`) == `d:`);
75 assert(dirName(`d:file`) == `d:`);
76 assert(dirName(`d:\`) == `d:\`);
77 assert(dirName(`d:\file`) == `d:\`);
78 assert(dirName(`d:\dir\file`) == `d:\dir`);
79 assert(dirName(`\\server\share\dir\file`) == `\\server\share\dir`);
80 assert(dirName(`\\server\share\file`) == `\\server\share`);
81 assert(dirName(`\\server\share\`) == `\\server\share`);
82 assert(dirName(`\\server\share`) == `\\server\share`);
83 }
84 }
85
86 @safe unittest
87 {
88 import std.path;
89
90 assert(rootName("") is null);
91 assert(rootName("foo") is null);
92 assert(rootName("/") == "/");
93 assert(rootName("/foo/bar") == "/");
94
95 version (Windows)
96 {
97 assert(rootName("d:foo") is null);
98 assert(rootName(`d:\foo`) == `d:\`);
99 assert(rootName(`\\server\share\foo`) == `\\server\share`);
100 assert(rootName(`\\server\share`) == `\\server\share`);
101 }
102 }
103
104 @safe unittest
105 {
106 import std.path;
107
108 import std.range : empty;
109 version (Posix) assert(driveName("c:/foo").empty);
110 version (Windows)
111 {
112 assert(driveName(`dir\file`).empty);
113 assert(driveName(`d:file`) == "d:");
114 assert(driveName(`d:\file`) == "d:");
115 assert(driveName("d:") == "d:");
116 assert(driveName(`\\server\share\file`) == `\\server\share`);
117 assert(driveName(`\\server\share\`) == `\\server\share`);
118 assert(driveName(`\\server\share`) == `\\server\share`);
119
120 static assert(driveName(`d:\file`) == "d:");
121 }
122 }
123
124 @safe unittest
125 {
126 import std.path;
127
128 version (Windows)
129 {
130 assert(stripDrive(`d:\dir\file`) == `\dir\file`);
131 assert(stripDrive(`\\server\share\dir\file`) == `\dir\file`);
132 }
133 }
134
135 @safe unittest
136 {
137 import std.path;
138
139 import std.range : empty;
140 assert(extension("file").empty);
141 assert(extension("file.") == ".");
142 assert(extension("file.ext"w) == ".ext");
143 assert(extension("file.ext1.ext2"d) == ".ext2");
144 assert(extension(".foo".dup).empty);
145 assert(extension(".foo.ext"w.dup) == ".ext");
146
147 static assert(extension("file").empty);
148 static assert(extension("file.ext") == ".ext");
149 }
150
151 @safe unittest
152 {
153 import std.path;
154
155 assert(stripExtension("file") == "file");
156 assert(stripExtension("file.ext") == "file");
157 assert(stripExtension("file.ext1.ext2") == "file.ext1");
158 assert(stripExtension("file.") == "file");
159 assert(stripExtension(".file") == ".file");
160 assert(stripExtension(".file.ext") == ".file");
161 assert(stripExtension("dir/file.ext") == "dir/file");
162 }
163
164 @safe unittest
165 {
166 import std.path;
167
168 assert(setExtension("file", "ext") == "file.ext");
169 assert(setExtension("file"w, ".ext"w) == "file.ext");
170 assert(setExtension("file."d, "ext"d) == "file.ext");
171 assert(setExtension("file.", ".ext") == "file.ext");
172 assert(setExtension("file.old"w, "new"w) == "file.new");
173 assert(setExtension("file.old"d, ".new"d) == "file.new");
174 }
175
176 @safe unittest
177 {
178 import std.path;
179
180 import std.array;
181 assert(withExtension("file", "ext").array == "file.ext");
182 assert(withExtension("file"w, ".ext"w).array == "file.ext");
183 assert(withExtension("file.ext"w, ".").array == "file.");
184
185 import std.utf : byChar, byWchar;
186 assert(withExtension("file".byChar, "ext").array == "file.ext");
187 assert(withExtension("file"w.byWchar, ".ext"w).array == "file.ext"w);
188 assert(withExtension("file.ext"w.byWchar, ".").array == "file."w);
189 }
190
191 @safe unittest
192 {
193 import std.path;
194
195 assert(defaultExtension("file", "ext") == "file.ext");
196 assert(defaultExtension("file", ".ext") == "file.ext");
197 assert(defaultExtension("file.", "ext") == "file.");
198 assert(defaultExtension("file.old", "new") == "file.old");
199 assert(defaultExtension("file.old", ".new") == "file.old");
200 }
201
202 @safe unittest
203 {
204 import std.path;
205
206 import std.array;
207 assert(withDefaultExtension("file", "ext").array == "file.ext");
208 assert(withDefaultExtension("file"w, ".ext").array == "file.ext"w);
209 assert(withDefaultExtension("file.", "ext").array == "file.");
210 assert(withDefaultExtension("file", "").array == "file.");
211
212 import std.utf : byChar, byWchar;
213 assert(withDefaultExtension("file".byChar, "ext").array == "file.ext");
214 assert(withDefaultExtension("file"w.byWchar, ".ext").array == "file.ext"w);
215 assert(withDefaultExtension("file.".byChar, "ext"d).array == "file.");
216 assert(withDefaultExtension("file".byChar, "").array == "file.");
217 }
218
219 @safe unittest
220 {
221 import std.path;
222
223 version (Posix)
224 {
225 assert(buildPath("foo", "bar", "baz") == "foo/bar/baz");
226 assert(buildPath("/foo/", "bar/baz") == "/foo/bar/baz");
227 assert(buildPath("/foo", "/bar") == "/bar");
228 }
229
230 version (Windows)
231 {
232 assert(buildPath("foo", "bar", "baz") == `foo\bar\baz`);
233 assert(buildPath(`c:\foo`, `bar\baz`) == `c:\foo\bar\baz`);
234 assert(buildPath("foo", `d:\bar`) == `d:\bar`);
235 assert(buildPath("foo", `\bar`) == `\bar`);
236 assert(buildPath(`c:\foo`, `\bar`) == `c:\bar`);
237 }
238 }
239
240 @safe unittest
241 {
242 import std.path;
243
244 import std.array;
245 version (Posix)
246 {
247 assert(chainPath("foo", "bar", "baz").array == "foo/bar/baz");
248 assert(chainPath("/foo/", "bar/baz").array == "/foo/bar/baz");
249 assert(chainPath("/foo", "/bar").array == "/bar");
250 }
251
252 version (Windows)
253 {
254 assert(chainPath("foo", "bar", "baz").array == `foo\bar\baz`);
255 assert(chainPath(`c:\foo`, `bar\baz`).array == `c:\foo\bar\baz`);
256 assert(chainPath("foo", `d:\bar`).array == `d:\bar`);
257 assert(chainPath("foo", `\bar`).array == `\bar`);
258 assert(chainPath(`c:\foo`, `\bar`).array == `c:\bar`);
259 }
260
261 import std.utf : byChar;
262 version (Posix)
263 {
264 assert(chainPath("foo", "bar", "baz").array == "foo/bar/baz");
265 assert(chainPath("/foo/".byChar, "bar/baz").array == "/foo/bar/baz");
266 assert(chainPath("/foo", "/bar".byChar).array == "/bar");
267 }
268
269 version (Windows)
270 {
271 assert(chainPath("foo", "bar", "baz").array == `foo\bar\baz`);
272 assert(chainPath(`c:\foo`.byChar, `bar\baz`).array == `c:\foo\bar\baz`);
273 assert(chainPath("foo", `d:\bar`).array == `d:\bar`);
274 assert(chainPath("foo", `\bar`.byChar).array == `\bar`);
275 assert(chainPath(`c:\foo`, `\bar`w).array == `c:\bar`);
276 }
277 }
278
279 @safe unittest
280 {
281 import std.path;
282
283 assert(buildNormalizedPath("foo", "..") == ".");
284
285 version (Posix)
286 {
287 assert(buildNormalizedPath("/foo/./bar/..//baz/") == "/foo/baz");
288 assert(buildNormalizedPath("../foo/.") == "../foo");
289 assert(buildNormalizedPath("/foo", "bar/baz/") == "/foo/bar/baz");
290 assert(buildNormalizedPath("/foo", "/bar/..", "baz") == "/baz");
291 assert(buildNormalizedPath("foo/./bar", "../../", "../baz") == "../baz");
292 assert(buildNormalizedPath("/foo/./bar", "../../baz") == "/baz");
293 }
294
295 version (Windows)
296 {
297 assert(buildNormalizedPath(`c:\foo\.\bar/..\\baz\`) == `c:\foo\baz`);
298 assert(buildNormalizedPath(`..\foo\.`) == `..\foo`);
299 assert(buildNormalizedPath(`c:\foo`, `bar\baz\`) == `c:\foo\bar\baz`);
300 assert(buildNormalizedPath(`c:\foo`, `bar/..`) == `c:\foo`);
301 assert(buildNormalizedPath(`\\server\share\foo`, `..\bar`) ==
302 `\\server\share\bar`);
303 }
304 }
305
306 @safe unittest
307 {
308 import std.path;
309
310 import std.array;
311 assert(asNormalizedPath("foo/..").array == ".");
312
313 version (Posix)
314 {
315 assert(asNormalizedPath("/foo/./bar/..//baz/").array == "/foo/baz");
316 assert(asNormalizedPath("../foo/.").array == "../foo");
317 assert(asNormalizedPath("/foo/bar/baz/").array == "/foo/bar/baz");
318 assert(asNormalizedPath("/foo/./bar/../../baz").array == "/baz");
319 }
320
321 version (Windows)
322 {
323 assert(asNormalizedPath(`c:\foo\.\bar/..\\baz\`).array == `c:\foo\baz`);
324 assert(asNormalizedPath(`..\foo\.`).array == `..\foo`);
325 assert(asNormalizedPath(`c:\foo\bar\baz\`).array == `c:\foo\bar\baz`);
326 assert(asNormalizedPath(`c:\foo\bar/..`).array == `c:\foo`);
327 assert(asNormalizedPath(`\\server\share\foo\..\bar`).array ==
328 `\\server\share\bar`);
329 }
330 }
331
332 @safe unittest
333 {
334 import std.path;
335
336 import std.algorithm.comparison : equal;
337 import std.conv : to;
338
339 assert(equal(pathSplitter("/"), ["/"]));
340 assert(equal(pathSplitter("/foo/bar"), ["/", "foo", "bar"]));
341 assert(equal(pathSplitter("foo/../bar//./"), ["foo", "..", "bar", "."]));
342
343 version (Posix)
344 {
345 assert(equal(pathSplitter("//foo/bar"), ["/", "foo", "bar"]));
346 }
347
348 version (Windows)
349 {
350 assert(equal(pathSplitter(`foo\..\bar\/.\`), ["foo", "..", "bar", "."]));
351 assert(equal(pathSplitter("c:"), ["c:"]));
352 assert(equal(pathSplitter(`c:\foo\bar`), [`c:\`, "foo", "bar"]));
353 assert(equal(pathSplitter(`c:foo\bar`), ["c:foo", "bar"]));
354 }
355 }
356
357 @safe unittest
358 {
359 import std.path;
360
361 version (Posix)
362 {
363 assert( isRooted("/"));
364 assert( isRooted("/foo"));
365 assert(!isRooted("foo"));
366 assert(!isRooted("../foo"));
367 }
368
369 version (Windows)
370 {
371 assert( isRooted(`\`));
372 assert( isRooted(`\foo`));
373 assert( isRooted(`d:\foo`));
374 assert( isRooted(`\\foo\bar`));
375 assert(!isRooted("foo"));
376 assert(!isRooted("d:foo"));
377 }
378 }
379
380 @safe unittest
381 {
382 import std.path;
383
384 version (Posix)
385 {
386 assert(absolutePath("some/file", "/foo/bar") == "/foo/bar/some/file");
387 assert(absolutePath("../file", "/foo/bar") == "/foo/bar/../file");
388 assert(absolutePath("/some/file", "/foo/bar") == "/some/file");
389 }
390
391 version (Windows)
392 {
393 assert(absolutePath(`some\file`, `c:\foo\bar`) == `c:\foo\bar\some\file`);
394 assert(absolutePath(`..\file`, `c:\foo\bar`) == `c:\foo\bar\..\file`);
395 assert(absolutePath(`c:\some\file`, `c:\foo\bar`) == `c:\some\file`);
396 assert(absolutePath(`\`, `c:\`) == `c:\`);
397 assert(absolutePath(`\some\file`, `c:\foo\bar`) == `c:\some\file`);
398 }
399 }
400
401 @system unittest
402 {
403 import std.path;
404
405 import std.array;
406 assert(asAbsolutePath(cast(string) null).array == "");
407 version (Posix)
408 {
409 assert(asAbsolutePath("/foo").array == "/foo");
410 }
411 version (Windows)
412 {
413 assert(asAbsolutePath("c:/foo").array == "c:/foo");
414 }
415 asAbsolutePath("foo");
416 }
417
418 @safe unittest
419 {
420 import std.path;
421
422 assert(relativePath("foo") == "foo");
423
424 version (Posix)
425 {
426 assert(relativePath("foo", "/bar") == "foo");
427 assert(relativePath("/foo/bar", "/foo/bar") == ".");
428 assert(relativePath("/foo/bar", "/foo/baz") == "../bar");
429 assert(relativePath("/foo/bar/baz", "/foo/woo/wee") == "../../bar/baz");
430 assert(relativePath("/foo/bar/baz", "/foo/bar") == "baz");
431 }
432 version (Windows)
433 {
434 assert(relativePath("foo", `c:\bar`) == "foo");
435 assert(relativePath(`c:\foo\bar`, `c:\foo\bar`) == ".");
436 assert(relativePath(`c:\foo\bar`, `c:\foo\baz`) == `..\bar`);
437 assert(relativePath(`c:\foo\bar\baz`, `c:\foo\woo\wee`) == `..\..\bar\baz`);
438 assert(relativePath(`c:\foo\bar\baz`, `c:\foo\bar`) == "baz");
439 assert(relativePath(`c:\foo\bar`, `d:\foo`) == `c:\foo\bar`);
440 }
441 }
442
443 @safe unittest
444 {
445 import std.path;
446
447 import std.array;
448 version (Posix)
449 {
450 assert(asRelativePath("foo", "/bar").array == "foo");
451 assert(asRelativePath("/foo/bar", "/foo/bar").array == ".");
452 assert(asRelativePath("/foo/bar", "/foo/baz").array == "../bar");
453 assert(asRelativePath("/foo/bar/baz", "/foo/woo/wee").array == "../../bar/baz");
454 assert(asRelativePath("/foo/bar/baz", "/foo/bar").array == "baz");
455 }
456 else version (Windows)
457 {
458 assert(asRelativePath("foo", `c:\bar`).array == "foo");
459 assert(asRelativePath(`c:\foo\bar`, `c:\foo\bar`).array == ".");
460 assert(asRelativePath(`c:\foo\bar`, `c:\foo\baz`).array == `..\bar`);
461 assert(asRelativePath(`c:\foo\bar\baz`, `c:\foo\woo\wee`).array == `..\..\bar\baz`);
462 assert(asRelativePath(`c:/foo/bar/baz`, `c:\foo\woo\wee`).array == `..\..\bar\baz`);
463 assert(asRelativePath(`c:\foo\bar\baz`, `c:\foo\bar`).array == "baz");
464 assert(asRelativePath(`c:\foo\bar`, `d:\foo`).array == `c:\foo\bar`);
465 assert(asRelativePath(`\\foo\bar`, `c:\foo`).array == `\\foo\bar`);
466 }
467 else
468 static assert(0);
469 }
470
471 @safe unittest
472 {
473 import std.path;
474
475 assert(filenameCharCmp('a', 'a') == 0);
476 assert(filenameCharCmp('a', 'b') < 0);
477 assert(filenameCharCmp('b', 'a') > 0);
478
479 version (linux)
480 {
481 // Same as calling filenameCharCmp!(CaseSensitive.yes)(a, b)
482 assert(filenameCharCmp('A', 'a') < 0);
483 assert(filenameCharCmp('a', 'A') > 0);
484 }
485 version (Windows)
486 {
487 // Same as calling filenameCharCmp!(CaseSensitive.no)(a, b)
488 assert(filenameCharCmp('a', 'A') == 0);
489 assert(filenameCharCmp('a', 'B') < 0);
490 assert(filenameCharCmp('A', 'b') < 0);
491 }
492 }
493
494 @safe unittest
495 {
496 import std.path;
497
498 assert(filenameCmp("abc", "abc") == 0);
499 assert(filenameCmp("abc", "abd") < 0);
500 assert(filenameCmp("abc", "abb") > 0);
501 assert(filenameCmp("abc", "abcd") < 0);
502 assert(filenameCmp("abcd", "abc") > 0);
503
504 version (linux)
505 {
506 // Same as calling filenameCmp!(CaseSensitive.yes)(filename1, filename2)
507 assert(filenameCmp("Abc", "abc") < 0);
508 assert(filenameCmp("abc", "Abc") > 0);
509 }
510 version (Windows)
511 {
512 // Same as calling filenameCmp!(CaseSensitive.no)(filename1, filename2)
513 assert(filenameCmp("Abc", "abc") == 0);
514 assert(filenameCmp("abc", "Abc") == 0);
515 assert(filenameCmp("Abc", "abD") < 0);
516 assert(filenameCmp("abc", "AbB") > 0);
517 }
518 }
519
520 @safe @nogc unittest
521 {
522 import std.path;
523
524 assert(globMatch("foo.bar", "*"));
525 assert(globMatch("foo.bar", "*.*"));
526 assert(globMatch(`foo/foo\bar`, "f*b*r"));
527 assert(globMatch("foo.bar", "f???bar"));
528 assert(globMatch("foo.bar", "[fg]???bar"));
529 assert(globMatch("foo.bar", "[!gh]*bar"));
530 assert(globMatch("bar.fooz", "bar.{foo,bif}z"));
531 assert(globMatch("bar.bifz", "bar.{foo,bif}z"));
532
533 version (Windows)
534 {
535 // Same as calling globMatch!(CaseSensitive.no)(path, pattern)
536 assert(globMatch("foo", "Foo"));
537 assert(globMatch("Goo.bar", "[fg]???bar"));
538 }
539 version (linux)
540 {
541 // Same as calling globMatch!(CaseSensitive.yes)(path, pattern)
542 assert(!globMatch("foo", "Foo"));
543 assert(!globMatch("Goo.bar", "[fg]???bar"));
544 }
545 }
546
547 @safe pure @nogc nothrow unittest
548 {
549 import std.path;
550
551 import std.utf : byCodeUnit;
552
553 assert(isValidFilename("hello.exe".byCodeUnit));
554 }
555
556 @safe pure @nogc nothrow unittest
557 {
558 import std.path;
559
560 assert(isValidPath("/foo/bar"));
561 assert(!isValidPath("/foo\0/bar"));
562 assert(isValidPath("/"));
563 assert(isValidPath("a"));
564
565 version (Windows)
566 {
567 assert(isValidPath(`c:\`));
568 assert(isValidPath(`c:\foo`));
569 assert(isValidPath(`c:\foo\.\bar\\\..\`));
570 assert(!isValidPath(`!:\foo`));
571 assert(!isValidPath(`c::\foo`));
572 assert(!isValidPath(`c:\foo?`));
573 assert(!isValidPath(`c:\foo.`));
574
575 assert(isValidPath(`\\server\share`));
576 assert(isValidPath(`\\server\share\foo`));
577 assert(isValidPath(`\\server\share\\foo`));
578 assert(!isValidPath(`\\\server\share\foo`));
579 assert(!isValidPath(`\\server\\share\foo`));
580 assert(!isValidPath(`\\ser*er\share\foo`));
581 assert(!isValidPath(`\\server\sha?e\foo`));
582 assert(!isValidPath(`\\server\share\|oo`));
583
584 assert(isValidPath(`\\?\<>:"?*|/\..\.`));
585 assert(!isValidPath("\\\\?\\foo\0bar"));
586
587 assert(!isValidPath(`\\.\PhysicalDisk1`));
588 assert(!isValidPath(`\\`));
589 }
590
591 import std.utf : byCodeUnit;
592 assert(isValidPath("/foo/bar".byCodeUnit));
593 }
594
595 @safe unittest
596 {
597 import std.path;
598
599 version (Posix)
600 {
601 import std.process : environment;
602
603 auto oldHome = environment["HOME"];
604 scope(exit) environment["HOME"] = oldHome;
605
606 environment["HOME"] = "dmd/test";
607 assert(expandTilde("~/") == "dmd/test/");
608 assert(expandTilde("~") == "dmd/test");
609 }
610 }
611