1 /++
2 $(H2 Scriptlike $(SCRIPTLIKE_VERSION))
3 
4 Extra Scriptlike-only functionality to complement and wrap $(MODULE_STD_PATH),
5 providing extra functionality, such as no-fail "try*" alternatives, and support
6 for Scriptlike's $(API_PATH_EXTR Path), command echoing and dry-run features.
7 
8 Modules:
9 $(UL
10 	$(LI $(MODULE_PATH_EXTR) )
11 	$(LI $(MODULE_PATH_WRAP) )
12 )
13 
14 Copyright: Copyright (C) 2014-2017 Nick Sabalausky
15 License:   zlib/libpng
16 Authors:   Nick Sabalausky
17 +/
18 module scriptlike.path;
19 
20 public import scriptlike.path.extras;
21 public import scriptlike.path.wrappers;
22 
23 // The unittests in this module mainly check that all the templates compile
24 // correctly and that the appropriate Phobos functions are correctly called.
25 //
26 // A completely thorough testing of the behavior of such functions is
27 // occasionally left to Phobos itself as it is outside the scope of these tests.
28 version(unittest_scriptlike_d)
29 unittest
30 {
31 	import std.algorithm;
32 	import std.conv;
33 	import std.datetime;
34 	import std.file;
35 	import std.process;
36 	import std.range;
37 	import std.stdio;
38 	import std..string;
39 	import std.traits;
40 	import std.typecons;
41 	import std.typetuple;
42 
43 	import std.stdio : writeln;
44 	writeln("Running Scriptlike unittests: std.path wrappers");
45 	
46 	alias dirSep = dirSeparator;
47 
48 	{
49 		auto e = Ext(".txt");
50 		assert(e != Ext(".dat"));
51 		assert(e == Ext(".txt"));
52 		version(Windows)
53 			assert(e == Ext(".TXT"));
54 		else version(OSX)
55 			assert(e == Ext(".TXT"));
56 		else version(Posix)
57 			assert(e != Ext(".TXT"));
58 		else
59 			static assert(0, "This platform not supported.");
60 		
61 		// Test the other comparison overloads
62 		assert(e != Ext(".dat"));
63 		assert(e == Ext(".txt"));
64 		assert(Ext(".dat") != e);
65 		assert(Ext(".txt") == e);
66 		assert(".dat" != e);
67 		assert(".txt" == e);
68 
69 		assert(Ext("foo"));
70 		assert(Ext(""));
71 		assert(Ext(null).toString() is null);
72 		assert(!Ext(null));
73 	}
74 
75 	auto p = Path();
76 	assert(p.raw == ".");
77 	assert(!p.empty);
78 	
79 	assert(Path("").empty);
80 	
81 	assert(Path("foo"));
82 	assert(Path(""));
83 	assert(Path(null).raw is null);
84 	assert(!Path(null));
85 	
86 	version(Windows)
87 		auto testStrings = ["/foo/bar", "/foo/bar/", `\foo\bar`, `\foo\bar\`];
88 	else version(Posix)
89 		auto testStrings = ["/foo/bar", "/foo/bar/"];
90 	else
91 		static assert(0, "This platform not supported.");
92 	
93 	foreach(str; testStrings)
94 	{
95 		writeln("  testing str: ", str);
96 		
97 		p = Path(str);
98 		assert(!p.empty);
99 		assert(p.raw == dirSep~"foo"~dirSep~"bar");
100 		
101 		p = Path(str);
102 		assert(p.raw == dirSep~"foo"~dirSep~"bar");
103 		assert(p.raw == p.raw);
104 		assert(p.toString()    == p.raw.to!string());
105 		
106 		assert(p.up.toString() == dirSep~"foo");
107 		assert(p.up.up.toString() == dirSep);
108 
109 		assert((p~"sub").toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"sub");
110 		assert((p~"sub"~"2").toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"sub"~dirSep~"2");
111 		assert((p~Path("sub")).toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"sub");
112 		
113 		version(Windows)
114 			assert((p~"sub dir").toString() == `"`~dirSep~"foo"~dirSep~"bar"~dirSep~"sub dir"~`"`);
115 		else version(Posix)
116 			assert((p~"sub dir").toString() == `'`~dirSep~"foo"~dirSep~"bar"~dirSep~`sub dir'`);
117 		else
118 			static assert(0, "This platform not supported.");
119 
120 		assert(("dir"~p).toString() == dirSep~"foo"~dirSep~"bar");
121 		assert(("dir"~Path(str[1..$])).toString() == "dir"~dirSep~"foo"~dirSep~"bar");
122 		
123 		p ~= "blah";
124 		assert(p.toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"blah");
125 		
126 		p ~= Path("more");
127 		assert(p.toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"blah"~dirSep~"more");
128 		
129 		p ~= "..";
130 		assert(p.toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"blah");
131 		
132 		p ~= Path("..");
133 		assert(p.toString() == dirSep~"foo"~dirSep~"bar");
134 		
135 		p ~= "sub dir";
136 		p ~= "..";
137 		assert(p.toString() == dirSep~"foo"~dirSep~"bar");
138 		
139 		p ~= "filename";
140 		assert((p~Ext(".txt")).toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"filename.txt");
141 		assert((p~Ext("txt")).toString()  == dirSep~"foo"~dirSep~"bar"~dirSep~"filename.txt");
142 		assert((p~Ext("")).toString()     == dirSep~"foo"~dirSep~"bar"~dirSep~"filename");
143 
144 		p ~= Ext(".ext");
145 		assert(p.toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"filename.ext");
146 		assert(p.baseName().toString() == "filename.ext");
147 		assert(p.dirName().toString() == dirSep~"foo"~dirSep~"bar");
148 		assert(p.rootName().toString() == dirSep);
149 		assert(p.driveName().toString() == "");
150 		assert(p.stripDrive().toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"filename.ext");
151 		version(Windows)
152 		{
153 			assert(( Path("C:"~p.raw) ).toString() == "C:"~dirSep~"foo"~dirSep~"bar"~dirSep~"filename.ext");
154 			assert(( Path("C:"~p.raw) ).stripDrive().toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"filename.ext");
155 		}
156 		assert(p.extension().toString() == ".ext");
157 		assert(p.stripExtension().toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"filename");
158 		assert(p.setExtension(".txt").toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"filename.txt");
159 		assert(p.setExtension("txt").toString()  == dirSep~"foo"~dirSep~"bar"~dirSep~"filename.txt");
160 		assert(p.setExtension("").toString()     == dirSep~"foo"~dirSep~"bar"~dirSep~"filename");
161 		assert(p.setExtension(Ext(".txt")).toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"filename.txt");
162 		assert(p.setExtension(Ext("txt")).toString()  == dirSep~"foo"~dirSep~"bar"~dirSep~"filename.txt");
163 		assert(p.setExtension(Ext("")).toString()     == dirSep~"foo"~dirSep~"bar"~dirSep~"filename");
164 
165 		assert(p.defaultExtension(".dat").toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"filename.ext");
166 		assert(p.stripExtension().defaultExtension(".dat").toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"filename.dat");
167 
168 		assert(equal(p.pathSplitter(), [dirSep, "foo", "bar", "filename.ext"]));
169 
170 		assert(p.isRooted());
171 		version(Windows)
172 			assert(!p.isAbsolute());
173 		else version(Posix)
174 			assert(p.isAbsolute());
175 		else
176 			static assert(0, "This platform not supported.");
177 
178 		assert(!( Path("dir"~p.raw) ).isRooted());
179 		assert(!( Path("dir"~p.raw) ).isAbsolute());
180 		
181 		version(Windows)
182 		{
183 			assert(( Path("dir"~p.raw) ).absolutePath("C:/main").toString() == "C:"~dirSep~"main"~dirSep~"dir"~dirSep~"foo"~dirSep~"bar"~dirSep~"filename.ext");
184 			assert(( Path("C:"~p.raw) ).relativePath("C:/foo").toString() == "bar"~dirSep~"filename.ext");
185 			assert(( Path("C:"~p.raw) ).relativePath("C:/foo/bar").toString() == "filename.ext");
186 		}
187 		else version(Posix)
188 		{
189 			assert(( Path("dir"~p.raw) ).absolutePath("/main").toString() == dirSep~"main"~dirSep~"dir"~dirSep~"foo"~dirSep~"bar"~dirSep~"filename.ext");
190 			assert(p.relativePath("/foo").toString() == "bar"~dirSep~"filename.ext");
191 			assert(p.relativePath("/foo/bar").toString() == "filename.ext");
192 		}
193 		else
194 			static assert(0, "This platform not supported.");
195 
196 		assert(p.filenameCmp(dirSep~"foo"~dirSep~"bar"~dirSep~"filename.ext") == 0);
197 		assert(p.filenameCmp(dirSep~"faa"~dirSep~"bat"~dirSep~"filename.ext") != 0);
198 		assert(p.globMatch("*foo*name.ext"));
199 		assert(!p.globMatch("*foo*Bname.ext"));
200 
201 		assert(!p.isValidFilename());
202 		assert(p.baseName().isValidFilename());
203 		assert(p.isValidPath());
204 		
205 		assert(p.expandTilde().toString() == dirSep~"foo"~dirSep~"bar"~dirSep~"filename.ext");
206 		
207 		assert(p != Path("/dir/subdir/filename.ext"));
208 		assert(p == Path("/foo/bar/filename.ext"));
209 		version(Windows)
210 			assert(p == Path("/FOO/BAR/FILENAME.EXT"));
211 		else version(OSX)
212 			assert(p == Path("/FOO/BAR/FILENAME.EXT"));
213 		else version(Posix)
214 			assert(p != Path("/FOO/BAR/FILENAME.EXT"));
215 		else
216 			static assert(0, "This platform not supported.");
217 		
218 		// Test the other comparison overloads
219 		assert(p != Path("/dir/subdir/filename.ext"));
220 		assert(p == Path("/foo/bar/filename.ext"));
221 		assert(Path("/dir/subdir/filename.ext") != p);
222 		assert(Path("/foo/bar/filename.ext")    == p);
223 		assert("/dir/subdir/filename.ext" != p);
224 		assert("/foo/bar/filename.ext"    == p);
225 	}
226 }