OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 import 'dart:io'; | |
6 | |
7 import "package:async_helper/async_helper.dart"; | |
8 import "package:expect/expect.dart"; | |
9 import "package:path/path.dart"; | |
10 | |
11 main() { | |
12 testCreateRecursiveSync(); | |
13 testCreateRecursive(); | |
14 } | |
15 | |
16 testCreateRecursiveSync() { | |
Anders Johnsen
2013/10/30 12:23:56
Can you split up these two functions to smaller te
Bill Hesse
2013/10/30 15:07:37
Done.
| |
17 var temp = Directory.systemTemp.createTempSync('directory_test'); | |
18 var a = new Directory(join(temp.path, 'a')); | |
19 var b = new Directory(join(a.path, 'b')); | |
20 var c = new Directory(join(b.path, 'c')); | |
21 void expectDirectories() { | |
22 Expect.isTrue(a.existsSync()); | |
23 Expect.isTrue(b.existsSync()); | |
24 Expect.isTrue(c.existsSync()); | |
25 } | |
26 | |
27 try { | |
28 Expect.throws(() => c.createSync()); | |
29 c.createSync(recursive: true); | |
30 expectDirectories(); | |
31 a.deleteSync(recursive: true); | |
32 | |
33 var d = new Directory('${c.path}/'); | |
34 d.createSync(recursive: true); | |
35 expectDirectories(); | |
36 Expect.isTrue(d.existsSync()); | |
37 a.deleteSync(recursive: true); | |
38 | |
39 var f = new File(join(c.path, 'file')); | |
40 Expect.throws(() => f.createSync()); | |
41 f.createSync(recursive: true); | |
42 expectDirectories(); | |
43 Expect.isTrue(f.existsSync()); | |
44 a.deleteSync(recursive: true); | |
45 | |
46 var l = new Link(join(c.path, 'link')); | |
47 Expect.throws(() => l.createSync(temp.path)); | |
48 l.createSync(temp.path, recursive:true); | |
49 expectDirectories(); | |
50 Expect.isTrue(l.existsSync()); | |
51 Expect.isTrue(new Directory(l.targetSync()).existsSync()); | |
52 a.deleteSync(recursive: true); | |
53 | |
54 // Test cases where the directories or the object already exist. | |
55 a.createSync(recursive: true); | |
56 Expect.isTrue(a.existsSync()); | |
57 a.createSync(recursive: true); | |
58 Expect.isTrue(a.existsSync()); | |
59 | |
60 f = new File(join(temp.path, 'file')); | |
61 f.createSync(recursive: true); | |
62 Expect.isTrue(f.existsSync()); | |
63 f.createSync(recursive: true); | |
64 Expect.isTrue(f.existsSync()); | |
65 | |
66 l = new Link(join(temp.path, 'link')); | |
67 l.createSync(temp.path, recursive: true); | |
68 Expect.isTrue(l.existsSync()); | |
69 Expect.throws(() => l.createSync(temp.path, recursive: true)); | |
70 Expect.isTrue(l.existsSync()); | |
71 } finally { | |
72 temp.deleteSync(recursive: true); | |
73 } | |
74 } | |
75 | |
76 testCreateRecursive() { | |
77 asyncStart(); | |
78 Directory temp; | |
79 Directory a; | |
80 Directory b; | |
81 Directory c; | |
82 Directory d; | |
83 File f; | |
84 Link l; | |
85 Function lift(Function f) => | |
86 (futureValue) => futureValue.then((value) => f(value)); | |
87 | |
88 Future expectDirectories() { | |
89 return lift(Expect.isTrue)(a.exists()) | |
90 .then((_) => lift(Expect.isTrue)(b.exists())) | |
91 .then((_) => lift(Expect.isTrue)(c.exists())); | |
92 } | |
93 | |
94 Future expectFileSystemException(Function f, String message) { | |
95 return f().then( | |
96 (_) => Expect.fail('Expected a FileSystemException: $message'), | |
97 onError: (e) => Expect.isTrue(e is FileSystemException)); | |
98 } | |
99 | |
100 Directory.systemTemp.createTemp('dart_directory').then((dir) { | |
101 temp = dir; | |
102 a = new Directory(join(temp.path, 'a')); | |
103 b = new Directory(join(a.path, 'b')); | |
104 c = new Directory(join(b.path, 'c')); | |
105 }).then((_) => expectFileSystemException(() => c.create(), 'c.create')) | |
106 .then((_) => c.create(recursive: true)) | |
107 .then((_) => expectDirectories()) | |
108 .then((_) => a.delete(recursive: true)) | |
109 | |
110 .then((_) => d = new Directory('${c.path}/')) | |
111 .then((_) => d.create(recursive: true)) | |
112 .then((_) => expectDirectories()) | |
113 .then((_) => lift(Expect.isTrue)(d.exists())) | |
114 .then((_) => a.delete(recursive: true)) | |
115 | |
116 .then((_) => f = new File(join(c.path, 'file'))) | |
117 .then((_) => expectFileSystemException(() => f.create(), 'f.create')) | |
118 .then((_) => f.create(recursive: true)) | |
119 .then((_) => expectDirectories()) | |
120 .then((_) => lift(Expect.isTrue)(f.exists())) | |
121 .then((_) => a.delete(recursive: true)) | |
122 | |
123 .then((_) => l = new Link(join(c.path, 'link'))) | |
124 .then((_) => expectFileSystemException(() => l.create(temp.path), | |
125 'l.create')) | |
126 .then((_) => l.create(temp.path, recursive: true)) | |
127 .then((_) => expectDirectories()) | |
128 .then((_) => lift(Expect.isTrue)(l.exists())) | |
129 .then((_) => lift(Expect.isTrue)(new Directory(l.targetSync()).exists())) | |
130 .then((_) => a.delete(recursive: true)) | |
131 | |
132 // Test cases where the directories or the object already exist. | |
133 .then((_) => a.create(recursive: true)) | |
134 .then((_) => lift(Expect.isTrue)(a.exists())) | |
135 .then((_) => a.create(recursive: true)) | |
136 .then((_) => lift(Expect.isTrue)(a.exists())) | |
137 | |
138 .then((_) => f = new File(join(temp.path, 'file'))) | |
139 .then((_) => f.create(recursive: true)) | |
140 .then((_) => lift(Expect.isTrue)(f.exists())) | |
141 .then((_) => f.create(recursive: true)) | |
142 .then((_) => lift(Expect.isTrue)(f.exists())) | |
143 | |
144 .then((_) => l = new Link(join(temp.path, 'link'))) | |
145 .then((_) => l.create(temp.path, recursive: true)) | |
146 .then((_) => lift(Expect.isTrue)(l.exists())) | |
147 .then((_) => expectFileSystemException( | |
148 () => l.create(temp.path, recursive: true), 'l.create existing')) | |
149 .then((_) => lift(Expect.isTrue)(l.exists())) | |
150 | |
151 .then((_) => asyncEnd()) | |
152 .whenComplete(() => temp.delete(recursive: true)); | |
153 } | |
OLD | NEW |