| 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 class FutureExpect { | 13 class FutureExpect { |
| 13 static Future isTrue(Future<bool> result) => | 14 static Future isTrue(Future<bool> result) => |
| 14 result.then((value) => Expect.isTrue(value)); | 15 result.then((value) => Expect.isTrue(value)); |
| 15 static Future isFalse(Future<bool> result) => | 16 static Future isFalse(Future<bool> result) => |
| 16 result.then((value) => Expect.isFalse(value)); | 17 result.then((value) => Expect.isFalse(value)); |
| 17 static Future equals(expected, Future result) => | 18 static Future equals(expected, Future result) => |
| 18 result.then((value) => Expect.equals(expected, value)); | 19 result.then((value) => Expect.equals(expected, value)); |
| 19 static Future listEquals(expected, Future result) => | 20 static Future listEquals(expected, Future result) => |
| 20 result.then((value) => Expect.listEquals(expected, value)); | 21 result.then((value) => Expect.listEquals(expected, value)); |
| 21 static Future throws(Future result) => | 22 static Future throws(Future result) => |
| 22 result.then((value) { | 23 result.then((value) { |
| 23 throw new ExpectException( | 24 throw new ExpectException( |
| 24 "FutureExpect.throws received $value instead of an exception"); | 25 "FutureExpect.throws received $value instead of an exception"); |
| 25 }, onError: (_) => null); | 26 }, onError: (_) => null); |
| 26 } | 27 } |
| 27 | 28 |
| 28 | 29 |
| 29 Future testCreate() { | 30 Future testCreate() { |
| 30 return new Directory('').createTemp().then((temp) { | 31 return new Directory('').createTemp().then((baseDir) { |
| 31 Path base = new Path(temp.path); | 32 String base = baseDir.path; |
| 32 Directory baseDir = new Directory.fromPath(base); | 33 String link = join(base, 'link'); |
| 33 String link = base.append('link').toNativePath(); | 34 String target = join(base, 'target'); |
| 34 String target = base.append('target').toNativePath(); | |
| 35 return new Directory(target).create() | 35 return new Directory(target).create() |
| 36 .then((_) => new Link(link).create(target)) | 36 .then((_) => new Link(link).create(target)) |
| 37 | 37 |
| 38 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, | 38 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, |
| 39 FileSystemEntity.type(link))) | 39 FileSystemEntity.type(link))) |
| 40 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, | 40 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, |
| 41 FileSystemEntity.type(target))) | 41 FileSystemEntity.type(target))) |
| 42 .then((_) => FutureExpect.equals(FileSystemEntityType.LINK, | 42 .then((_) => FutureExpect.equals(FileSystemEntityType.LINK, |
| 43 FileSystemEntity.type(link, followLinks: false))) | 43 FileSystemEntity.type(link, followLinks: false))) |
| 44 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, | 44 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, |
| 45 FileSystemEntity.type(target, followLinks: false))) | 45 FileSystemEntity.type(target, followLinks: false))) |
| 46 .then((_) => FutureExpect.isTrue(FileSystemEntity.isLink(link))) | 46 .then((_) => FutureExpect.isTrue(FileSystemEntity.isLink(link))) |
| 47 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(target))) | 47 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(target))) |
| 48 .then((_) => FutureExpect.isTrue(new Directory(link).exists())) | 48 .then((_) => FutureExpect.isTrue(new Directory(link).exists())) |
| 49 .then((_) => FutureExpect.isTrue(new Directory(target).exists())) | 49 .then((_) => FutureExpect.isTrue(new Directory(target).exists())) |
| 50 .then((_) => FutureExpect.isTrue(new Link(link).exists())) | 50 .then((_) => FutureExpect.isTrue(new Link(link).exists())) |
| 51 .then((_) => FutureExpect.isFalse(new Link(target).exists())) | 51 .then((_) => FutureExpect.isFalse(new Link(target).exists())) |
| 52 .then((_) => FutureExpect.equals(target, new Link(link).target())) | 52 .then((_) => FutureExpect.equals(target, new Link(link).target())) |
| 53 .then((_) => FutureExpect.throws(new Link(target).target())) | 53 .then((_) => FutureExpect.throws(new Link(target).target())) |
| 54 .then((_) { | 54 .then((_) { |
| 55 String createdThroughLink = | 55 String createdThroughLink = join(base, 'link', 'createdThroughLink'); |
| 56 base.append('link/createdThroughLink').toNativePath(); | 56 String createdDirectly = join(base, 'target', 'createdDirectly'); |
| 57 String createdDirectly = | 57 String createdFile = join(base, 'link', 'createdFile'); |
| 58 base.append('target/createdDirectly').toNativePath(); | |
| 59 String createdFile = | |
| 60 base.append('target/createdFile').toNativePath(); | |
| 61 return new Directory(createdThroughLink).create() | 58 return new Directory(createdThroughLink).create() |
| 62 .then((_) => new Directory(createdDirectly).create()) | 59 .then((_) => new Directory(createdDirectly).create()) |
| 63 .then((_) => new File(createdFile).create()) | 60 .then((_) => new File(createdFile).create()) |
| 64 .then((_) => FutureExpect.isTrue( | 61 .then((_) => FutureExpect.isTrue( |
| 65 new Directory(createdThroughLink).exists())) | 62 new Directory(createdThroughLink).exists())) |
| 66 .then((_) => FutureExpect.isTrue( | 63 .then((_) => FutureExpect.isTrue( |
| 67 new Directory(createdDirectly).exists())) | 64 new Directory(createdDirectly).exists())) |
| 68 .then((_) => FutureExpect.isTrue( | 65 .then((_) => FutureExpect.isTrue( |
| 69 new Directory.fromPath(base.append('link/createdDirectly')).exists())) | 66 new Directory(join(base, 'link', 'createdDirectly')).exists())) |
| 70 .then((_) => FutureExpect.isTrue(new Directory.fromPath( | 67 .then((_) => FutureExpect.isTrue( |
| 71 base.append('target/createdThroughLink')).exists())) | 68 new Directory(join(base, 'target', 'createdThroughLink')).exists())) |
| 72 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, | 69 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, |
| 73 FileSystemEntity.type(createdThroughLink, followLinks: false))) | 70 FileSystemEntity.type(createdThroughLink, followLinks: false))) |
| 74 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, | 71 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, |
| 75 FileSystemEntity.type(createdDirectly, followLinks: false))) | 72 FileSystemEntity.type(createdDirectly, followLinks: false))) |
| 76 | 73 |
| 77 // Test FileSystemEntity.identical on files, directories, and links, | 74 // Test FileSystemEntity.identical on files, directories, and links, |
| 78 // reached by different paths. | 75 // reached by different paths. |
| 79 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( | 76 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( |
| 80 createdDirectly, | 77 createdDirectly, |
| 81 createdDirectly))) | 78 createdDirectly))) |
| 82 .then((_) => FutureExpect.isFalse(FileSystemEntity.identical( | 79 .then((_) => FutureExpect.isFalse(FileSystemEntity.identical( |
| 83 createdDirectly, | 80 createdDirectly, |
| 84 createdThroughLink))) | 81 createdThroughLink))) |
| 85 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( | 82 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( |
| 86 createdDirectly, | 83 createdDirectly, |
| 87 base.append('link/createdDirectly').toNativePath()))) | 84 join(base, 'link', 'createdDirectly')))) |
| 88 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( | 85 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( |
| 89 createdThroughLink, | 86 createdThroughLink, |
| 90 base.append('target/createdThroughLink').toNativePath()))) | 87 join(base, 'target', 'createdThroughLink')))) |
| 91 .then((_) => FutureExpect.isFalse(FileSystemEntity.identical( | 88 .then((_) => FutureExpect.isFalse(FileSystemEntity.identical( |
| 92 target, | 89 target, |
| 93 link))) | 90 link))) |
| 94 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( | 91 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( |
| 95 link, | 92 link, |
| 96 link))) | 93 link))) |
| 97 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( | 94 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( |
| 98 target, | 95 target, |
| 99 target))) | 96 target))) |
| 100 .then((_) => new Link(link).target()) | 97 .then((_) => new Link(link).target()) |
| 101 .then((linkTarget) => FutureExpect.isTrue(FileSystemEntity.identical( | 98 .then((linkTarget) => FutureExpect.isTrue(FileSystemEntity.identical( |
| 102 target, | 99 target, |
| 103 linkTarget))) | 100 linkTarget))) |
| 104 .then((_) => new File(".").fullPath()) | 101 .then((_) => new File(".").fullPath()) |
| 105 .then((fullCurrentDir) => FutureExpect.isTrue(FileSystemEntity.identical( | 102 .then((fullCurrentDir) => FutureExpect.isTrue(FileSystemEntity.identical( |
| 106 ".", | 103 ".", |
| 107 fullCurrentDir))) | 104 fullCurrentDir))) |
| 108 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( | 105 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( |
| 109 createdFile, | 106 createdFile, |
| 110 createdFile))) | 107 createdFile))) |
| 111 .then((_) => FutureExpect.isFalse(FileSystemEntity.identical( | 108 .then((_) => FutureExpect.isFalse(FileSystemEntity.identical( |
| 112 createdFile, | 109 createdFile, |
| 113 createdDirectly))) | 110 createdDirectly))) |
| 114 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( | 111 .then((_) => FutureExpect.isTrue(FileSystemEntity.identical( |
| 115 createdFile, | 112 createdFile, |
| 116 base.append('link/createdFile').toNativePath()))) | 113 join(base, 'link', 'createdFile')))) |
| 117 .then((_) => FutureExpect.throws(FileSystemEntity.identical( | 114 .then((_) => FutureExpect.throws(FileSystemEntity.identical( |
| 118 createdFile, | 115 createdFile, |
| 119 base.append('link/foo').toNativePath()))) | 116 join(base, 'link', 'does_not_exist')))) |
| 120 | 117 |
| 121 .then((_) => testDirectoryListing(base, baseDir)) | 118 .then((_) => testDirectoryListing(base, baseDir)) |
| 122 .then((_) => new Directory(target).delete(recursive: true)) | 119 .then((_) => new Directory(target).delete(recursive: true)) |
| 123 .then((_) { | 120 .then((_) { |
| 124 List<Future> futures = []; | 121 List<Future> futures = []; |
| 125 for (bool recursive in [true, false]) { | 122 for (bool recursive in [true, false]) { |
| 126 for (bool followLinks in [true, false]) { | 123 for (bool followLinks in [true, false]) { |
| 127 var result = baseDir.listSync(recursive: recursive, | 124 var result = baseDir.listSync(recursive: recursive, |
| 128 followLinks: followLinks); | 125 followLinks: followLinks); |
| 129 Expect.equals(1, result.length); | 126 Expect.equals(1, result.length); |
| 130 Expect.isTrue(result[0] is Link); | 127 Expect.isTrue(result[0] is Link); |
| 131 futures.add(FutureExpect.isTrue( | 128 futures.add(FutureExpect.isTrue( |
| 132 baseDir.list(recursive: recursive, | 129 baseDir.list(recursive: recursive, |
| 133 followLinks: followLinks) | 130 followLinks: followLinks) |
| 134 .single.then((element) => element is Link))); | 131 .single.then((element) => element is Link))); |
| 135 } | 132 } |
| 136 } | 133 } |
| 137 return Future.wait(futures); | 134 return Future.wait(futures); |
| 138 }) | 135 }) |
| 139 .then((_) => baseDir.delete(recursive: true)); | 136 .then((_) => baseDir.delete(recursive: true)); |
| 140 }); | 137 }); |
| 141 }); | 138 }); |
| 142 } | 139 } |
| 143 | 140 |
| 144 | 141 |
| 145 Future testCreateLoopingLink() { | 142 Future testCreateLoopingLink() { |
| 146 return new Directory('').createTemp() | 143 return new Directory('').createTemp() |
| 147 .then((dir) => new Path(dir.path)) | 144 .then((dir) => dir.path) |
| 148 .then((Path base) => | 145 .then((String base) => |
| 149 new Directory.fromPath(base.append('a/b/c')).create(recursive: true) | 146 new Directory(join(base, 'a', 'b', 'c')).create(recursive: true) |
| 150 .then((_) => new Link.fromPath(base.append('a/b/c/d')) | 147 .then((_) => new Link(join(base, 'a', 'b', 'c', 'd')) |
| 151 .create(base.append('a/b').toNativePath())) | 148 .create(join(base, 'a', 'b'))) |
| 152 .then((_) => new Link.fromPath(base.append('a/b/c/e')) | 149 .then((_) => new Link(join(base, 'a', 'b', 'c', 'e')) |
| 153 .create(base.append('a').toNativePath())) | 150 .create(join(base, 'a'))) |
| 154 .then((_) => new Directory.fromPath(base.append('a')) | 151 .then((_) => new Directory(join(base, 'a')) |
| 155 .list(recursive: true, followLinks: false).last) | 152 .list(recursive: true, followLinks: false).last) |
| 156 // This directory listing must terminate, even though it contains loops. | 153 // This directory listing must terminate, even though it contains loops. |
| 157 .then((_) => new Directory.fromPath(base.append('a')) | 154 .then((_) => new Directory(join(base, 'a')) |
| 158 .list(recursive: true, followLinks: true).last) | 155 .list(recursive: true, followLinks: true).last) |
| 159 // This directory listing must terminate, even though it contains loops. | 156 // This directory listing must terminate, even though it contains loops. |
| 160 .then((_) => new Directory.fromPath(base.append('a/b/c')) | 157 .then((_) => new Directory(join(base, 'a', 'b', 'c')) |
| 161 .list(recursive: true, followLinks: true).last) | 158 .list(recursive: true, followLinks: true).last) |
| 162 .then((_) => new Directory.fromPath(base).delete(recursive: true)) | 159 .then((_) => new Directory(base).delete(recursive: true)) |
| 163 ); | 160 .then((_) => FutureExpect.isFalse(new Directory(base).exists())) |
| 161 ); |
| 164 } | 162 } |
| 165 | 163 |
| 166 | 164 |
| 167 Future testRename() { | 165 Future testRename() { |
| 168 Future testRename(Path base, String target) { | 166 Future testRename(String base , String target) { |
| 169 Link link1; | 167 Link link1; |
| 170 Link link2; | 168 Link link2; |
| 171 return new Link.fromPath(base.append('c')).create(target) | 169 return new Link(join(base, 'c')).create(target) |
| 172 .then((link) { | 170 .then((link) { |
| 173 link1 = link; | 171 link1 = link; |
| 174 Expect.isTrue(link1.existsSync()); | 172 Expect.isTrue(link1.existsSync()); |
| 175 return link1.rename(base.append('d').toNativePath()); | 173 return link1.rename(join(base, 'd')); |
| 176 }) | 174 }) |
| 177 .then((link) { | 175 .then((link) { |
| 178 link2 = link; | 176 link2 = link; |
| 179 Expect.isFalse(link1.existsSync()); | 177 Expect.isFalse(link1.existsSync()); |
| 180 Expect.isTrue(link2.existsSync()); | 178 Expect.isTrue(link2.existsSync()); |
| 181 return link2.delete(); | 179 return link2.delete(); |
| 182 }) | 180 }) |
| 183 .then((_) => Expect.isFalse(link2.existsSync())); | 181 .then((_) => Expect.isFalse(link2.existsSync())); |
| 184 } | 182 } |
| 185 | 183 |
| 186 return new Directory('').createTemp().then((baseDir) { | 184 return new Directory('').createTemp().then((baseDir) { |
| 187 Path base = new Path(baseDir.path); | 185 String base = baseDir.path; |
| 188 var targetsFutures = []; | 186 var targetsFutures = []; |
| 189 targetsFutures.add(new Directory.fromPath(base.append('a')).create()); | 187 targetsFutures.add(new Directory(join(base, 'a')).create()); |
| 190 if (Platform.isWindows) { | 188 if (Platform.isWindows) { |
| 191 // Currently only links to directories are supported on Windows. | 189 // Currently only links to directories are supported on Windows. |
| 192 targetsFutures.add( | 190 targetsFutures.add( |
| 193 new Directory.fromPath(base.append('b')).create()); | 191 new Directory(join(base, 'b')).create()); |
| 194 } else { | 192 } else { |
| 195 targetsFutures.add(new File.fromPath(base.append('b')).create()); | 193 targetsFutures.add(new File(join(base, 'b')).create()); |
| 196 } | 194 } |
| 197 return Future.wait(targetsFutures).then((targets) { | 195 return Future.wait(targetsFutures).then((targets) { |
| 198 return testRename(base, targets[0].path) | 196 return testRename(base, targets[0].path) |
| 199 .then((_) => testRename(base, targets[1].path)) | 197 .then((_) => testRename(base, targets[1].path)) |
| 200 .then((_) => baseDir.delete(recursive: true)); | 198 .then((_) => baseDir.delete(recursive: true)); |
| 201 }); | 199 }); |
| 202 }); | 200 }); |
| 203 } | 201 } |
| 204 | 202 |
| 205 Future testDirectoryListing(Path base, Directory baseDir) { | 203 Future testDirectoryListing(String base, Directory baseDir) { |
| 206 Map makeExpected(bool recursive, bool followLinks) { | 204 Map makeExpected(bool recursive, bool followLinks) { |
| 207 Map expected = new Map(); | 205 Map expected = new Map(); |
| 208 expected['target'] = 'Directory'; | 206 expected['target'] = 'Directory'; |
| 209 expected['link'] = followLinks ? 'Directory' : 'Link'; | 207 expected['link'] = followLinks ? 'Directory' : 'Link'; |
| 210 if (recursive) { | 208 if (recursive) { |
| 211 expected['target/createdDirectly'] = 'Directory'; | 209 expected[join('target', 'createdDirectly')] = 'Directory'; |
| 212 expected['target/createdThroughLink'] = 'Directory'; | 210 expected[join('target', 'createdThroughLink')] = 'Directory'; |
| 213 expected['target/createdFile'] = 'File'; | 211 expected[join('target', 'createdFile')] = 'File'; |
| 214 if (followLinks) { | 212 if (followLinks) { |
| 215 expected['link/createdDirectly'] = 'Directory'; | 213 expected[join('link', 'createdDirectly')] = 'Directory'; |
| 216 expected['link/createdThroughLink'] = 'Directory'; | 214 expected[join('link', 'createdThroughLink')] = 'Directory'; |
| 217 expected['link/createdFile'] = 'File'; | 215 expected[join('link', 'createdFile')] = 'File'; |
| 218 } | 216 } |
| 219 } | 217 } |
| 220 return expected; | 218 return expected; |
| 221 } | 219 } |
| 222 | 220 |
| 223 void checkEntity(FileSystemEntity x, Map expected) { | 221 void checkEntity(FileSystemEntity x, Map expected) { |
| 224 String ending = new Path(x.path).relativeTo(base).toString(); | 222 String ending = relative(x.path, from: base); |
| 225 Expect.isNotNull(expected[ending]); | 223 Expect.isNotNull(expected[ending]); |
| 226 Expect.isTrue(x.toString().startsWith(expected[ending])); | 224 Expect.isTrue(x.toString().startsWith(expected[ending])); |
| 227 expected[ending] = 'Found'; | 225 expected[ending] = 'Found'; |
| 228 } | 226 } |
| 229 | 227 |
| 230 List futures = []; | 228 List futures = []; |
| 231 for (bool recursive in [true, false]) { | 229 for (bool recursive in [true, false]) { |
| 232 for (bool followLinks in [true, false]) { | 230 for (bool followLinks in [true, false]) { |
| 233 Map expected = makeExpected(recursive, followLinks); | 231 Map expected = makeExpected(recursive, followLinks); |
| 234 for (var x in baseDir.listSync(recursive: recursive, | 232 for (var x in baseDir.listSync(recursive: recursive, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 249 }) | 247 }) |
| 250 ); | 248 ); |
| 251 } | 249 } |
| 252 } | 250 } |
| 253 return Future.wait(futures); | 251 return Future.wait(futures); |
| 254 } | 252 } |
| 255 | 253 |
| 256 main() { | 254 main() { |
| 257 ReceivePort keepAlive = new ReceivePort(); | 255 ReceivePort keepAlive = new ReceivePort(); |
| 258 testCreate() | 256 testCreate() |
| 259 .then((_) => testCreateLoopingLink()) | 257 .then((_) => testCreateLoopingLink()) |
| 260 .then((_) => testRename()) | 258 .then((_) => testRename()) |
| 261 .then((_) => keepAlive.close()); | 259 .then((_) => keepAlive.close()); |
| 262 } | 260 } |
| OLD | NEW |