OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 'dart:io'; |
| 7 |
| 8 import 'package:glob/glob.dart'; |
| 9 import 'package:glob/src/utils.dart'; |
| 10 import 'package:path/path.dart' as p; |
| 11 import 'package:scheduled_test/descriptor.dart' as d; |
| 12 import 'package:scheduled_test/scheduled_test.dart'; |
| 13 |
| 14 String sandbox; |
| 15 |
| 16 void main() { |
| 17 setUp(() { |
| 18 scheduleSandbox(); |
| 19 |
| 20 d.dir("foo", [ |
| 21 d.file("bar"), |
| 22 d.dir("baz", [ |
| 23 d.file("bang"), |
| 24 d.file("qux") |
| 25 ]) |
| 26 ]).create(); |
| 27 }); |
| 28 |
| 29 group("list()", () { |
| 30 test("fails if the context doesn't match the system context", () { |
| 31 expect(new Glob("*", context: p.url).list, throwsStateError); |
| 32 }); |
| 33 |
| 34 test("reports exceptions for non-existent directories", () { |
| 35 schedule(() { |
| 36 expect(new Glob("non/existent/**").list().toList(), |
| 37 throwsA(new isInstanceOf<FileSystemException>())); |
| 38 }); |
| 39 }); |
| 40 }); |
| 41 |
| 42 group("listSync()", () { |
| 43 test("fails if the context doesn't match the system context", () { |
| 44 expect(new Glob("*", context: p.url).listSync, throwsStateError); |
| 45 }); |
| 46 |
| 47 test("reports exceptions for non-existent directories", () { |
| 48 schedule(() { |
| 49 expect(new Glob("non/existent/**").listSync, |
| 50 throwsA(new isInstanceOf<FileSystemException>())); |
| 51 }); |
| 52 }); |
| 53 }); |
| 54 |
| 55 syncAndAsync((list) { |
| 56 group("literals", () { |
| 57 test("lists a single literal", () { |
| 58 expect(list("foo/baz/qux"), |
| 59 completion(equals([p.join("foo", "baz", "qux")]))); |
| 60 }); |
| 61 |
| 62 test("lists a non-matching literal", () { |
| 63 expect(list("foo/baz/nothing"), completion(isEmpty)); |
| 64 }); |
| 65 }); |
| 66 |
| 67 group("star", () { |
| 68 test("lists within filenames but not across directories", () { |
| 69 expect(list("foo/b*"), completion(unorderedEquals([ |
| 70 p.join("foo", "bar"), |
| 71 p.join("foo", "baz") |
| 72 ]))); |
| 73 }); |
| 74 |
| 75 test("lists the empy string", () { |
| 76 expect(list("foo/bar*"), completion(equals([p.join("foo", "bar")]))); |
| 77 }); |
| 78 }); |
| 79 |
| 80 group("double star", () { |
| 81 test("lists within filenames", () { |
| 82 expect(list("foo/baz/**"), completion(unorderedEquals([ |
| 83 p.join("foo", "baz", "qux"), |
| 84 p.join("foo", "baz", "bang") |
| 85 ]))); |
| 86 }); |
| 87 |
| 88 test("lists the empty string", () { |
| 89 expect(list("foo/bar**"), completion(equals([p.join("foo", "bar")]))); |
| 90 }); |
| 91 |
| 92 test("lists recursively", () { |
| 93 expect(list("foo/**"), completion(unorderedEquals([ |
| 94 p.join("foo", "bar"), |
| 95 p.join("foo", "baz"), |
| 96 p.join("foo", "baz", "qux"), |
| 97 p.join("foo", "baz", "bang") |
| 98 ]))); |
| 99 }); |
| 100 |
| 101 test("combines with literals", () { |
| 102 expect(list("foo/ba**"), completion(unorderedEquals([ |
| 103 p.join("foo", "bar"), |
| 104 p.join("foo", "baz"), |
| 105 p.join("foo", "baz", "qux"), |
| 106 p.join("foo", "baz", "bang") |
| 107 ]))); |
| 108 }); |
| 109 |
| 110 test("lists recursively in the middle of a glob", () { |
| 111 d.dir("deep", [ |
| 112 d.dir("a", [ |
| 113 d.dir("b", [ |
| 114 d.dir("c", [ |
| 115 d.file("d"), |
| 116 d.file("long-file") |
| 117 ]), |
| 118 d.dir("long-dir", [d.file("x")]) |
| 119 ]) |
| 120 ]) |
| 121 ]).create(); |
| 122 |
| 123 expect(list("deep/**/?/?"), completion(unorderedEquals([ |
| 124 p.join("deep", "a", "b", "c"), |
| 125 p.join("deep", "a", "b", "c", "d") |
| 126 ]))); |
| 127 }); |
| 128 }); |
| 129 |
| 130 group("any char", () { |
| 131 test("matches a character", () { |
| 132 expect(list("foo/ba?"), completion(unorderedEquals([ |
| 133 p.join("foo", "bar"), |
| 134 p.join("foo", "baz") |
| 135 ]))); |
| 136 }); |
| 137 |
| 138 test("doesn't match a separator", () { |
| 139 expect(list("foo?bar"), completion(isEmpty)); |
| 140 }); |
| 141 }); |
| 142 |
| 143 group("range", () { |
| 144 test("matches a range of characters", () { |
| 145 expect(list("foo/ba[a-z]"), completion(unorderedEquals([ |
| 146 p.join("foo", "bar"), |
| 147 p.join("foo", "baz") |
| 148 ]))); |
| 149 }); |
| 150 |
| 151 test("matches a specific list of characters", () { |
| 152 expect(list("foo/ba[rz]"), completion(unorderedEquals([ |
| 153 p.join("foo", "bar"), |
| 154 p.join("foo", "baz") |
| 155 ]))); |
| 156 }); |
| 157 |
| 158 test("doesn't match outside its range", () { |
| 159 expect(list("foo/ba[a-x]"), |
| 160 completion(unorderedEquals([p.join("foo", "bar")]))); |
| 161 }); |
| 162 |
| 163 test("doesn't match outside its specific list", () { |
| 164 expect(list("foo/ba[rx]"), |
| 165 completion(unorderedEquals([p.join("foo", "bar")]))); |
| 166 }); |
| 167 }); |
| 168 |
| 169 test("the same file shouldn't be non-recursively listed multiple times", |
| 170 () { |
| 171 d.dir("multi", [ |
| 172 d.dir("start-end", [d.file("file")]) |
| 173 ]).create(); |
| 174 |
| 175 expect(list("multi/{start-*/f*,*-end/*e}"), |
| 176 completion(equals([p.join("multi", "start-end", "file")]))); |
| 177 }); |
| 178 |
| 179 test("the same file shouldn't be recursively listed multiple times", () { |
| 180 d.dir("multi", [ |
| 181 d.dir("a", [ |
| 182 d.dir("b", [ |
| 183 d.file("file"), |
| 184 d.dir("c", [ |
| 185 d.file("file") |
| 186 ]) |
| 187 ]), |
| 188 d.dir("x", [ |
| 189 d.dir("y", [ |
| 190 d.file("file") |
| 191 ]) |
| 192 ]) |
| 193 ]) |
| 194 ]).create(); |
| 195 |
| 196 expect(list("multi/{*/*/*/file,a/**/file}"), completion(unorderedEquals([ |
| 197 p.join("multi", "a", "b", "file"), |
| 198 p.join("multi", "a", "b", "c", "file"), |
| 199 p.join("multi", "a", "x", "y", "file") |
| 200 ]))); |
| 201 }); |
| 202 |
| 203 group("with symlinks", () { |
| 204 setUp(() { |
| 205 schedule(() { |
| 206 return new Link(p.join(sandbox, "dir", "link")) |
| 207 .create(p.join(sandbox, "foo", "baz"), recursive: true); |
| 208 }, "symlink foo/baz to dir/link"); |
| 209 }); |
| 210 |
| 211 test("follows symlinks by default", () { |
| 212 expect(list("dir/**"), completion(unorderedEquals([ |
| 213 p.join("dir", "link"), |
| 214 p.join("dir", "link", "bang"), |
| 215 p.join("dir", "link", "qux") |
| 216 ]))); |
| 217 }); |
| 218 |
| 219 test("doesn't follow symlinks with followLinks: false", () { |
| 220 expect(list("dir/**", followLinks: false), |
| 221 completion(equals([p.join("dir", "link")]))); |
| 222 }); |
| 223 |
| 224 test("shouldn't crash on broken symlinks", () { |
| 225 schedule(() { |
| 226 return new Directory(p.join(sandbox, "foo")).delete(recursive: true); |
| 227 }); |
| 228 |
| 229 expect(list("dir/**"), completion(equals([p.join("dir", "link")]))); |
| 230 }); |
| 231 }); |
| 232 |
| 233 test("always lists recursively with recursive: true", () { |
| 234 expect(list("foo", recursive: true), completion(unorderedEquals([ |
| 235 "foo", |
| 236 p.join("foo", "bar"), |
| 237 p.join("foo", "baz"), |
| 238 p.join("foo", "baz", "qux"), |
| 239 p.join("foo", "baz", "bang") |
| 240 ]))); |
| 241 }); |
| 242 |
| 243 test("lists an absolute glob", () { |
| 244 expect(schedule(() { |
| 245 var pattern = separatorToForwardSlash( |
| 246 p.absolute(p.join(sandbox, 'foo/baz/**'))); |
| 247 |
| 248 return list(pattern); |
| 249 }), completion(unorderedEquals([ |
| 250 p.join("foo", "baz", "bang"), |
| 251 p.join("foo", "baz", "qux") |
| 252 ]))); |
| 253 }); |
| 254 |
| 255 test("lists a subdirectory that sometimes exists", () { |
| 256 d.dir("top", [ |
| 257 d.dir("dir1", [ |
| 258 d.dir("subdir", [d.file("file")]) |
| 259 ]), |
| 260 d.dir("dir2", []) |
| 261 ]).create(); |
| 262 |
| 263 expect(list("top/*/subdir/**"), |
| 264 completion(equals([p.join("top", "dir1", "subdir", "file")]))); |
| 265 }); |
| 266 }); |
| 267 } |
| 268 |
| 269 typedef Future<List<String>> ListFn(String glob, |
| 270 {bool recursive, bool followLinks}); |
| 271 |
| 272 /// Runs [callback] in two groups with two values of [listFn]: one that uses |
| 273 /// [Glob.list], one that uses [Glob.listSync]. |
| 274 void syncAndAsync(callback(ListFn listFn)) { |
| 275 group("async", () { |
| 276 callback((glob, {recursive: false, followLinks: true}) { |
| 277 return schedule(() { |
| 278 return new Glob(glob, recursive: recursive) |
| 279 .list(root: sandbox, followLinks: followLinks) |
| 280 .map((entity) => p.relative(entity.path, from: sandbox)) |
| 281 .toList(); |
| 282 }, 'listing $glob'); |
| 283 }); |
| 284 }); |
| 285 |
| 286 group("sync", () { |
| 287 callback((glob, {recursive: false, followLinks: true}) { |
| 288 return schedule(() { |
| 289 return new Glob(glob, recursive: recursive) |
| 290 .listSync(root: sandbox, followLinks: followLinks) |
| 291 .map((entity) => p.relative(entity.path, from: sandbox)) |
| 292 .toList(); |
| 293 }, 'listing $glob'); |
| 294 }); |
| 295 }); |
| 296 } |
| 297 |
| 298 void scheduleSandbox() { |
| 299 schedule(() { |
| 300 return Directory.systemTemp.createTemp('glob_').then((dir) { |
| 301 sandbox = dir.path; |
| 302 d.defaultRoot = sandbox; |
| 303 }); |
| 304 }, 'creating sandbox'); |
| 305 |
| 306 currentSchedule.onComplete.schedule(() { |
| 307 d.defaultRoot = null; |
| 308 if (sandbox == null) return null; |
| 309 var oldSandbox = sandbox; |
| 310 sandbox = null; |
| 311 return new Directory(oldSandbox).delete(recursive: true); |
| 312 }); |
| 313 } |
OLD | NEW |