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