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