| 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 // Dart test program for testing error handling in directory I/O. | 5 // Dart test program for testing error handling in directory I/O. |
| 6 | 6 |
| 7 #import("dart:io"); | 7 #import("dart:io"); |
| 8 #import("dart:isolate"); | 8 #import("dart:isolate"); |
| 9 | 9 |
| 10 Directory tempDir() { | 10 Directory tempDir() { |
| 11 var d = new Directory(''); | 11 var d = new Directory(''); |
| 12 d.createTempSync(); | 12 d.createTempSync(); |
| 13 return d; | 13 return d; |
| 14 } | 14 } |
| 15 | 15 |
| 16 | 16 |
| 17 bool checkCreateInNonExistentFileException(e) { | 17 bool checkCreateInNonExistentFileException(e) { |
| 18 Expect.isTrue(e is DirectoryIOException); | 18 Expect.isTrue(e is DirectoryIOException); |
| 19 Expect.isTrue(e.osError != null); | 19 Expect.isTrue(e.osError != null); |
| 20 Expect.isTrue(e.toString().indexOf("Creation failed") != -1); | 20 Expect.isTrue(e.toString().indexOf("Creation failed") != -1); |
| 21 if (Platform.operatingSystem() == "linux") { | 21 if (Platform.operatingSystem == "linux") { |
| 22 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 22 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 23 Expect.equals(2, e.osError.errorCode); | 23 Expect.equals(2, e.osError.errorCode); |
| 24 } else if (Platform.operatingSystem() == "macos") { | 24 } else if (Platform.operatingSystem == "macos") { |
| 25 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 25 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 26 Expect.equals(2, e.osError.errorCode); | 26 Expect.equals(2, e.osError.errorCode); |
| 27 } else if (Platform.operatingSystem() == "windows") { | 27 } else if (Platform.operatingSystem == "windows") { |
| 28 Expect.isTrue( | 28 Expect.isTrue( |
| 29 e.toString().indexOf( | 29 e.toString().indexOf( |
| 30 "The system cannot find the path specified") != -1); | 30 "The system cannot find the path specified") != -1); |
| 31 Expect.equals(3, e.osError.errorCode); | 31 Expect.equals(3, e.osError.errorCode); |
| 32 } | 32 } |
| 33 | 33 |
| 34 return true; | 34 return true; |
| 35 } | 35 } |
| 36 | 36 |
| 37 | 37 |
| 38 void testCreateInNonExistent(Directory temp, Function done) { | 38 void testCreateInNonExistent(Directory temp, Function done) { |
| 39 Directory inNonExistent = new Directory("${temp.path}/nonExistent/xxx"); | 39 Directory inNonExistent = new Directory("${temp.path}/nonExistent/xxx"); |
| 40 Expect.throws(() => inNonExistent.createSync(), | 40 Expect.throws(() => inNonExistent.createSync(), |
| 41 (e) => checkCreateInNonExistentFileException(e)); | 41 (e) => checkCreateInNonExistentFileException(e)); |
| 42 | 42 |
| 43 inNonExistent.create(() => Expect.fail("Unreachable code")); | 43 inNonExistent.create(() => Expect.fail("Unreachable code")); |
| 44 inNonExistent.onError = (e) { | 44 inNonExistent.onError = (e) { |
| 45 checkCreateInNonExistentFileException(e); | 45 checkCreateInNonExistentFileException(e); |
| 46 done(); | 46 done(); |
| 47 }; | 47 }; |
| 48 } | 48 } |
| 49 | 49 |
| 50 | 50 |
| 51 bool checkCreateTempInNonExistentFileException(e) { | 51 bool checkCreateTempInNonExistentFileException(e) { |
| 52 Expect.isTrue(e is DirectoryIOException); | 52 Expect.isTrue(e is DirectoryIOException); |
| 53 Expect.isTrue(e.osError != null); | 53 Expect.isTrue(e.osError != null); |
| 54 Expect.isTrue(e.toString().indexOf( | 54 Expect.isTrue(e.toString().indexOf( |
| 55 "Creation of temporary directory failed") != -1); | 55 "Creation of temporary directory failed") != -1); |
| 56 if (Platform.operatingSystem() == "linux") { | 56 if (Platform.operatingSystem == "linux") { |
| 57 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 57 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 58 Expect.equals(2, e.osError.errorCode); | 58 Expect.equals(2, e.osError.errorCode); |
| 59 } else if (Platform.operatingSystem() == "macos") { | 59 } else if (Platform.operatingSystem == "macos") { |
| 60 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 60 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 61 Expect.equals(2, e.osError.errorCode); | 61 Expect.equals(2, e.osError.errorCode); |
| 62 } else if (Platform.operatingSystem() == "windows") { | 62 } else if (Platform.operatingSystem == "windows") { |
| 63 Expect.isTrue( | 63 Expect.isTrue( |
| 64 e.toString().indexOf( | 64 e.toString().indexOf( |
| 65 "The system cannot find the path specified") != -1); | 65 "The system cannot find the path specified") != -1); |
| 66 Expect.equals(3, e.osError.errorCode); | 66 Expect.equals(3, e.osError.errorCode); |
| 67 } | 67 } |
| 68 | 68 |
| 69 return true; | 69 return true; |
| 70 } | 70 } |
| 71 | 71 |
| 72 | 72 |
| 73 void testCreateTempInNonExistent(Directory temp, Function done) { | 73 void testCreateTempInNonExistent(Directory temp, Function done) { |
| 74 Directory nonExistent = new Directory("${temp.path}/nonExistent/xxx"); | 74 Directory nonExistent = new Directory("${temp.path}/nonExistent/xxx"); |
| 75 Expect.throws(() => nonExistent.createTempSync(), | 75 Expect.throws(() => nonExistent.createTempSync(), |
| 76 (e) => checkCreateTempInNonExistentFileException(e)); | 76 (e) => checkCreateTempInNonExistentFileException(e)); |
| 77 | 77 |
| 78 nonExistent.createTemp(() => Expect.fail("Unreachable code")); | 78 nonExistent.createTemp(() => Expect.fail("Unreachable code")); |
| 79 nonExistent.onError = (e) { | 79 nonExistent.onError = (e) { |
| 80 checkCreateTempInNonExistentFileException(e); | 80 checkCreateTempInNonExistentFileException(e); |
| 81 done(); | 81 done(); |
| 82 }; | 82 }; |
| 83 } | 83 } |
| 84 | 84 |
| 85 | 85 |
| 86 bool checkDeleteNonExistentFileException(e) { | 86 bool checkDeleteNonExistentFileException(e) { |
| 87 Expect.isTrue(e is DirectoryIOException); | 87 Expect.isTrue(e is DirectoryIOException); |
| 88 Expect.isTrue(e.osError != null); | 88 Expect.isTrue(e.osError != null); |
| 89 Expect.isTrue(e.toString().indexOf("Deletion failed") != -1); | 89 Expect.isTrue(e.toString().indexOf("Deletion failed") != -1); |
| 90 if (Platform.operatingSystem() == "linux") { | 90 if (Platform.operatingSystem == "linux") { |
| 91 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 91 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 92 } else if (Platform.operatingSystem() == "macos") { | 92 } else if (Platform.operatingSystem == "macos") { |
| 93 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 93 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 94 } else if (Platform.operatingSystem() == "windows") { | 94 } else if (Platform.operatingSystem == "windows") { |
| 95 Expect.isTrue( | 95 Expect.isTrue( |
| 96 e.toString().indexOf( | 96 e.toString().indexOf( |
| 97 "The system cannot find the file specified") != -1); | 97 "The system cannot find the file specified") != -1); |
| 98 } | 98 } |
| 99 // File not not found has error code 2 on all supported platforms. | 99 // File not not found has error code 2 on all supported platforms. |
| 100 Expect.equals(2, e.osError.errorCode); | 100 Expect.equals(2, e.osError.errorCode); |
| 101 | 101 |
| 102 return true; | 102 return true; |
| 103 } | 103 } |
| 104 | 104 |
| 105 | 105 |
| 106 void testDeleteNonExistent(Directory temp, Function done) { | 106 void testDeleteNonExistent(Directory temp, Function done) { |
| 107 Directory nonExistent = new Directory("${temp.path}/nonExistent"); | 107 Directory nonExistent = new Directory("${temp.path}/nonExistent"); |
| 108 Expect.throws(() => nonExistent.deleteSync(), | 108 Expect.throws(() => nonExistent.deleteSync(), |
| 109 (e) => checkDeleteNonExistentFileException(e)); | 109 (e) => checkDeleteNonExistentFileException(e)); |
| 110 | 110 |
| 111 nonExistent.delete(() => Expect.fail("Unreachable code")); | 111 nonExistent.delete(() => Expect.fail("Unreachable code")); |
| 112 nonExistent.onError = (e) { | 112 nonExistent.onError = (e) { |
| 113 checkDeleteNonExistentFileException(e); | 113 checkDeleteNonExistentFileException(e); |
| 114 done(); | 114 done(); |
| 115 }; | 115 }; |
| 116 } | 116 } |
| 117 | 117 |
| 118 | 118 |
| 119 bool checkDeleteRecursivelyNonExistentFileException(e) { | 119 bool checkDeleteRecursivelyNonExistentFileException(e) { |
| 120 Expect.isTrue(e is DirectoryIOException); | 120 Expect.isTrue(e is DirectoryIOException); |
| 121 Expect.isTrue(e.osError != null); | 121 Expect.isTrue(e.osError != null); |
| 122 Expect.isTrue(e.toString().indexOf("Deletion failed") != -1); | 122 Expect.isTrue(e.toString().indexOf("Deletion failed") != -1); |
| 123 if (Platform.operatingSystem() == "linux") { | 123 if (Platform.operatingSystem == "linux") { |
| 124 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 124 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 125 Expect.equals(2, e.osError.errorCode); | 125 Expect.equals(2, e.osError.errorCode); |
| 126 } else if (Platform.operatingSystem() == "macos") { | 126 } else if (Platform.operatingSystem == "macos") { |
| 127 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 127 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 128 Expect.equals(2, e.osError.errorCode); | 128 Expect.equals(2, e.osError.errorCode); |
| 129 } else if (Platform.operatingSystem() == "windows") { | 129 } else if (Platform.operatingSystem == "windows") { |
| 130 Expect.isTrue( | 130 Expect.isTrue( |
| 131 e.toString().indexOf( | 131 e.toString().indexOf( |
| 132 "The system cannot find the path specified") != -1); | 132 "The system cannot find the path specified") != -1); |
| 133 Expect.equals(3, e.osError.errorCode); | 133 Expect.equals(3, e.osError.errorCode); |
| 134 } | 134 } |
| 135 | 135 |
| 136 return true; | 136 return true; |
| 137 } | 137 } |
| 138 | 138 |
| 139 | 139 |
| 140 void testDeleteRecursivelyNonExistent(Directory temp, Function done) { | 140 void testDeleteRecursivelyNonExistent(Directory temp, Function done) { |
| 141 Directory nonExistent = new Directory("${temp.path}/nonExistent"); | 141 Directory nonExistent = new Directory("${temp.path}/nonExistent"); |
| 142 Expect.throws(() => nonExistent.deleteRecursivelySync(), | 142 Expect.throws(() => nonExistent.deleteRecursivelySync(), |
| 143 (e) => checkDeleteRecursivelyNonExistentFileException(e)); | 143 (e) => checkDeleteRecursivelyNonExistentFileException(e)); |
| 144 | 144 |
| 145 nonExistent.deleteRecursively(() => Expect.fail("Unreachable code")); | 145 nonExistent.deleteRecursively(() => Expect.fail("Unreachable code")); |
| 146 nonExistent.onError = (e) { | 146 nonExistent.onError = (e) { |
| 147 checkDeleteRecursivelyNonExistentFileException(e); | 147 checkDeleteRecursivelyNonExistentFileException(e); |
| 148 done(); | 148 done(); |
| 149 }; | 149 }; |
| 150 } | 150 } |
| 151 | 151 |
| 152 | 152 |
| 153 bool checkListNonExistentFileException(e) { | 153 bool checkListNonExistentFileException(e) { |
| 154 Expect.isTrue(e is DirectoryIOException); | 154 Expect.isTrue(e is DirectoryIOException); |
| 155 Expect.isTrue(e.osError != null); | 155 Expect.isTrue(e.osError != null); |
| 156 Expect.isTrue(e.toString().indexOf("Directory listing failed") != -1); | 156 Expect.isTrue(e.toString().indexOf("Directory listing failed") != -1); |
| 157 if (Platform.operatingSystem() == "linux") { | 157 if (Platform.operatingSystem == "linux") { |
| 158 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 158 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 159 Expect.equals(2, e.osError.errorCode); | 159 Expect.equals(2, e.osError.errorCode); |
| 160 } else if (Platform.operatingSystem() == "macos") { | 160 } else if (Platform.operatingSystem == "macos") { |
| 161 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); | 161 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); |
| 162 Expect.equals(2, e.osError.errorCode); | 162 Expect.equals(2, e.osError.errorCode); |
| 163 } else if (Platform.operatingSystem() == "windows") { | 163 } else if (Platform.operatingSystem == "windows") { |
| 164 Expect.isTrue( | 164 Expect.isTrue( |
| 165 e.toString().indexOf( | 165 e.toString().indexOf( |
| 166 "The system cannot find the path specified") != -1); | 166 "The system cannot find the path specified") != -1); |
| 167 Expect.equals(3, e.osError.errorCode); | 167 Expect.equals(3, e.osError.errorCode); |
| 168 } | 168 } |
| 169 | 169 |
| 170 return true; | 170 return true; |
| 171 } | 171 } |
| 172 | 172 |
| 173 | 173 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 198 } | 198 } |
| 199 | 199 |
| 200 | 200 |
| 201 main() { | 201 main() { |
| 202 runTest(testCreateInNonExistent); | 202 runTest(testCreateInNonExistent); |
| 203 runTest(testCreateTempInNonExistent); | 203 runTest(testCreateTempInNonExistent); |
| 204 runTest(testDeleteNonExistent); | 204 runTest(testDeleteNonExistent); |
| 205 runTest(testDeleteRecursivelyNonExistent); | 205 runTest(testDeleteRecursivelyNonExistent); |
| 206 runTest(testListNonExistent); | 206 runTest(testListNonExistent); |
| 207 } | 207 } |
| OLD | NEW |