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 library io_test; | 5 library io_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
11 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
12 | 12 |
13 import '../lib/src/io.dart'; | 13 import '../lib/src/io.dart'; |
14 import 'test_pub.dart'; | 14 import 'test_pub.dart'; |
15 | 15 |
16 main() { | 16 main() { |
17 initConfig(); | 17 initConfig(); |
18 | 18 |
19 group('listDir', () { | 19 group('listDir', () { |
20 test('ignores hidden files by default', () { | 20 test('ignores hidden files by default', () { |
21 expect(withTempDir((temp) { | 21 expect(withTempDir((temp) { |
22 writeTextFile(path.join(temp, 'file1.txt'), ''); | 22 writeTextFile(path.join(temp, 'file1.txt'), ''); |
23 writeTextFile(path.join(temp, 'file2.txt'), ''); | 23 writeTextFile(path.join(temp, 'file2.txt'), ''); |
24 writeTextFile(path.join(temp, '.file3.txt'), ''); | 24 writeTextFile(path.join(temp, '.file3.txt'), ''); |
25 createDir(path.join(temp, '.subdir')); | 25 createDir(path.join(temp, '.subdir')); |
26 writeTextFile(path.join(temp, '.subdir', 'file3.txt'), ''); | 26 writeTextFile(path.join(temp, '.subdir', 'file3.txt'), ''); |
27 | 27 |
28 expect(listDir(temp, recursive: true), unorderedEquals([ | 28 expect( |
29 path.join(temp, 'file1.txt'), | 29 listDir(temp, recursive: true), |
30 path.join(temp, 'file2.txt') | 30 unorderedEquals([path.join(temp, 'file1.txt'), path.join(temp, 'file
2.txt')])); |
31 ])); | |
32 }), completes); | 31 }), completes); |
33 }); | 32 }); |
34 | 33 |
35 test('includes hidden files when told to', () { | 34 test('includes hidden files when told to', () { |
36 expect(withTempDir((temp) { | 35 expect(withTempDir((temp) { |
37 writeTextFile(path.join(temp, 'file1.txt'), ''); | 36 writeTextFile(path.join(temp, 'file1.txt'), ''); |
38 writeTextFile(path.join(temp, 'file2.txt'), ''); | 37 writeTextFile(path.join(temp, 'file2.txt'), ''); |
39 writeTextFile(path.join(temp, '.file3.txt'), ''); | 38 writeTextFile(path.join(temp, '.file3.txt'), ''); |
40 createDir(path.join(temp, '.subdir')); | 39 createDir(path.join(temp, '.subdir')); |
41 writeTextFile(path.join(temp, '.subdir', 'file3.txt'), ''); | 40 writeTextFile(path.join(temp, '.subdir', 'file3.txt'), ''); |
42 | 41 |
43 expect(listDir(temp, recursive: true, includeHidden: true), | 42 expect( |
44 unorderedEquals([ | 43 listDir(temp, recursive: true, includeHidden: true), |
45 path.join(temp, 'file1.txt'), | 44 unorderedEquals( |
46 path.join(temp, 'file2.txt'), | 45 [ |
47 path.join(temp, '.file3.txt'), | 46 path.join(temp, 'file1.txt'), |
48 path.join(temp, '.subdir'), | 47 path.join(temp, 'file2.txt'), |
49 path.join(temp, '.subdir', 'file3.txt') | 48 path.join(temp, '.file3.txt'), |
50 ])); | 49 path.join(temp, '.subdir'), |
| 50 path.join(temp, '.subdir', 'file3.txt')])); |
51 }), completes); | 51 }), completes); |
52 }); | 52 }); |
53 | 53 |
54 test("doesn't ignore hidden files above the directory being listed", () { | 54 test("doesn't ignore hidden files above the directory being listed", () { |
55 expect(withTempDir((temp) { | 55 expect(withTempDir((temp) { |
56 var dir = path.join(temp, '.foo', 'bar'); | 56 var dir = path.join(temp, '.foo', 'bar'); |
57 ensureDir(dir); | 57 ensureDir(dir); |
58 writeTextFile(path.join(dir, 'file1.txt'), ''); | 58 writeTextFile(path.join(dir, 'file1.txt'), ''); |
59 writeTextFile(path.join(dir, 'file2.txt'), ''); | 59 writeTextFile(path.join(dir, 'file2.txt'), ''); |
60 writeTextFile(path.join(dir, 'file3.txt'), ''); | 60 writeTextFile(path.join(dir, 'file3.txt'), ''); |
61 | 61 |
62 expect(listDir(dir, recursive: true), unorderedEquals([ | 62 expect( |
63 path.join(dir, 'file1.txt'), | 63 listDir(dir, recursive: true), |
64 path.join(dir, 'file2.txt'), | 64 unorderedEquals( |
65 path.join(dir, 'file3.txt') | 65 [ |
66 ])); | 66 path.join(dir, 'file1.txt'), |
| 67 path.join(dir, 'file2.txt'), |
| 68 path.join(dir, 'file3.txt')])); |
67 }), completes); | 69 }), completes); |
68 }); | 70 }); |
69 }); | 71 }); |
70 | 72 |
71 group('canonicalize', () { | 73 group('canonicalize', () { |
72 test('resolves a non-link', () { | 74 test('resolves a non-link', () { |
73 expect(withCanonicalTempDir((temp) { | 75 expect(withCanonicalTempDir((temp) { |
74 var filePath = path.join(temp, 'file'); | 76 var filePath = path.join(temp, 'file'); |
75 writeTextFile(filePath, ''); | 77 writeTextFile(filePath, ''); |
76 expect(canonicalize(filePath), equals(filePath)); | 78 expect(canonicalize(filePath), equals(filePath)); |
77 }), completes); | 79 }), completes); |
78 }); | 80 }); |
79 | 81 |
80 test('resolves a non-existent file', () { | 82 test('resolves a non-existent file', () { |
81 expect(withCanonicalTempDir((temp) { | 83 expect(withCanonicalTempDir((temp) { |
82 expect(canonicalize(path.join(temp, 'nothing')), | 84 expect( |
| 85 canonicalize(path.join(temp, 'nothing')), |
83 equals(path.join(temp, 'nothing'))); | 86 equals(path.join(temp, 'nothing'))); |
84 }), completes); | 87 }), completes); |
85 }); | 88 }); |
86 | 89 |
87 test('resolves a symlink', () { | 90 test('resolves a symlink', () { |
88 expect(withCanonicalTempDir((temp) { | 91 expect(withCanonicalTempDir((temp) { |
89 createDir(path.join(temp, 'linked-dir')); | 92 createDir(path.join(temp, 'linked-dir')); |
90 createSymlink( | 93 createSymlink(path.join(temp, 'linked-dir'), path.join(temp, 'dir')); |
91 path.join(temp, 'linked-dir'), | |
92 path.join(temp, 'dir')); | |
93 expect( | 94 expect( |
94 canonicalize(path.join(temp, 'dir')), | 95 canonicalize(path.join(temp, 'dir')), |
95 equals(path.join(temp, 'linked-dir'))); | 96 equals(path.join(temp, 'linked-dir'))); |
96 }), completes); | 97 }), completes); |
97 }); | 98 }); |
98 | 99 |
99 test('resolves a relative symlink', () { | 100 test('resolves a relative symlink', () { |
100 expect(withCanonicalTempDir((temp) { | 101 expect(withCanonicalTempDir((temp) { |
101 createDir(path.join(temp, 'linked-dir')); | 102 createDir(path.join(temp, 'linked-dir')); |
102 createSymlink( | 103 createSymlink( |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 }), completes); | 174 }), completes); |
174 }); | 175 }); |
175 | 176 |
176 test('resolves a vertically recursive symlink', () { | 177 test('resolves a vertically recursive symlink', () { |
177 expect(withCanonicalTempDir((temp) { | 178 expect(withCanonicalTempDir((temp) { |
178 var dir = path.join(temp, 'dir'); | 179 var dir = path.join(temp, 'dir'); |
179 var subdir = path.join(dir, 'subdir'); | 180 var subdir = path.join(dir, 'subdir'); |
180 createDir(dir); | 181 createDir(dir); |
181 createSymlink(dir, subdir); | 182 createSymlink(dir, subdir); |
182 expect( | 183 expect( |
183 canonicalize(path.join(temp, 'dir', 'subdir', 'subdir', 'subdir', | 184 canonicalize( |
184 'subdir', 'file')), | 185 path.join(temp, 'dir', 'subdir', 'subdir', 'subdir', 'subdir', '
file')), |
185 equals(path.join(dir, 'file'))); | 186 equals(path.join(dir, 'file'))); |
186 }), completes); | 187 }), completes); |
187 }); | 188 }); |
188 | 189 |
189 test('resolves a symlink that links to a path that needs more resolving', | 190 test( |
| 191 'resolves a symlink that links to a path that needs more resolving', |
190 () { | 192 () { |
191 expect(withCanonicalTempDir((temp) { | 193 expect(withCanonicalTempDir((temp) { |
192 var dir = path.join(temp, 'dir'); | 194 var dir = path.join(temp, 'dir'); |
193 var linkdir = path.join(temp, 'linkdir'); | 195 var linkdir = path.join(temp, 'linkdir'); |
194 var linkfile = path.join(dir, 'link'); | 196 var linkfile = path.join(dir, 'link'); |
195 createDir(dir); | 197 createDir(dir); |
196 createSymlink(dir, linkdir); | 198 createSymlink(dir, linkdir); |
197 createSymlink(path.join(linkdir, 'file'), linkfile); | 199 createSymlink(path.join(linkdir, 'file'), linkfile); |
198 expect( | 200 expect(canonicalize(linkfile), equals(path.join(dir, 'file'))); |
199 canonicalize(linkfile), | |
200 equals(path.join(dir, 'file'))); | |
201 }), completes); | 201 }), completes); |
202 }); | 202 }); |
203 | 203 |
204 test('resolves a pair of pathologically-recursive symlinks', () { | 204 test('resolves a pair of pathologically-recursive symlinks', () { |
205 expect(withCanonicalTempDir((temp) { | 205 expect(withCanonicalTempDir((temp) { |
206 var foo = path.join(temp, 'foo'); | 206 var foo = path.join(temp, 'foo'); |
207 var subfoo = path.join(foo, 'subfoo'); | 207 var subfoo = path.join(foo, 'subfoo'); |
208 var bar = path.join(temp, 'bar'); | 208 var bar = path.join(temp, 'bar'); |
209 var subbar = path.join(bar, 'subbar'); | 209 var subbar = path.join(bar, 'subbar'); |
210 createSymlink(subbar, foo); | 210 createSymlink(subbar, foo); |
211 createSymlink(subfoo, bar); | 211 createSymlink(subfoo, bar); |
212 expect( | 212 expect( |
213 canonicalize(subfoo), | 213 canonicalize(subfoo), |
214 equals(path.join(subfoo, 'subbar', 'subfoo'))); | 214 equals(path.join(subfoo, 'subbar', 'subfoo'))); |
215 }), completes); | 215 }), completes); |
216 }); | 216 }); |
217 }); | 217 }); |
218 | 218 |
219 testExistencePredicate("entryExists", entryExists, | 219 testExistencePredicate( |
| 220 "entryExists", |
| 221 entryExists, |
220 forFile: true, | 222 forFile: true, |
221 forFileSymlink: true, | 223 forFileSymlink: true, |
222 forMultiLevelFileSymlink: true, | 224 forMultiLevelFileSymlink: true, |
223 forDirectory: true, | 225 forDirectory: true, |
224 forDirectorySymlink: true, | 226 forDirectorySymlink: true, |
225 forMultiLevelDirectorySymlink: true, | 227 forMultiLevelDirectorySymlink: true, |
226 forBrokenSymlink: true, | 228 forBrokenSymlink: true, |
227 forMultiLevelBrokenSymlink: true); | 229 forMultiLevelBrokenSymlink: true); |
228 | 230 |
229 testExistencePredicate("linkExists", linkExists, | 231 testExistencePredicate( |
| 232 "linkExists", |
| 233 linkExists, |
230 forFile: false, | 234 forFile: false, |
231 forFileSymlink: true, | 235 forFileSymlink: true, |
232 forMultiLevelFileSymlink: true, | 236 forMultiLevelFileSymlink: true, |
233 forDirectory: false, | 237 forDirectory: false, |
234 forDirectorySymlink: true, | 238 forDirectorySymlink: true, |
235 forMultiLevelDirectorySymlink: true, | 239 forMultiLevelDirectorySymlink: true, |
236 forBrokenSymlink: true, | 240 forBrokenSymlink: true, |
237 forMultiLevelBrokenSymlink: true); | 241 forMultiLevelBrokenSymlink: true); |
238 | 242 |
239 testExistencePredicate("fileExists", fileExists, | 243 testExistencePredicate( |
| 244 "fileExists", |
| 245 fileExists, |
240 forFile: true, | 246 forFile: true, |
241 forFileSymlink: true, | 247 forFileSymlink: true, |
242 forMultiLevelFileSymlink: true, | 248 forMultiLevelFileSymlink: true, |
243 forDirectory: false, | 249 forDirectory: false, |
244 forDirectorySymlink: false, | 250 forDirectorySymlink: false, |
245 forMultiLevelDirectorySymlink: false, | 251 forMultiLevelDirectorySymlink: false, |
246 forBrokenSymlink: false, | 252 forBrokenSymlink: false, |
247 forMultiLevelBrokenSymlink: false); | 253 forMultiLevelBrokenSymlink: false); |
248 | 254 |
249 testExistencePredicate("dirExists", dirExists, | 255 testExistencePredicate( |
| 256 "dirExists", |
| 257 dirExists, |
250 forFile: false, | 258 forFile: false, |
251 forFileSymlink: false, | 259 forFileSymlink: false, |
252 forMultiLevelFileSymlink: false, | 260 forMultiLevelFileSymlink: false, |
253 forDirectory: true, | 261 forDirectory: true, |
254 forDirectorySymlink: true, | 262 forDirectorySymlink: true, |
255 forMultiLevelDirectorySymlink: true, | 263 forMultiLevelDirectorySymlink: true, |
256 forBrokenSymlink: false, | 264 forBrokenSymlink: false, |
257 forMultiLevelBrokenSymlink: false); | 265 forMultiLevelBrokenSymlink: false); |
258 } | 266 } |
259 | 267 |
260 void testExistencePredicate(String name, bool predicate(String path), | 268 void testExistencePredicate(String name, bool predicate(String path), |
261 {bool forFile, | 269 {bool forFile, bool forFileSymlink, bool forMultiLevelFileSymlink, |
262 bool forFileSymlink, | 270 bool forDirectory, bool forDirectorySymlink, bool forMultiLevelDirectorySyml
ink, |
263 bool forMultiLevelFileSymlink, | 271 bool forBrokenSymlink, bool forMultiLevelBrokenSymlink}) { |
264 bool forDirectory, | |
265 bool forDirectorySymlink, | |
266 bool forMultiLevelDirectorySymlink, | |
267 bool forBrokenSymlink, | |
268 bool forMultiLevelBrokenSymlink}) { | |
269 group(name, () { | 272 group(name, () { |
270 test('returns $forFile for a file', () { | 273 test('returns $forFile for a file', () { |
271 expect(withTempDir((temp) { | 274 expect(withTempDir((temp) { |
272 var file = path.join(temp, "test.txt"); | 275 var file = path.join(temp, "test.txt"); |
273 writeTextFile(file, "contents"); | 276 writeTextFile(file, "contents"); |
274 expect(predicate(file), equals(forFile)); | 277 expect(predicate(file), equals(forFile)); |
275 }), completes); | 278 }), completes); |
276 }); | 279 }); |
277 | 280 |
278 test('returns $forDirectory for a directory', () { | 281 test('returns $forDirectory for a directory', () { |
279 expect(withTempDir((temp) { | 282 expect(withTempDir((temp) { |
280 var file = path.join(temp, "dir"); | 283 var file = path.join(temp, "dir"); |
281 createDir(file); | 284 createDir(file); |
282 expect(predicate(file), equals(forDirectory)); | 285 expect(predicate(file), equals(forDirectory)); |
283 }), completes); | 286 }), completes); |
284 }); | 287 }); |
285 | 288 |
286 test('returns $forDirectorySymlink for a symlink to a directory', () { | 289 test('returns $forDirectorySymlink for a symlink to a directory', () { |
287 expect(withTempDir((temp) { | 290 expect(withTempDir((temp) { |
288 var targetPath = path.join(temp, "dir"); | 291 var targetPath = path.join(temp, "dir"); |
289 var symlinkPath = path.join(temp, "linkdir"); | 292 var symlinkPath = path.join(temp, "linkdir"); |
290 createDir(targetPath); | 293 createDir(targetPath); |
291 createSymlink(targetPath, symlinkPath); | 294 createSymlink(targetPath, symlinkPath); |
292 expect(predicate(symlinkPath), equals(forDirectorySymlink)); | 295 expect(predicate(symlinkPath), equals(forDirectorySymlink)); |
293 }), completes); | 296 }), completes); |
294 }); | 297 }); |
295 | 298 |
296 test('returns $forMultiLevelDirectorySymlink for a multi-level symlink to ' | 299 test( |
297 'a directory', () { | 300 'returns $forMultiLevelDirectorySymlink for a multi-level symlink to ' |
| 301 'a directory', |
| 302 () { |
298 expect(withTempDir((temp) { | 303 expect(withTempDir((temp) { |
299 var targetPath = path.join(temp, "dir"); | 304 var targetPath = path.join(temp, "dir"); |
300 var symlink1Path = path.join(temp, "link1dir"); | 305 var symlink1Path = path.join(temp, "link1dir"); |
301 var symlink2Path = path.join(temp, "link2dir"); | 306 var symlink2Path = path.join(temp, "link2dir"); |
302 createDir(targetPath); | 307 createDir(targetPath); |
303 createSymlink(targetPath, symlink1Path); | 308 createSymlink(targetPath, symlink1Path); |
304 createSymlink(symlink1Path, symlink2Path); | 309 createSymlink(symlink1Path, symlink2Path); |
305 expect(predicate(symlink2Path), | 310 expect(predicate(symlink2Path), equals(forMultiLevelDirectorySymlink)); |
306 equals(forMultiLevelDirectorySymlink)); | |
307 }), completes); | 311 }), completes); |
308 }); | 312 }); |
309 | 313 |
310 test('returns $forBrokenSymlink for a broken symlink', () { | 314 test('returns $forBrokenSymlink for a broken symlink', () { |
311 expect(withTempDir((temp) { | 315 expect(withTempDir((temp) { |
312 var targetPath = path.join(temp, "dir"); | 316 var targetPath = path.join(temp, "dir"); |
313 var symlinkPath = path.join(temp, "linkdir"); | 317 var symlinkPath = path.join(temp, "linkdir"); |
314 createDir(targetPath); | 318 createDir(targetPath); |
315 createSymlink(targetPath, symlinkPath); | 319 createSymlink(targetPath, symlinkPath); |
316 deleteEntry(targetPath); | 320 deleteEntry(targetPath); |
317 expect(predicate(symlinkPath), equals(forBrokenSymlink)); | 321 expect(predicate(symlinkPath), equals(forBrokenSymlink)); |
318 }), completes); | 322 }), completes); |
319 }); | 323 }); |
320 | 324 |
321 test('returns $forMultiLevelBrokenSymlink for a multi-level broken symlink', | 325 test( |
| 326 'returns $forMultiLevelBrokenSymlink for a multi-level broken symlink', |
322 () { | 327 () { |
323 expect(withTempDir((temp) { | 328 expect(withTempDir((temp) { |
324 var targetPath = path.join(temp, "dir"); | 329 var targetPath = path.join(temp, "dir"); |
325 var symlink1Path = path.join(temp, "link1dir"); | 330 var symlink1Path = path.join(temp, "link1dir"); |
326 var symlink2Path = path.join(temp, "link2dir"); | 331 var symlink2Path = path.join(temp, "link2dir"); |
327 createDir(targetPath); | 332 createDir(targetPath); |
328 createSymlink(targetPath, symlink1Path); | 333 createSymlink(targetPath, symlink1Path); |
329 createSymlink(symlink1Path, symlink2Path); | 334 createSymlink(symlink1Path, symlink2Path); |
330 deleteEntry(targetPath); | 335 deleteEntry(targetPath); |
331 expect(predicate(symlink2Path), equals(forMultiLevelBrokenSymlink)); | 336 expect(predicate(symlink2Path), equals(forMultiLevelBrokenSymlink)); |
332 }), completes); | 337 }), completes); |
333 }); | 338 }); |
334 | 339 |
335 // Windows doesn't support symlinking to files. | 340 // Windows doesn't support symlinking to files. |
336 if (Platform.operatingSystem != 'windows') { | 341 if (Platform.operatingSystem != 'windows') { |
337 test('returns $forFileSymlink for a symlink to a file', () { | 342 test('returns $forFileSymlink for a symlink to a file', () { |
338 expect(withTempDir((temp) { | 343 expect(withTempDir((temp) { |
339 var targetPath = path.join(temp, "test.txt"); | 344 var targetPath = path.join(temp, "test.txt"); |
340 var symlinkPath = path.join(temp, "link.txt"); | 345 var symlinkPath = path.join(temp, "link.txt"); |
341 writeTextFile(targetPath, "contents"); | 346 writeTextFile(targetPath, "contents"); |
342 createSymlink(targetPath, symlinkPath); | 347 createSymlink(targetPath, symlinkPath); |
343 expect(predicate(symlinkPath), equals(forFileSymlink)); | 348 expect(predicate(symlinkPath), equals(forFileSymlink)); |
344 }), completes); | 349 }), completes); |
345 }); | 350 }); |
346 | 351 |
347 test('returns $forMultiLevelFileSymlink for a multi-level symlink to a ' | 352 test( |
348 'file', () { | 353 'returns $forMultiLevelFileSymlink for a multi-level symlink to a ' 'f
ile', |
| 354 () { |
349 expect(withTempDir((temp) { | 355 expect(withTempDir((temp) { |
350 var targetPath = path.join(temp, "test.txt"); | 356 var targetPath = path.join(temp, "test.txt"); |
351 var symlink1Path = path.join(temp, "link1.txt"); | 357 var symlink1Path = path.join(temp, "link1.txt"); |
352 var symlink2Path = path.join(temp, "link2.txt"); | 358 var symlink2Path = path.join(temp, "link2.txt"); |
353 writeTextFile(targetPath, "contents"); | 359 writeTextFile(targetPath, "contents"); |
354 createSymlink(targetPath, symlink1Path); | 360 createSymlink(targetPath, symlink1Path); |
355 createSymlink(symlink1Path, symlink2Path); | 361 createSymlink(symlink1Path, symlink2Path); |
356 expect(predicate(symlink2Path), equals(forMultiLevelFileSymlink)); | 362 expect(predicate(symlink2Path), equals(forMultiLevelFileSymlink)); |
357 }), completes); | 363 }), completes); |
358 }); | 364 }); |
359 } | 365 } |
360 }); | 366 }); |
361 } | 367 } |
362 | 368 |
363 /// Like [withTempDir], but canonicalizes the path before passing it to [fn]. | 369 /// Like [withTempDir], but canonicalizes the path before passing it to [fn]. |
364 Future withCanonicalTempDir(Future fn(String path)) => | 370 Future withCanonicalTempDir(Future fn(String path)) => |
365 withTempDir((temp) => fn(canonicalize(temp))); | 371 withTempDir((temp) => fn(canonicalize(temp))); |
OLD | NEW |