1 /++
2 $(H2 Scriptlike $(SCRIPTLIKE_VERSION))
3 
4 Extra Scriptlike-only functionality to complement and wrap $(MODULE_STD_FILE),
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_FILE_EXTR) )
11 	$(LI $(MODULE_FILE_WRAP) )
12 )
13 
14 Copyright: Copyright (C) 2014-2017 Nick Sabalausky
15 License:   zlib/libpng
16 Authors:   Nick Sabalausky
17 +/
18 module scriptlike.file;
19 
20 public import scriptlike.file.extras;
21 public import scriptlike.file.wrappers;
22 
23 version(unittest_scriptlike_d)
24 unittest
25 {
26 	import std.algorithm : equal;
27 	import std.conv;
28 	import std.datetime : SysTime;
29 	static import std.file;
30 	static import std.path;
31 	import std..string;
32 	import std.traits;
33 	import std.typecons;
34 
35 	import scriptlike.path;
36 	import scriptlike.core : tmpName;
37 	
38 	import std.stdio : writeln;
39 	import std.process : thisProcessID;
40 	alias copy = scriptlike.file.wrappers.copy;
41 
42 	writeln("Running Scriptlike unittests: std.file wrappers");
43 	
44 	immutable tempname1 = tmpName("1");
45 	immutable tempname2 = tmpName("2");
46 	immutable tempname3 = tmpName("3", "somefile");
47 	auto tempPath  = Path(tempname1);
48 	auto tempPath2 = Path(tempname2);
49 	auto tempPath3 = Path(tempname3);
50 	
51 	void testA(T)(T tempPath)
52 	{
53 		scope(exit)
54 		{
55 			if(std.file.exists(tempname1)) std.file.remove(tempname1);
56 		}
57 
58 		tempPath.write("stuff");
59 
60 		tempPath.append(" more");
61 		assert(tempPath.read(3) == "stu");
62 		assert(tempPath.read() == "stuff more");
63 		assert(tempPath.readText() == "stuff more");
64 		assert(tempPath.getSize() == 10);
65 
66 		auto parsed = tempPath.slurp!(string, string)("%s %s");
67 		assert(equal(parsed, [tuple("stuff", "more")]));
68 		
69 		SysTime timeA, timeB, timeC;
70 		tempPath.getTimes(timeA, timeB);
71 		version(Windows)
72 			tempPath.getTimesWin(timeA, timeB, timeC);
73 		tempPath.setTimes(timeA, timeB);
74 		timeA = tempPath.timeLastModified();
75 		timeA = tempPath.timeLastModified(timeB);
76 		
77 		uint attr;
78 		attr = tempPath.getAttributes();
79 		attr = tempPath.getLinkAttributes();
80 		
81 		assert(tempPath.exists());
82 		assert(tempPath.isFile());
83 		assert(tempPath.existsAsFile());
84 		assert(!tempPath.isDir());
85 		assert(!tempPath.existsAsDir());
86 		assert(!tempPath.isSymlink());
87 		assert(!tempPath.existsAsSymlink());
88 		tempPath.remove();
89 		assert(!tempPath.exists());
90 		assert(!tempPath.existsAsFile());
91 		assert(!tempPath.existsAsDir());
92 		assert(!tempPath.existsAsSymlink());
93 	}
94 
95 	import std.stdio : stdout;
96 	writeln("    testA with string"); stdout.flush();
97 	testA(tempPath.raw); // Test with string
98 
99 	writeln("    testA with Path"); stdout.flush();
100 	testA(tempPath); // Test with Path
101 
102 	writeln("    more..."); stdout.flush();
103 	{
104 		assert(!tempPath.exists());
105 		assert(!tempPath2.exists());
106 
107 		scope(exit)
108 		{
109 			if(std.file.exists(tempname1)) std.file.remove(tempname1);
110 			if(std.file.exists(tempname2)) std.file.remove(tempname2);
111 		}
112 		tempPath.write("ABC");
113 		
114 		assert(tempPath.existsAsFile());
115 		assert(!tempPath2.exists());
116 
117 		tempPath.rename(tempPath2);
118 		
119 		assert(!tempPath.exists());
120 		assert(tempPath2.existsAsFile());
121 		
122 		tempPath2.copy(tempPath);
123 		
124 		assert(tempPath.existsAsFile());
125 		assert(tempPath2.existsAsFile());
126 	}
127 	
128 	{
129 		scope(exit)
130 		{
131 			if(std.file.exists(tempname1)) std.file.rmdir(tempname1);
132 			if(std.file.exists(tempname3)) std.file.rmdir(tempname3);
133 			if(std.file.exists( std.path.dirName(tempname3) )) std.file.rmdir( std.path.dirName(tempname3) );
134 		}
135 		
136 		assert(!tempPath.exists());
137 		assert(!tempPath3.exists());
138 		
139 		tempPath.mkdir();
140 		assert(tempPath.exists());
141 		assert(!tempPath.isFile());
142 		assert(!tempPath.existsAsFile());
143 		assert(tempPath.isDir());
144 		assert(tempPath.existsAsDir());
145 		assert(!tempPath.isSymlink());
146 		assert(!tempPath.existsAsSymlink());
147 
148 		tempPath3.mkdirRecurse();
149 		assert(tempPath3.exists());
150 		assert(!tempPath3.isFile());
151 		assert(!tempPath3.existsAsFile());
152 		assert(tempPath3.isDir());
153 		assert(tempPath3.existsAsDir());
154 		assert(!tempPath3.isSymlink());
155 		assert(!tempPath3.existsAsSymlink());
156 		
157 		auto saveDirName = std.file.getcwd();
158 		auto saveDir = Path(saveDirName);
159 		scope(exit) chdir(saveDirName);
160 
161 		tempPath.chdir();
162 		assert(getcwd() == tempname1);
163 		saveDir.chdir();
164 		assert(getcwd() == saveDirName);
165 		
166 		auto entries1 = (tempPath3~"..").dirEntries(SpanMode.shallow);
167 		assert(!entries1.empty);
168 		auto entries2 = (tempPath3~"..").dirEntries("*", SpanMode.shallow);
169 		assert(!entries2.empty);
170 		auto entries3 = (tempPath3~"..").dirEntries("TUNA TUNA THIS DOES NOT EXIST TUNA WHEE", SpanMode.shallow);
171 		assert(entries3.empty);
172 		
173 		tempPath.rmdir();
174 		assert(!tempPath.exists());
175 		assert(!tempPath.existsAsFile());
176 		assert(!tempPath.existsAsDir());
177 		assert(!tempPath.existsAsSymlink());
178 
179 		tempPath3.rmdirRecurse();
180 		assert(!tempPath.exists());
181 		assert(!tempPath.existsAsFile());
182 		assert(!tempPath.existsAsDir());
183 		assert(!tempPath.existsAsSymlink());
184 	}
185 	
186 	{
187 		version(Posix)
188 		{
189 			assert(!tempPath.exists());
190 			assert(!tempPath2.exists());
191 
192 			scope(exit)
193 			{
194 				if(std.file.exists(tempname2)) std.file.remove(tempname2);
195 				if(std.file.exists(tempname1)) std.file.remove(tempname1);
196 			}
197 			tempPath.write("DEF");
198 			
199 			tempPath.symlink(tempPath2);
200 			assert(tempPath2.exists());
201 			assert(tempPath2.isFile());
202 			assert(tempPath2.existsAsFile());
203 			assert(!tempPath2.isDir());
204 			assert(!tempPath2.existsAsDir());
205 			assert(tempPath2.isSymlink());
206 			assert(tempPath2.existsAsSymlink());
207 			
208 			auto linkTarget = tempPath2.readLink();
209 			assert(linkTarget.raw == tempname1);
210 		}
211 	}
212 	
213 	{
214 		assert(!tempPath.exists());
215 
216 		scope(exit)
217 		{
218 			if(std.file.exists(tempname1)) std.file.remove(tempname1);
219 		}
220 
221 		import scriptlike.process;
222 		run(`echo TestScriptStuff > `~tempPath.to!string());
223 		assert(tempPath.exists());
224 		assert(tempPath.isFile());
225 		assert((cast(string)tempPath.read()).strip() == "TestScriptStuff");
226 		tempPath.remove();
227 		assert(!tempPath.exists());
228 
229 		auto errlevel = tryRun(`echo TestScriptStuff > `~tempPath.to!string());
230 		assert(tempPath.exists());
231 		assert(tempPath.isFile());
232 		assert((cast(string)tempPath.read()).strip() == "TestScriptStuff");
233 		assert(errlevel == 0);
234 		tempPath.remove();
235 		assert(!tempPath.exists());
236 
237 		import scriptlike.process;
238 		getcwd().run(`echo TestScriptStuff > `~tempPath.to!string());
239 		getcwd().tryRun(`echo TestScriptStuff > `~tempPath.to!string());
240 	}
241 	
242 	{
243 		assert(!tempPath3.exists());
244 		assert(!tempPath3.up.exists());
245 
246 		scope(exit)
247 		{
248 			if(std.file.exists(tempname3)) std.file.remove(tempname3);
249 			if(std.file.exists( std.path.dirName(tempname3) )) std.file.rmdir( std.path.dirName(tempname3) );
250 		}
251 		
252 		tempPath3.up.mkdir();
253 		assert(tempPath3.up.exists());
254 		assert(tempPath3.up.isDir());
255 				
256 		import scriptlike.process;
257 		tempPath3.up.run(`echo MoreTestStuff > `~tempPath3.baseName().to!string());
258 		assert(tempPath3.exists());
259 		assert(tempPath3.isFile());
260 		assert((cast(string)tempPath3.read()).strip() == "MoreTestStuff");
261 	}
262 
263 	{
264 		scope(exit)
265 		{
266 			if(std.file.exists(tempname1)) std.file.rmdir(tempname1);
267 			if(std.file.exists(tempname3)) std.file.rmdir(tempname3);
268 			if(std.file.exists( std.path.dirName(tempname3) )) std.file.rmdir( std.path.dirName(tempname3) );
269 		}
270 		
271 		assert(!tempPath.exists());
272 		assert(!tempPath3.exists());
273 		
274 		assert(!tempPath.tryRmdir());
275 		assert(!tempPath.tryRmdirRecurse());
276 		assert(!tempPath.tryRemove());
277 		assert(!tempPath.tryRename(tempPath3));
278 		version(Posix) assert(!tempPath.trySymlink(tempPath3));
279 		assert(!tempPath.tryCopy(tempPath3));
280 
281 		assert(tempPath.tryMkdir());
282 		assert(tempPath.exists());
283 		assert(!tempPath.tryMkdir());
284 		assert(!tempPath.tryMkdirRecurse());
285 
286 		assert(tempPath.tryRmdir());
287 		assert(!tempPath.exists());
288 
289 		assert(tempPath.tryMkdirRecurse());
290 		assert(tempPath.exists());
291 		assert(!tempPath.tryMkdirRecurse());
292 	}
293 }