Browse files

Should correctly match single directories when using trailing /. Closes

  • Loading branch information...
1 parent e5b14b1 commit 67b6a8bb7ce3018f9c0b4bd7bc3ba2cc10f160df @cowboy committed Jul 20, 2012
Showing with 21 additions and 9 deletions.
  1. +12 −6 lib/glob.js
  2. +9 −3 test/glob_test.js
View
18 lib/glob.js
@@ -52,10 +52,8 @@ exports.glob = function(pattern, options) {
maxDepth = options.maxDepth || 99;
absPattern = pattern;
}
- // Iterate over all sub-files and directories in a brute-force and not-at-all
- // efficient way.
- var filepaths = [];
- (function recurse(dirpath, depth) {
+ // Build file list, recursively (only up to a certain depth).
+ var recurse = function(dirpath, depth) {
// Abort if, for whatever reason, dirpath doesn't exist.
if (!existsSync(dirpath)) { return; }
// Read directory contents.
@@ -81,8 +79,16 @@ exports.glob = function(pattern, options) {
filepaths.push(filepath);
}
});
- }(betterBase, 1));
-
+ };
+ // Build files list.
+ var filepaths = [];
+ if (betterPattern) {
+ // A pattern exists, recurse starting at betterBase.
+ recurse(betterBase, 1);
+ } else {
+ // No pattern exists, just use betterBase path.
+ filepaths.push(betterBase);
+ }
// Normalize filepaths and remove those that don't match pattern.
filepaths = filepaths.map(normalize).filter(function(filepath) {
return exports.minimatch(filepath, absPattern, options);
View
12 test/glob_test.js
@@ -41,8 +41,10 @@ function relFileList(arr) {
exports['globsync'] = {
'relative': function(test) {
- test.expect(11);
+ test.expect(13);
test.deepEqual(globsync.glob('fixture/*'), fileList([0,1]), 'test/fixture/* should match');
+ test.deepEqual(globsync.glob('fixture/foo'), fileList([1]), 'test/fixture/foo should match');
+ test.deepEqual(globsync.glob('fixture/foo/'), fileList([1]), 'test/fixture/foo/ should match');
test.deepEqual(globsync.glob('fixture/*/*'), fileList([2,6]), 'test/fixture/* should match');
test.deepEqual(globsync.glob('fixture/**'), fileList([0,1,2,3,4,5,6]), 'test/fixture/** should match');
test.deepEqual(globsync.glob('fixture/**/'), fileList([1,2,3]), 'test/fixture/**/ should match');
@@ -56,9 +58,11 @@ exports['globsync'] = {
test.done();
},
'absolute': function(test) {
- test.expect(9);
+ test.expect(11);
var prefix = path.resolve(process.cwd());
test.deepEqual(globsync.glob(makeAbsolute('fixture/*')), fileList([0,1], prefix), makeAbsolute('test/fixture/*') + ' should match');
+ test.deepEqual(globsync.glob(makeAbsolute('fixture/foo')), fileList([1], prefix), makeAbsolute('test/fixture/foo') + ' should match');
+ test.deepEqual(globsync.glob(makeAbsolute('fixture/foo/')), fileList([1], prefix), makeAbsolute('test/fixture/foo/') + ' should match');
test.deepEqual(globsync.glob(makeAbsolute('fixture/*/*')), fileList([2,6], prefix), makeAbsolute('test/fixture/*/*') + ' should match');
test.deepEqual(globsync.glob(makeAbsolute('fixture/**')), fileList([0,1,2,3,4,5,6], prefix), makeAbsolute('test/fixture/**') + ' should match');
test.deepEqual(globsync.glob(makeAbsolute('fixture/**/')), fileList([1,2,3], prefix), makeAbsolute('test/fixture/**/') + ' should match');
@@ -70,10 +74,12 @@ exports['globsync'] = {
test.done();
},
'wacky': function(test) {
- test.expect(9);
+ test.expect(11);
process.chdir('../lib');
var prefix = '../test/';
test.deepEqual(globsync.glob('../test/fixture/*'), fileList([0,1], prefix), '../test/fixture/* should match');
+ test.deepEqual(globsync.glob('../test/fixture/foo'), fileList([1], prefix), '../test/fixture/foo should match');
+ test.deepEqual(globsync.glob('../test/fixture/foo/'), fileList([1], prefix), '../test/fixture/foo/ should match');
test.deepEqual(globsync.glob('../test/fixture/*/*'), fileList([2,6], prefix), '../test/fixture/*/* should match');
test.deepEqual(globsync.glob('../test/fixture/**'), fileList([0,1,2,3,4,5,6], prefix), '../test/fixture/** should match');
test.deepEqual(globsync.glob('../test/fixture/**/'), fileList([1,2,3], prefix), '../test/fixture/**/ should match');

0 comments on commit 67b6a8b

Please sign in to comment.