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 "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import "dart:async"; | 8 import "dart:async"; |
9 import "dart:io"; | 9 import "dart:io"; |
10 import "dart:isolate"; | 10 import "dart:isolate"; |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 return true; | 29 return true; |
30 } | 30 } |
31 | 31 |
32 | 32 |
33 void testCreateInNonExistent(Directory temp, Function done) { | 33 void testCreateInNonExistent(Directory temp, Function done) { |
34 Directory inNonExistent = new Directory("${temp.path}/nonExistent/xxx"); | 34 Directory inNonExistent = new Directory("${temp.path}/nonExistent/xxx"); |
35 Expect.throws(() => inNonExistent.createSync(), | 35 Expect.throws(() => inNonExistent.createSync(), |
36 (e) => checkCreateInNonExistentFileException(e)); | 36 (e) => checkCreateInNonExistentFileException(e)); |
37 | 37 |
38 inNonExistent.create().catchError((e) { | 38 inNonExistent.create().catchError((error) { |
39 checkCreateInNonExistentFileException(e.error); | 39 checkCreateInNonExistentFileException(error); |
40 done(); | 40 done(); |
41 }); | 41 }); |
42 } | 42 } |
43 | 43 |
44 | 44 |
45 bool checkCreateTempInNonExistentFileException(e) { | 45 bool checkCreateTempInNonExistentFileException(e) { |
46 Expect.isTrue(e is DirectoryIOException); | 46 Expect.isTrue(e is DirectoryIOException); |
47 Expect.isTrue(e.osError != null); | 47 Expect.isTrue(e.osError != null); |
48 if (Platform.operatingSystem == "linux") { | 48 if (Platform.operatingSystem == "linux") { |
49 Expect.equals(2, e.osError.errorCode); | 49 Expect.equals(2, e.osError.errorCode); |
50 } else if (Platform.operatingSystem == "macos") { | 50 } else if (Platform.operatingSystem == "macos") { |
51 Expect.equals(2, e.osError.errorCode); | 51 Expect.equals(2, e.osError.errorCode); |
52 } else if (Platform.operatingSystem == "windows") { | 52 } else if (Platform.operatingSystem == "windows") { |
53 Expect.equals(3, e.osError.errorCode); | 53 Expect.equals(3, e.osError.errorCode); |
54 } | 54 } |
55 | 55 |
56 return true; | 56 return true; |
57 } | 57 } |
58 | 58 |
59 | 59 |
60 void testCreateTempInNonExistent(Directory temp, Function done) { | 60 void testCreateTempInNonExistent(Directory temp, Function done) { |
61 Directory nonExistent = new Directory("${temp.path}/nonExistent/xxx"); | 61 Directory nonExistent = new Directory("${temp.path}/nonExistent/xxx"); |
62 Expect.throws(() => nonExistent.createTempSync(), | 62 Expect.throws(() => nonExistent.createTempSync(), |
63 (e) => checkCreateTempInNonExistentFileException(e)); | 63 (e) => checkCreateTempInNonExistentFileException(e)); |
64 | 64 |
65 nonExistent.createTemp().catchError((e) { | 65 nonExistent.createTemp().catchError((error) { |
66 checkCreateTempInNonExistentFileException(e.error); | 66 checkCreateTempInNonExistentFileException(error); |
67 done(); | 67 done(); |
68 }); | 68 }); |
69 } | 69 } |
70 | 70 |
71 | 71 |
72 bool checkDeleteNonExistentFileException(e) { | 72 bool checkDeleteNonExistentFileException(e) { |
73 Expect.isTrue(e is DirectoryIOException); | 73 Expect.isTrue(e is DirectoryIOException); |
74 Expect.isTrue(e.osError != null); | 74 Expect.isTrue(e.osError != null); |
75 // File not not found has error code 2 on all supported platforms. | 75 // File not not found has error code 2 on all supported platforms. |
76 Expect.equals(2, e.osError.errorCode); | 76 Expect.equals(2, e.osError.errorCode); |
77 | 77 |
78 return true; | 78 return true; |
79 } | 79 } |
80 | 80 |
81 | 81 |
82 void testDeleteNonExistent(Directory temp, Function done) { | 82 void testDeleteNonExistent(Directory temp, Function done) { |
83 Directory nonExistent = new Directory("${temp.path}/nonExistent"); | 83 Directory nonExistent = new Directory("${temp.path}/nonExistent"); |
84 Expect.throws(() => nonExistent.deleteSync(), | 84 Expect.throws(() => nonExistent.deleteSync(), |
85 (e) => checkDeleteNonExistentFileException(e)); | 85 (e) => checkDeleteNonExistentFileException(e)); |
86 | 86 |
87 nonExistent.delete().catchError((e) { | 87 nonExistent.delete().catchError((error) { |
88 checkDeleteNonExistentFileException(e.error); | 88 checkDeleteNonExistentFileException(error); |
89 done(); | 89 done(); |
90 }); | 90 }); |
91 } | 91 } |
92 | 92 |
93 | 93 |
94 bool checkDeleteRecursivelyNonExistentFileException(e) { | 94 bool checkDeleteRecursivelyNonExistentFileException(e) { |
95 Expect.isTrue(e is DirectoryIOException); | 95 Expect.isTrue(e is DirectoryIOException); |
96 Expect.isTrue(e.osError != null); | 96 Expect.isTrue(e.osError != null); |
97 Expect.isTrue(e.toString().indexOf("Deletion failed") != -1); | 97 Expect.isTrue(e.toString().indexOf("Deletion failed") != -1); |
98 // File not not found has error code 2 on all supported platforms. | 98 // File not not found has error code 2 on all supported platforms. |
99 Expect.equals(2, e.osError.errorCode); | 99 Expect.equals(2, e.osError.errorCode); |
100 | 100 |
101 return true; | 101 return true; |
102 } | 102 } |
103 | 103 |
104 | 104 |
105 void testDeleteRecursivelyNonExistent(Directory temp, Function done) { | 105 void testDeleteRecursivelyNonExistent(Directory temp, Function done) { |
106 Directory nonExistent = new Directory("${temp.path}/nonExistent"); | 106 Directory nonExistent = new Directory("${temp.path}/nonExistent"); |
107 Expect.throws(() => nonExistent.deleteSync(recursive: true), | 107 Expect.throws(() => nonExistent.deleteSync(recursive: true), |
108 (e) => checkDeleteRecursivelyNonExistentFileException(e)); | 108 (e) => checkDeleteRecursivelyNonExistentFileException(e)); |
109 | 109 |
110 nonExistent.delete(recursive: true).catchError((e) { | 110 nonExistent.delete(recursive: true).catchError((error) { |
111 checkDeleteRecursivelyNonExistentFileException(e.error); | 111 checkDeleteRecursivelyNonExistentFileException(error); |
112 done(); | 112 done(); |
113 }); | 113 }); |
114 } | 114 } |
115 | 115 |
116 | 116 |
117 bool checkListNonExistentFileException(e) { | 117 bool checkListNonExistentFileException(e) { |
118 Expect.isTrue(e is DirectoryIOException); | 118 Expect.isTrue(e is DirectoryIOException); |
119 Expect.isTrue(e.osError != null); | 119 Expect.isTrue(e.osError != null); |
120 Expect.isTrue(e.toString().indexOf("Directory listing failed") != -1); | 120 Expect.isTrue(e.toString().indexOf("Directory listing failed") != -1); |
121 if (Platform.operatingSystem == "linux") { | 121 if (Platform.operatingSystem == "linux") { |
122 Expect.equals(2, e.osError.errorCode); | 122 Expect.equals(2, e.osError.errorCode); |
123 } else if (Platform.operatingSystem == "macos") { | 123 } else if (Platform.operatingSystem == "macos") { |
124 Expect.equals(2, e.osError.errorCode); | 124 Expect.equals(2, e.osError.errorCode); |
125 } else if (Platform.operatingSystem == "windows") { | 125 } else if (Platform.operatingSystem == "windows") { |
126 Expect.equals(3, e.osError.errorCode); | 126 Expect.equals(3, e.osError.errorCode); |
127 } | 127 } |
128 | 128 |
129 return true; | 129 return true; |
130 } | 130 } |
131 | 131 |
132 | 132 |
133 bool checkAsyncListNonExistentFileException(e) { | 133 bool checkAsyncListNonExistentFileException(error) { |
134 Expect.isTrue(e is AsyncError); | 134 return checkListNonExistentFileException(error); |
135 return checkListNonExistentFileException(e.error); | |
136 } | 135 } |
137 | 136 |
138 | 137 |
139 void testListNonExistent(Directory temp, Function done) { | 138 void testListNonExistent(Directory temp, Function done) { |
140 Directory nonExistent = new Directory("${temp.path}/nonExistent"); | 139 Directory nonExistent = new Directory("${temp.path}/nonExistent"); |
141 Expect.throws(() => nonExistent.listSync(), (e) => e is DirectoryIOException); | 140 Expect.throws(() => nonExistent.listSync(), (e) => e is DirectoryIOException); |
142 nonExistent.list().listen( | 141 nonExistent.list().listen( |
143 (_) => Expect.fail("listing should not succeed"), | 142 (_) => Expect.fail("listing should not succeed"), |
144 onError: (e) { | 143 onError: (e) { |
145 checkAsyncListNonExistentFileException(e); | 144 checkAsyncListNonExistentFileException(e); |
146 done(); | 145 done(); |
147 }); | 146 }); |
148 } | 147 } |
149 | 148 |
150 | 149 |
151 void testRenameNonExistent(Directory temp, Function done) { | 150 void testRenameNonExistent(Directory temp, Function done) { |
152 Directory nonExistent = new Directory("${temp.path}/nonExistent"); | 151 Directory nonExistent = new Directory("${temp.path}/nonExistent"); |
153 var newPath = "${temp.path}/nonExistent2"; | 152 var newPath = "${temp.path}/nonExistent2"; |
154 Expect.throws(() => nonExistent.renameSync(newPath), | 153 Expect.throws(() => nonExistent.renameSync(newPath), |
155 (e) => e is DirectoryIOException); | 154 (e) => e is DirectoryIOException); |
156 var renameDone = nonExistent.rename(newPath); | 155 var renameDone = nonExistent.rename(newPath); |
157 renameDone.then((ignore) => Expect.fail('rename non existent')) | 156 renameDone.then((ignore) => Expect.fail('rename non existent')) |
158 .catchError((e) { | 157 .catchError((error) { |
159 Expect.isTrue(e.error is DirectoryIOException); | 158 Expect.isTrue(error is DirectoryIOException); |
160 done(); | 159 done(); |
161 }); | 160 }); |
162 } | 161 } |
163 | 162 |
164 | 163 |
165 void testRenameFileAsDirectory(Directory temp, Function done) { | 164 void testRenameFileAsDirectory(Directory temp, Function done) { |
166 File f = new File("${temp.path}/file"); | 165 File f = new File("${temp.path}/file"); |
167 var newPath = "${temp.path}/file2"; | 166 var newPath = "${temp.path}/file2"; |
168 f.createSync(); | 167 f.createSync(); |
169 var d = new Directory(f.path); | 168 var d = new Directory(f.path); |
170 Expect.throws(() => d.renameSync(newPath), | 169 Expect.throws(() => d.renameSync(newPath), |
171 (e) => e is DirectoryIOException); | 170 (e) => e is DirectoryIOException); |
172 var renameDone = d.rename(newPath); | 171 var renameDone = d.rename(newPath); |
173 renameDone.then((ignore) => Expect.fail('rename file as directory')) | 172 renameDone.then((ignore) => Expect.fail('rename file as directory')) |
174 .catchError((e) { | 173 .catchError((error) { |
175 Expect.isTrue(e.error is DirectoryIOException); | 174 Expect.isTrue(error is DirectoryIOException); |
176 done(); | 175 done(); |
177 }); | 176 }); |
178 } | 177 } |
179 | 178 |
180 | 179 |
181 testRenameOverwriteFile(Directory temp, Function done) { | 180 testRenameOverwriteFile(Directory temp, Function done) { |
182 var d = new Directory(''); | 181 var d = new Directory(''); |
183 var temp1 = d.createTempSync(); | 182 var temp1 = d.createTempSync(); |
184 var fileName = '${temp.path}/x'; | 183 var fileName = '${temp.path}/x'; |
185 new File(fileName).createSync(); | 184 new File(fileName).createSync(); |
186 Expect.throws(() => temp1.renameSync(fileName), | 185 Expect.throws(() => temp1.renameSync(fileName), |
187 (e) => e is DirectoryIOException); | 186 (e) => e is DirectoryIOException); |
188 var renameDone = temp1.rename(fileName); | 187 var renameDone = temp1.rename(fileName); |
189 renameDone.then((ignore) => Expect.fail('rename dir overwrite file')) | 188 renameDone.then((ignore) => Expect.fail('rename dir overwrite file')) |
190 .catchError((e) { | 189 .catchError((error) { |
191 Expect.isTrue(e.error is DirectoryIOException); | 190 Expect.isTrue(error is DirectoryIOException); |
192 temp1.deleteSync(recursive: true); | 191 temp1.deleteSync(recursive: true); |
193 done(); | 192 done(); |
194 }); | 193 }); |
195 } | 194 } |
196 | 195 |
197 | 196 |
198 void runTest(Function test) { | 197 void runTest(Function test) { |
199 // Create a temporary directory for the test. | 198 // Create a temporary directory for the test. |
200 var temp = new Directory('').createTempSync(); | 199 var temp = new Directory('').createTempSync(); |
201 | 200 |
(...skipping 12 matching lines...) Expand all Loading... |
214 main() { | 213 main() { |
215 runTest(testCreateInNonExistent); | 214 runTest(testCreateInNonExistent); |
216 runTest(testCreateTempInNonExistent); | 215 runTest(testCreateTempInNonExistent); |
217 runTest(testDeleteNonExistent); | 216 runTest(testDeleteNonExistent); |
218 runTest(testDeleteRecursivelyNonExistent); | 217 runTest(testDeleteRecursivelyNonExistent); |
219 runTest(testListNonExistent); | 218 runTest(testListNonExistent); |
220 runTest(testRenameNonExistent); | 219 runTest(testRenameNonExistent); |
221 runTest(testRenameFileAsDirectory); | 220 runTest(testRenameFileAsDirectory); |
222 runTest(testRenameOverwriteFile); | 221 runTest(testRenameOverwriteFile); |
223 } | 222 } |
OLD | NEW |