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