| 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:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 import "package:path/path.dart"; |
| 6 import "dart:async"; | 7 import "dart:async"; |
| 7 import "dart:io"; | 8 import "dart:io"; |
| 8 import "dart:isolate"; | 9 import "dart:isolate"; |
| 9 | 10 |
| 10 // Test the dart:io Link class. | 11 // Test the dart:io Link class. |
| 11 | 12 |
| 12 testCreateSync() { | 13 testCreateSync() { |
| 13 Path base = new Path(new Directory('').createTempSync().path); | 14 String base = new Directory('').createTempSync().path; |
| 14 String link = base.append('link').toNativePath(); | 15 String link = join(base, 'link'); |
| 15 String target = base.append('target').toNativePath(); | 16 String target = join(base, 'target'); |
| 16 new Directory(target).createSync(); | 17 new Directory(target).createSync(); |
| 17 new Link(link).createSync(target); | 18 new Link(link).createSync(target); |
| 18 | 19 |
| 19 Expect.equals(FileSystemEntityType.DIRECTORY, | 20 Expect.equals(FileSystemEntityType.DIRECTORY, |
| 20 FileSystemEntity.typeSync(link)); | 21 FileSystemEntity.typeSync(link)); |
| 21 Expect.equals(FileSystemEntityType.DIRECTORY, | 22 Expect.equals(FileSystemEntityType.DIRECTORY, |
| 22 FileSystemEntity.typeSync(target)); | 23 FileSystemEntity.typeSync(target)); |
| 23 Expect.equals(FileSystemEntityType.LINK, | 24 Expect.equals(FileSystemEntityType.LINK, |
| 24 FileSystemEntity.typeSync(link, followLinks: false)); | 25 FileSystemEntity.typeSync(link, followLinks: false)); |
| 25 Expect.equals(FileSystemEntityType.DIRECTORY, | 26 Expect.equals(FileSystemEntityType.DIRECTORY, |
| 26 FileSystemEntity.typeSync(target, followLinks: false)); | 27 FileSystemEntity.typeSync(target, followLinks: false)); |
| 27 Expect.isTrue(FileSystemEntity.isLinkSync(link)); | 28 Expect.isTrue(FileSystemEntity.isLinkSync(link)); |
| 28 Expect.isFalse(FileSystemEntity.isLinkSync(target)); | 29 Expect.isFalse(FileSystemEntity.isLinkSync(target)); |
| 29 Expect.isTrue(new Directory(link).existsSync()); | 30 Expect.isTrue(new Directory(link).existsSync()); |
| 30 Expect.isTrue(new Directory(target).existsSync()); | 31 Expect.isTrue(new Directory(target).existsSync()); |
| 31 Expect.isTrue(new Link(link).existsSync()); | 32 Expect.isTrue(new Link(link).existsSync()); |
| 32 Expect.isFalse(new Link(target).existsSync()); | 33 Expect.isFalse(new Link(target).existsSync()); |
| 33 Expect.equals(target, new Link(link).targetSync()); | 34 Expect.equals(target, new Link(link).targetSync()); |
| 34 Expect.throws(() => new Link(target).targetSync()); | 35 Expect.throws(() => new Link(target).targetSync()); |
| 35 | 36 |
| 36 String createdThroughLink = | 37 String createdThroughLink = join(base, 'link', 'createdThroughLink'); |
| 37 base.append('link/createdThroughLink').toNativePath(); | 38 String createdDirectly = join(base, 'target', 'createdDirectly'); |
| 38 String createdDirectly = base.append('target/createdDirectly').toNativePath(); | |
| 39 new Directory(createdThroughLink).createSync(); | 39 new Directory(createdThroughLink).createSync(); |
| 40 new Directory(createdDirectly).createSync(); | 40 new Directory(createdDirectly).createSync(); |
| 41 Expect.isTrue(new Directory(createdThroughLink).existsSync()); | 41 Expect.isTrue(new Directory(createdThroughLink).existsSync()); |
| 42 Expect.isTrue(new Directory(createdDirectly).existsSync()); | 42 Expect.isTrue(new Directory(createdDirectly).existsSync()); |
| 43 Expect.isTrue(new Directory.fromPath(base.append('link/createdDirectly')) | 43 Expect.isTrue(new Directory(join(base, 'link', 'createdDirectly')) |
| 44 .existsSync()); | 44 .existsSync()); |
| 45 Expect.isTrue(new Directory.fromPath(base.append('target/createdThroughLink')) | 45 Expect.isTrue(new Directory(join(base, 'target', 'createdThroughLink')) |
| 46 .existsSync()); | 46 .existsSync()); |
| 47 Expect.equals(FileSystemEntityType.DIRECTORY, | 47 Expect.equals(FileSystemEntityType.DIRECTORY, |
| 48 FileSystemEntity.typeSync(createdThroughLink, | 48 FileSystemEntity.typeSync(createdThroughLink, |
| 49 followLinks: false)); | 49 followLinks: false)); |
| 50 Expect.equals(FileSystemEntityType.DIRECTORY, | 50 Expect.equals(FileSystemEntityType.DIRECTORY, |
| 51 FileSystemEntity.typeSync(createdDirectly, followLinks: false)); | 51 FileSystemEntity.typeSync(createdDirectly, followLinks: false)); |
| 52 | 52 |
| 53 // Test FileSystemEntity.identical on files, directories, and links, | 53 // Test FileSystemEntity.identical on files, directories, and links, |
| 54 // reached by different paths. | 54 // reached by different paths. |
| 55 Expect.isTrue(FileSystemEntity.identicalSync(createdDirectly, | 55 Expect.isTrue(FileSystemEntity.identicalSync(createdDirectly, |
| 56 createdDirectly)); | 56 createdDirectly)); |
| 57 Expect.isFalse(FileSystemEntity.identicalSync(createdDirectly, | 57 Expect.isFalse(FileSystemEntity.identicalSync(createdDirectly, |
| 58 createdThroughLink)); | 58 createdThroughLink)); |
| 59 Expect.isTrue(FileSystemEntity.identicalSync(createdDirectly, | 59 Expect.isTrue(FileSystemEntity.identicalSync(createdDirectly, |
| 60 base.append('link/createdDirectly').toNativePath())); | 60 join(base, 'link', 'createdDirectly'))); |
| 61 Expect.isTrue(FileSystemEntity.identicalSync(createdThroughLink, | 61 Expect.isTrue(FileSystemEntity.identicalSync(createdThroughLink, |
| 62 base.append('target/createdThroughLink').toNativePath())); | 62 join(base, 'target', 'createdThroughLink'))); |
| 63 | 63 |
| 64 Expect.isFalse(FileSystemEntity.identicalSync(target, link)); | 64 Expect.isFalse(FileSystemEntity.identicalSync(target, link)); |
| 65 Expect.isTrue(FileSystemEntity.identicalSync(link, link)); | 65 Expect.isTrue(FileSystemEntity.identicalSync(link, link)); |
| 66 Expect.isTrue(FileSystemEntity.identicalSync(target, target)); | 66 Expect.isTrue(FileSystemEntity.identicalSync(target, target)); |
| 67 Expect.isTrue(FileSystemEntity.identicalSync(target, | 67 Expect.isTrue(FileSystemEntity.identicalSync(target, |
| 68 new Link(link).targetSync())); | 68 new Link(link).targetSync())); |
| 69 String absolutePath = new File(".").fullPathSync(); | 69 String absolutePath = new File(".").fullPathSync(); |
| 70 Expect.isTrue(FileSystemEntity.identicalSync(".", absolutePath)); | 70 Expect.isTrue(FileSystemEntity.identicalSync(".", absolutePath)); |
| 71 | 71 |
| 72 String createdFile = base.append('target/createdFile').toNativePath(); | 72 String createdFile = join(base, 'target', 'createdFile'); |
| 73 new File(createdFile).createSync(); | 73 new File(createdFile).createSync(); |
| 74 Expect.isTrue(FileSystemEntity.identicalSync(createdFile, createdFile)); | 74 Expect.isTrue(FileSystemEntity.identicalSync(createdFile, createdFile)); |
| 75 Expect.isFalse(FileSystemEntity.identicalSync(createdFile, createdDirectly)); | 75 Expect.isFalse(FileSystemEntity.identicalSync(createdFile, createdDirectly)); |
| 76 Expect.isTrue(FileSystemEntity.identicalSync(createdFile, | 76 Expect.isTrue(FileSystemEntity.identicalSync(createdFile, |
| 77 base.append('link/createdFile').toNativePath())); | 77 join(base, 'link', 'createdFile'))); |
| 78 Expect.throws(() => FileSystemEntity.identicalSync(createdFile, | 78 Expect.throws(() => FileSystemEntity.identicalSync(createdFile, |
| 79 base.append('link/foo').toNativePath())); | 79 join(base, 'link', 'does_not_exist'))); |
| 80 | 80 |
| 81 var baseDir = new Directory.fromPath(base); | 81 var baseDir = new Directory(base); |
| 82 | 82 |
| 83 Map makeExpected(bool recursive, bool followLinks) { | 83 Map makeExpected(bool recursive, bool followLinks) { |
| 84 Map expected = new Map(); | 84 Map expected = new Map(); |
| 85 expected['target'] = 'Directory'; | 85 expected['target'] = 'Directory'; |
| 86 expected['link'] = followLinks ? 'Directory' : 'Link'; | 86 expected['link'] = followLinks ? 'Directory' : 'Link'; |
| 87 if (recursive) { | 87 if (recursive) { |
| 88 expected['target/createdDirectly'] = 'Directory'; | 88 expected[join('target', 'createdDirectly')] = 'Directory'; |
| 89 expected['target/createdThroughLink'] = 'Directory'; | 89 expected[join('target', 'createdThroughLink')] = 'Directory'; |
| 90 expected['target/createdFile'] = 'File'; | 90 expected[join('target', 'createdFile')] = 'File'; |
| 91 if (followLinks) { | 91 if (followLinks) { |
| 92 expected['link/createdDirectly'] = 'Directory'; | 92 expected[join('link', 'createdDirectly')] = 'Directory'; |
| 93 expected['link/createdThroughLink'] = 'Directory'; | 93 expected[join('link', 'createdThroughLink')] = 'Directory'; |
| 94 expected['link/createdFile'] = 'File'; | 94 expected[join('link', 'createdFile')] = 'File'; |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 return expected; | 97 return expected; |
| 98 } | 98 } |
| 99 | 99 |
| 100 void checkEntity(FileSystemEntity x, Map expected) { | 100 void checkEntity(FileSystemEntity x, Map expected) { |
| 101 String ending = new Path(x.path).relativeTo(base).toString(); | 101 String ending = relative(x.path, from:base); |
| 102 Expect.isNotNull(expected[ending]); | 102 Expect.isNotNull(expected[ending]); |
| 103 Expect.isTrue(x.toString().startsWith(expected[ending])); | 103 Expect.isTrue(x.toString().startsWith(expected[ending])); |
| 104 expected[ending] = 'Found'; | 104 expected[ending] = 'Found'; |
| 105 } | 105 } |
| 106 | 106 |
| 107 List futures = []; | 107 List futures = []; |
| 108 for (bool recursive in [true, false]) { | 108 for (bool recursive in [true, false]) { |
| 109 for (bool followLinks in [true, false]) { | 109 for (bool followLinks in [true, false]) { |
| 110 Map expected = makeExpected(recursive, followLinks); | 110 Map expected = makeExpected(recursive, followLinks); |
| 111 for (var x in baseDir.listSync(recursive: recursive, | 111 for (var x in baseDir.listSync(recursive: recursive, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 140 followLinks: followLinks); | 140 followLinks: followLinks); |
| 141 Expect.equals(1, result.length); | 141 Expect.equals(1, result.length); |
| 142 Expect.isTrue(result[0] is Link); | 142 Expect.isTrue(result[0] is Link); |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 baseDir.deleteSync(recursive: true); | 145 baseDir.deleteSync(recursive: true); |
| 146 }); | 146 }); |
| 147 } | 147 } |
| 148 | 148 |
| 149 testCreateLoopingLink() { | 149 testCreateLoopingLink() { |
| 150 Path base = new Path(new Directory('').createTempSync().path); | 150 String base = new Directory('').createTempSync().path; |
| 151 new Directory.fromPath(base.append('a/b/c')).create(recursive: true) | 151 new Directory(join(base, 'a', 'b', 'c')).create(recursive: true) |
| 152 .then((_) => | 152 .then((_) => |
| 153 new Link.fromPath(base.append('a/b/c/d')) | 153 new Link(join(base, 'a', 'b', 'c', 'd')).create(join(base, 'a', 'b'))) |
| 154 .create(base.append('a/b').toNativePath())) | 154 .then((_) => |
| 155 .then((_) => | 155 new Link(join(base, 'a', 'b', 'c', 'e')).create(join(base, 'a'))) |
| 156 new Link.fromPath(base.append('a/b/c/e')) | 156 .then((_) => |
| 157 .create(base.append('a').toNativePath())) | 157 new Directory(join(base, 'a')) |
| 158 .then((_) => | |
| 159 new Directory.fromPath(base.append('a')) | |
| 160 .list(recursive: true, followLinks: false) | 158 .list(recursive: true, followLinks: false) |
| 161 .last) | 159 .last) |
| 162 .then((_) => | 160 .then((_) => |
| 163 // This directory listing must terminate, even though it contains loops. | 161 // This directory listing must terminate, even though it contains loops. |
| 164 new Directory.fromPath(base.append('a')) | 162 new Directory(join(base, 'a')) |
| 165 .list(recursive: true, followLinks: true) | 163 .list(recursive: true, followLinks: true) |
| 166 .last) | 164 .last) |
| 167 .then((_) => | 165 .then((_) => |
| 168 // This directory listing must terminate, even though it contains loops. | 166 // This directory listing must terminate, even though it contains loops. |
| 169 new Directory.fromPath(base.append('a/b/c')) | 167 new Directory(join(base, 'a', 'b', 'c')) |
| 170 .list(recursive: true, followLinks: true) | 168 .list(recursive: true, followLinks: true) |
| 171 .last) | 169 .last) |
| 172 .then((_) => | 170 .then((_) => |
| 173 new Directory.fromPath(base).delete(recursive: true)); | 171 new Directory(base).delete(recursive: true)); |
| 174 } | 172 } |
| 175 | 173 |
| 176 testRenameSync() { | 174 testRenameSync() { |
| 177 testRename(Path base, String target) { | 175 testRename(String base, String target) { |
| 178 Link link1 = new Link.fromPath(base.append('c'))..createSync(target); | 176 Link link1 = new Link(join(base, 'c'))..createSync(target); |
| 179 Expect.isTrue(link1.existsSync()); | 177 Expect.isTrue(link1.existsSync()); |
| 180 Link link2 = link1.renameSync(base.append('d').toNativePath()); | 178 Link link2 = link1.renameSync(join(base, 'd')); |
| 181 Expect.isFalse(link1.existsSync()); | 179 Expect.isFalse(link1.existsSync()); |
| 182 Expect.isTrue(link2.existsSync()); | 180 Expect.isTrue(link2.existsSync()); |
| 183 link2.deleteSync(); | 181 link2.deleteSync(); |
| 184 Expect.isFalse(link2.existsSync()); | 182 Expect.isFalse(link2.existsSync()); |
| 185 } | 183 } |
| 186 | 184 |
| 187 Directory baseDir = new Directory('').createTempSync(); | 185 Directory baseDir = new Directory('').createTempSync(); |
| 188 Path base = new Path(baseDir.path); | 186 String base = baseDir.path; |
| 189 Directory dir = new Directory.fromPath(base.append('a'))..createSync(); | 187 Directory dir = new Directory(join(base, 'a'))..createSync(); |
| 190 File file = new File.fromPath(base.append('b'))..createSync(); | 188 File file = new File(join(base, 'b'))..createSync(); |
| 191 | 189 |
| 192 testRename(base, file.path); | 190 testRename(base, file.path); |
| 193 testRename(base, dir.path); | 191 testRename(base, dir.path); |
| 194 | 192 |
| 195 baseDir.deleteSync(recursive: true); | 193 baseDir.deleteSync(recursive: true); |
| 196 } | 194 } |
| 197 | 195 |
| 198 void testLinkErrorSync() { | 196 void testLinkErrorSync() { |
| 199 Expect.throws(() => | 197 Expect.throws(() => |
| 200 new Link('some-dir-that-doent exist/some link file/bla/fisk').createSync( | 198 new Link('some-dir-that-doent exist/some link file/bla/fisk').createSync( |
| 201 'bla bla bla/b lalal/blfir/sdfred/es'), | 199 'bla bla bla/b lalal/blfir/sdfred/es'), |
| 202 (e) => e is LinkException); | 200 (e) => e is LinkException); |
| 203 } | 201 } |
| 204 | 202 |
| 205 main() { | 203 main() { |
| 206 testCreateSync(); | 204 testCreateSync(); |
| 207 testCreateLoopingLink(); | 205 testCreateLoopingLink(); |
| 208 testRenameSync(); | 206 testRenameSync(); |
| 209 testLinkErrorSync(); | 207 testLinkErrorSync(); |
| 210 } | 208 } |
| OLD | NEW |