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 "dart:async"; |
| 6 import "package:expect/expect.dart"; |
| 7 import "dart:io"; |
| 8 import "dart:isolate"; |
| 9 |
| 10 |
| 11 class FutureExpect { |
| 12 static Future isTrue(Future<bool> result) => |
| 13 result.then((value) => Expect.isTrue(value)); |
| 14 static Future isFalse(Future<bool> result) => |
| 15 result.then((value) => Expect.isFalse(value)); |
| 16 static Future equals(expected, Future result) => |
| 17 result.then((value) => Expect.equals(expected, value)); |
| 18 static Future listEquals(expected, Future result) => |
| 19 result.then((value) => Expect.listEquals(expected, value)); |
| 20 static Future throws(Future result) => |
| 21 result.then((value) { |
| 22 throw new ExpectException( |
| 23 "FutureExpect.throws received $value instead of an exception"); |
| 24 }, onError: (_) => null); |
| 25 } |
| 26 |
| 27 |
| 28 Future testFileExistsCreate() { |
| 29 return new Directory('').createTemp().then((temp) { |
| 30 var x = '${temp.path}${Platform.pathSeparator}x'; |
| 31 var y = '${temp.path}${Platform.pathSeparator}y'; |
| 32 return new Link(y).create(x) |
| 33 .then((link) => Expect.equals(y, link.path)) |
| 34 .then((_) => FutureExpect.isFalse(new File(y).exists())) |
| 35 .then((_) => FutureExpect.isFalse(new File(x).exists())) |
| 36 .then((_) => FutureExpect.isTrue(FileSystemEntity.isLink(y))) |
| 37 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(x))) |
| 38 .then((_) => FutureExpect.equals(FileSystemEntityType.NOT_FOUND, |
| 39 FileSystemEntity.type(y))) |
| 40 .then((_) => FutureExpect.equals(FileSystemEntityType.NOT_FOUND, |
| 41 FileSystemEntity.type(x))) |
| 42 .then((_) => FutureExpect.equals(FileSystemEntityType.LINK, |
| 43 FileSystemEntity.type(y, |
| 44 followLinks: false))) |
| 45 .then((_) => FutureExpect.equals(FileSystemEntityType.NOT_FOUND, |
| 46 FileSystemEntity.type(x, |
| 47 followLinks: false))) |
| 48 .then((_) => FutureExpect.equals(x, new Link(y).target())) |
| 49 .then((_) => new File(y).create()) |
| 50 .then((yFile) => Expect.equals(y, yFile.path)) |
| 51 .then((_) => FutureExpect.isTrue(new File(y).exists())) |
| 52 .then((_) => FutureExpect.isTrue(new File(x).exists())) |
| 53 .then((_) => FutureExpect.isTrue(FileSystemEntity.isLink(y))) |
| 54 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(x))) |
| 55 .then((_) => FutureExpect.isTrue(FileSystemEntity.isFile(y))) |
| 56 .then((_) => FutureExpect.isTrue(FileSystemEntity.isFile(x))) |
| 57 .then((_) => FutureExpect.equals(FileSystemEntityType.FILE, |
| 58 FileSystemEntity.type(y))) |
| 59 .then((_) => FutureExpect.equals(FileSystemEntityType.FILE, |
| 60 FileSystemEntity.type(x))) |
| 61 .then((_) => FutureExpect.equals(FileSystemEntityType.LINK, |
| 62 FileSystemEntity.type(y, |
| 63 followLinks: false))) |
| 64 .then((_) => FutureExpect.equals(FileSystemEntityType.FILE, |
| 65 FileSystemEntity.type(x, |
| 66 followLinks: false))) |
| 67 .then((_) => FutureExpect.equals(x, new Link(y).target())) |
| 68 .then((_) => new File(x).delete()) |
| 69 .then((xDeletedFile) => Expect.equals(x, xDeletedFile.path)) |
| 70 .then((_) => new Directory(x).create()) |
| 71 .then((xCreatedDirectory) => Expect.equals(x, xCreatedDirectory.path)) |
| 72 .then((_) => FutureExpect.isTrue(FileSystemEntity.isLink(y))) |
| 73 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(x))) |
| 74 .then((_) => FutureExpect.isTrue(FileSystemEntity.isDirectory(y))) |
| 75 .then((_) => FutureExpect.isTrue(FileSystemEntity.isDirectory(x))) |
| 76 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, |
| 77 FileSystemEntity.type(y))) |
| 78 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, |
| 79 FileSystemEntity.type(x))) |
| 80 .then((_) => FutureExpect.equals(FileSystemEntityType.LINK, |
| 81 FileSystemEntity.type(y, |
| 82 followLinks: false))) |
| 83 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, |
| 84 FileSystemEntity.type(x, |
| 85 followLinks: false))) |
| 86 .then((_) => FutureExpect.equals(x, new Link(y).target())) |
| 87 .then((_) => new Link(y).delete()) |
| 88 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(y))) |
| 89 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(x))) |
| 90 .then((_) => FutureExpect.equals(FileSystemEntityType.NOT_FOUND, |
| 91 FileSystemEntity.type(y))) |
| 92 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, |
| 93 FileSystemEntity.type(x))) |
| 94 .then((_) => FutureExpect.throws(new Link(y).target())) |
| 95 .then((_) => temp.delete(recursive: true)); |
| 96 }); |
| 97 } |
| 98 |
| 99 |
| 100 Future testFileDelete() { |
| 101 return new Directory('').createTemp().then((temp) { |
| 102 var x = '${temp.path}${Platform.pathSeparator}x'; |
| 103 var y = '${temp.path}${Platform.pathSeparator}y'; |
| 104 return new File(x).create() |
| 105 .then((_) => new Link(y).create(x)) |
| 106 .then((_) => FutureExpect.isTrue(new File(x).exists())) |
| 107 .then((_) => FutureExpect.isTrue(new File(y).exists())) |
| 108 .then((_) => new File(y).delete()) |
| 109 .then((_) => FutureExpect.isTrue(new File(x).exists())) |
| 110 .then((_) => FutureExpect.isFalse(new File(y).exists())) |
| 111 .then((_) => new Link(y).create(x)) |
| 112 .then((_) => FutureExpect.isTrue(new File(x).exists())) |
| 113 .then((_) => FutureExpect.isTrue(new File(y).exists())) |
| 114 .then((_) => new File(y).delete()) |
| 115 .then((_) => FutureExpect.isTrue(new File(x).exists())) |
| 116 .then((_) => FutureExpect.isFalse(new File(y).exists())) |
| 117 .then((_) => temp.delete(recursive: true)); |
| 118 }); |
| 119 } |
| 120 |
| 121 |
| 122 Future testFileWriteRead() { |
| 123 return new Directory('').createTemp().then((temp) { |
| 124 var x = '${temp.path}${Platform.pathSeparator}x'; |
| 125 var y = '${temp.path}${Platform.pathSeparator}y'; |
| 126 var data = "asdf".codeUnits; |
| 127 return new File(x).create() |
| 128 .then((_) => new Link(y).create(x)) |
| 129 .then((_) => |
| 130 (new File(y).openWrite(mode: FileMode.WRITE)..add(data)).close()) |
| 131 .then((_) => FutureExpect.listEquals(data, new File(y).readAsBytes())) |
| 132 .then((_) => FutureExpect.listEquals(data, new File(x).readAsBytes())) |
| 133 .then((_) => temp.delete(recursive: true)); |
| 134 }); |
| 135 } |
| 136 |
| 137 |
| 138 Future testDirectoryExistsCreate() { |
| 139 return new Directory('').createTemp().then((temp) { |
| 140 var x = '${temp.path}${Platform.pathSeparator}x'; |
| 141 var y = '${temp.path}${Platform.pathSeparator}y'; |
| 142 return new Link(y).create(x) |
| 143 .then((_) => FutureExpect.isFalse(new Directory(x).exists())) |
| 144 .then((_) => FutureExpect.isFalse(new Directory(y).exists())) |
| 145 .then((_) => FutureExpect.throws(new Directory(y).create())) |
| 146 .then((_) => temp.delete(recursive: true)); |
| 147 }); |
| 148 } |
| 149 |
| 150 |
| 151 Future testDirectoryDelete() { |
| 152 return new Directory('').createTemp().then((temp) => |
| 153 new Directory('').createTemp().then((temp2) { |
| 154 var y = '${temp.path}${Platform.pathSeparator}y'; |
| 155 var x = '${temp2.path}${Platform.pathSeparator}x'; |
| 156 var link = new Directory(y); |
| 157 return new File(x).create() |
| 158 .then((_) => new Link(y).create(temp2.path)) |
| 159 .then((_) => FutureExpect.isTrue(link.exists())) |
| 160 .then((_) => FutureExpect.isTrue(temp2.exists())) |
| 161 .then((_) => link.delete()) |
| 162 .then((_) => FutureExpect.isFalse(link.exists())) |
| 163 .then((_) => FutureExpect.isTrue(temp2.exists())) |
| 164 .then((_) => new Link(y).create(temp2.path)) |
| 165 .then((_) => FutureExpect.isTrue(link.exists())) |
| 166 .then((_) => temp.delete(recursive: true)) |
| 167 .then((_) => FutureExpect.isFalse(link.exists())) |
| 168 .then((_) => FutureExpect.isFalse(temp.exists())) |
| 169 .then((_) => FutureExpect.isTrue(temp2.exists())) |
| 170 .then((_) => FutureExpect.isTrue(new File(x).exists())) |
| 171 .then((_) => temp2.delete(recursive: true)); |
| 172 })); |
| 173 } |
| 174 |
| 175 |
| 176 Future testDirectoryListing() { |
| 177 return new Directory('').createTemp().then((temp) => |
| 178 new Directory('').createTemp().then((temp2) { |
| 179 var sep = Platform.pathSeparator; |
| 180 var y = '${temp.path}${sep}y'; |
| 181 var x = '${temp2.path}${sep}x'; |
| 182 return new File(x).create() |
| 183 .then((_) => new Link(y).create(temp2.path)) |
| 184 .then((_) => temp.list(recursive: true).singleWhere( |
| 185 (entry) => entry is File)) |
| 186 .then((file) => Expect.isTrue(file.path.endsWith('$y${sep}x'))) |
| 187 .then((_) => temp.list(recursive: true).singleWhere( |
| 188 (entry) => entry is Directory)) |
| 189 .then((dir) => Expect.isTrue(dir.path.endsWith('y'))) |
| 190 .then((_) => temp.delete(recursive: true)) |
| 191 .then((_) => temp2.delete(recursive: true)); |
| 192 })); |
| 193 } |
| 194 |
| 195 |
| 196 Future testDirectoryListingBrokenLink() { |
| 197 return new Directory('').createTemp().then((temp) { |
| 198 var x = '${temp.path}${Platform.pathSeparator}x'; |
| 199 var link = '${temp.path}${Platform.pathSeparator}link'; |
| 200 var doesNotExist = 'this_thing_does_not_exist'; |
| 201 bool sawFile = false; |
| 202 bool sawLink = false; |
| 203 return new File(x).create() |
| 204 .then((_) => new Link(link).create(doesNotExist)) |
| 205 .then((_) => temp.list(recursive: true).forEach( |
| 206 (entity) { |
| 207 if (entity is File) { |
| 208 Expect.isFalse(sawFile); |
| 209 sawFile = true; |
| 210 Expect.isTrue(entity.path.endsWith(x)); |
| 211 } else { |
| 212 Expect.isTrue(entity is Link); |
| 213 Expect.isFalse(sawLink); |
| 214 sawLink = true; |
| 215 Expect.isTrue(entity.path.endsWith(link)); |
| 216 } |
| 217 return true; |
| 218 })) |
| 219 .then((_) => temp.delete(recursive: true)); |
| 220 }); |
| 221 } |
| 222 |
| 223 |
| 224 main() { |
| 225 // Links on Windows are tested by windows_file_system_[async_]links_test. |
| 226 if (Platform.operatingSystem != 'windows') { |
| 227 ReceivePort keepAlive = new ReceivePort(); |
| 228 testFileExistsCreate() |
| 229 .then((_) => testFileDelete()) |
| 230 .then((_) => testFileWriteRead()) |
| 231 .then((_) => testDirectoryExistsCreate()) |
| 232 .then((_) => testDirectoryDelete()) |
| 233 .then((_) => testDirectoryListing()) |
| 234 .then((_) => testDirectoryListingBrokenLink()) |
| 235 .then((_) => keepAlive.close()); |
| 236 } |
| 237 } |
OLD | NEW |