Skip to content

Commit

Permalink
Avoid running the same file multiple times (#856)
Browse files Browse the repository at this point in the history
* avoid running the same file multiple times

* refactor `alreadySearchingParent` to make it a little easier to understand
  • Loading branch information
jamestalmage authored and sindresorhus committed May 23, 2016
1 parent 178175f commit 3763109
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/ava-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,33 @@ function handlePaths(files, excludePatterns) {
// convert pinkie-promise to Bluebird promise
files = Promise.resolve(globby(files.concat(excludePatterns)));

var searchedParents = Object.create(null);
var foundFiles = Object.create(null);

function alreadySearchingParent(dir) {
if (searchedParents[dir]) {
return true;
}

var parentDir = path.dirname(dir);

if (parentDir === dir) {
// We have reached the root path.
return false;
}

return alreadySearchingParent(parentDir);
}

return files
.map(function (file) {
if (fs.statSync(file).isDirectory()) {
if (alreadySearchingParent(file)) {
return null;
}

searchedParents[file] = true;

var pattern = path.join(file, '**', '*.js');

if (process.platform === 'win32') {
Expand All @@ -216,10 +240,15 @@ function handlePaths(files, excludePatterns) {
})
.then(flatten)
.filter(function (file) {
return path.extname(file) === '.js' && path.basename(file)[0] !== '_';
return file && path.extname(file) === '.js' && path.basename(file)[0] !== '_';
})
.map(function (file) {
return path.resolve(file);
})
.filter(function (file) {
var alreadyFound = foundFiles[file];
foundFiles[file] = true;
return !alreadyFound;
});
}

Expand Down
9 changes: 9 additions & 0 deletions test/ava-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,12 @@ test('sourceMatcher - providing negation patterns', function (t) {
t.false(matcher('foo/bar.js'));
t.end();
});

test('findFiles - does not return duplicates of the same file', function (t) {
var avaFiles = new AvaFiles(['**/ava-files/no-duplicates/**']);

avaFiles.findTestFiles().then(function (files) {
t.is(files.length, 2);
t.end();
});
});
1 change: 1 addition & 0 deletions test/fixture/ava-files/no-duplicates/lib/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty
1 change: 1 addition & 0 deletions test/fixture/ava-files/no-duplicates/lib/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty

0 comments on commit 3763109

Please sign in to comment.