Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(189)

Side by Side Diff: tests/standalone/io/directory_error_test.dart

Issue 16123036: Clean up dart:io exceptions. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/io/websocket.dart ('k') | tests/standalone/io/directory_list_nonexistent_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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";
11 11
12 Directory tempDir() { 12 Directory tempDir() {
13 return new Directory('').createTempSync(); 13 return new Directory('').createTempSync();
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 DirectoryException);
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.equals(2, e.osError.errorCode); 22 Expect.equals(2, e.osError.errorCode);
23 } else if (Platform.operatingSystem == "macos") { 23 } else if (Platform.operatingSystem == "macos") {
24 Expect.equals(2, e.osError.errorCode); 24 Expect.equals(2, e.osError.errorCode);
25 } else if (Platform.operatingSystem == "windows") { 25 } else if (Platform.operatingSystem == "windows") {
26 Expect.equals(3, e.osError.errorCode); 26 Expect.equals(3, e.osError.errorCode);
27 } 27 }
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((error) { 38 inNonExistent.create().catchError((error) {
39 checkCreateInNonExistentFileException(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 DirectoryException);
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((error) { 65 nonExistent.createTemp().catchError((error) {
66 checkCreateTempInNonExistentFileException(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 DirectoryException);
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((error) { 87 nonExistent.delete().catchError((error) {
88 checkDeleteNonExistentFileException(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 DirectoryException);
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((error) { 110 nonExistent.delete(recursive: true).catchError((error) {
111 checkDeleteRecursivelyNonExistentFileException(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 DirectoryException);
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(error) { 133 bool checkAsyncListNonExistentFileException(error) {
134 return checkListNonExistentFileException(error); 134 return checkListNonExistentFileException(error);
135 } 135 }
136 136
137 137
138 void testListNonExistent(Directory temp, Function done) { 138 void testListNonExistent(Directory temp, Function done) {
139 Directory nonExistent = new Directory("${temp.path}/nonExistent"); 139 Directory nonExistent = new Directory("${temp.path}/nonExistent");
140 Expect.throws(() => nonExistent.listSync(), (e) => e is DirectoryIOException); 140 Expect.throws(() => nonExistent.listSync(), (e) => e is DirectoryException);
141 nonExistent.list().listen( 141 nonExistent.list().listen(
142 (_) => Expect.fail("listing should not succeed"), 142 (_) => Expect.fail("listing should not succeed"),
143 onError: (e) { 143 onError: (e) {
144 checkAsyncListNonExistentFileException(e); 144 checkAsyncListNonExistentFileException(e);
145 done(); 145 done();
146 }); 146 });
147 } 147 }
148 148
149 149
150 void testRenameNonExistent(Directory temp, Function done) { 150 void testRenameNonExistent(Directory temp, Function done) {
151 Directory nonExistent = new Directory("${temp.path}/nonExistent"); 151 Directory nonExistent = new Directory("${temp.path}/nonExistent");
152 var newPath = "${temp.path}/nonExistent2"; 152 var newPath = "${temp.path}/nonExistent2";
153 Expect.throws(() => nonExistent.renameSync(newPath), 153 Expect.throws(() => nonExistent.renameSync(newPath),
154 (e) => e is DirectoryIOException); 154 (e) => e is DirectoryException);
155 var renameDone = nonExistent.rename(newPath); 155 var renameDone = nonExistent.rename(newPath);
156 renameDone.then((ignore) => Expect.fail('rename non existent')) 156 renameDone.then((ignore) => Expect.fail('rename non existent'))
157 .catchError((error) { 157 .catchError((error) {
158 Expect.isTrue(error is DirectoryIOException); 158 Expect.isTrue(error is DirectoryException);
159 done(); 159 done();
160 }); 160 });
161 } 161 }
162 162
163 163
164 void testRenameFileAsDirectory(Directory temp, Function done) { 164 void testRenameFileAsDirectory(Directory temp, Function done) {
165 File f = new File("${temp.path}/file"); 165 File f = new File("${temp.path}/file");
166 var newPath = "${temp.path}/file2"; 166 var newPath = "${temp.path}/file2";
167 f.createSync(); 167 f.createSync();
168 var d = new Directory(f.path); 168 var d = new Directory(f.path);
169 Expect.throws(() => d.renameSync(newPath), 169 Expect.throws(() => d.renameSync(newPath),
170 (e) => e is DirectoryIOException); 170 (e) => e is DirectoryException);
171 var renameDone = d.rename(newPath); 171 var renameDone = d.rename(newPath);
172 renameDone.then((ignore) => Expect.fail('rename file as directory')) 172 renameDone.then((ignore) => Expect.fail('rename file as directory'))
173 .catchError((error) { 173 .catchError((error) {
174 Expect.isTrue(error is DirectoryIOException); 174 Expect.isTrue(error is DirectoryException);
175 done(); 175 done();
176 }); 176 });
177 } 177 }
178 178
179 179
180 testRenameOverwriteFile(Directory temp, Function done) { 180 testRenameOverwriteFile(Directory temp, Function done) {
181 var d = new Directory(''); 181 var d = new Directory('');
182 var temp1 = d.createTempSync(); 182 var temp1 = d.createTempSync();
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 DirectoryIOException); 186 (e) => e is DirectoryException);
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 DirectoryIOException); 190 Expect.isTrue(error is DirectoryException);
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 = new Directory('').createTempSync(); 199 var temp = new Directory('').createTempSync();
200 200
(...skipping 12 matching lines...) Expand all
213 main() { 213 main() {
214 runTest(testCreateInNonExistent); 214 runTest(testCreateInNonExistent);
215 runTest(testCreateTempInNonExistent); 215 runTest(testCreateTempInNonExistent);
216 runTest(testDeleteNonExistent); 216 runTest(testDeleteNonExistent);
217 runTest(testDeleteRecursivelyNonExistent); 217 runTest(testDeleteRecursivelyNonExistent);
218 runTest(testListNonExistent); 218 runTest(testListNonExistent);
219 runTest(testRenameNonExistent); 219 runTest(testRenameNonExistent);
220 runTest(testRenameFileAsDirectory); 220 runTest(testRenameFileAsDirectory);
221 runTest(testRenameOverwriteFile); 221 runTest(testRenameOverwriteFile);
222 } 222 }
OLDNEW
« no previous file with comments | « sdk/lib/io/websocket.dart ('k') | tests/standalone/io/directory_list_nonexistent_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698