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