| 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:async"; | 7 import "dart:async"; |
| 8 import "dart:io"; | 8 import "dart:io"; |
| 9 | 9 |
| 10 import "package:async_helper/async_helper.dart"; | 10 import "package:async_helper/async_helper.dart"; |
| 11 import "package:expect/expect.dart"; | 11 import "package:expect/expect.dart"; |
| 12 | 12 |
| 13 class DirectoryTest { | 13 class DirectoryTest { |
| 14 static void testListing() { | 14 static void testListing() { |
| 15 bool listedDir = false; | 15 bool listedDir = false; |
| 16 bool listedFile = false; | 16 bool listedFile = false; |
| 17 | 17 |
| 18 Directory directory = new Directory("").createTempSync(); | 18 Directory directory = |
| 19 Directory.systemTemp.createTempSync('dart_directory_test'); |
| 19 Directory subDirectory = new Directory("${directory.path}/subdir"); | 20 Directory subDirectory = new Directory("${directory.path}/subdir"); |
| 20 Expect.isTrue('$directory'.contains(directory.path)); | 21 Expect.isTrue('$directory'.contains(directory.path)); |
| 21 Expect.isFalse(subDirectory.existsSync()); | 22 Expect.isFalse(subDirectory.existsSync()); |
| 22 subDirectory.createSync(); | 23 subDirectory.createSync(); |
| 23 Expect.isTrue(subDirectory.existsSync()); | 24 Expect.isTrue(subDirectory.existsSync()); |
| 24 File f = new File('${subDirectory.path}/file.txt'); | 25 File f = new File('${subDirectory.path}/file.txt'); |
| 25 File fLong = new File('${directory.path}/subdir/../subdir/file.txt'); | 26 File fLong = new File('${directory.path}/subdir/../subdir/file.txt'); |
| 26 Expect.isFalse(f.existsSync()); | 27 Expect.isFalse(f.existsSync()); |
| 27 f.createSync(); | 28 f.createSync(); |
| 28 | 29 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 }); | 81 }); |
| 81 }); | 82 }); |
| 82 | 83 |
| 83 // Listing is asynchronous, so nothing should be listed at this | 84 // Listing is asynchronous, so nothing should be listed at this |
| 84 // point. | 85 // point. |
| 85 Expect.isFalse(listedDir); | 86 Expect.isFalse(listedDir); |
| 86 Expect.isFalse(listedFile); | 87 Expect.isFalse(listedFile); |
| 87 } | 88 } |
| 88 | 89 |
| 89 static void testListingTailingPaths() { | 90 static void testListingTailingPaths() { |
| 90 Directory directory = new Directory("").createTempSync(); | 91 Directory directory = |
| 92 Directory.systemTemp.createTempSync('dart_directory_test'); |
| 91 Directory subDirectory = new Directory("${directory.path}/subdir/"); | 93 Directory subDirectory = new Directory("${directory.path}/subdir/"); |
| 92 subDirectory.createSync(); | 94 subDirectory.createSync(); |
| 93 File f = new File('${subDirectory.path}/file.txt'); | 95 File f = new File('${subDirectory.path}/file.txt'); |
| 94 f.createSync(); | 96 f.createSync(); |
| 95 | 97 |
| 96 void test(entry) { | 98 void test(entry) { |
| 97 Expect.isFalse(entry.path.contains(new RegExp('[\\/][\\/]'))); | 99 Expect.isFalse(entry.path.contains(new RegExp('[\\/][\\/]'))); |
| 98 } | 100 } |
| 99 | 101 |
| 100 subDirectory.listSync().forEach(test); | 102 subDirectory.listSync().forEach(test); |
| 101 | 103 |
| 102 subDirectory.list().listen(test, onDone: () { | 104 subDirectory.list().listen(test, onDone: () { |
| 103 directory.deleteSync(recursive: true); | 105 directory.deleteSync(recursive: true); |
| 104 }); | 106 }); |
| 105 } | 107 } |
| 106 | 108 |
| 107 static void testListNonExistent() { | 109 static void testListNonExistent() { |
| 108 setupListerHandlers(Stream<FileSystemEntity> stream) { | 110 setupListerHandlers(Stream<FileSystemEntity> stream) { |
| 109 stream.listen( | 111 stream.listen( |
| 110 (_) => Expect.fail("Listing of non-existing directory should fail"), | 112 (_) => Expect.fail("Listing of non-existing directory should fail"), |
| 111 onError: (error) { | 113 onError: (error) { |
| 112 Expect.isTrue(error is DirectoryException); | 114 Expect.isTrue(error is DirectoryException); |
| 113 }); | 115 }); |
| 114 } | 116 } |
| 115 new Directory("").createTemp().then((d) { | 117 Directory.systemTemp.createTemp('dart_directory').then((d) { |
| 116 d.delete().then((ignore) { | 118 d.delete().then((ignore) { |
| 117 setupListerHandlers(d.list()); | 119 setupListerHandlers(d.list()); |
| 118 setupListerHandlers(d.list(recursive: true)); | 120 setupListerHandlers(d.list(recursive: true)); |
| 119 }); | 121 }); |
| 120 }); | 122 }); |
| 121 } | 123 } |
| 122 | 124 |
| 123 static void testListTooLongName() { | 125 static void testListTooLongName() { |
| 124 new Directory("").createTemp().then((d) { | 126 Directory.systemTemp.createTemp('dart_directory').then((d) { |
| 125 var errors = 0; | 127 var errors = 0; |
| 126 asyncStart(); | 128 asyncStart(); |
| 127 setupListHandlers(Stream<FileSystemEntity> stream) { | 129 setupListHandlers(Stream<FileSystemEntity> stream) { |
| 128 stream.listen( | 130 stream.listen( |
| 129 (_) => Expect.fail("Listing of non-existing directory should fail"), | 131 (_) => Expect.fail("Listing of non-existing directory should fail"), |
| 130 onError: (error) { | 132 onError: (error) { |
| 131 Expect.isTrue(error is DirectoryException); | 133 Expect.isTrue(error is DirectoryException); |
| 132 if (++errors == 2) { | 134 if (++errors == 2) { |
| 133 d.delete(recursive: true).then((_) { | 135 d.delete(recursive: true).then((_) { |
| 134 asyncEnd(); | 136 asyncEnd(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 156 static void testDeleteNonExistent() { | 158 static void testDeleteNonExistent() { |
| 157 // Test that deleting a non-existing directory fails. | 159 // Test that deleting a non-existing directory fails. |
| 158 setupFutureHandlers(future) { | 160 setupFutureHandlers(future) { |
| 159 future.then((ignore) { | 161 future.then((ignore) { |
| 160 Expect.fail("Deletion of non-existing directory should fail"); | 162 Expect.fail("Deletion of non-existing directory should fail"); |
| 161 }).catchError((error) { | 163 }).catchError((error) { |
| 162 Expect.isTrue(error is DirectoryException); | 164 Expect.isTrue(error is DirectoryException); |
| 163 }); | 165 }); |
| 164 } | 166 } |
| 165 | 167 |
| 166 new Directory("").createTemp().then((d) { | 168 Directory.systemTemp.createTemp('dart_directory').then((d) { |
| 167 d.delete().then((ignore) { | 169 d.delete().then((ignore) { |
| 168 setupFutureHandlers(d.delete()); | 170 setupFutureHandlers(d.delete()); |
| 169 setupFutureHandlers(d.delete(recursive: true)); | 171 setupFutureHandlers(d.delete(recursive: true)); |
| 170 }); | 172 }); |
| 171 }); | 173 }); |
| 172 } | 174 } |
| 173 | 175 |
| 174 static void testDeleteTooLongName() { | 176 static void testDeleteTooLongName() { |
| 175 asyncStart(); | 177 asyncStart(); |
| 176 new Directory("").createTemp().then((d) { | 178 Directory.systemTemp.createTemp('dart_directory').then((d) { |
| 177 var subDirName = 'subdir'; | 179 var subDirName = 'subdir'; |
| 178 var subDir = new Directory("${d.path}/$subDirName"); | 180 var subDir = new Directory("${d.path}/$subDirName"); |
| 179 subDir.create().then((ignore) { | 181 subDir.create().then((ignore) { |
| 180 // Construct a long string of the form | 182 // Construct a long string of the form |
| 181 // 'tempdir/subdir/../subdir/../subdir'. | 183 // 'tempdir/subdir/../subdir/../subdir'. |
| 182 var buffer = new StringBuffer(); | 184 var buffer = new StringBuffer(); |
| 183 buffer.write(subDir.path); | 185 buffer.write(subDir.path); |
| 184 for (var i = 0; i < 1000; i++) { | 186 for (var i = 0; i < 1000; i++) { |
| 185 buffer.write("/../${subDirName}"); | 187 buffer.write("/../${subDirName}"); |
| 186 } | 188 } |
| 187 var long = new Directory("${buffer.toString()}"); | 189 var long = new Directory("${buffer.toString()}"); |
| 188 var errors = 0; | 190 var errors = 0; |
| 189 onError(error) { | 191 onError(error) { |
| 190 Expect.isTrue(error is DirectoryException); | 192 Expect.isTrue(error is DirectoryException); |
| 191 if (++errors == 2) { | 193 if (++errors == 2) { |
| 192 d.delete(recursive: true).then((_) => asyncEnd()); | 194 d.delete(recursive: true).then((_) => asyncEnd()); |
| 193 } | 195 } |
| 194 return true; | 196 return true; |
| 195 } | 197 } |
| 196 long.delete().catchError(onError); | 198 long.delete().catchError(onError); |
| 197 long.delete(recursive: true).catchError(onError); | 199 long.delete(recursive: true).catchError(onError); |
| 198 }); | 200 }); |
| 199 }); | 201 }); |
| 200 } | 202 } |
| 201 | 203 |
| 202 static void testDeleteNonExistentSync() { | 204 static void testDeleteNonExistentSync() { |
| 203 Directory d = new Directory("").createTempSync(); | 205 Directory d = Directory.systemTemp.createTempSync('dart_directory_test'); |
| 204 d.deleteSync(); | 206 d.deleteSync(); |
| 205 Expect.throws(d.deleteSync); | 207 Expect.throws(d.deleteSync); |
| 206 Expect.throws(() => d.deleteSync(recursive: true)); | 208 Expect.throws(() => d.deleteSync(recursive: true)); |
| 207 } | 209 } |
| 208 | 210 |
| 209 static void testDeleteTooLongNameSync() { | 211 static void testDeleteTooLongNameSync() { |
| 210 Directory d = new Directory("").createTempSync(); | 212 Directory d = Directory.systemTemp.createTempSync('dart_directory_test'); |
| 211 var subDirName = 'subdir'; | 213 var subDirName = 'subdir'; |
| 212 var subDir = new Directory("${d.path}/$subDirName"); | 214 var subDir = new Directory("${d.path}/$subDirName"); |
| 213 subDir.createSync(); | 215 subDir.createSync(); |
| 214 // Construct a long string of the form | 216 // Construct a long string of the form |
| 215 // 'tempdir/subdir/../subdir/../subdir'. | 217 // 'tempdir/subdir/../subdir/../subdir'. |
| 216 var buffer = new StringBuffer(); | 218 var buffer = new StringBuffer(); |
| 217 buffer.write(subDir.path); | 219 buffer.write(subDir.path); |
| 218 for (var i = 0; i < 1000; i++) { | 220 for (var i = 0; i < 1000; i++) { |
| 219 buffer.write("/../${subDirName}"); | 221 buffer.write("/../${subDirName}"); |
| 220 } | 222 } |
| 221 var long = new Directory("${buffer.toString()}"); | 223 var long = new Directory("${buffer.toString()}"); |
| 222 Expect.throws(long.deleteSync); | 224 Expect.throws(long.deleteSync); |
| 223 Expect.throws(() => long.deleteSync(recursive: true)); | 225 Expect.throws(() => long.deleteSync(recursive: true)); |
| 224 d.deleteSync(recursive: true); | 226 d.deleteSync(recursive: true); |
| 225 } | 227 } |
| 226 | 228 |
| 227 static void testExistsCreateDelete() { | 229 static void testExistsCreateDelete() { |
| 228 new Directory("").createTemp().then((d) { | 230 Directory.systemTemp.createTemp('dart_directory').then((d) { |
| 229 d.exists().then((bool exists) { | 231 d.exists().then((bool exists) { |
| 230 Expect.isTrue(exists); | 232 Expect.isTrue(exists); |
| 231 Directory created = new Directory("${d.path}/subdir"); | 233 Directory created = new Directory("${d.path}/subdir"); |
| 232 created.create().then((ignore) { | 234 created.create().then((ignore) { |
| 233 created.exists().then((bool exists) { | 235 created.exists().then((bool exists) { |
| 234 Expect.isTrue(exists); | 236 Expect.isTrue(exists); |
| 235 created.delete().then((ignore) { | 237 created.delete().then((ignore) { |
| 236 created.exists().then((bool exists) { | 238 created.exists().then((bool exists) { |
| 237 Expect.isFalse(exists); | 239 Expect.isFalse(exists); |
| 238 d.delete().then((ignore) { | 240 d.delete().then((ignore) { |
| 239 d.exists().then((bool exists) { | 241 d.exists().then((bool exists) { |
| 240 Expect.isFalse(exists); | 242 Expect.isFalse(exists); |
| 241 }); | 243 }); |
| 242 }); | 244 }); |
| 243 }); | 245 }); |
| 244 }); | 246 }); |
| 245 }); | 247 }); |
| 246 }); | 248 }); |
| 247 }); | 249 }); |
| 248 }); | 250 }); |
| 249 } | 251 } |
| 250 | 252 |
| 251 static void testExistsCreateDeleteSync() { | 253 static void testExistsCreateDeleteSync() { |
| 252 Directory d = new Directory("").createTempSync(); | 254 Directory d = Directory.systemTemp.createTempSync('dart_directory_test'); |
| 253 Directory d2 = new Directory('${d.path}/'); | 255 Directory d2 = new Directory('${d.path}/'); |
| 254 Expect.isTrue(d.existsSync()); | 256 Expect.isTrue(d.existsSync()); |
| 255 Expect.isTrue(d2.existsSync()); | 257 Expect.isTrue(d2.existsSync()); |
| 256 Directory created = new Directory("${d.path}/subdir"); | 258 Directory created = new Directory("${d.path}/subdir"); |
| 257 created.createSync(); | 259 created.createSync(); |
| 258 Expect.isTrue(created.existsSync()); | 260 Expect.isTrue(created.existsSync()); |
| 259 created.deleteSync(); | 261 created.deleteSync(); |
| 260 Expect.isFalse(created.existsSync()); | 262 Expect.isFalse(created.existsSync()); |
| 261 d.deleteSync(); | 263 d.deleteSync(); |
| 262 Expect.isFalse(d.existsSync()); | 264 Expect.isFalse(d.existsSync()); |
| 263 } | 265 } |
| 264 | 266 |
| 265 static void testDeleteLinkSync() { | 267 static void testDeleteLinkSync() { |
| 266 Directory tmp = new Directory("").createTempSync(); | 268 Directory tmp = Directory.systemTemp.createTempSync('dart_directory_test'); |
| 267 var path = "${tmp.path}${Platform.pathSeparator}"; | 269 var path = "${tmp.path}${Platform.pathSeparator}"; |
| 268 Directory d = new Directory("${path}target"); | 270 Directory d = new Directory("${path}target"); |
| 269 d.createSync(); | 271 d.createSync(); |
| 270 Link l = new Link("${path}symlink"); | 272 Link l = new Link("${path}symlink"); |
| 271 l.createSync("${path}target"); | 273 l.createSync("${path}target"); |
| 272 Expect.isTrue(d.existsSync()); | 274 Expect.isTrue(d.existsSync()); |
| 273 Expect.isTrue(l.existsSync()); | 275 Expect.isTrue(l.existsSync()); |
| 274 new Directory(l.path).deleteSync(recursive: true); | 276 new Directory(l.path).deleteSync(recursive: true); |
| 275 Expect.isTrue(d.existsSync()); | 277 Expect.isTrue(d.existsSync()); |
| 276 Expect.isFalse(l.existsSync()); | 278 Expect.isFalse(l.existsSync()); |
| 277 d.deleteSync(); | 279 d.deleteSync(); |
| 278 Expect.isFalse(d.existsSync()); | 280 Expect.isFalse(d.existsSync()); |
| 279 tmp.deleteSync(); | 281 tmp.deleteSync(); |
| 280 } | 282 } |
| 281 | 283 |
| 282 static void testDeleteLinkAsFileSync() { | 284 static void testDeleteLinkAsFileSync() { |
| 283 Directory tmp = new Directory("").createTempSync(); | 285 Directory tmp = Directory.systemTemp.createTempSync('dart_directory_test'); |
| 284 var path = "${tmp.path}${Platform.pathSeparator}"; | 286 var path = "${tmp.path}${Platform.pathSeparator}"; |
| 285 Directory d = new Directory("${path}target"); | 287 Directory d = new Directory("${path}target"); |
| 286 d.createSync(); | 288 d.createSync(); |
| 287 Link l = new Link("${path}symlink"); | 289 Link l = new Link("${path}symlink"); |
| 288 l.createSync("${path}target"); | 290 l.createSync("${path}target"); |
| 289 Expect.isTrue(d.existsSync()); | 291 Expect.isTrue(d.existsSync()); |
| 290 Expect.isTrue(l.existsSync()); | 292 Expect.isTrue(l.existsSync()); |
| 291 new Link(l.path).deleteSync(); | 293 new Link(l.path).deleteSync(); |
| 292 Expect.isTrue(d.existsSync()); | 294 Expect.isTrue(d.existsSync()); |
| 293 Expect.isFalse(l.existsSync()); | 295 Expect.isFalse(l.existsSync()); |
| 294 d.deleteSync(); | 296 d.deleteSync(); |
| 295 Expect.isFalse(d.existsSync()); | 297 Expect.isFalse(d.existsSync()); |
| 296 tmp.deleteSync(); | 298 tmp.deleteSync(); |
| 297 } | 299 } |
| 298 | 300 |
| 299 static void testDeleteBrokenLinkAsFileSync() { | 301 static void testDeleteBrokenLinkAsFileSync() { |
| 300 Directory tmp = new Directory("").createTempSync(); | 302 Directory tmp = Directory.systemTemp.createTempSync('dart_directory_test'); |
| 301 var path = "${tmp.path}${Platform.pathSeparator}"; | 303 var path = "${tmp.path}${Platform.pathSeparator}"; |
| 302 Directory d = new Directory("${path}target"); | 304 Directory d = new Directory("${path}target"); |
| 303 d.createSync(); | 305 d.createSync(); |
| 304 Link l = new Link("${path}symlink"); | 306 Link l = new Link("${path}symlink"); |
| 305 l.createSync("${path}target"); | 307 l.createSync("${path}target"); |
| 306 d.deleteSync(); | 308 d.deleteSync(); |
| 307 Expect.isFalse(d.existsSync()); | 309 Expect.isFalse(d.existsSync()); |
| 308 Expect.isTrue(l.existsSync()); | 310 Expect.isTrue(l.existsSync()); |
| 309 new Link(l.path).deleteSync(); | 311 new Link(l.path).deleteSync(); |
| 310 Expect.isFalse(l.existsSync()); | 312 Expect.isFalse(l.existsSync()); |
| 311 Expect.isFalse(d.existsSync()); | 313 Expect.isFalse(d.existsSync()); |
| 312 tmp.deleteSync(); | 314 tmp.deleteSync(); |
| 313 } | 315 } |
| 314 | 316 |
| 315 static void testListBrokenLinkSync() { | 317 static void testListBrokenLinkSync() { |
| 316 Directory tmp = new Directory("").createTempSync(); | 318 Directory tmp = Directory.systemTemp.createTempSync('dart_directory_test'); |
| 317 var path = "${tmp.path}${Platform.pathSeparator}"; | 319 var path = "${tmp.path}${Platform.pathSeparator}"; |
| 318 Directory d = new Directory("${path}target"); | 320 Directory d = new Directory("${path}target"); |
| 319 d.createSync(); | 321 d.createSync(); |
| 320 Link l = new Link("${path}symlink"); | 322 Link l = new Link("${path}symlink"); |
| 321 l.createSync("${path}target"); | 323 l.createSync("${path}target"); |
| 322 d.deleteSync(); | 324 d.deleteSync(); |
| 323 int count = 0; | 325 int count = 0; |
| 324 tmp.list(followLinks: true).listen( | 326 tmp.list(followLinks: true).listen( |
| 325 (file) { | 327 (file) { |
| 326 count++; | 328 count++; |
| 327 Expect.isTrue(file is Link); | 329 Expect.isTrue(file is Link); |
| 328 }, | 330 }, |
| 329 onDone: () { | 331 onDone: () { |
| 330 Expect.equals(1, count); | 332 Expect.equals(1, count); |
| 331 l.deleteSync(); | 333 l.deleteSync(); |
| 332 tmp.deleteSync(); | 334 tmp.deleteSync(); |
| 333 }); | 335 }); |
| 334 } | 336 } |
| 335 | 337 |
| 336 static void testListLinkSync() { | 338 static void testListLinkSync() { |
| 337 Directory tmp = new Directory("").createTempSync(); | 339 Directory tmp = Directory.systemTemp.createTempSync('dart_directory_test'); |
| 338 var path = "${tmp.path}${Platform.pathSeparator}"; | 340 var path = "${tmp.path}${Platform.pathSeparator}"; |
| 339 Directory d = new Directory("${path}target"); | 341 Directory d = new Directory("${path}target"); |
| 340 d.createSync(); | 342 d.createSync(); |
| 341 Link l = new Link("${path}symlink"); | 343 Link l = new Link("${path}symlink"); |
| 342 l.createSync("${path}target"); | 344 l.createSync("${path}target"); |
| 343 int count = 0; | 345 int count = 0; |
| 344 tmp.list(followLinks: true).listen( | 346 tmp.list(followLinks: true).listen( |
| 345 (file) { | 347 (file) { |
| 346 count++; | 348 count++; |
| 347 Expect.isTrue(file is Directory); | 349 Expect.isTrue(file is Directory); |
| 348 }, | 350 }, |
| 349 onDone: () { | 351 onDone: () { |
| 350 Expect.equals(2, count); | 352 Expect.equals(2, count); |
| 351 l.deleteSync(); | 353 l.deleteSync(); |
| 352 d.deleteSync(); | 354 d.deleteSync(); |
| 353 tmp.deleteSync(); | 355 tmp.deleteSync(); |
| 354 }); | 356 }); |
| 355 } | 357 } |
| 356 | 358 |
| 357 static void testCreateTemp([String template = ""]) { | 359 static void testCreateTemp() { |
| 358 asyncStart(); | 360 Directory base = new Directory('/tmp'); |
| 359 Directory dir = new Directory(template); | 361 String template = 'dart_temp_dir'; |
| 360 Future.wait([dir.createTemp(), dir.createTemp()]) | 362 if (base.existsSync()) { |
| 361 .then((tempDirs) { | 363 asyncStart(); |
| 364 Future.wait([base.createTemp(template), base.createTemp(template)]) |
| 365 .then((tempDirs) { |
| 362 Expect.notEquals(tempDirs[0].path, tempDirs[1].path); | 366 Expect.notEquals(tempDirs[0].path, tempDirs[1].path); |
| 363 for (Directory t in tempDirs) { | 367 for (Directory t in tempDirs) { |
| 364 Expect.isTrue(t.existsSync()); | 368 Expect.isTrue(t.existsSync()); |
| 365 t.deleteSync(); | 369 t.deleteSync(); |
| 366 Expect.isFalse(t.existsSync()); | 370 Expect.isFalse(t.existsSync()); |
| 367 } | 371 } |
| 368 asyncEnd(); | 372 asyncEnd(); |
| 369 }); | 373 }); |
| 370 } | |
| 371 | |
| 372 static void testCreateTempTemplate() { | |
| 373 if (new Directory("/tmp").existsSync()) { | |
| 374 testCreateTemp("/tmp/dart_temp_dir_"); | |
| 375 } | 374 } |
| 376 } | 375 } |
| 377 | 376 |
| 377 static void testCreateSystemTemp() { |
| 378 String template = 'dart_system_temp_dir'; |
| 379 asyncStart(); |
| 380 Future.wait([Directory.systemTemp.createTemp(template), |
| 381 Directory.systemTemp.createTemp(template)]) |
| 382 .then((tempDirs) { |
| 383 Expect.notEquals(tempDirs[0].path, tempDirs[1].path); |
| 384 for (Directory t in tempDirs) { |
| 385 Expect.isTrue(t.existsSync()); |
| 386 t.deleteSync(); |
| 387 Expect.isFalse(t.existsSync()); |
| 388 } |
| 389 asyncEnd(); |
| 390 }); |
| 391 } |
| 392 |
| 378 static void testCreateDeleteTemp() { | 393 static void testCreateDeleteTemp() { |
| 379 new Directory("").createTemp().then((tempDirectory) { | 394 Directory.systemTemp.createTemp('dart_directory').then((tempDirectory) { |
| 380 String filename = | 395 String filename = |
| 381 "${tempDirectory.path}${Platform.pathSeparator}dart_testfile"; | 396 "${tempDirectory.path}${Platform.pathSeparator}dart_testfile"; |
| 382 File file = new File(filename); | 397 File file = new File(filename); |
| 383 Expect.isFalse(file.existsSync()); | 398 Expect.isFalse(file.existsSync()); |
| 384 file.create().then((ignore) { | 399 file.create().then((ignore) { |
| 385 file.exists().then((exists) { | 400 file.exists().then((exists) { |
| 386 Expect.isTrue(exists); | 401 Expect.isTrue(exists); |
| 387 // Try to delete the directory containing the file - should throw. | 402 // Try to delete the directory containing the file - should throw. |
| 388 Expect.throws(tempDirectory.deleteSync); | 403 Expect.throws(tempDirectory.deleteSync); |
| 389 Expect.isTrue(tempDirectory.existsSync()); | 404 Expect.isTrue(tempDirectory.existsSync()); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 testDeleteNonExistentSync(); | 438 testDeleteNonExistentSync(); |
| 424 testDeleteTooLongNameSync(); | 439 testDeleteTooLongNameSync(); |
| 425 testExistsCreateDelete(); | 440 testExistsCreateDelete(); |
| 426 testExistsCreateDeleteSync(); | 441 testExistsCreateDeleteSync(); |
| 427 testDeleteLinkSync(); | 442 testDeleteLinkSync(); |
| 428 testDeleteLinkAsFileSync(); | 443 testDeleteLinkAsFileSync(); |
| 429 testDeleteBrokenLinkAsFileSync(); | 444 testDeleteBrokenLinkAsFileSync(); |
| 430 testListBrokenLinkSync(); | 445 testListBrokenLinkSync(); |
| 431 testListLinkSync(); | 446 testListLinkSync(); |
| 432 testCreateTemp(); | 447 testCreateTemp(); |
| 448 testCreateSystemTemp(); |
| 433 testCreateDeleteTemp(); | 449 testCreateDeleteTemp(); |
| 434 testCurrent(); | 450 testCurrent(); |
| 435 testEquals(); | 451 testEquals(); |
| 436 } | 452 } |
| 437 } | 453 } |
| 438 | 454 |
| 439 | 455 |
| 440 class NestedTempDirectoryTest { | 456 class NestedTempDirectoryTest { |
| 441 List<Directory> createdDirectories; | 457 List<Directory> createdDirectories; |
| 442 Directory current; | 458 Directory current; |
| 443 | 459 |
| 444 NestedTempDirectoryTest.run() | 460 NestedTempDirectoryTest.run() |
| 445 : createdDirectories = new List<Directory>() { | 461 : createdDirectories = new List<Directory>() { |
| 446 new Directory("").createTemp().then(createPhaseCallback); | 462 Directory.systemTemp.createTemp('dart_directory').then(createPhaseCallback); |
| 447 } | 463 } |
| 448 | 464 |
| 449 void createPhaseCallback(temp) { | 465 void createPhaseCallback(temp) { |
| 450 createdDirectories.add(temp); | 466 createdDirectories.add(temp); |
| 451 int nestingDepth = 6; | 467 int nestingDepth = 6; |
| 452 var os = Platform.operatingSystem; | 468 var os = Platform.operatingSystem; |
| 453 if (os == "windows") nestingDepth = 2; | 469 if (os == "windows") nestingDepth = 2; |
| 454 if (createdDirectories.length < nestingDepth) { | 470 if (createdDirectories.length < nestingDepth) { |
| 455 temp.createTemp('nested_temp_dir_${createdDirectories.length}_') | 471 temp.createTemp('nested_temp_dir_${createdDirectories.length}_') |
| 456 .then(createPhaseCallback); | 472 .then(createPhaseCallback); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 483 if (os == "windows") { | 499 if (os == "windows") { |
| 484 return "*"; | 500 return "*"; |
| 485 } | 501 } |
| 486 return null; | 502 return null; |
| 487 } | 503 } |
| 488 | 504 |
| 489 | 505 |
| 490 testCreateTempErrorSync() { | 506 testCreateTempErrorSync() { |
| 491 var location = illegalTempDirectoryLocation(); | 507 var location = illegalTempDirectoryLocation(); |
| 492 if (location != null) { | 508 if (location != null) { |
| 493 Expect.throws(new Directory(location).createTempSync, | 509 Expect.throws(() => new Directory(location).createTempSync('dart_tempdir'), |
| 494 (e) => e is DirectoryException); | 510 (e) => e is DirectoryException); |
| 495 } | 511 } |
| 496 } | 512 } |
| 497 | 513 |
| 498 | 514 |
| 499 testCreateTempError() { | 515 testCreateTempError() { |
| 500 var location = illegalTempDirectoryLocation(); | 516 var location = illegalTempDirectoryLocation(); |
| 501 if (location == null) return; | 517 if (location == null) return; |
| 502 | 518 |
| 503 asyncStart(); | 519 asyncStart(); |
| 504 var future = new Directory(location).createTemp(); | 520 var future = new Directory(location).createTemp('dart_tempdir'); |
| 505 future.catchError((_) => asyncEnd()); | 521 future.catchError((_) => asyncEnd()); |
| 506 } | 522 } |
| 507 | 523 |
| 508 | 524 |
| 509 testCreateExistingSync() { | 525 testCreateExistingSync() { |
| 510 // Test that creating an existing directory succeeds. | 526 // Test that creating an existing directory succeeds. |
| 511 var d = new Directory(''); | 527 var temp = Directory.systemTemp.createTempSync('directory_test'); |
| 512 var temp = d.createTempSync(); | |
| 513 var subDir = new Directory('${temp.path}/flaf'); | 528 var subDir = new Directory('${temp.path}/flaf'); |
| 514 Expect.isFalse(subDir.existsSync()); | 529 Expect.isFalse(subDir.existsSync()); |
| 515 subDir.createSync(); | 530 subDir.createSync(); |
| 516 Expect.isTrue(subDir.existsSync()); | 531 Expect.isTrue(subDir.existsSync()); |
| 517 subDir.createSync(); | 532 subDir.createSync(); |
| 518 Expect.isTrue(subDir.existsSync()); | 533 Expect.isTrue(subDir.existsSync()); |
| 519 temp.deleteSync(recursive: true); | 534 temp.deleteSync(recursive: true); |
| 520 } | 535 } |
| 521 | 536 |
| 522 | 537 |
| 523 testCreateExisting() { | 538 testCreateExisting() { |
| 524 // Test that creating an existing directory succeeds. | 539 // Test that creating an existing directory succeeds. |
| 525 asyncStart(); | 540 asyncStart(); |
| 526 var d = new Directory(''); | 541 Directory.systemTemp.createTemp('dart_directory').then((temp) { |
| 527 d.createTemp().then((temp) { | |
| 528 var subDir = new Directory('${temp.path}/flaf'); | 542 var subDir = new Directory('${temp.path}/flaf'); |
| 529 subDir.exists().then((dirExists) { | 543 subDir.exists().then((dirExists) { |
| 530 Expect.isFalse(dirExists); | 544 Expect.isFalse(dirExists); |
| 531 subDir.create().then((_) { | 545 subDir.create().then((_) { |
| 532 subDir.exists().then((dirExists) { | 546 subDir.exists().then((dirExists) { |
| 533 Expect.isTrue(dirExists); | 547 Expect.isTrue(dirExists); |
| 534 subDir.create().then((_) { | 548 subDir.create().then((_) { |
| 535 subDir.exists().then((dirExists) { | 549 subDir.exists().then((dirExists) { |
| 536 Expect.isTrue(dirExists); | 550 Expect.isTrue(dirExists); |
| 537 temp.delete(recursive: true).then((_) { | 551 temp.delete(recursive: true).then((_) { |
| 538 asyncEnd(); | 552 asyncEnd(); |
| 539 }); | 553 }); |
| 540 }); | 554 }); |
| 541 }); | 555 }); |
| 542 }); | 556 }); |
| 543 }); | 557 }); |
| 544 }); | 558 }); |
| 545 }); | 559 }); |
| 546 } | 560 } |
| 547 | 561 |
| 548 | 562 |
| 549 testCreateDirExistingFileSync() { | 563 testCreateDirExistingFileSync() { |
| 550 // Test that creating an existing directory succeeds. | 564 // Test that creating an existing directory succeeds. |
| 551 var d = new Directory(''); | 565 var temp = Directory.systemTemp.createTempSync('directory_test'); |
| 552 var temp = d.createTempSync(); | |
| 553 var path = '${temp.path}/flaf'; | 566 var path = '${temp.path}/flaf'; |
| 554 var file = new File(path); | 567 var file = new File(path); |
| 555 file.createSync(); | 568 file.createSync(); |
| 556 Expect.isTrue(file.existsSync()); | 569 Expect.isTrue(file.existsSync()); |
| 557 Expect.throws(new Directory(path).createSync, | 570 Expect.throws(new Directory(path).createSync, |
| 558 (e) => e is DirectoryException); | 571 (e) => e is DirectoryException); |
| 559 temp.deleteSync(recursive: true); | 572 temp.deleteSync(recursive: true); |
| 560 } | 573 } |
| 561 | 574 |
| 562 | 575 |
| 563 testCreateDirExistingFile() { | 576 testCreateDirExistingFile() { |
| 564 // Test that creating an existing directory succeeds. | 577 // Test that creating an existing directory succeeds. |
| 565 asyncStart(); | 578 asyncStart(); |
| 566 var d = new Directory(''); | 579 Directory.systemTemp.createTemp('dart_directory').then((temp) { |
| 567 d.createTemp().then((temp) { | |
| 568 var path = '${temp.path}/flaf'; | 580 var path = '${temp.path}/flaf'; |
| 569 var file = new File(path); | 581 var file = new File(path); |
| 570 var subDir = new Directory(path); | 582 var subDir = new Directory(path); |
| 571 file.create().then((_) { | 583 file.create().then((_) { |
| 572 subDir.create() | 584 subDir.create() |
| 573 .then((_) { Expect.fail("dir create should fail on existing file"); }) | 585 .then((_) { Expect.fail("dir create should fail on existing file"); }) |
| 574 .catchError((error) { | 586 .catchError((error) { |
| 575 Expect.isTrue(error is DirectoryException); | 587 Expect.isTrue(error is DirectoryException); |
| 576 temp.delete(recursive: true).then((_) { | 588 temp.delete(recursive: true).then((_) { |
| 577 asyncEnd(); | 589 asyncEnd(); |
| 578 }); | 590 }); |
| 579 }); | 591 }); |
| 580 }); | 592 }); |
| 581 }); | 593 }); |
| 582 } | 594 } |
| 583 | 595 |
| 584 | 596 |
| 585 testCreateRecursiveSync() { | 597 testCreateRecursiveSync() { |
| 586 var temp = new Directory('').createTempSync(); | 598 var temp = Directory.systemTemp.createTempSync('directory_test'); |
| 587 var d = new Directory('${temp.path}/a/b/c'); | 599 var d = new Directory('${temp.path}/a/b/c'); |
| 588 d.createSync(recursive: true); | 600 d.createSync(recursive: true); |
| 589 Expect.isTrue(new Directory('${temp.path}/a').existsSync()); | 601 Expect.isTrue(new Directory('${temp.path}/a').existsSync()); |
| 590 Expect.isTrue(new Directory('${temp.path}/a/b').existsSync()); | 602 Expect.isTrue(new Directory('${temp.path}/a/b').existsSync()); |
| 591 Expect.isTrue(new Directory('${temp.path}/a/b/c').existsSync()); | 603 Expect.isTrue(new Directory('${temp.path}/a/b/c').existsSync()); |
| 592 temp.deleteSync(recursive: true); | 604 temp.deleteSync(recursive: true); |
| 593 } | 605 } |
| 594 | 606 |
| 595 | 607 |
| 596 testCreateRecursive() { | 608 testCreateRecursive() { |
| 597 asyncStart(); | 609 asyncStart(); |
| 598 new Directory('').createTemp().then((temp) { | 610 Directory.systemTemp.createTemp('dart_directory').then((temp) { |
| 599 var d = new Directory('${temp.path}/a/b/c'); | 611 var d = new Directory('${temp.path}/a/b/c'); |
| 600 d.create(recursive: true).then((_) { | 612 d.create(recursive: true).then((_) { |
| 601 Expect.isTrue(new Directory('${temp.path}/a').existsSync()); | 613 Expect.isTrue(new Directory('${temp.path}/a').existsSync()); |
| 602 Expect.isTrue(new Directory('${temp.path}/a/b').existsSync()); | 614 Expect.isTrue(new Directory('${temp.path}/a/b').existsSync()); |
| 603 Expect.isTrue(new Directory('${temp.path}/a/b/c').existsSync()); | 615 Expect.isTrue(new Directory('${temp.path}/a/b/c').existsSync()); |
| 604 temp.deleteSync(recursive: true); | 616 temp.deleteSync(recursive: true); |
| 605 asyncEnd(); | 617 asyncEnd(); |
| 606 }); | 618 }); |
| 607 }); | 619 }); |
| 608 } | 620 } |
| 609 | 621 |
| 610 | 622 |
| 611 testRename() { | 623 testRename() { |
| 612 var d = new Directory(''); | 624 var temp1 = Directory.systemTemp.createTempSync('directory_test'); |
| 613 var temp1 = d.createTempSync(); | 625 var temp2 = Directory.systemTemp.createTempSync('directory_test'); |
| 614 var temp2 = d.createTempSync(); | |
| 615 var temp3 = temp1.renameSync(temp2.path); | 626 var temp3 = temp1.renameSync(temp2.path); |
| 616 Expect.isFalse(temp1.existsSync()); | 627 Expect.isFalse(temp1.existsSync()); |
| 617 Expect.isTrue(temp2.existsSync()); | 628 Expect.isTrue(temp2.existsSync()); |
| 618 Expect.equals(temp3.path, temp2.path); | 629 Expect.equals(temp3.path, temp2.path); |
| 619 | 630 |
| 620 temp2.rename(temp1.path).then((temp4) { | 631 temp2.rename(temp1.path).then((temp4) { |
| 621 Expect.isFalse(temp3.existsSync()); | 632 Expect.isFalse(temp3.existsSync()); |
| 622 Expect.isFalse(temp2.existsSync()); | 633 Expect.isFalse(temp2.existsSync()); |
| 623 Expect.isTrue(temp1.existsSync()); | 634 Expect.isTrue(temp1.existsSync()); |
| 624 Expect.isTrue(temp4.existsSync()); | 635 Expect.isTrue(temp4.existsSync()); |
| 625 Expect.equals(temp1.path, temp4.path); | 636 Expect.equals(temp1.path, temp4.path); |
| 626 temp1.deleteSync(recursive: true); | 637 temp1.deleteSync(recursive: true); |
| 627 }); | 638 }); |
| 628 } | 639 } |
| 629 | 640 |
| 630 main() { | 641 main() { |
| 631 DirectoryTest.testMain(); | 642 DirectoryTest.testMain(); |
| 632 NestedTempDirectoryTest.testMain(); | 643 NestedTempDirectoryTest.testMain(); |
| 633 testCreateTempErrorSync(); | 644 testCreateTempErrorSync(); |
| 634 testCreateTempError(); | 645 testCreateTempError(); |
| 635 testCreateExistingSync(); | 646 testCreateExistingSync(); |
| 636 testCreateExisting(); | 647 testCreateExisting(); |
| 637 testCreateDirExistingFileSync(); | 648 testCreateDirExistingFileSync(); |
| 638 testCreateDirExistingFile(); | 649 testCreateDirExistingFile(); |
| 639 testCreateRecursive(); | 650 testCreateRecursive(); |
| 640 testCreateRecursiveSync(); | 651 testCreateRecursiveSync(); |
| 641 testRename(); | 652 testRename(); |
| 642 } | 653 } |
| OLD | NEW |