Fix case-insensitive absolute listing. This goes to show that we shouldn't avoid using path functions in the name of efficiency. We were using a raw substring to get a relative path, and it failed for absolute roots because they didn't have an extra "/" added to the end. Closes dart-lang/glob#4 R=rnystrom@google.com Review URL: https://codereview.chromium.org//1773293002 .
diff --git a/pkgs/glob/CHANGELOG.md b/pkgs/glob/CHANGELOG.md index eec6f31..7563251 100644 --- a/pkgs/glob/CHANGELOG.md +++ b/pkgs/glob/CHANGELOG.md
@@ -1,3 +1,7 @@ +## 1.1.1 + +* Fix a bug where listing an absolute glob with `caseInsensitive: false` failed. + ## 1.1.0 * Add a `caseSensitive` named parameter to `new Glob()` that controls whether
diff --git a/pkgs/glob/lib/src/list_tree.dart b/pkgs/glob/lib/src/list_tree.dart index eff8c7b..57ed067 100644 --- a/pkgs/glob/lib/src/list_tree.dart +++ b/pkgs/glob/lib/src/list_tree.dart
@@ -314,7 +314,7 @@ Stream<FileSystemEntity> list(String dir, {bool followLinks: true}) { if (isRecursive) { return new Directory(dir).list(recursive: true, followLinks: followLinks) - .where((entity) => _matches(entity.path.substring(dir.length + 1))); + .where((entity) => _matches(p.relative(entity.path, from: dir))); } var resultPool = new StreamPool(); @@ -333,7 +333,7 @@ var resultController = new StreamController(sync: true); resultPool.add(resultController.stream); new Directory(dir).list(followLinks: followLinks).listen((entity) { - var basename = entity.path.substring(dir.length + 1); + var basename = p.relative(entity.path, from: dir); if (_matches(basename)) resultController.add(entity); children.forEach((sequence, child) { @@ -368,7 +368,7 @@ if (isRecursive) { return new Directory(dir) .listSync(recursive: true, followLinks: followLinks) - .where((entity) => _matches(entity.path.substring(dir.length + 1))); + .where((entity) => _matches(p.relative(entity.path, from: dir))); } // Don't spawn extra [Directory.listSync] calls when we already know exactly @@ -383,7 +383,7 @@ return new Directory(dir).listSync(followLinks: followLinks) .expand((entity) { var entities = []; - var basename = entity.path.substring(dir.length + 1); + var basename = p.relative(entity.path, from: dir); if (_matches(basename)) entities.add(entity); if (entity is! Directory) return entities;
diff --git a/pkgs/glob/test/glob_test.dart b/pkgs/glob/test/glob_test.dart index f82106a..4cbb453 100644 --- a/pkgs/glob/test/glob_test.dart +++ b/pkgs/glob/test/glob_test.dart
@@ -3,6 +3,7 @@ // BSD-style license that can be found in the LICENSE file. import 'package:glob/glob.dart'; +import 'package:path/path.dart' as p; import 'package:test/test.dart'; void main() {
diff --git a/pkgs/glob/test/list_test.dart b/pkgs/glob/test/list_test.dart index 948af08..d59e273 100644 --- a/pkgs/glob/test/list_test.dart +++ b/pkgs/glob/test/list_test.dart
@@ -275,6 +275,19 @@ ]))); }); + // Regression test for #4. + test("lists an absolute case-insensitive glob", () { + expect(schedule(() { + var pattern = separatorToForwardSlash( + p.absolute(p.join(sandbox, 'foo/Baz/**'))); + + return list(pattern, caseSensitive: false); + }), completion(unorderedEquals([ + p.join("foo", "baz", "bang"), + p.join("foo", "baz", "qux") + ]))); + }); + test("lists a subdirectory that sometimes exists", () { d.dir("top", [ d.dir("dir1", [