OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // | 4 // |
5 // Directory listing test. | 5 // Directory listing test. |
6 | 6 |
7 #import("dart:io"); | 7 #import("dart:io"); |
8 #import("dart:isolate"); | 8 #import("dart:isolate"); |
9 | 9 |
10 class DirectoryTest { | 10 class DirectoryTest { |
(...skipping 27 matching lines...) Expand all Loading... |
38 Expect.isTrue(completed, "directory listing did not complete"); | 38 Expect.isTrue(completed, "directory listing did not complete"); |
39 Expect.isTrue(listedDir, "directory not found"); | 39 Expect.isTrue(listedDir, "directory not found"); |
40 Expect.isTrue(listedFile, "file not found"); | 40 Expect.isTrue(listedFile, "file not found"); |
41 directory.deleteRecursively(() { | 41 directory.deleteRecursively(() { |
42 f.exists((exists) => Expect.isFalse(exists)); | 42 f.exists((exists) => Expect.isFalse(exists)); |
43 directory.exists((exists) => Expect.isFalse(exists)); | 43 directory.exists((exists) => Expect.isFalse(exists)); |
44 subDirectory.exists((exists) => Expect.isFalse(exists)); | 44 subDirectory.exists((exists) => Expect.isFalse(exists)); |
45 }); | 45 }); |
46 }; | 46 }; |
47 | 47 |
48 directory.onError = (error) { | 48 directory.onError = (e) { |
49 Expect.fail("error listing directory: $error"); | 49 Expect.fail("error listing directory: $e"); |
50 }; | 50 }; |
51 | 51 |
52 directory.list(recursive: true); | 52 directory.list(recursive: true); |
53 | 53 |
54 // Listing is asynchronous, so nothing should be listed at this | 54 // Listing is asynchronous, so nothing should be listed at this |
55 // point. | 55 // point. |
56 Expect.isFalse(listedDir); | 56 Expect.isFalse(listedDir); |
57 Expect.isFalse(listedFile); | 57 Expect.isFalse(listedFile); |
58 } | 58 } |
59 | 59 |
60 static void testListNonExistent() { | 60 static void testListNonExistent() { |
61 Directory d = new Directory(""); | 61 Directory d = new Directory(""); |
62 d.onError = (error) { | 62 d.onError = (e) { |
63 Expect.fail("Directory error: $error"); | 63 Expect.fail("Directory error: $e"); |
64 }; | 64 }; |
65 d.createTemp(() { | 65 d.createTemp(() { |
66 d.delete(() { | 66 d.delete(() { |
67 // Test that listing a non-existing directory fails. | 67 // Test that listing a non-existing directory fails. |
68 d.onError = (error) { | 68 d.onError = (e) { |
69 // TODO(ager): When directory errors have been changed to | 69 Expect.isTrue(e is DirectoryIOException); |
70 // post back exceptions, check that we get the right exception | |
71 // type here. | |
72 }; | 70 }; |
73 d.onFile = (file) { | 71 d.onFile = (file) { |
74 Expect.fail("Listing of non-existing directory should fail"); | 72 Expect.fail("Listing of non-existing directory should fail"); |
75 }; | 73 }; |
76 d.onDir = (dir) { | 74 d.onDir = (dir) { |
77 Expect.fail("Listing of non-existing directory should fail"); | 75 Expect.fail("Listing of non-existing directory should fail"); |
78 }; | 76 }; |
79 d.onDone = (done) { | 77 d.onDone = (done) { |
80 Expect.isFalse(done); | 78 Expect.isFalse(done); |
81 }; | 79 }; |
82 d.list(); | 80 d.list(); |
83 d.list(recursive: true); | 81 d.list(recursive: true); |
84 }); | 82 }); |
85 }); | 83 }); |
86 } | 84 } |
87 | 85 |
88 static void testListTooLongName() { | 86 static void testListTooLongName() { |
89 Directory d = new Directory(""); | 87 Directory d = new Directory(""); |
90 d.onError = (error) { | 88 d.onError = (e) { |
91 Expect.fail("Directory error: $error"); | 89 Expect.fail("Directory error: $e"); |
92 }; | 90 }; |
93 d.createTemp(() { | 91 d.createTemp(() { |
94 var subDirName = 'subdir'; | 92 var subDirName = 'subdir'; |
95 var subDir = new Directory("${d.path}/$subDirName"); | 93 var subDir = new Directory("${d.path}/$subDirName"); |
96 subDir.onError = (error) { | 94 subDir.onError = (e) { |
97 Expect.fail("Directory error: $error"); | 95 Expect.fail("Directory error: $e"); |
98 }; | 96 }; |
99 subDir.create(() { | 97 subDir.create(() { |
100 // Construct a long string of the form | 98 // Construct a long string of the form |
101 // 'tempdir/subdir/../subdir/../subdir'. | 99 // 'tempdir/subdir/../subdir/../subdir'. |
102 var buffer = new StringBuffer(); | 100 var buffer = new StringBuffer(); |
103 buffer.add(subDir.path); | 101 buffer.add(subDir.path); |
104 for (var i = 0; i < 1000; i++) { | 102 for (var i = 0; i < 1000; i++) { |
105 buffer.add("/../${subDirName}"); | 103 buffer.add("/../${subDirName}"); |
106 } | 104 } |
107 var long = new Directory("${buffer.toString()}"); | 105 var long = new Directory("${buffer.toString()}"); |
108 var errors = 0; | 106 var errors = 0; |
109 long.onError = (error) { | 107 long.onError = (e) { |
110 // TODO(ager): When directory errors have been changed to | 108 Expect.isTrue(e is DirectoryIOException); |
111 // post back exceptions, check that we get the right exception | |
112 // type here. | |
113 if (++errors == 2) { | 109 if (++errors == 2) { |
114 d.deleteRecursively(() => null); | 110 d.deleteRecursively(() => null); |
115 } | 111 } |
116 }; | 112 }; |
117 long.onFile = (file) { | 113 long.onFile = (file) { |
118 Expect.fail("Listing of non-existing directory should fail"); | 114 Expect.fail("Listing of non-existing directory should fail"); |
119 }; | 115 }; |
120 long.onDir = (dir) { | 116 long.onDir = (dir) { |
121 Expect.fail("Listing of non-existing directory should fail"); | 117 Expect.fail("Listing of non-existing directory should fail"); |
122 }; | 118 }; |
123 long.onDone = (done) { | 119 long.onDone = (done) { |
124 Expect.isFalse(done); | 120 Expect.isFalse(done); |
125 }; | 121 }; |
126 long.list(); | 122 long.list(); |
127 long.list(recursive: true); | 123 long.list(recursive: true); |
128 }); | 124 }); |
129 }); | 125 }); |
130 } | 126 } |
131 | 127 |
132 static void testDeleteNonExistent() { | 128 static void testDeleteNonExistent() { |
133 Directory d = new Directory(""); | 129 Directory d = new Directory(""); |
134 d.onError = (error) { | 130 d.onError = (e) { |
135 Expect.fail("Directory error: $error"); | 131 Expect.fail("Directory error: $e"); |
136 }; | 132 }; |
137 d.createTemp(() { | 133 d.createTemp(() { |
138 d.delete(() { | 134 d.delete(() { |
139 // Test that deleting a non-existing directory fails. | 135 // Test that deleting a non-existing directory fails. |
140 d.onError = (error) { | 136 d.onError = (e) { |
141 // TODO(ager): When directory errors have been changed to | 137 Expect.isTrue(e is DirectoryIOException); |
142 // post back exceptions, check that we get the right exception | |
143 // type here. | |
144 }; | 138 }; |
145 d.delete(() { | 139 d.delete(() { |
146 Expect.fail("Deletion of non-existing directory should fail"); | 140 Expect.fail("Deletion of non-existing directory should fail"); |
147 }); | 141 }); |
148 d.deleteRecursively(() { | 142 d.deleteRecursively(() { |
149 Expect.fail("Deletion of non-existing directory should fail"); | 143 Expect.fail("Deletion of non-existing directory should fail"); |
150 }); | 144 }); |
151 }); | 145 }); |
152 }); | 146 }); |
153 } | 147 } |
154 | 148 |
155 static void testDeleteTooLongName() { | 149 static void testDeleteTooLongName() { |
156 Directory d = new Directory(""); | 150 Directory d = new Directory(""); |
157 d.onError = (error) { | 151 d.onError = (e) { |
158 Expect.fail("Directory error: $error"); | 152 Expect.fail("Directory error: $e"); |
159 }; | 153 }; |
160 d.createTemp(() { | 154 d.createTemp(() { |
161 var subDirName = 'subdir'; | 155 var subDirName = 'subdir'; |
162 var subDir = new Directory("${d.path}/$subDirName"); | 156 var subDir = new Directory("${d.path}/$subDirName"); |
163 subDir.onError = (error) { | 157 subDir.onError = (e) { |
164 Expect.fail("Directory error: $error"); | 158 Expect.fail("Directory error: $e"); |
165 }; | 159 }; |
166 subDir.create(() { | 160 subDir.create(() { |
167 // Construct a long string of the form | 161 // Construct a long string of the form |
168 // 'tempdir/subdir/../subdir/../subdir'. | 162 // 'tempdir/subdir/../subdir/../subdir'. |
169 var buffer = new StringBuffer(); | 163 var buffer = new StringBuffer(); |
170 buffer.add(subDir.path); | 164 buffer.add(subDir.path); |
171 for (var i = 0; i < 1000; i++) { | 165 for (var i = 0; i < 1000; i++) { |
172 buffer.add("/../${subDirName}"); | 166 buffer.add("/../${subDirName}"); |
173 } | 167 } |
174 var long = new Directory("${buffer.toString()}"); | 168 var long = new Directory("${buffer.toString()}"); |
175 var errors = 0; | 169 var errors = 0; |
176 long.onError = (error) { | 170 long.onError = (e) { |
177 // TODO(ager): When directory errors have been changed to | 171 Expect.isTrue(e is DirectoryIOException); |
178 // post back exceptions, check that we get the right exception | |
179 // type here. | |
180 if (++errors == 2) { | 172 if (++errors == 2) { |
181 d.deleteRecursively(() => null); | 173 d.deleteRecursively(() => null); |
182 } | 174 } |
183 }; | 175 }; |
184 long.delete(() { | 176 long.delete(() { |
185 Expect.fail("Deletion of a directory with a long name should fail"); | 177 Expect.fail("Deletion of a directory with a long name should fail"); |
186 }); | 178 }); |
187 long.deleteRecursively(() { | 179 long.deleteRecursively(() { |
188 Expect.fail("Deletion of a directory with a long name should fail"); | 180 Expect.fail("Deletion of a directory with a long name should fail"); |
189 }); | 181 }); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 bool stage1bDone = false; | 254 bool stage1bDone = false; |
263 bool emptyTemplateTestRunning = false; | 255 bool emptyTemplateTestRunning = false; |
264 | 256 |
265 // Stages 0 through 2 run twice, the second time with an empty path. | 257 // Stages 0 through 2 run twice, the second time with an empty path. |
266 Function stage0; | 258 Function stage0; |
267 Function stage1a; | 259 Function stage1a; |
268 Function stage1b; | 260 Function stage1b; |
269 Function stage2; | 261 Function stage2; |
270 Function stage3; // Loops to stage 0. | 262 Function stage3; // Loops to stage 0. |
271 | 263 |
272 Function error(String message) { | 264 Function error(e) { |
273 Expect.fail("Directory onError: $message"); | 265 Expect.fail("Directory onError: $e"); |
274 } | 266 } |
275 | 267 |
276 stage0 = () { | 268 stage0 = () { |
277 tempDir1.onError = error; | 269 tempDir1.onError = error; |
278 tempDir1.createTemp(stage1a); | 270 tempDir1.createTemp(stage1a); |
279 tempDir2.onError = error; | 271 tempDir2.onError = error; |
280 tempDir2.createTemp(stage1b); | 272 tempDir2.createTemp(stage1b); |
281 }; | 273 }; |
282 | 274 |
283 stage1a = () { | 275 stage1a = () { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 } | 318 } |
327 } | 319 } |
328 | 320 |
329 static void testCreateDeleteTemp() { | 321 static void testCreateDeleteTemp() { |
330 Directory tempDirectory = new Directory(""); | 322 Directory tempDirectory = new Directory(""); |
331 tempDirectory.createTemp(() { | 323 tempDirectory.createTemp(() { |
332 String filename = tempDirectory.path + | 324 String filename = tempDirectory.path + |
333 new Platform().pathSeparator() + "dart_testfile"; | 325 new Platform().pathSeparator() + "dart_testfile"; |
334 File file = new File(filename); | 326 File file = new File(filename); |
335 Expect.isFalse(file.existsSync()); | 327 Expect.isFalse(file.existsSync()); |
336 file.onError = (error) { | 328 file.onError = (e) { |
337 Expect.fail("testCreateTemp file.onError called: $error"); | 329 Expect.fail("testCreateTemp file.onError called: $e"); |
338 }; | 330 }; |
339 file.create(() { | 331 file.create(() { |
340 file.exists((bool exists) { | 332 file.exists((bool exists) { |
341 Expect.isTrue(exists); | 333 Expect.isTrue(exists); |
342 // Try to delete the directory containing the file - should throw. | 334 // Try to delete the directory containing the file - should throw. |
343 Expect.throws(tempDirectory.deleteSync); | 335 Expect.throws(tempDirectory.deleteSync); |
344 Expect.isTrue(tempDirectory.existsSync()); | 336 Expect.isTrue(tempDirectory.existsSync()); |
345 | 337 |
346 // Delete the file, and then delete the directory. | 338 // Delete the file, and then delete the directory. |
347 file.delete(() { | 339 file.delete(() { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 List<Directory> createdDirectories; | 373 List<Directory> createdDirectories; |
382 Directory current; | 374 Directory current; |
383 | 375 |
384 NestedTempDirectoryTest.run() | 376 NestedTempDirectoryTest.run() |
385 : createdDirectories = new List<Directory>(), | 377 : createdDirectories = new List<Directory>(), |
386 current = new Directory("") { | 378 current = new Directory("") { |
387 current.onError = errorCallback; | 379 current.onError = errorCallback; |
388 current.createTemp(createPhaseCallback); | 380 current.createTemp(createPhaseCallback); |
389 } | 381 } |
390 | 382 |
391 void errorCallback(error) { | 383 void errorCallback(e) { |
392 Expect.fail("Error callback called in NestedTempDirectoryTest: $error"); | 384 Expect.fail("Error callback called in NestedTempDirectoryTest: $e"); |
393 } | 385 } |
394 | 386 |
395 void createPhaseCallback() { | 387 void createPhaseCallback() { |
396 createdDirectories.add(current); | 388 createdDirectories.add(current); |
397 int nestingDepth = 6; | 389 int nestingDepth = 6; |
398 var os = new Platform().operatingSystem(); | 390 var os = new Platform().operatingSystem(); |
399 if (os == "windows") nestingDepth = 2; | 391 if (os == "windows") nestingDepth = 2; |
400 if (createdDirectories.length < nestingDepth) { | 392 if (createdDirectories.length < nestingDepth) { |
401 current = new Directory( | 393 current = new Directory( |
402 current.path + "/nested_temp_dir_${createdDirectories.length}_"); | 394 current.path + "/nested_temp_dir_${createdDirectories.length}_"); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 var location = illegalTempDirectoryLocation(); | 440 var location = illegalTempDirectoryLocation(); |
449 if (location == null) return; | 441 if (location == null) return; |
450 | 442 |
451 var resultPort = new ReceivePort(); | 443 var resultPort = new ReceivePort(); |
452 resultPort.receive((String message, ignored) { | 444 resultPort.receive((String message, ignored) { |
453 resultPort.close(); | 445 resultPort.close(); |
454 Expect.equals("error", message); | 446 Expect.equals("error", message); |
455 }); | 447 }); |
456 | 448 |
457 Directory dir = new Directory(location); | 449 Directory dir = new Directory(location); |
458 dir.onError = (error) { resultPort.toSendPort().send("error"); }; | 450 dir.onError = (e) { resultPort.toSendPort().send("error"); }; |
459 dir.createTemp(() => resultPort.toSendPort().send("success")); | 451 dir.createTemp(() => resultPort.toSendPort().send("success")); |
460 } | 452 } |
461 | 453 |
462 | 454 |
463 main() { | 455 main() { |
464 DirectoryTest.testMain(); | 456 DirectoryTest.testMain(); |
465 NestedTempDirectoryTest.testMain(); | 457 NestedTempDirectoryTest.testMain(); |
466 testCreateTempErrorSync(); | 458 testCreateTempErrorSync(); |
467 testCreateTempError(); | 459 testCreateTempError(); |
468 } | 460 } |
OLD | NEW |