OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library io_test; | |
6 | |
7 import 'dart:io'; | |
8 | |
9 import 'package:pathos/path.dart' as path; | |
10 import 'package:unittest/unittest.dart'; | |
11 | |
12 import '../../pub/io.dart'; | |
13 import '../../pub/utils.dart'; | |
14 import 'test_pub.dart'; | |
15 | |
16 main() { | |
17 initConfig(); | |
18 | |
19 group('listDir', () { | |
20 test('lists a simple directory non-recursively', () { | |
21 expect(withTempDir((temp) { | |
22 writeTextFile(path.join(temp, 'file1.txt'), ''); | |
23 writeTextFile(path.join(temp, 'file2.txt'), ''); | |
24 createDir(path.join(temp, 'subdir')); | |
25 writeTextFile(path.join(temp, 'subdir', 'file3.txt'), ''); | |
26 | |
27 expect(listDir(temp), unorderedEquals([ | |
28 path.join(temp, 'file1.txt'), | |
29 path.join(temp, 'file2.txt'), | |
30 path.join(temp, 'subdir') | |
31 ])); | |
32 }), completes); | |
33 }); | |
34 | |
35 test('lists a simple directory recursively', () { | |
36 expect(withTempDir((temp) { | |
37 writeTextFile(path.join(temp, 'file1.txt'), ''); | |
38 writeTextFile(path.join(temp, 'file2.txt'), ''); | |
39 createDir(path.join(temp, 'subdir')); | |
40 writeTextFile(path.join(temp, 'subdir', 'file3.txt'), ''); | |
41 | |
42 expect(listDir(temp, recursive: true), unorderedEquals([ | |
43 path.join(temp, 'file1.txt'), | |
44 path.join(temp, 'file2.txt'), | |
45 path.join(temp, 'subdir'), | |
46 path.join(temp, 'subdir', 'file3.txt'), | |
47 ])); | |
48 }), completes); | |
49 }); | |
50 | |
51 test('ignores hidden files by default', () { | |
52 expect(withTempDir((temp) { | |
53 writeTextFile(path.join(temp, 'file1.txt'), ''); | |
54 writeTextFile(path.join(temp, 'file2.txt'), ''); | |
55 writeTextFile(path.join(temp, '.file3.txt'), ''); | |
56 createDir(path.join(temp, '.subdir')); | |
57 writeTextFile(path.join(temp, '.subdir', 'file3.txt'), ''); | |
58 | |
59 expect(listDir(temp, recursive: true), unorderedEquals([ | |
60 path.join(temp, 'file1.txt'), | |
61 path.join(temp, 'file2.txt') | |
62 ])); | |
63 }), completes); | |
64 }); | |
65 | |
66 test('includes hidden files when told to', () { | |
67 expect(withTempDir((temp) { | |
68 writeTextFile(path.join(temp, 'file1.txt'), ''); | |
69 writeTextFile(path.join(temp, 'file2.txt'), ''); | |
70 writeTextFile(path.join(temp, '.file3.txt'), ''); | |
71 createDir(path.join(temp, '.subdir')); | |
72 writeTextFile(path.join(temp, '.subdir', 'file3.txt'), ''); | |
73 | |
74 expect(listDir(temp, recursive: true, includeHidden: true), | |
75 unorderedEquals([ | |
76 path.join(temp, 'file1.txt'), | |
77 path.join(temp, 'file2.txt'), | |
78 path.join(temp, '.file3.txt'), | |
79 path.join(temp, '.subdir'), | |
80 path.join(temp, '.subdir', 'file3.txt') | |
81 ])); | |
82 }), completes); | |
83 }); | |
84 | |
85 test('returns the unresolved paths for symlinks', () { | |
86 expect(withTempDir((temp) { | |
87 var dirToList = path.join(temp, 'dir-to-list'); | |
88 createDir(path.join(temp, 'dir1')); | |
89 writeTextFile(path.join(temp, 'dir1', 'file1.txt'), ''); | |
90 createDir(path.join(temp, 'dir2')); | |
91 writeTextFile(path.join(temp, 'dir2', 'file2.txt'), ''); | |
92 createDir(dirToList); | |
93 createSymlink( | |
94 path.join(temp, 'dir1'), | |
95 path.join(dirToList, 'linked-dir1')); | |
96 createDir(path.join(dirToList, 'subdir')); | |
97 createSymlink( | |
98 path.join(temp, 'dir2'), | |
99 path.join(dirToList, 'subdir', 'linked-dir2')); | |
100 | |
101 expect(listDir(dirToList, recursive: true), unorderedEquals([ | |
102 path.join(dirToList, 'linked-dir1'), | |
103 path.join(dirToList, 'linked-dir1', 'file1.txt'), | |
104 path.join(dirToList, 'subdir'), | |
105 path.join(dirToList, 'subdir', 'linked-dir2'), | |
106 path.join(dirToList, 'subdir', 'linked-dir2', 'file2.txt'), | |
107 ])); | |
108 }), completes); | |
109 }); | |
110 | |
111 test('works with recursive symlinks', () { | |
112 expect(withTempDir((temp) { | |
113 writeTextFile(path.join(temp, 'file1.txt'), ''); | |
114 createSymlink(temp, path.join(temp, 'linkdir')); | |
115 | |
116 expect(listDir(temp, recursive: true), unorderedEquals([ | |
117 path.join(temp, 'file1.txt'), | |
118 path.join(temp, 'linkdir') | |
119 ])); | |
120 }), completes); | |
121 }); | |
122 | |
123 test('treats a broken symlink as a file', () { | |
124 expect(withTempDir((temp) { | |
125 writeTextFile(path.join(temp, 'file1.txt'), ''); | |
126 createDir(path.join(temp, 'dir')); | |
127 createSymlink(path.join(temp, 'dir'), path.join(temp, 'linkdir')); | |
128 deleteEntry(path.join(temp, 'dir')); | |
129 | |
130 expect(listDir(temp, recursive: true), unorderedEquals([ | |
131 path.join(temp, 'file1.txt'), | |
132 path.join(temp, 'linkdir') | |
133 ])); | |
134 }), completes); | |
135 }); | |
136 }); | |
137 | |
138 testExistencePredicate("entryExists", entryExists, | |
139 forFile: true, | |
140 forFileSymlink: true, | |
141 forMultiLevelFileSymlink: true, | |
142 forDirectory: true, | |
143 forDirectorySymlink: true, | |
144 forMultiLevelDirectorySymlink: true, | |
145 forBrokenSymlink: true, | |
146 forMultiLevelBrokenSymlink: true); | |
147 | |
148 testExistencePredicate("linkExists", linkExists, | |
149 forFile: false, | |
150 forFileSymlink: true, | |
151 forMultiLevelFileSymlink: true, | |
152 forDirectory: false, | |
153 forDirectorySymlink: true, | |
154 forMultiLevelDirectorySymlink: true, | |
155 forBrokenSymlink: true, | |
156 forMultiLevelBrokenSymlink: true); | |
157 | |
158 testExistencePredicate("fileExists", fileExists, | |
159 forFile: true, | |
160 forFileSymlink: true, | |
161 forMultiLevelFileSymlink: true, | |
162 forDirectory: false, | |
163 forDirectorySymlink: false, | |
164 forMultiLevelDirectorySymlink: false, | |
165 forBrokenSymlink: false, | |
166 forMultiLevelBrokenSymlink: false); | |
167 | |
168 testExistencePredicate("dirExists", dirExists, | |
169 forFile: false, | |
170 forFileSymlink: false, | |
171 forMultiLevelFileSymlink: false, | |
172 forDirectory: true, | |
173 forDirectorySymlink: true, | |
174 forMultiLevelDirectorySymlink: true, | |
175 forBrokenSymlink: false, | |
176 forMultiLevelBrokenSymlink: false); | |
177 } | |
178 | |
179 void testExistencePredicate(String name, bool predicate(String path), | |
180 {bool forFile, | |
181 bool forFileSymlink, | |
182 bool forMultiLevelFileSymlink, | |
183 bool forDirectory, | |
184 bool forDirectorySymlink, | |
185 bool forMultiLevelDirectorySymlink, | |
186 bool forBrokenSymlink, | |
187 bool forMultiLevelBrokenSymlink}) { | |
188 group(name, () { | |
189 test('returns $forFile for a file', () { | |
190 expect(withTempDir((temp) { | |
191 var file = path.join(temp, "test.txt"); | |
192 writeTextFile(file, "contents"); | |
193 expect(predicate(file), equals(forFile)); | |
194 }), completes); | |
195 }); | |
196 | |
197 test('returns $forDirectory for a directory', () { | |
198 expect(withTempDir((temp) { | |
199 var file = path.join(temp, "dir"); | |
200 createDir(file); | |
201 expect(predicate(file), equals(forDirectory)); | |
202 }), completes); | |
203 }); | |
204 | |
205 test('returns $forDirectorySymlink for a symlink to a directory', () { | |
206 expect(withTempDir((temp) { | |
207 var targetPath = path.join(temp, "dir"); | |
208 var symlinkPath = path.join(temp, "linkdir"); | |
209 createDir(targetPath); | |
210 createSymlink(targetPath, symlinkPath); | |
211 expect(predicate(symlinkPath), equals(forDirectorySymlink)); | |
212 }), completes); | |
213 }); | |
214 | |
215 test('returns $forMultiLevelDirectorySymlink for a multi-level symlink to ' | |
216 'a directory', () { | |
217 expect(withTempDir((temp) { | |
218 var targetPath = path.join(temp, "dir"); | |
219 var symlink1Path = path.join(temp, "link1dir"); | |
220 var symlink2Path = path.join(temp, "link2dir"); | |
221 createDir(targetPath); | |
222 createSymlink(targetPath, symlink1Path); | |
223 createSymlink(symlink1Path, symlink2Path); | |
224 expect(predicate(symlink2Path), | |
225 equals(forMultiLevelDirectorySymlink)); | |
226 }), completes); | |
227 }); | |
228 | |
229 test('returns $forBrokenSymlink for a broken symlink', () { | |
230 expect(withTempDir((temp) { | |
231 var targetPath = path.join(temp, "dir"); | |
232 var symlinkPath = path.join(temp, "linkdir"); | |
233 createDir(targetPath); | |
234 createSymlink(targetPath, symlinkPath); | |
235 deleteEntry(targetPath); | |
236 expect(predicate(symlinkPath), equals(forBrokenSymlink)); | |
237 }), completes); | |
238 }); | |
239 | |
240 test('returns $forMultiLevelBrokenSymlink for a multi-level broken symlink', | |
241 () { | |
242 expect(withTempDir((temp) { | |
243 var targetPath = path.join(temp, "dir"); | |
244 var symlink1Path = path.join(temp, "link1dir"); | |
245 var symlink2Path = path.join(temp, "link2dir"); | |
246 createDir(targetPath); | |
247 createSymlink(targetPath, symlink1Path); | |
248 createSymlink(symlink1Path, symlink2Path); | |
249 deleteEntry(targetPath); | |
250 expect(predicate(symlink2Path), equals(forMultiLevelBrokenSymlink)); | |
251 }), completes); | |
252 }); | |
253 | |
254 // Windows doesn't support symlinking to files. | |
255 if (Platform.operatingSystem != 'windows') { | |
256 test('returns $forFileSymlink for a symlink to a file', () { | |
257 expect(withTempDir((temp) { | |
258 var targetPath = path.join(temp, "test.txt"); | |
259 var symlinkPath = path.join(temp, "link.txt"); | |
260 writeTextFile(targetPath, "contents"); | |
261 createSymlink(targetPath, symlinkPath); | |
262 expect(predicate(symlinkPath), equals(forFileSymlink)); | |
263 }), completes); | |
264 }); | |
265 | |
266 test('returns $forMultiLevelFileSymlink for a multi-level symlink to a ' | |
267 'file', () { | |
268 expect(withTempDir((temp) { | |
269 var targetPath = path.join(temp, "test.txt"); | |
270 var symlink1Path = path.join(temp, "link1.txt"); | |
271 var symlink2Path = path.join(temp, "link2.txt"); | |
272 writeTextFile(targetPath, "contents"); | |
273 createSymlink(targetPath, symlink1Path); | |
274 createSymlink(symlink1Path, symlink2Path); | |
275 expect(predicate(symlink2Path), equals(forMultiLevelFileSymlink)); | |
276 }), completes); | |
277 }); | |
278 } | |
279 }); | |
280 } | |
OLD | NEW |