OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Dart test program for testing file I/O. | 5 // Dart test program for testing file I/O. |
6 | 6 |
7 // OtherResources=empty_file | 7 // OtherResources=empty_file |
8 // OtherResources=file_test.txt | 8 // OtherResources=file_test.txt |
9 // OtherResources=fixed_length_file | 9 // OtherResources=fixed_length_file |
10 // OtherResources=read_as_text.dat | 10 // OtherResources=read_as_text.dat |
(...skipping 1243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1254 | 1254 |
1255 static void testLastModified() { | 1255 static void testLastModified() { |
1256 asyncTestStarted(); | 1256 asyncTestStarted(); |
1257 new File(Platform.executable).lastModified().then((modified) { | 1257 new File(Platform.executable).lastModified().then((modified) { |
1258 Expect.isTrue(modified is DateTime); | 1258 Expect.isTrue(modified is DateTime); |
1259 Expect.isTrue(modified.isBefore(new DateTime.now())); | 1259 Expect.isTrue(modified.isBefore(new DateTime.now())); |
1260 asyncTestDone("testLastModified"); | 1260 asyncTestDone("testLastModified"); |
1261 }); | 1261 }); |
1262 } | 1262 } |
1263 | 1263 |
| 1264 static void testLastAccessed() { |
| 1265 asyncTestStarted(); |
| 1266 new File(Platform.executable).lastAccessed().then((accessed) { |
| 1267 Expect.isTrue(accessed is DateTime); |
| 1268 Expect.isTrue(accessed.isBefore(new DateTime.now())); |
| 1269 asyncTestDone("testLastAccessed"); |
| 1270 }); |
| 1271 } |
| 1272 |
1264 static void testDoubleAsyncOperation() { | 1273 static void testDoubleAsyncOperation() { |
1265 asyncTestStarted(); | 1274 asyncTestStarted(); |
1266 var file = new File(Platform.executable).openSync(); | 1275 var file = new File(Platform.executable).openSync(); |
1267 var completer = new Completer(); | 1276 var completer = new Completer(); |
1268 int done = 0; | 1277 int done = 0; |
1269 bool error = false; | 1278 bool error = false; |
1270 void getLength() { | 1279 void getLength() { |
1271 file.length() | 1280 file.length() |
1272 .catchError((e) { error = true; }) | 1281 .catchError((e) { error = true; }) |
1273 .whenComplete(() { | 1282 .whenComplete(() { |
1274 if (++done == 2) { | 1283 if (++done == 2) { |
1275 asyncTestDone("testDoubleAsyncOperation"); | 1284 asyncTestDone("testDoubleAsyncOperation"); |
1276 Expect.isTrue(error); | 1285 Expect.isTrue(error); |
1277 file.lengthSync(); | 1286 file.lengthSync(); |
1278 file.closeSync(); | 1287 file.closeSync(); |
1279 } | 1288 } |
1280 }); | 1289 }); |
1281 } | 1290 } |
1282 getLength(); | 1291 getLength(); |
1283 getLength(); | 1292 getLength(); |
1284 Expect.throws(() => file.lengthSync()); | 1293 Expect.throws(() => file.lengthSync()); |
1285 } | 1294 } |
1286 | 1295 |
1287 static void testLastModifiedSync() { | 1296 static void testLastModifiedSync() { |
1288 var modified = new File(Platform.executable).lastModifiedSync(); | 1297 var modified = new File(Platform.executable).lastModifiedSync(); |
1289 Expect.isTrue(modified is DateTime); | 1298 Expect.isTrue(modified is DateTime); |
1290 Expect.isTrue(modified.isBefore(new DateTime.now())); | 1299 Expect.isTrue(modified.isBefore(new DateTime.now())); |
1291 } | 1300 } |
1292 | 1301 |
| 1302 static void testLastAccessedSync() { |
| 1303 var accessed = new File(Platform.executable).lastAccessedSync(); |
| 1304 Expect.isTrue(accessed is DateTime); |
| 1305 Expect.isTrue(accessed.isBefore(new DateTime.now())); |
| 1306 } |
| 1307 |
1293 static void testLastModifiedSyncDirectory() { | 1308 static void testLastModifiedSyncDirectory() { |
1294 Directory tmp = tempDirectory.createTempSync('file_last_modified_test_'); | 1309 Directory tmp = tempDirectory.createTempSync('file_last_modified_test_'); |
1295 String dirPath = '${tmp.path}/dir'; | 1310 String dirPath = '${tmp.path}/dir'; |
1296 new Directory(dirPath).createSync(); | 1311 new Directory(dirPath).createSync(); |
1297 try { | 1312 try { |
1298 new File(dirPath).lastModifiedSync(); | 1313 new File(dirPath).lastModifiedSync(); |
1299 Expect.fail('Expected operation to throw'); | 1314 Expect.fail('Expected operation to throw'); |
1300 } catch (e) { | 1315 } catch (e) { |
1301 if (e is! FileSystemException) { | 1316 if (e is! FileSystemException) { |
1302 print(e); | 1317 print(e); |
1303 } | 1318 } |
1304 Expect.isTrue(e is FileSystemException); | 1319 Expect.isTrue(e is FileSystemException); |
1305 } finally { | 1320 } finally { |
1306 tmp.deleteSync(recursive: true); | 1321 tmp.deleteSync(recursive: true); |
1307 } | 1322 } |
1308 } | 1323 } |
1309 | 1324 |
| 1325 static void testLastAccessedSyncDirectory() { |
| 1326 Directory tmp = tempDirectory.createTempSync('file_last_accessed_test_'); |
| 1327 String dirPath = '${tmp.path}/dir'; |
| 1328 new Directory(dirPath).createSync(); |
| 1329 try { |
| 1330 new File(dirPath).lastAccessedSync(); |
| 1331 Expect.fail('Expected operation to throw'); |
| 1332 } catch (e) { |
| 1333 if (e is! FileSystemException) { |
| 1334 print(e); |
| 1335 } |
| 1336 Expect.isTrue(e is FileSystemException); |
| 1337 } finally { |
| 1338 tmp.deleteSync(recursive: true); |
| 1339 } |
| 1340 } |
| 1341 |
| 1342 static void testSetLastModifiedSync() { |
| 1343 String newFilePath = '${tempDirectory.path}/set_last_modified_sync_test'; |
| 1344 File file = new File(newFilePath); |
| 1345 file.createSync(); |
| 1346 DateTime modifiedTime = new DateTime(2016, 1, 1); |
| 1347 file.setLastModifiedSync(modifiedTime); |
| 1348 FileStat stat = file.statSync(); |
| 1349 Expect.equals(2016, stat.modified.year); |
| 1350 Expect.equals(1, stat.modified.month); |
| 1351 Expect.equals(1, stat.modified.day); |
| 1352 } |
| 1353 |
| 1354 |
| 1355 static testSetLastModified() async { |
| 1356 asyncTestStarted(); |
| 1357 String newFilePath = '${tempDirectory.path}/set_last_modified_test'; |
| 1358 File file = new File(newFilePath); |
| 1359 file.createSync(); |
| 1360 DateTime modifiedTime = new DateTime(2016, 1, 1); |
| 1361 await file.setLastModified(modifiedTime); |
| 1362 FileStat stat = await file.stat(); |
| 1363 Expect.equals(2016, stat.modified.year); |
| 1364 Expect.equals(1, stat.modified.month); |
| 1365 Expect.equals(1, stat.modified.day); |
| 1366 asyncTestDone("testSetLastModified"); |
| 1367 } |
| 1368 |
| 1369 |
| 1370 static void testSetLastModifiedSyncDirectory() { |
| 1371 Directory tmp = tempDirectory.createTempSync('file_last_modified_test_'); |
| 1372 String dirPath = '${tmp.path}/dir'; |
| 1373 new Directory(dirPath).createSync(); |
| 1374 try { |
| 1375 DateTime modifiedTime = new DateTime(2016, 1, 1); |
| 1376 new File(dirPath).setLastModifiedSync(modifiedTime); |
| 1377 Expect.fail('Expected operation to throw'); |
| 1378 } catch (e) { |
| 1379 if (e is! FileSystemException) { |
| 1380 print(e); |
| 1381 } |
| 1382 Expect.isTrue(e is FileSystemException); |
| 1383 } finally { |
| 1384 tmp.deleteSync(recursive: true); |
| 1385 } |
| 1386 } |
| 1387 |
| 1388 static void testSetLastAccessedSync() { |
| 1389 String newFilePath = '${tempDirectory.path}/set_last_accessed_sync_test'; |
| 1390 File file = new File(newFilePath); |
| 1391 file.createSync(); |
| 1392 DateTime accessedTime = new DateTime(2016, 1, 1); |
| 1393 file.setLastAccessedSync(accessedTime); |
| 1394 FileStat stat = file.statSync(); |
| 1395 Expect.equals(2016, stat.accessed.year); |
| 1396 Expect.equals(1, stat.accessed.month); |
| 1397 Expect.equals(1, stat.accessed.day); |
| 1398 } |
| 1399 |
| 1400 |
| 1401 static testSetLastAccessed() async { |
| 1402 asyncTestStarted(); |
| 1403 String newFilePath = '${tempDirectory.path}/set_last_accessed_test'; |
| 1404 File file = new File(newFilePath); |
| 1405 file.createSync(); |
| 1406 DateTime accessedTime = new DateTime(2016, 1, 1); |
| 1407 await file.setLastAccessed(accessedTime); |
| 1408 FileStat stat = await file.stat(); |
| 1409 Expect.equals(2016, stat.accessed.year); |
| 1410 Expect.equals(1, stat.accessed.month); |
| 1411 Expect.equals(1, stat.accessed.day); |
| 1412 asyncTestDone("testSetLastAccessed"); |
| 1413 } |
| 1414 |
| 1415 |
| 1416 static void testSetLastAccessedSyncDirectory() { |
| 1417 Directory tmp = tempDirectory.createTempSync('file_last_accessed_test_'); |
| 1418 String dirPath = '${tmp.path}/dir'; |
| 1419 new Directory(dirPath).createSync(); |
| 1420 try { |
| 1421 DateTime accessedTime = new DateTime(2016, 1, 1); |
| 1422 new File(dirPath).setLastAccessedSync(accessedTime); |
| 1423 Expect.fail('Expected operation to throw'); |
| 1424 } catch (e) { |
| 1425 if (e is! FileSystemException) { |
| 1426 print(e); |
| 1427 } |
| 1428 Expect.isTrue(e is FileSystemException); |
| 1429 } finally { |
| 1430 tmp.deleteSync(recursive: true); |
| 1431 } |
| 1432 } |
| 1433 |
1310 // Test that opens the same file for writing then for appending to test | 1434 // Test that opens the same file for writing then for appending to test |
1311 // that the file is not truncated when opened for appending. | 1435 // that the file is not truncated when opened for appending. |
1312 static void testAppend() { | 1436 static void testAppend() { |
1313 asyncTestStarted(); | 1437 asyncTestStarted(); |
1314 var file = new File('${tempDirectory.path}/out_append'); | 1438 var file = new File('${tempDirectory.path}/out_append'); |
1315 file.open(mode: WRITE).then((openedFile) { | 1439 file.open(mode: WRITE).then((openedFile) { |
1316 openedFile.writeString("asdf").then((ignore) { | 1440 openedFile.writeString("asdf").then((ignore) { |
1317 openedFile.close().then((ignore) { | 1441 openedFile.close().then((ignore) { |
1318 file.open(mode: APPEND).then((openedFile) { | 1442 file.open(mode: APPEND).then((openedFile) { |
1319 openedFile.length().then((length) { | 1443 openedFile.length().then((length) { |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1474 testLengthSync(); | 1598 testLengthSync(); |
1475 testPositionSync(); | 1599 testPositionSync(); |
1476 testOpenDirectoryAsFile(); | 1600 testOpenDirectoryAsFile(); |
1477 testOpenDirectoryAsFileSync(); | 1601 testOpenDirectoryAsFileSync(); |
1478 testReadAsBytesSync(); | 1602 testReadAsBytesSync(); |
1479 testReadAsBytesSyncEmptyFile(); | 1603 testReadAsBytesSyncEmptyFile(); |
1480 testReadAsTextSync(); | 1604 testReadAsTextSync(); |
1481 testReadAsTextSyncEmptyFile(); | 1605 testReadAsTextSyncEmptyFile(); |
1482 testReadAsLinesSync(); | 1606 testReadAsLinesSync(); |
1483 testLastModifiedSync(); | 1607 testLastModifiedSync(); |
| 1608 testLastAccessedSync(); |
1484 | 1609 |
1485 createTempDirectory(() { | 1610 createTempDirectory(() { |
1486 testLength(); | 1611 testLength(); |
1487 testLengthSyncDirectory(); | 1612 testLengthSyncDirectory(); |
1488 testReadWrite(); | 1613 testReadWrite(); |
1489 testReadWriteSync(); | 1614 testReadWriteSync(); |
1490 testReadWriteNoArgsSync(); | 1615 testReadWriteNoArgsSync(); |
1491 testReadWriteStream(); | 1616 testReadWriteStream(); |
1492 testReadEmptyFileSync(); | 1617 testReadEmptyFileSync(); |
1493 testReadEmptyFile(); | 1618 testReadEmptyFile(); |
(...skipping 23 matching lines...) Expand all Loading... |
1517 testWriteFromOffset(); | 1642 testWriteFromOffset(); |
1518 testDirectory(); | 1643 testDirectory(); |
1519 testDirectorySync(); | 1644 testDirectorySync(); |
1520 testWriteStringUtf8(); | 1645 testWriteStringUtf8(); |
1521 testWriteStringUtf8Sync(); | 1646 testWriteStringUtf8Sync(); |
1522 testRename(targetExists: false); | 1647 testRename(targetExists: false); |
1523 testRenameSync(targetExists: false); | 1648 testRenameSync(targetExists: false); |
1524 testRename(targetExists: true); | 1649 testRename(targetExists: true); |
1525 testRenameSync(targetExists: true); | 1650 testRenameSync(targetExists: true); |
1526 testLastModified(); | 1651 testLastModified(); |
| 1652 testLastAccessed(); |
1527 testLastModifiedSyncDirectory(); | 1653 testLastModifiedSyncDirectory(); |
| 1654 testLastAccessedSyncDirectory(); |
| 1655 testSetLastModified(); |
| 1656 testSetLastModifiedSync(); |
| 1657 testSetLastModifiedSyncDirectory(); |
| 1658 testSetLastAccessed(); |
| 1659 testSetLastAccessedSync(); |
| 1660 testSetLastAccessedSyncDirectory(); |
1528 testDoubleAsyncOperation(); | 1661 testDoubleAsyncOperation(); |
1529 asyncEnd(); | 1662 asyncEnd(); |
1530 }); | 1663 }); |
1531 } | 1664 } |
1532 } | 1665 } |
1533 | 1666 |
1534 main() { | 1667 main() { |
1535 FileTest.testMain(); | 1668 FileTest.testMain(); |
1536 } | 1669 } |
OLD | NEW |