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