Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // Directory listing test. | 5 // Directory listing test. |
| 6 | 6 |
| 7 class DirectoryTest { | 7 class DirectoryTest { |
| 8 static void testListing() { | 8 static void testListing() { |
| 9 bool listedDir = false; | 9 bool listedDir = false; |
| 10 bool listedFile = false; | 10 bool listedFile = false; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 directory.list(recursive: true); | 48 directory.list(recursive: true); |
| 49 | 49 |
| 50 // Listing is asynchronous, so nothing should be listed at this | 50 // Listing is asynchronous, so nothing should be listed at this |
| 51 // point. | 51 // point. |
| 52 Expect.isFalse(listedDir); | 52 Expect.isFalse(listedDir); |
| 53 Expect.isFalse(listedFile); | 53 Expect.isFalse(listedFile); |
| 54 } | 54 } |
| 55 | 55 |
| 56 static void testExistsCreateDelete() { | 56 static void testExistsCreateDelete() { |
|
Søren Gjesse
2011/12/20 09:12:40
We should avoid using /tmp, or at least check for
Mads Ager (google)
2011/12/20 09:26:24
We have one test that does the check. Let's just u
| |
| 57 Directory d = new Directory("/tmp/dart_temp_dir_"); | 57 Directory d = new Directory("/tmp/dart_temp_dir_"); |
| 58 d.createTempHandler = () { | |
| 59 d.existsHandler = (bool exists) { | |
| 60 Expect.isTrue(exists); | |
| 61 Directory created = new Directory("${d.path}/subdir"); | |
| 62 created.createHandler = () { | |
| 63 created.existsHandler = (bool exists) { | |
| 64 Expect.isTrue(exists); | |
| 65 created.deleteHandler = () { | |
| 66 created.existsHandler = (bool exists) { | |
| 67 Expect.isFalse(exists); | |
| 68 d.deleteHandler = () { | |
| 69 d.existsHandler = (bool exists) { | |
| 70 Expect.isFalse(exists); | |
| 71 }; | |
| 72 d.exists(); | |
| 73 }; | |
| 74 d.delete(); | |
| 75 }; | |
| 76 created.exists(); | |
| 77 }; | |
| 78 created.delete(); | |
| 79 }; | |
| 80 created.exists(); | |
| 81 }; | |
| 82 created.create(); | |
| 83 }; | |
| 84 d.exists(); | |
| 85 }; | |
| 86 d.createTemp(); | |
| 87 } | |
| 88 | |
| 89 static void testExistsCreateDeleteSync() { | |
| 90 Directory d = new Directory("/tmp/dart_temp_dir_"); | |
|
Søren Gjesse
2011/12/20 09:12:40
Ditto.
Mads Ager (google)
2011/12/20 09:26:24
Done.
| |
| 58 d.createTempSync(); | 91 d.createTempSync(); |
| 59 Expect.isTrue(d.existsSync()); | 92 Expect.isTrue(d.existsSync()); |
| 60 Directory created = new Directory("${d.path}/subdir"); | 93 Directory created = new Directory("${d.path}/subdir"); |
| 61 created.createSync(); | 94 created.createSync(); |
| 62 Expect.isTrue(created.existsSync()); | 95 Expect.isTrue(created.existsSync()); |
| 63 created.deleteSync(); | 96 created.deleteSync(); |
| 64 Expect.isFalse(created.existsSync()); | 97 Expect.isFalse(created.existsSync()); |
| 65 d.deleteSync(); | 98 d.deleteSync(); |
| 66 Expect.isFalse(d.existsSync()); | 99 Expect.isFalse(d.existsSync()); |
| 100 } | |
| 67 | 101 |
| 102 static void testCreateTemp() { | |
| 68 Directory tempDir1 = new Directory("/tmp/dart_temp_dir_"); | 103 Directory tempDir1 = new Directory("/tmp/dart_temp_dir_"); |
|
Søren Gjesse
2011/12/20 09:12:40
Ditto.
Mads Ager (google)
2011/12/20 09:26:24
This one does the check further down.
| |
| 69 Directory tempDir2 = new Directory("/tmp/dart_temp_dir_"); | 104 Directory tempDir2 = new Directory("/tmp/dart_temp_dir_"); |
| 70 bool stage1aDone = false; | 105 bool stage1aDone = false; |
| 71 bool stage1bDone = false; | 106 bool stage1bDone = false; |
| 72 bool emptyTemplateTestRunning = false; | 107 bool emptyTemplateTestRunning = false; |
| 73 | 108 |
| 74 // Stages 0 through 2 run twice, the second time with an empty path. | 109 // Stages 0 through 2 run twice, the second time with an empty path. |
| 75 Function stage0; | 110 Function stage0; |
| 76 Function stage1a; | 111 Function stage1a; |
| 77 Function stage1b; | 112 Function stage1b; |
| 78 Function stage2; | 113 Function stage2; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 }; | 165 }; |
| 131 | 166 |
| 132 if (new Directory("/tmp").existsSync()) { | 167 if (new Directory("/tmp").existsSync()) { |
| 133 stage0(); | 168 stage0(); |
| 134 } else { | 169 } else { |
| 135 emptyTemplateTestRunning = true; | 170 emptyTemplateTestRunning = true; |
| 136 stage3(); | 171 stage3(); |
| 137 } | 172 } |
| 138 } | 173 } |
| 139 | 174 |
| 140 static void testCreateTemp() { | 175 static void testCreateDeleteTemp() { |
| 141 Directory tempDirectory = new Directory(""); | 176 Directory tempDirectory = new Directory(""); |
| 142 tempDirectory.createTempHandler = () { | 177 tempDirectory.createTempHandler = () { |
| 143 String filename = tempDirectory.path + | 178 String filename = tempDirectory.path + |
| 144 new Platform().pathSeparator() + "dart_testfile"; | 179 new Platform().pathSeparator() + "dart_testfile"; |
| 145 File file = new File(filename); | 180 File file = new File(filename); |
| 146 Expect.isFalse(file.existsSync()); | 181 Expect.isFalse(file.existsSync()); |
| 147 file.errorHandler = (error) { | 182 file.errorHandler = (error) { |
| 148 Expect.fail("testCreateTemp file.errorHandler called: $error"); | 183 Expect.fail("testCreateTemp file.errorHandler called: $error"); |
| 149 }; | 184 }; |
| 150 file.createHandler = () { | 185 file.createHandler = () { |
| 151 file.openHandler = (RandomAccessFile openedFile) { | 186 file.existsHandler = (bool exists) { |
| 152 openedFile.writeList([65, 66, 67, 13], 0, 4); | 187 Expect.isTrue(exists); |
| 153 openedFile.noPendingWriteHandler = () { | 188 // Try to delete the directory containing the file - should throw. |
| 154 openedFile.length(); | 189 bool threw_exception = false; |
| 155 }; | 190 try { |
|
Søren Gjesse
2011/12/20 09:12:40
Can't we use Expect.throws here?
Mads Ager (google)
2011/12/20 09:26:24
Done.
| |
| 156 openedFile.lengthHandler = (int length) { | 191 tempDirectory.deleteSync(); |
| 157 Expect.equals(4, length); | 192 } catch (var e) { |
| 158 openedFile.close(); | |
| 159 }; | |
| 160 openedFile.closeHandler = () { | |
| 161 file.exists(); | |
| 162 }; | |
| 163 file.existsHandler = (bool exists) { | |
| 164 Expect.isTrue(exists); | |
| 165 // Try to delete the directory containing the file - should throw. | |
| 166 bool threw_exception = false; | |
| 167 try { | |
| 168 tempDirectory.deleteSync(); | |
| 169 } catch (var e) { | |
| 170 Expect.isTrue(tempDirectory.existsSync()); | |
| 171 threw_exception = true; | |
| 172 } | |
| 173 Expect.isTrue(threw_exception); | |
| 174 Expect.isTrue(tempDirectory.existsSync()); | 193 Expect.isTrue(tempDirectory.existsSync()); |
| 194 threw_exception = true; | |
| 195 } | |
| 196 Expect.isTrue(threw_exception); | |
| 197 Expect.isTrue(tempDirectory.existsSync()); | |
| 175 | 198 |
| 176 // Delete the file, and then delete the directory. | 199 // Delete the file, and then delete the directory. |
| 177 file.delete(); | |
| 178 }; | |
| 179 file.deleteHandler = () { | 200 file.deleteHandler = () { |
| 180 tempDirectory.deleteSync(); | 201 tempDirectory.deleteSync(); |
| 181 Expect.isFalse(tempDirectory.existsSync()); | 202 Expect.isFalse(tempDirectory.existsSync()); |
| 182 }; | 203 }; |
| 204 file.delete(); | |
| 183 }; | 205 }; |
| 184 file.open(writable: true); | 206 file.exists(); |
| 185 }; | 207 }; |
| 186 file.create(); | 208 file.create(); |
| 187 }; | 209 }; |
| 188 tempDirectory.createTemp(); | 210 tempDirectory.createTemp(); |
| 189 } | 211 } |
| 190 | 212 |
| 191 static void testNestedTempDirectory() { | |
| 192 var test = new NestedTempDirectoryTest(); | |
| 193 } | |
| 194 | |
| 195 | |
| 196 static void testMain() { | 213 static void testMain() { |
| 197 testListing(); | 214 testListing(); |
| 198 testExistsCreateDelete(); | 215 testExistsCreateDelete(); |
| 216 testExistsCreateDeleteSync(); | |
| 199 testCreateTemp(); | 217 testCreateTemp(); |
| 200 testNestedTempDirectory(); | 218 testCreateDeleteTemp(); |
| 201 } | 219 } |
| 202 } | 220 } |
| 203 | 221 |
| 204 | 222 |
| 205 class NestedTempDirectoryTest { | 223 class NestedTempDirectoryTest { |
| 206 List<Directory> createdDirectories; | 224 List<Directory> createdDirectories; |
| 207 Directory current; | 225 Directory current; |
| 208 | 226 |
| 209 NestedTempDirectoryTest(): createdDirectories = new List<Directory>(); | 227 NestedTempDirectoryTest.run() |
| 228 : createdDirectories = new List<Directory>(), | |
| 229 current = new Directory("") { | |
| 230 current.createTempHandler = createPhaseCallback; | |
| 231 current.errorHandler = errorCallback; | |
| 232 current.createTemp(); | |
| 233 } | |
| 210 | 234 |
| 211 void errorCallback(error) { | 235 void errorCallback(error) { |
| 212 Expect.fail("Error callback called in NestedTempDirectoryTest: $error"); | 236 Expect.fail("Error callback called in NestedTempDirectoryTest: $error"); |
| 213 } | 237 } |
| 214 | 238 |
| 215 void createPhaseCallback() { | 239 void createPhaseCallback() { |
| 216 createdDirectories.add(current); | 240 createdDirectories.add(current); |
| 217 int nestingDepth = 6; | 241 int nestingDepth = 6; |
| 218 var os = new Platform().operatingSystem(); | 242 var os = new Platform().operatingSystem(); |
| 219 if (os == "windows") nestingDepth = 2; | 243 if (os == "windows") nestingDepth = 2; |
| 220 if (createdDirectories.length < nestingDepth) { | 244 if (createdDirectories.length < nestingDepth) { |
| 221 current = new Directory( | 245 current = new Directory( |
| 222 current.path + "/nested_temp_dir_${createdDirectories.length}_"); | 246 current.path + "/nested_temp_dir_${createdDirectories.length}_"); |
| 223 current.errorHandler = errorCallback; | 247 current.errorHandler = errorCallback; |
| 224 current.createTempHandler = createPhaseCallback; | 248 current.createTempHandler = createPhaseCallback; |
| 225 current.createTemp(); | 249 current.createTemp(); |
| 226 } else { | 250 } else { |
| 227 deletePhaseCallback(); | 251 deletePhaseCallback(); |
| 228 } | 252 } |
| 229 } | 253 } |
| 230 | 254 |
| 231 void deletePhaseCallback() { | 255 void deletePhaseCallback() { |
| 232 if (!createdDirectories.isEmpty()) { | 256 if (!createdDirectories.isEmpty()) { |
| 233 current = createdDirectories.removeLast(); | 257 current = createdDirectories.removeLast(); |
| 234 current.deleteSync(); | 258 current.deleteSync(); |
| 235 deletePhaseCallback(); | 259 deletePhaseCallback(); |
| 236 } | 260 } |
| 237 } | 261 } |
| 238 | 262 |
| 239 void startTest() { | |
| 240 current = new Directory(""); | |
| 241 current.createTempHandler = createPhaseCallback; | |
| 242 current.errorHandler = errorCallback; | |
| 243 current.createTemp(); | |
| 244 } | |
| 245 | |
| 246 static void testMain() { | 263 static void testMain() { |
| 247 new NestedTempDirectoryTest().startTest(); | 264 new NestedTempDirectoryTest.run(); |
| 248 new NestedTempDirectoryTest().startTest(); | 265 new NestedTempDirectoryTest.run(); |
| 249 } | 266 } |
| 250 } | 267 } |
| 251 | 268 |
| 252 | 269 |
| 253 String illegalTempDirectoryLocation() { | 270 String illegalTempDirectoryLocation() { |
| 254 // Determine a platform specific illegal location for a temporary directory. | 271 // Determine a platform specific illegal location for a temporary directory. |
| 255 var os = new Platform().operatingSystem(); | 272 var os = new Platform().operatingSystem(); |
| 256 if (os == "linux" || os == "macos") { | 273 if (os == "linux" || os == "macos") { |
| 257 return "/dev/zero/"; | 274 return "/dev/zero/"; |
| 258 } | 275 } |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 287 dir.createTemp(); | 304 dir.createTemp(); |
| 288 } | 305 } |
| 289 | 306 |
| 290 | 307 |
| 291 main() { | 308 main() { |
| 292 DirectoryTest.testMain(); | 309 DirectoryTest.testMain(); |
| 293 NestedTempDirectoryTest.testMain(); | 310 NestedTempDirectoryTest.testMain(); |
| 294 testCreateTempErrorSync(); | 311 testCreateTempErrorSync(); |
| 295 testCreateTempError(); | 312 testCreateTempError(); |
| 296 } | 313 } |
| OLD | NEW |