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 library io_test; | 5 library io_test; |
6 | 6 |
| 7 import 'dart:async'; |
7 import 'dart:io'; | 8 import 'dart:io'; |
8 | 9 |
9 import 'package:pathos/path.dart' as path; | 10 import 'package:pathos/path.dart' as path; |
10 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
11 | 12 |
12 import '../lib/src/io.dart'; | 13 import '../lib/src/io.dart'; |
13 import '../lib/src/utils.dart'; | 14 import '../lib/src/utils.dart'; |
14 import 'test_pub.dart'; | 15 import 'test_pub.dart'; |
15 | 16 |
16 main() { | 17 main() { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 expect(listDir(temp, recursive: true), unorderedEquals([ | 131 expect(listDir(temp, recursive: true), unorderedEquals([ |
131 path.join(temp, 'file1.txt'), | 132 path.join(temp, 'file1.txt'), |
132 path.join(temp, 'linkdir') | 133 path.join(temp, 'linkdir') |
133 ])); | 134 ])); |
134 }), completes); | 135 }), completes); |
135 }); | 136 }); |
136 }); | 137 }); |
137 | 138 |
138 group('canonicalize', () { | 139 group('canonicalize', () { |
139 test('resolves a non-link', () { | 140 test('resolves a non-link', () { |
140 expect(withTempDir((temp) { | 141 expect(withCanonicalTempDir((temp) { |
141 var filePath = path.join(temp, 'file'); | 142 var filePath = path.join(temp, 'file'); |
142 writeTextFile(filePath, ''); | 143 writeTextFile(filePath, ''); |
143 expect(canonicalize(filePath), equals(filePath)); | 144 expect(canonicalize(filePath), equals(filePath)); |
144 }), completes); | 145 }), completes); |
145 }); | 146 }); |
146 | 147 |
147 test('resolves a non-existent file', () { | 148 test('resolves a non-existent file', () { |
148 expect(withTempDir((temp) { | 149 expect(withCanonicalTempDir((temp) { |
149 expect(canonicalize(path.join(temp, 'nothing')), | 150 expect(canonicalize(path.join(temp, 'nothing')), |
150 equals(path.join(temp, 'nothing'))); | 151 equals(path.join(temp, 'nothing'))); |
151 }), completes); | 152 }), completes); |
152 }); | 153 }); |
153 | 154 |
154 test('resolves a symlink', () { | 155 test('resolves a symlink', () { |
155 expect(withTempDir((temp) { | 156 expect(withCanonicalTempDir((temp) { |
156 createDir(path.join(temp, 'linked-dir')); | 157 createDir(path.join(temp, 'linked-dir')); |
157 createSymlink( | 158 createSymlink( |
158 path.join(temp, 'linked-dir'), | 159 path.join(temp, 'linked-dir'), |
159 path.join(temp, 'dir')); | 160 path.join(temp, 'dir')); |
160 expect( | 161 expect( |
161 canonicalize(path.join(temp, 'dir')), | 162 canonicalize(path.join(temp, 'dir')), |
162 equals(path.join(temp, 'linked-dir'))); | 163 equals(path.join(temp, 'linked-dir'))); |
163 }), completes); | 164 }), completes); |
164 }); | 165 }); |
165 | 166 |
166 test('resolves a relative symlink', () { | 167 test('resolves a relative symlink', () { |
167 expect(withTempDir((temp) { | 168 expect(withCanonicalTempDir((temp) { |
168 createDir(path.join(temp, 'linked-dir')); | 169 createDir(path.join(temp, 'linked-dir')); |
169 createSymlink( | 170 createSymlink( |
170 path.join(temp, 'linked-dir'), | 171 path.join(temp, 'linked-dir'), |
171 path.join(temp, 'dir'), | 172 path.join(temp, 'dir'), |
172 relative: true); | 173 relative: true); |
173 expect( | 174 expect( |
174 canonicalize(path.join(temp, 'dir')), | 175 canonicalize(path.join(temp, 'dir')), |
175 equals(path.join(temp, 'linked-dir'))); | 176 equals(path.join(temp, 'linked-dir'))); |
176 }), completes); | 177 }), completes); |
177 }); | 178 }); |
178 | 179 |
179 test('resolves a single-level horizontally recursive symlink', () { | 180 test('resolves a single-level horizontally recursive symlink', () { |
180 expect(withTempDir((temp) { | 181 expect(withCanonicalTempDir((temp) { |
181 var linkPath = path.join(temp, 'foo'); | 182 var linkPath = path.join(temp, 'foo'); |
182 createSymlink(linkPath, linkPath); | 183 createSymlink(linkPath, linkPath); |
183 expect(canonicalize(linkPath), equals(linkPath)); | 184 expect(canonicalize(linkPath), equals(linkPath)); |
184 }), completes); | 185 }), completes); |
185 }); | 186 }); |
186 | 187 |
187 test('resolves a multi-level horizontally recursive symlink', () { | 188 test('resolves a multi-level horizontally recursive symlink', () { |
188 expect(withTempDir((temp) { | 189 expect(withCanonicalTempDir((temp) { |
189 var fooPath = path.join(temp, 'foo'); | 190 var fooPath = path.join(temp, 'foo'); |
190 var barPath = path.join(temp, 'bar'); | 191 var barPath = path.join(temp, 'bar'); |
191 var bazPath = path.join(temp, 'baz'); | 192 var bazPath = path.join(temp, 'baz'); |
192 createSymlink(barPath, fooPath); | 193 createSymlink(barPath, fooPath); |
193 createSymlink(bazPath, barPath); | 194 createSymlink(bazPath, barPath); |
194 createSymlink(fooPath, bazPath); | 195 createSymlink(fooPath, bazPath); |
195 expect(canonicalize(fooPath), equals(fooPath)); | 196 expect(canonicalize(fooPath), equals(fooPath)); |
196 expect(canonicalize(barPath), equals(barPath)); | 197 expect(canonicalize(barPath), equals(barPath)); |
197 expect(canonicalize(bazPath), equals(bazPath)); | 198 expect(canonicalize(bazPath), equals(bazPath)); |
198 | 199 |
199 createSymlink(fooPath, path.join(temp, 'outer')); | 200 createSymlink(fooPath, path.join(temp, 'outer')); |
200 expect(canonicalize(path.join(temp, 'outer')), equals(fooPath)); | 201 expect(canonicalize(path.join(temp, 'outer')), equals(fooPath)); |
201 }), completes); | 202 }), completes); |
202 }); | 203 }); |
203 | 204 |
204 test('resolves a broken symlink', () { | 205 test('resolves a broken symlink', () { |
205 expect(withTempDir((temp) { | 206 expect(withCanonicalTempDir((temp) { |
206 createSymlink(path.join(temp, 'nonexistent'), path.join(temp, 'foo')); | 207 createSymlink(path.join(temp, 'nonexistent'), path.join(temp, 'foo')); |
207 expect( | 208 expect( |
208 canonicalize(path.join(temp, 'foo')), | 209 canonicalize(path.join(temp, 'foo')), |
209 equals(path.join(temp, 'nonexistent'))); | 210 equals(path.join(temp, 'nonexistent'))); |
210 }), completes); | 211 }), completes); |
211 }); | 212 }); |
212 | 213 |
213 test('resolves multiple nested symlinks', () { | 214 test('resolves multiple nested symlinks', () { |
214 expect(withTempDir((temp) { | 215 expect(withCanonicalTempDir((temp) { |
215 var dir1 = path.join(temp, 'dir1'); | 216 var dir1 = path.join(temp, 'dir1'); |
216 var dir2 = path.join(temp, 'dir2'); | 217 var dir2 = path.join(temp, 'dir2'); |
217 var subdir1 = path.join(dir1, 'subdir1'); | 218 var subdir1 = path.join(dir1, 'subdir1'); |
218 var subdir2 = path.join(dir2, 'subdir2'); | 219 var subdir2 = path.join(dir2, 'subdir2'); |
219 createDir(dir2); | 220 createDir(dir2); |
220 createDir(subdir2); | 221 createDir(subdir2); |
221 createSymlink(dir2, dir1); | 222 createSymlink(dir2, dir1); |
222 createSymlink(subdir2, subdir1); | 223 createSymlink(subdir2, subdir1); |
223 expect( | 224 expect( |
224 canonicalize(path.join(subdir1, 'file')), | 225 canonicalize(path.join(subdir1, 'file')), |
225 equals(path.join(subdir2, 'file'))); | 226 equals(path.join(subdir2, 'file'))); |
226 }), completes); | 227 }), completes); |
227 }); | 228 }); |
228 | 229 |
229 test('resolves a nested vertical symlink', () { | 230 test('resolves a nested vertical symlink', () { |
230 expect(withTempDir((temp) { | 231 expect(withCanonicalTempDir((temp) { |
231 var dir1 = path.join(temp, 'dir1'); | 232 var dir1 = path.join(temp, 'dir1'); |
232 var dir2 = path.join(temp, 'dir2'); | 233 var dir2 = path.join(temp, 'dir2'); |
233 var subdir = path.join(dir1, 'subdir'); | 234 var subdir = path.join(dir1, 'subdir'); |
234 createDir(dir1); | 235 createDir(dir1); |
235 createDir(dir2); | 236 createDir(dir2); |
236 createSymlink(dir2, subdir); | 237 createSymlink(dir2, subdir); |
237 expect( | 238 expect( |
238 canonicalize(path.join(subdir, 'file')), | 239 canonicalize(path.join(subdir, 'file')), |
239 equals(path.join(dir2, 'file'))); | 240 equals(path.join(dir2, 'file'))); |
240 }), completes); | 241 }), completes); |
241 }); | 242 }); |
242 | 243 |
243 test('resolves a vertically recursive symlink', () { | 244 test('resolves a vertically recursive symlink', () { |
244 expect(withTempDir((temp) { | 245 expect(withCanonicalTempDir((temp) { |
245 var dir = path.join(temp, 'dir'); | 246 var dir = path.join(temp, 'dir'); |
246 var subdir = path.join(dir, 'subdir'); | 247 var subdir = path.join(dir, 'subdir'); |
247 createDir(dir); | 248 createDir(dir); |
248 createSymlink(dir, subdir); | 249 createSymlink(dir, subdir); |
249 expect( | 250 expect( |
250 canonicalize(path.join(temp, 'dir', 'subdir', 'subdir', 'subdir', | 251 canonicalize(path.join(temp, 'dir', 'subdir', 'subdir', 'subdir', |
251 'subdir', 'file')), | 252 'subdir', 'file')), |
252 equals(path.join(dir, 'file'))); | 253 equals(path.join(dir, 'file'))); |
253 }), completes); | 254 }), completes); |
254 }); | 255 }); |
| 256 |
| 257 test('resolves a symlink that links to a path that needs more resolving', |
| 258 () { |
| 259 expect(withCanonicalTempDir((temp) { |
| 260 var dir = path.join(temp, 'dir'); |
| 261 var linkdir = path.join(temp, 'linkdir'); |
| 262 var linkfile = path.join(dir, 'link'); |
| 263 createDir(dir); |
| 264 createSymlink(dir, linkdir); |
| 265 createSymlink(path.join(linkdir, 'file'), linkfile); |
| 266 expect( |
| 267 canonicalize(linkfile), |
| 268 equals(path.join(dir, 'file'))); |
| 269 }), completes); |
| 270 }); |
| 271 |
| 272 test('resolves a pair of pathologically-recursive symlinks', () { |
| 273 expect(withCanonicalTempDir((temp) { |
| 274 var foo = path.join(temp, 'foo'); |
| 275 var subfoo = path.join(foo, 'subfoo'); |
| 276 var bar = path.join(temp, 'bar'); |
| 277 var subbar = path.join(bar, 'subbar'); |
| 278 createSymlink(subbar, foo); |
| 279 createSymlink(subfoo, bar); |
| 280 expect( |
| 281 canonicalize(subfoo), |
| 282 equals(path.join(subfoo, 'subbar', 'subfoo'))); |
| 283 }), completes); |
| 284 }); |
255 }); | 285 }); |
256 | 286 |
257 testExistencePredicate("entryExists", entryExists, | 287 testExistencePredicate("entryExists", entryExists, |
258 forFile: true, | 288 forFile: true, |
259 forFileSymlink: true, | 289 forFileSymlink: true, |
260 forMultiLevelFileSymlink: true, | 290 forMultiLevelFileSymlink: true, |
261 forDirectory: true, | 291 forDirectory: true, |
262 forDirectorySymlink: true, | 292 forDirectorySymlink: true, |
263 forMultiLevelDirectorySymlink: true, | 293 forMultiLevelDirectorySymlink: true, |
264 forBrokenSymlink: true, | 294 forBrokenSymlink: true, |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 var symlink2Path = path.join(temp, "link2.txt"); | 420 var symlink2Path = path.join(temp, "link2.txt"); |
391 writeTextFile(targetPath, "contents"); | 421 writeTextFile(targetPath, "contents"); |
392 createSymlink(targetPath, symlink1Path); | 422 createSymlink(targetPath, symlink1Path); |
393 createSymlink(symlink1Path, symlink2Path); | 423 createSymlink(symlink1Path, symlink2Path); |
394 expect(predicate(symlink2Path), equals(forMultiLevelFileSymlink)); | 424 expect(predicate(symlink2Path), equals(forMultiLevelFileSymlink)); |
395 }), completes); | 425 }), completes); |
396 }); | 426 }); |
397 } | 427 } |
398 }); | 428 }); |
399 } | 429 } |
| 430 |
| 431 /// Like [withTempDir], but canonicalizes the path before passing it to [fn]. |
| 432 Future withCanonicalTempDir(Future fn(String path)) => |
| 433 withTempDir((temp) => fn(canonicalize(temp))); |
OLD | NEW |