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 // Dart test program for testing FileSystemEntity.resolveSymbolicLinks | |
6 | |
7 import "package:expect/expect.dart"; | |
8 import "package:path/path.dart"; | |
9 import "package:async_helper/async_helper.dart"; | |
10 import 'dart:async'; | |
11 import 'dart:io'; | |
12 | |
13 main() { | |
14 String testsDir = dirname(dirname(dirname(Platform.script))); | |
15 // All of these tests test that resolveSymbolicLinks gives a path | |
16 // that points to the same place as the original, and that it removes | |
17 // all links, .., and . segments, and that it produces an absolute path. | |
18 asyncTest(() => testFile(join( | |
19 testsDir, 'standalone', 'io', 'resolve_symbolic_links_test.dart'))); | |
20 asyncTest(() => testFile(join(testsDir, 'standalone', 'io', '..', 'io', | |
21 'resolve_symbolic_links_test.dart'))); | |
22 | |
23 asyncTest(() => testDir(join(testsDir, 'standalone', 'io'))); | |
24 asyncTest(() => testDir(join(testsDir, 'lib', '..', 'standalone', 'io'))); | |
25 // Test a relative path. | |
26 asyncTest(() => testDir('.')); | |
27 if (Platform.isWindows) { | |
28 asyncTest(() =>testFile(join('\\\\?\\$testsDir', 'standalone', 'io', | |
29 'resolve_symbolic_links_test.dart'))); | |
30 asyncTest(() => testDir('\\\\?\\$testsDir')); | |
31 } | |
32 asyncTest(() => new Directory('').createTemp().then((tempDir) { | |
33 String temp = tempDir.path; | |
34 return makeEntities(temp).then((_) => Future.wait( | |
35 [testFile(join(temp, 'dir1', 'file1')), | |
36 testFile(join(temp, 'link1', 'file2')), | |
37 testDir(join(temp, 'dir1', 'dir2', '..', '.', '..', 'dir1')), | |
38 testDir(join(temp, 'dir1', 'dir2', '..', '.', '..', 'dir1')), | |
39 testLink(join(temp, 'link1'))])) | |
40 .then((_) { | |
41 if (Platform.isWindows) { | |
42 // Windows applies '..' to a link without resolving the link first. | |
43 return Future.wait([ | |
44 testFile(join( | |
45 temp, 'dir1', '..', 'link1', '..', 'dir1', 'dir2', 'file2')), | |
46 testDir(join(temp, 'dir1', '..', 'link1', '..', 'dir1')), | |
47 testLink(join(temp, 'link1', '..', 'link1'))]); | |
48 } else { | |
49 // Non-Windows platforms resolve the link before adding the '..'. | |
50 return Future.wait([ | |
51 testFile(join( | |
52 temp, 'dir1', '..', 'link1', '..', 'dir2', 'file2')), | |
53 testDir(join(temp, 'dir1', '..', 'link1', '..', 'dir2')), | |
54 testLink(join(temp, 'link1', '..', '..', 'link1'))]); | |
55 } | |
56 }) | |
57 .then((_) { | |
58 Directory.current = temp; | |
59 return Future.wait([ | |
60 testFile('dir1/dir2/file2'), // Test forward slashes on Windows too. | |
61 testFile('link1/file2'), | |
62 testFile(join('dir1', '..', 'dir1', '.', 'file1')), | |
63 testDir('.'), | |
64 testLink('link1')]); | |
65 }) | |
66 .then((_) { | |
67 Directory.current = 'link1'; | |
68 if (Platform.isWindows) { | |
69 return Future.wait([ | |
70 testFile('file2'), | |
71 // Windows applies '..' to a link without resolving the link first. | |
72 testFile('..\\dir1\\file1'), | |
73 testLink('.'), | |
74 testDir('..'), | |
75 testLink('..\\link1')]); | |
76 } else { | |
77 return Future.wait([ | |
78 testFile('file2'), | |
79 // On non-Windows the link is changed to dir1/dir2 before .. happens. | |
80 testFile('../dir2/file2'), | |
81 testDir('.'), | |
82 testDir('..'), | |
83 testLink('../../link1')]); | |
84 } | |
85 }) | |
86 .whenComplete(() { | |
87 Directory.current = testsDir; | |
88 tempDir.delete(recursive: true); | |
89 }); | |
90 })); | |
91 } | |
92 | |
93 Future makeEntities(String temp) { | |
94 return new Directory(join(temp, 'dir1', 'dir2')).create(recursive: true) | |
95 .then((_) => new File(join(temp, 'dir1', 'dir2', 'file2')).create()) | |
96 .then((_) => new File(join(temp, 'dir1', 'file1')).create()) | |
97 .then((_) => new Link(join(temp, 'link1')) | |
98 .create(join(temp, 'dir1', 'dir2'))); | |
99 } | |
100 | |
101 Future testFile(String name) { | |
102 // We test that f.resolveSymbolicLinks points to the same place | |
103 // as f, because the actual resolved path is not easily predictable. | |
104 // The location of the temp directory varies from system to system, | |
105 // and its path includes symbolic links on some systems. | |
106 //Expect.isTrue(FileSystemEntity.identicalSync(name, | |
107 // new File(name).resolveSymbolicLinksSync())); | |
108 return new File(name).resolveSymbolicLinks().then((String resolved) { | |
109 //Expect.isTrue(FileSystemEntity.identicalSync(name, resolved)); | |
110 Expect.isTrue(isAbsolute(resolved)); | |
111 // Test that resolveSymbolicLinks removes all links, .., and . segments. | |
112 Expect.isFalse(resolved.contains('..')); | |
113 Expect.isFalse(resolved.contains('./')); | |
114 Expect.isFalse(resolved.contains('link1')); | |
115 }); | |
116 } | |
117 | |
118 Future testDir(String name) { | |
119 Expect.isTrue(FileSystemEntity.identicalSync(name, | |
120 new Directory(name).resolveSymbolicLinksSync())); | |
121 return new Directory(name).resolveSymbolicLinks().then((String resolved) { | |
122 Expect.isTrue(FileSystemEntity.identicalSync(name, resolved)); | |
123 Expect.isTrue(isAbsolute(resolved)); | |
124 // Test that resolveSymbolicLinks removes all links, .., and . segments. | |
125 Expect.isFalse(resolved.contains('..')); | |
126 Expect.isFalse(resolved.contains('./')); | |
127 Expect.isFalse(resolved.contains('link1')); | |
128 }); | |
129 } | |
130 | |
131 Future testLink(String name) { | |
132 Expect.isFalse(FileSystemEntity.identicalSync(name, | |
133 new Link(name).resolveSymbolicLinksSync())); | |
134 Expect.isTrue(FileSystemEntity.identicalSync(new Link(name).targetSync(), | |
135 new Link(name).resolveSymbolicLinksSync())); | |
136 return new Link(name).resolveSymbolicLinks().then((String resolved) { | |
137 Expect.isFalse(FileSystemEntity.identicalSync(name, resolved)); | |
138 Expect.isTrue(isAbsolute(resolved)); | |
139 // Test that resolveSymbolicLinks removes all links, .., and . segments. | |
140 Expect.isFalse(resolved.contains('..')); | |
141 Expect.isFalse(resolved.contains('./')); | |
142 Expect.isFalse(resolved.contains('link1')); | |
143 return new Link(name).target() | |
144 .then((targetName) => FileSystemEntity.identical(targetName, resolved)) | |
145 .then((identical) => Expect.isTrue(identical)); | |
146 }); | |
147 } | |
OLD | NEW |