Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: tests/standalone/src/DirectoryTest.dart

Issue 9572005: Move all tests using dart:io to a separate directory (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 //
5 // Directory listing test.
6
7 #import("dart:io");
8 #import("dart:isolate");
9
10 class DirectoryTest {
11 static void testListing() {
12 bool listedDir = false;
13 bool listedFile = false;
14
15 Directory directory = new Directory("");
16 directory.createTempSync();
17 Directory subDirectory = new Directory("${directory.path}/subdir");
18 Expect.isFalse(subDirectory.existsSync());
19 subDirectory.createSync();
20 File f = new File('${subDirectory.path}/file.txt');
21 Expect.isFalse(f.existsSync());
22 f.createSync();
23
24 directory.onDir = (dir) {
25 listedDir = true;
26 Expect.isTrue(dir.contains(directory.path));
27 Expect.isTrue(dir.contains('subdir'));
28 };
29
30 directory.onFile = (f) {
31 listedFile = true;
32 Expect.isTrue(f.contains(directory.path));
33 Expect.isTrue(f.contains('subdir'));
34 Expect.isTrue(f.contains('file.txt'));
35 };
36
37 directory.onDone = (completed) {
38 Expect.isTrue(completed, "directory listing did not complete");
39 Expect.isTrue(listedDir, "directory not found");
40 Expect.isTrue(listedFile, "file not found");
41 directory.deleteRecursively(() {
42 f.exists((exists) => Expect.isFalse(exists));
43 directory.exists((exists) => Expect.isFalse(exists));
44 subDirectory.exists((exists) => Expect.isFalse(exists));
45 });
46 };
47
48 directory.onError = (error) {
49 Expect.fail("error listing directory: $error");
50 };
51
52 directory.list(recursive: true);
53
54 // Listing is asynchronous, so nothing should be listed at this
55 // point.
56 Expect.isFalse(listedDir);
57 Expect.isFalse(listedFile);
58 }
59
60 static void testListNonExistent() {
61 Directory d = new Directory("");
62 d.onError = (error) {
63 Expect.fail("Directory error: $error");
64 };
65 d.createTemp(() {
66 d.delete(() {
67 // Test that listing a non-existing directory fails.
68 d.onError = (error) {
69 // TODO(ager): When directory errors have been changed to
70 // post back exceptions, check that we get the right exception
71 // type here.
72 };
73 d.onFile = (file) {
74 Expect.fail("Listing of non-existing directory should fail");
75 };
76 d.onDir = (dir) {
77 Expect.fail("Listing of non-existing directory should fail");
78 };
79 d.onDone = (done) {
80 Expect.isFalse(done);
81 };
82 d.list();
83 d.list(recursive: true);
84 });
85 });
86 }
87
88 static void testListTooLongName() {
89 Directory d = new Directory("");
90 d.onError = (error) {
91 Expect.fail("Directory error: $error");
92 };
93 d.createTemp(() {
94 var subDirName = 'subdir';
95 var subDir = new Directory("${d.path}/$subDirName");
96 subDir.onError = (error) {
97 Expect.fail("Directory error: $error");
98 };
99 subDir.create(() {
100 // Construct a long string of the form
101 // 'tempdir/subdir/../subdir/../subdir'.
102 var buffer = new StringBuffer();
103 buffer.add(subDir.path);
104 for (var i = 0; i < 1000; i++) {
105 buffer.add("/../${subDirName}");
106 }
107 var long = new Directory("${buffer.toString()}");
108 var errors = 0;
109 long.onError = (error) {
110 // TODO(ager): When directory errors have been changed to
111 // post back exceptions, check that we get the right exception
112 // type here.
113 if (++errors == 2) {
114 d.deleteRecursively(() => null);
115 }
116 };
117 long.onFile = (file) {
118 Expect.fail("Listing of non-existing directory should fail");
119 };
120 long.onDir = (dir) {
121 Expect.fail("Listing of non-existing directory should fail");
122 };
123 long.onDone = (done) {
124 Expect.isFalse(done);
125 };
126 long.list();
127 long.list(recursive: true);
128 });
129 });
130 }
131
132 static void testDeleteNonExistent() {
133 Directory d = new Directory("");
134 d.onError = (error) {
135 Expect.fail("Directory error: $error");
136 };
137 d.createTemp(() {
138 d.delete(() {
139 // Test that deleting a non-existing directory fails.
140 d.onError = (error) {
141 // TODO(ager): When directory errors have been changed to
142 // post back exceptions, check that we get the right exception
143 // type here.
144 };
145 d.delete(() {
146 Expect.fail("Deletion of non-existing directory should fail");
147 });
148 d.deleteRecursively(() {
149 Expect.fail("Deletion of non-existing directory should fail");
150 });
151 });
152 });
153 }
154
155 static void testDeleteTooLongName() {
156 Directory d = new Directory("");
157 d.onError = (error) {
158 Expect.fail("Directory error: $error");
159 };
160 d.createTemp(() {
161 var subDirName = 'subdir';
162 var subDir = new Directory("${d.path}/$subDirName");
163 subDir.onError = (error) {
164 Expect.fail("Directory error: $error");
165 };
166 subDir.create(() {
167 // Construct a long string of the form
168 // 'tempdir/subdir/../subdir/../subdir'.
169 var buffer = new StringBuffer();
170 buffer.add(subDir.path);
171 for (var i = 0; i < 1000; i++) {
172 buffer.add("/../${subDirName}");
173 }
174 var long = new Directory("${buffer.toString()}");
175 var errors = 0;
176 long.onError = (error) {
177 // TODO(ager): When directory errors have been changed to
178 // post back exceptions, check that we get the right exception
179 // type here.
180 if (++errors == 2) {
181 d.deleteRecursively(() => null);
182 }
183 };
184 long.delete(() {
185 Expect.fail("Deletion of a directory with a long name should fail");
186 });
187 long.deleteRecursively(() {
188 Expect.fail("Deletion of a directory with a long name should fail");
189 });
190 });
191 });
192 }
193
194 static void testDeleteNonExistentSync() {
195 Directory d = new Directory("");
196 d.createTempSync();
197 d.deleteSync();
198 Expect.throws(d.deleteSync);
199 Expect.throws(() => d.deleteRecursivelySync());
200 }
201
202 static void testDeleteTooLongNameSync() {
203 Directory d = new Directory("");
204 d.createTempSync();
205 var subDirName = 'subdir';
206 var subDir = new Directory("${d.path}/$subDirName");
207 subDir.createSync();
208 // Construct a long string of the form
209 // 'tempdir/subdir/../subdir/../subdir'.
210 var buffer = new StringBuffer();
211 buffer.add(subDir.path);
212 for (var i = 0; i < 1000; i++) {
213 buffer.add("/../${subDirName}");
214 }
215 var long = new Directory("${buffer.toString()}");
216 Expect.throws(long.deleteSync);
217 Expect.throws(() => long.deleteRecursivelySync());
218 }
219
220 static void testExistsCreateDelete() {
221 Directory d = new Directory("");
222 d.createTemp(() {
223 d.exists((bool exists) {
224 Expect.isTrue(exists);
225 Directory created = new Directory("${d.path}/subdir");
226 created.create(() {
227 created.exists((bool exists) {
228 Expect.isTrue(exists);
229 created.delete(() {
230 created.exists((bool exists) {
231 Expect.isFalse(exists);
232 d.delete(() {
233 d.exists((bool exists) {
234 Expect.isFalse(exists);
235 });
236 });
237 });
238 });
239 });
240 });
241 });
242 });
243 }
244
245 static void testExistsCreateDeleteSync() {
246 Directory d = new Directory("");
247 d.createTempSync();
248 Expect.isTrue(d.existsSync());
249 Directory created = new Directory("${d.path}/subdir");
250 created.createSync();
251 Expect.isTrue(created.existsSync());
252 created.deleteSync();
253 Expect.isFalse(created.existsSync());
254 d.deleteSync();
255 Expect.isFalse(d.existsSync());
256 }
257
258 static void testCreateTemp() {
259 Directory tempDir1 = new Directory("/tmp/dart_temp_dir_");
260 Directory tempDir2 = new Directory("/tmp/dart_temp_dir_");
261 bool stage1aDone = false;
262 bool stage1bDone = false;
263 bool emptyTemplateTestRunning = false;
264
265 // Stages 0 through 2 run twice, the second time with an empty path.
266 Function stage0;
267 Function stage1a;
268 Function stage1b;
269 Function stage2;
270 Function stage3; // Loops to stage 0.
271
272 Function error(String message) {
273 Expect.fail("Directory onError: $message");
274 }
275
276 stage0 = () {
277 tempDir1.onError = error;
278 tempDir1.createTemp(stage1a);
279 tempDir2.onError = error;
280 tempDir2.createTemp(stage1b);
281 };
282
283 stage1a = () {
284 stage1aDone = true;
285 Expect.isTrue(tempDir1.existsSync());
286 if (stage1bDone) {
287 stage2();
288 }
289 };
290
291 stage1b = () {
292 stage1bDone = true;
293 Expect.isTrue(tempDir2.existsSync());
294 if (stage1aDone) {
295 stage2();
296 }
297 };
298
299 stage2 = () {
300 Expect.notEquals(tempDir1.path, tempDir2.path);
301 tempDir1.deleteSync();
302 tempDir2.deleteSync();
303 Expect.isFalse(tempDir1.existsSync());
304 Expect.isFalse(tempDir2.existsSync());
305 if (!emptyTemplateTestRunning) {
306 emptyTemplateTestRunning = true;
307 stage3();
308 } else {
309 // Done with test.
310 }
311 };
312
313 stage3 = () {
314 tempDir1 = new Directory("");
315 tempDir2 = new Directory("");
316 stage1aDone = false;
317 stage1bDone = false;
318 stage0();
319 };
320
321 if (new Directory("/tmp").existsSync()) {
322 stage0();
323 } else {
324 emptyTemplateTestRunning = true;
325 stage3();
326 }
327 }
328
329 static void testCreateDeleteTemp() {
330 Directory tempDirectory = new Directory("");
331 tempDirectory.createTemp(() {
332 String filename = tempDirectory.path +
333 new Platform().pathSeparator() + "dart_testfile";
334 File file = new File(filename);
335 Expect.isFalse(file.existsSync());
336 file.onError = (error) {
337 Expect.fail("testCreateTemp file.onError called: $error");
338 };
339 file.create(() {
340 file.exists((bool exists) {
341 Expect.isTrue(exists);
342 // Try to delete the directory containing the file - should throw.
343 Expect.throws(tempDirectory.deleteSync);
344 Expect.isTrue(tempDirectory.existsSync());
345
346 // Delete the file, and then delete the directory.
347 file.delete(() {
348 tempDirectory.deleteSync();
349 Expect.isFalse(tempDirectory.existsSync());
350 });
351 });
352 });
353 });
354 }
355
356 static void testMain() {
357 testListing();
358 testListNonExistent();
359 testListTooLongName();
360 testDeleteNonExistent();
361 testDeleteTooLongName();
362 testDeleteNonExistentSync();
363 testDeleteTooLongNameSync();
364 testExistsCreateDelete();
365 testExistsCreateDeleteSync();
366 testCreateTemp();
367 testCreateDeleteTemp();
368 }
369 }
370
371
372 class NestedTempDirectoryTest {
373 List<Directory> createdDirectories;
374 Directory current;
375
376 NestedTempDirectoryTest.run()
377 : createdDirectories = new List<Directory>(),
378 current = new Directory("") {
379 current.onError = errorCallback;
380 current.createTemp(createPhaseCallback);
381 }
382
383 void errorCallback(error) {
384 Expect.fail("Error callback called in NestedTempDirectoryTest: $error");
385 }
386
387 void createPhaseCallback() {
388 createdDirectories.add(current);
389 int nestingDepth = 6;
390 var os = new Platform().operatingSystem();
391 if (os == "windows") nestingDepth = 2;
392 if (createdDirectories.length < nestingDepth) {
393 current = new Directory(
394 current.path + "/nested_temp_dir_${createdDirectories.length}_");
395 current.onError = errorCallback;
396 current.createTemp(createPhaseCallback);
397 } else {
398 deletePhaseCallback();
399 }
400 }
401
402 void deletePhaseCallback() {
403 if (!createdDirectories.isEmpty()) {
404 current = createdDirectories.removeLast();
405 current.deleteSync();
406 deletePhaseCallback();
407 }
408 }
409
410 static void testMain() {
411 new NestedTempDirectoryTest.run();
412 new NestedTempDirectoryTest.run();
413 }
414 }
415
416
417 String illegalTempDirectoryLocation() {
418 // Determine a platform specific illegal location for a temporary directory.
419 var os = new Platform().operatingSystem();
420 if (os == "linux" || os == "macos") {
421 return "/dev/zero/";
422 }
423 if (os == "windows") {
424 return "*";
425 }
426 return null;
427 }
428
429
430 testCreateTempErrorSync() {
431 var location = illegalTempDirectoryLocation();
432 if (location != null) {
433 Expect.throws(new Directory(location).createTempSync,
434 (e) => e is DirectoryException);
435 }
436 }
437
438
439 testCreateTempError() {
440 var location = illegalTempDirectoryLocation();
441 if (location == null) return;
442
443 var resultPort = new ReceivePort.singleShot();
444 resultPort.receive((String message, ignored) {
445 Expect.equals("error", message);
446 });
447
448 Directory dir = new Directory(location);
449 dir.onError = (error) { resultPort.toSendPort().send("error"); };
450 dir.createTemp(() => resultPort.toSendPort().send("success"));
451 }
452
453
454 main() {
455 DirectoryTest.testMain();
456 NestedTempDirectoryTest.testMain();
457 testCreateTempErrorSync();
458 testCreateTempError();
459 }
OLDNEW
« no previous file with comments | « tests/standalone/src/DirectoryInvalidArgumentsTest.dart ('k') | tests/standalone/src/EchoServerStreamTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698