| 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 import "package:async_helper/async_helper.dart"; | 5 import "package:async_helper/async_helper.dart"; |
| 6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
| 7 import "package:path/path.dart"; | 7 import "package:path/path.dart"; |
| 8 import "dart:async"; | 8 import "dart:async"; |
| 9 import "dart:io"; | 9 import "dart:io"; |
| 10 import "dart:isolate"; | 10 import "dart:isolate"; |
| 11 | 11 |
| 12 // Test the dart:io Link class. | 12 // Test the dart:io Link class. |
| 13 | 13 |
| 14 testCreateSync() { | 14 testCreateSync() { |
| 15 asyncStart(); | 15 asyncStart(); |
| 16 String base = Directory.systemTemp.createTempSync('dart_link').path; | 16 String base = Directory.systemTemp.createTempSync('dart_link').path; |
| 17 if (isRelative(base)) { | 17 if (isRelative(base)) { |
| 18 Expect.fail( | 18 Expect.fail( |
| 19 'Link tests expect absolute paths to system temporary directories. ' | 19 'Link tests expect absolute paths to system temporary directories. ' |
| 20 'A relative path in TMPDIR gives relative paths to them.'); | 20 'A relative path in TMPDIR gives relative paths to them.'); |
| 21 } | 21 } |
| 22 String link = join(base, 'link'); | 22 String link = join(base, 'link'); |
| 23 String target = join(base, 'target'); | 23 String target = join(base, 'target'); |
| 24 new Directory(target).createSync(); | 24 new Directory(target).createSync(); |
| 25 new Link(link).createSync(target); | 25 new Link(link).createSync(target); |
| 26 Expect.equals( |
| 27 FileSystemEntityType.DIRECTORY, FileSystemEntity.typeSync(link)); |
| 28 Expect.equals( |
| 29 FileSystemEntityType.DIRECTORY, FileSystemEntity.typeSync(target)); |
| 30 Expect.equals(FileSystemEntityType.LINK, |
| 31 FileSystemEntity.typeSync(link, followLinks: false)); |
| 26 Expect.equals(FileSystemEntityType.DIRECTORY, | 32 Expect.equals(FileSystemEntityType.DIRECTORY, |
| 27 FileSystemEntity.typeSync(link)); | 33 FileSystemEntity.typeSync(target, followLinks: false)); |
| 28 Expect.equals(FileSystemEntityType.DIRECTORY, | |
| 29 FileSystemEntity.typeSync(target)); | |
| 30 Expect.equals(FileSystemEntityType.LINK, | |
| 31 FileSystemEntity.typeSync(link, followLinks: false)); | |
| 32 Expect.equals(FileSystemEntityType.DIRECTORY, | |
| 33 FileSystemEntity.typeSync(target, followLinks: false)); | |
| 34 Expect.isTrue(FileSystemEntity.isLinkSync(link)); | 34 Expect.isTrue(FileSystemEntity.isLinkSync(link)); |
| 35 Expect.isFalse(FileSystemEntity.isLinkSync(target)); | 35 Expect.isFalse(FileSystemEntity.isLinkSync(target)); |
| 36 Expect.isTrue(new Directory(link).existsSync()); | 36 Expect.isTrue(new Directory(link).existsSync()); |
| 37 Expect.isTrue(new Directory(target).existsSync()); | 37 Expect.isTrue(new Directory(target).existsSync()); |
| 38 Expect.isTrue(new Link(link).existsSync()); | 38 Expect.isTrue(new Link(link).existsSync()); |
| 39 Expect.isFalse(new Link(target).existsSync()); | 39 Expect.isFalse(new Link(target).existsSync()); |
| 40 Expect.equals(target, new Link(link).targetSync()); | 40 Expect.equals(target, new Link(link).targetSync()); |
| 41 Expect.throws(() => new Link(target).targetSync()); | 41 Expect.throws(() => new Link(target).targetSync()); |
| 42 | 42 |
| 43 String createdThroughLink = join(base, 'link', 'createdThroughLink'); | 43 String createdThroughLink = join(base, 'link', 'createdThroughLink'); |
| 44 String createdDirectly = join(base, 'target', 'createdDirectly'); | 44 String createdDirectly = join(base, 'target', 'createdDirectly'); |
| 45 new Directory(createdThroughLink).createSync(); | 45 new Directory(createdThroughLink).createSync(); |
| 46 new Directory(createdDirectly).createSync(); | 46 new Directory(createdDirectly).createSync(); |
| 47 Expect.isTrue(new Directory(createdThroughLink).existsSync()); | 47 Expect.isTrue(new Directory(createdThroughLink).existsSync()); |
| 48 Expect.isTrue(new Directory(createdDirectly).existsSync()); | 48 Expect.isTrue(new Directory(createdDirectly).existsSync()); |
| 49 Expect.isTrue(new Directory(join(base, 'link', 'createdDirectly')) | 49 Expect.isTrue( |
| 50 .existsSync()); | 50 new Directory(join(base, 'link', 'createdDirectly')).existsSync()); |
| 51 Expect.isTrue(new Directory(join(base, 'target', 'createdThroughLink')) | 51 Expect.isTrue( |
| 52 .existsSync()); | 52 new Directory(join(base, 'target', 'createdThroughLink')).existsSync()); |
| 53 Expect.equals(FileSystemEntityType.DIRECTORY, | 53 Expect.equals(FileSystemEntityType.DIRECTORY, |
| 54 FileSystemEntity.typeSync(createdThroughLink, | 54 FileSystemEntity.typeSync(createdThroughLink, followLinks: false)); |
| 55 followLinks: false)); | |
| 56 Expect.equals(FileSystemEntityType.DIRECTORY, | 55 Expect.equals(FileSystemEntityType.DIRECTORY, |
| 57 FileSystemEntity.typeSync(createdDirectly, followLinks: false)); | 56 FileSystemEntity.typeSync(createdDirectly, followLinks: false)); |
| 58 | 57 |
| 59 // Test FileSystemEntity.identical on files, directories, and links, | 58 // Test FileSystemEntity.identical on files, directories, and links, |
| 60 // reached by different paths. | 59 // reached by different paths. |
| 61 Expect.isTrue(FileSystemEntity.identicalSync(createdDirectly, | 60 Expect |
| 62 createdDirectly)); | 61 .isTrue(FileSystemEntity.identicalSync(createdDirectly, createdDirectly)); |
| 63 Expect.isFalse(FileSystemEntity.identicalSync(createdDirectly, | 62 Expect.isFalse( |
| 64 createdThroughLink)); | 63 FileSystemEntity.identicalSync(createdDirectly, createdThroughLink)); |
| 65 Expect.isTrue(FileSystemEntity.identicalSync(createdDirectly, | 64 Expect.isTrue(FileSystemEntity.identicalSync( |
| 66 join(base, 'link', 'createdDirectly'))); | 65 createdDirectly, join(base, 'link', 'createdDirectly'))); |
| 67 Expect.isTrue(FileSystemEntity.identicalSync(createdThroughLink, | 66 Expect.isTrue(FileSystemEntity.identicalSync( |
| 68 join(base, 'target', 'createdThroughLink'))); | 67 createdThroughLink, join(base, 'target', 'createdThroughLink'))); |
| 69 | 68 |
| 70 Expect.isFalse(FileSystemEntity.identicalSync(target, link)); | 69 Expect.isFalse(FileSystemEntity.identicalSync(target, link)); |
| 71 Expect.isTrue(FileSystemEntity.identicalSync(link, link)); | 70 Expect.isTrue(FileSystemEntity.identicalSync(link, link)); |
| 72 Expect.isTrue(FileSystemEntity.identicalSync(target, target)); | 71 Expect.isTrue(FileSystemEntity.identicalSync(target, target)); |
| 73 Expect.isTrue(FileSystemEntity.identicalSync(target, | 72 Expect.isTrue( |
| 74 new Link(link).targetSync())); | 73 FileSystemEntity.identicalSync(target, new Link(link).targetSync())); |
| 75 String absolutePath = new File(".").resolveSymbolicLinksSync(); | 74 String absolutePath = new File(".").resolveSymbolicLinksSync(); |
| 76 Expect.isTrue(FileSystemEntity.identicalSync(".", absolutePath)); | 75 Expect.isTrue(FileSystemEntity.identicalSync(".", absolutePath)); |
| 77 | 76 |
| 78 String createdFile = join(base, 'target', 'createdFile'); | 77 String createdFile = join(base, 'target', 'createdFile'); |
| 79 new File(createdFile).createSync(); | 78 new File(createdFile).createSync(); |
| 80 Expect.isTrue(FileSystemEntity.identicalSync(createdFile, createdFile)); | 79 Expect.isTrue(FileSystemEntity.identicalSync(createdFile, createdFile)); |
| 81 Expect.isFalse(FileSystemEntity.identicalSync(createdFile, createdDirectly)); | 80 Expect.isFalse(FileSystemEntity.identicalSync(createdFile, createdDirectly)); |
| 82 Expect.isTrue(FileSystemEntity.identicalSync(createdFile, | 81 Expect.isTrue(FileSystemEntity.identicalSync( |
| 83 join(base, 'link', 'createdFile'))); | 82 createdFile, join(base, 'link', 'createdFile'))); |
| 84 Expect.throws(() => FileSystemEntity.identicalSync(createdFile, | 83 Expect.throws(() => FileSystemEntity.identicalSync( |
| 85 join(base, 'link', 'does_not_exist'))); | 84 createdFile, join(base, 'link', 'does_not_exist'))); |
| 86 | 85 |
| 87 var baseDir = new Directory(base); | 86 var baseDir = new Directory(base); |
| 88 | 87 |
| 89 Map makeExpected(bool recursive, bool followLinks) { | 88 Map makeExpected(bool recursive, bool followLinks) { |
| 90 Map expected = new Map(); | 89 Map expected = new Map(); |
| 91 expected['target'] = 'Directory'; | 90 expected['target'] = 'Directory'; |
| 92 expected['link'] = followLinks ? 'Directory' : 'Link'; | 91 expected['link'] = followLinks ? 'Directory' : 'Link'; |
| 93 if (recursive) { | 92 if (recursive) { |
| 94 expected[join('target', 'createdDirectly')] = 'Directory'; | 93 expected[join('target', 'createdDirectly')] = 'Directory'; |
| 95 expected[join('target', 'createdThroughLink')] = 'Directory'; | 94 expected[join('target', 'createdThroughLink')] = 'Directory'; |
| 96 expected[join('target', 'createdFile')] = 'File'; | 95 expected[join('target', 'createdFile')] = 'File'; |
| 97 if (followLinks) { | 96 if (followLinks) { |
| 98 expected[join('link', 'createdDirectly')] = 'Directory'; | 97 expected[join('link', 'createdDirectly')] = 'Directory'; |
| 99 expected[join('link', 'createdThroughLink')] = 'Directory'; | 98 expected[join('link', 'createdThroughLink')] = 'Directory'; |
| 100 expected[join('link', 'createdFile')] = 'File'; | 99 expected[join('link', 'createdFile')] = 'File'; |
| 101 } | 100 } |
| 102 } | 101 } |
| 103 return expected; | 102 return expected; |
| 104 } | 103 } |
| 105 | 104 |
| 106 void checkEntity(FileSystemEntity x, Map expected) { | 105 void checkEntity(FileSystemEntity x, Map expected) { |
| 107 String ending = relative(x.path, from:base); | 106 String ending = relative(x.path, from: base); |
| 108 Expect.isNotNull(expected[ending]); | 107 Expect.isNotNull(expected[ending]); |
| 109 Expect.isTrue(x.toString().startsWith(expected[ending])); | 108 Expect.isTrue(x.toString().startsWith(expected[ending])); |
| 110 expected[ending] = 'Found'; | 109 expected[ending] = 'Found'; |
| 111 } | 110 } |
| 112 | 111 |
| 113 List futures = []; | 112 List futures = []; |
| 114 for (bool recursive in [true, false]) { | 113 for (bool recursive in [true, false]) { |
| 115 for (bool followLinks in [true, false]) { | 114 for (bool followLinks in [true, false]) { |
| 116 Map expected = makeExpected(recursive, followLinks); | 115 Map expected = makeExpected(recursive, followLinks); |
| 117 for (var x in baseDir.listSync(recursive: recursive, | 116 for (var x |
| 118 followLinks: followLinks)) { | 117 in baseDir.listSync(recursive: recursive, followLinks: followLinks)) { |
| 119 checkEntity(x, expected); | 118 checkEntity(x, expected); |
| 120 } | 119 } |
| 121 for (var v in expected.values) { | 120 for (var v in expected.values) { |
| 122 Expect.equals('Found', v); | 121 Expect.equals('Found', v); |
| 123 } | 122 } |
| 124 expected = makeExpected(recursive, followLinks); | 123 expected = makeExpected(recursive, followLinks); |
| 125 // We use Stream.reduce to run a function on each entry, and return | 124 // We use Stream.reduce to run a function on each entry, and return |
| 126 // a future that completes when done. | 125 // a future that completes when done. |
| 127 var f = new Completer(); | 126 var f = new Completer(); |
| 128 futures.add(f.future); | 127 futures.add(f.future); |
| 129 baseDir.list(recursive: recursive, followLinks: followLinks).listen( | 128 baseDir.list(recursive: recursive, followLinks: followLinks).listen( |
| 130 (entity) { | 129 (entity) { |
| 131 checkEntity(entity, expected); | 130 checkEntity(entity, expected); |
| 132 }, | 131 }, onDone: () { |
| 133 onDone: () { | 132 for (var v in expected.values) { |
| 134 for (var v in expected.values) { | 133 Expect.equals('Found', v); |
| 135 Expect.equals('Found', v); | 134 } |
| 136 } | 135 f.complete(null); |
| 137 f.complete(null); | 136 }); |
| 138 }); | |
| 139 } | 137 } |
| 140 } | 138 } |
| 141 Future.wait(futures).then((_) { | 139 Future.wait(futures).then((_) { |
| 142 new Directory(target).deleteSync(recursive: true); | 140 new Directory(target).deleteSync(recursive: true); |
| 143 for (bool recursive in [true, false]) { | 141 for (bool recursive in [true, false]) { |
| 144 for (bool followLinks in [true, false]) { | 142 for (bool followLinks in [true, false]) { |
| 145 var result = baseDir.listSync(recursive: recursive, | 143 var result = |
| 146 followLinks: followLinks); | 144 baseDir.listSync(recursive: recursive, followLinks: followLinks); |
| 147 Expect.equals(1, result.length); | 145 Expect.equals(1, result.length); |
| 148 Expect.isTrue(result[0] is Link); | 146 Expect.isTrue(result[0] is Link); |
| 149 } | 147 } |
| 150 } | 148 } |
| 151 baseDir.deleteSync(recursive: true); | 149 baseDir.deleteSync(recursive: true); |
| 152 asyncEnd(); | 150 asyncEnd(); |
| 153 }); | 151 }); |
| 154 } | 152 } |
| 155 | 153 |
| 156 testCreateLoopingLink() { | 154 testCreateLoopingLink() { |
| 157 asyncStart(); | 155 asyncStart(); |
| 158 String base = Directory.systemTemp.createTempSync('dart_link').path; | 156 String base = Directory.systemTemp.createTempSync('dart_link').path; |
| 159 new Directory(join(base, 'a', 'b', 'c')).create(recursive: true) | 157 new Directory(join(base, 'a', 'b', 'c')) |
| 160 .then((_) => | 158 .create(recursive: true) |
| 161 new Link(join(base, 'a', 'b', 'c', 'd')).create(join(base, 'a', 'b'))) | 159 .then((_) => |
| 162 .then((_) => | 160 new Link(join(base, 'a', 'b', 'c', 'd')).create(join(base, 'a', 'b'))) |
| 163 new Link(join(base, 'a', 'b', 'c', 'e')).create(join(base, 'a'))) | 161 .then((_) => |
| 164 .then((_) => | 162 new Link(join(base, 'a', 'b', 'c', 'e')).create(join(base, 'a'))) |
| 165 new Directory(join(base, 'a')) | 163 .then((_) => new Directory(join(base, 'a')) |
| 166 .list(recursive: true, followLinks: false) | 164 .list(recursive: true, followLinks: false) |
| 167 .last) | 165 .last) |
| 168 .then((_) => | 166 .then((_) => |
| 169 // This directory listing must terminate, even though it contains loops. | 167 // This directory listing must terminate, even though it contains loop
s. |
| 170 new Directory(join(base, 'a')) | 168 new Directory(join(base, 'a')) |
| 171 .list(recursive: true, followLinks: true) | 169 .list(recursive: true, followLinks: true) |
| 172 .last) | 170 .last) |
| 173 .then((_) => | 171 .then((_) => |
| 174 // This directory listing must terminate, even though it contains loops. | 172 // This directory listing must terminate, even though it contains loop
s. |
| 175 new Directory(join(base, 'a', 'b', 'c')) | 173 new Directory(join(base, 'a', 'b', 'c')) |
| 176 .list(recursive: true, followLinks: true) | 174 .list(recursive: true, followLinks: true) |
| 177 .last) | 175 .last) |
| 178 .whenComplete(() { | 176 .whenComplete(() { |
| 179 new Directory(base).deleteSync(recursive: true); | 177 new Directory(base).deleteSync(recursive: true); |
| 180 asyncEnd(); | 178 asyncEnd(); |
| 181 }); | 179 }); |
| 182 } | 180 } |
| 183 | 181 |
| 184 testRenameSync() { | 182 testRenameSync() { |
| 185 testRename(String base, String target) { | 183 testRename(String base, String target) { |
| 186 Link link1 = new Link(join(base, 'c'))..createSync(target); | 184 Link link1 = new Link(join(base, 'c'))..createSync(target); |
| 187 Expect.isTrue(link1.existsSync()); | 185 Expect.isTrue(link1.existsSync()); |
| 188 Link link2 = link1.renameSync(join(base, 'd')); | 186 Link link2 = link1.renameSync(join(base, 'd')); |
| 189 Expect.isFalse(link1.existsSync()); | 187 Expect.isFalse(link1.existsSync()); |
| 190 Expect.isTrue(link2.existsSync()); | 188 Expect.isTrue(link2.existsSync()); |
| 191 link2.deleteSync(); | 189 link2.deleteSync(); |
| 192 Expect.isFalse(link2.existsSync()); | 190 Expect.isFalse(link2.existsSync()); |
| 193 } | 191 } |
| 194 | 192 |
| 195 Directory baseDir = Directory.systemTemp.createTempSync('dart_link'); | 193 Directory baseDir = Directory.systemTemp.createTempSync('dart_link'); |
| 196 String base = baseDir.path; | 194 String base = baseDir.path; |
| 197 Directory dir = new Directory(join(base, 'a'))..createSync(); | 195 Directory dir = new Directory(join(base, 'a'))..createSync(); |
| 198 File file = new File(join(base, 'b'))..createSync(); | 196 File file = new File(join(base, 'b'))..createSync(); |
| 199 | 197 |
| 200 testRename(base, file.path); | 198 testRename(base, file.path); |
| 201 testRename(base, dir.path); | 199 testRename(base, dir.path); |
| 202 | 200 |
| 203 baseDir.deleteSync(recursive: true); | 201 baseDir.deleteSync(recursive: true); |
| 204 } | 202 } |
| 205 | 203 |
| 206 void testLinkErrorSync() { | 204 void testLinkErrorSync() { |
| 207 Expect.throws(() => | 205 Expect.throws( |
| 208 new Link('some-dir-that-doent exist/some link file/bla/fisk').createSync( | 206 () => new Link('some-dir-that-doent exist/some link file/bla/fisk') |
| 209 'bla bla bla/b lalal/blfir/sdfred/es'), | 207 .createSync('bla bla bla/b lalal/blfir/sdfred/es'), |
| 210 (e) => e is FileSystemException); | 208 (e) => e is FileSystemException); |
| 211 } | 209 } |
| 212 | 210 |
| 213 checkExists(String filePath) => Expect.isTrue(new File(filePath).existsSync()); | 211 checkExists(String filePath) => Expect.isTrue(new File(filePath).existsSync()); |
| 214 | 212 |
| 215 testRelativeLinksSync() { | 213 testRelativeLinksSync() { |
| 216 Directory tempDirectory = Directory.systemTemp.createTempSync('dart_link'); | 214 Directory tempDirectory = Directory.systemTemp.createTempSync('dart_link'); |
| 217 String temp = tempDirectory.path; | 215 String temp = tempDirectory.path; |
| 218 String oldWorkingDirectory = Directory.current.path; | 216 String oldWorkingDirectory = Directory.current.path; |
| 219 // Make directories and files to test links. | 217 // Make directories and files to test links. |
| 220 new Directory(join(temp, 'dir1', 'dir2')).createSync(recursive: true); | 218 new Directory(join(temp, 'dir1', 'dir2')).createSync(recursive: true); |
| 221 new File(join(temp, 'dir1', 'file1')).createSync(); | 219 new File(join(temp, 'dir1', 'file1')).createSync(); |
| 222 new File(join(temp, 'dir1', 'dir2', 'file2')).createSync(); | 220 new File(join(temp, 'dir1', 'dir2', 'file2')).createSync(); |
| 223 // Make links whose path and/or target is given by a relative path. | 221 // Make links whose path and/or target is given by a relative path. |
| 224 new Link(join(temp, 'dir1', 'link1_2')).createSync('dir2'); | 222 new Link(join(temp, 'dir1', 'link1_2')).createSync('dir2'); |
| 225 Directory.current = temp; | 223 Directory.current = temp; |
| 226 new Link('link0_2').createSync(join('dir1', 'dir2')); | 224 new Link('link0_2').createSync(join('dir1', 'dir2')); |
| 227 new Link(join('dir1', 'link1_0')).createSync('..'); | 225 new Link(join('dir1', 'link1_0')).createSync('..'); |
| 228 Directory.current = 'dir1'; | 226 Directory.current = 'dir1'; |
| 229 new Link(join('..', 'link0_1')).createSync('dir1'); | 227 new Link(join('..', 'link0_1')).createSync('dir1'); |
| 230 new Link(join('dir2', 'link2_1')).createSync(join(temp, 'dir1')); | 228 new Link(join('dir2', 'link2_1')).createSync(join(temp, 'dir1')); |
| 231 new Link(join(temp, 'dir1', 'dir2', 'link2_0')).createSync(join('..', '..')); | 229 new Link(join(temp, 'dir1', 'dir2', 'link2_0')).createSync(join('..', '..')); |
| 232 // Test that the links go to the right targets. | 230 // Test that the links go to the right targets. |
| 233 checkExists(join('..', 'link0_1', 'file1')); | 231 checkExists(join('..', 'link0_1', 'file1')); |
| 234 checkExists(join('..', 'link0_2', 'file2')); | 232 checkExists(join('..', 'link0_2', 'file2')); |
| 235 checkExists(join('link1_0', 'dir1', 'file1')); | 233 checkExists(join('link1_0', 'dir1', 'file1')); |
| 236 checkExists(join('link1_2', 'file2')); | 234 checkExists(join('link1_2', 'file2')); |
| 237 checkExists(join('dir2', 'link2_0', 'dir1', 'file1')); | 235 checkExists(join('dir2', 'link2_0', 'dir1', 'file1')); |
| 238 checkExists(join('dir2', 'link2_1', 'file1')); | 236 checkExists(join('dir2', 'link2_1', 'file1')); |
| 239 // Clean up | 237 // Clean up |
| 240 Directory.current = oldWorkingDirectory; | 238 Directory.current = oldWorkingDirectory; |
| 241 tempDirectory.deleteSync(recursive: true); | 239 tempDirectory.deleteSync(recursive: true); |
| 242 } | 240 } |
| 243 | 241 |
| 244 main() { | 242 main() { |
| 245 testCreateSync(); | 243 testCreateSync(); |
| 246 testCreateLoopingLink(); | 244 testCreateLoopingLink(); |
| 247 testRenameSync(); | 245 testRenameSync(); |
| 248 testLinkErrorSync(); | 246 testLinkErrorSync(); |
| 249 testRelativeLinksSync(); | 247 testRelativeLinksSync(); |
| 250 } | 248 } |
| OLD | NEW |