Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function walk (dir, options, callback) {
if (!enoent) {
if (options.ignoreDotFiles && path.basename(f)[0] === '.') return done && callback(null, callback.files);
if (options.filter && !options.filter(f, stat)) return done && callback(null, callback.files);
if (options.ignoreDirectoryPattern && options.ignoreDirectoryPattern.test(f)) return done && callback(null, callback.files);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think stat.isDirectory() is necessary, otherwise not only the directories but also the files match to ignoreDirectoryPattern is also ignored.

Copy link

@kena0ki kena0ki Apr 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: I also tried to fix this issue. Here is my patch.

callback.files[f] = stat;
if (stat.isDirectory() && !(options.ignoreDirectoryPattern && options.ignoreDirectoryPattern.test(f))) walk(f, options, callback);
done = callback.pending === 0;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"scripts": {
"release:major": "bash scripts/release.sh major",
"release:minor": "bash scripts/release.sh minor",
"release:patch": "bash scripts/release.sh patch"
"release:patch": "bash scripts/release.sh patch",
"test": "node node_modules/jasmine/bin/jasmine.js test/*.test.js"
},
"keywords": [
"util",
Expand Down Expand Up @@ -35,6 +36,7 @@
"main": "./main",
"dependencies": {
"exec-sh": "^0.2.0",
"jasmine": "^2.5.2",
"minimist": "^1.2.0"
}
}
96 changes: 96 additions & 0 deletions test/options.ignoreDirectoryPattern.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
var watch = require('../main.js');
var fs = require('fs');

var filesCallCount;

var mockFileTree = {
'root': true,
'root/test1.js': false,
'root/test2': true,
'root/test3.js': false,
'root/test2/test4.js': false
};

//Mock Class: fs.Stats (https://nodejs.org/api/fs.html#fs_class_fs_stats)
var Stat = function(dir) {
this.isDirectory = function() {
if (typeof mockFileTree[dir] === 'undefined') {
throw ('Tests error: description not found: ' + dir);
}
return mockFileTree[dir];
}
}

//Mock fs.stat (https://nodejs.org/api/fs.html#fs_fs_stat_path_callback)
fs.stat = function(dir, callback) {
setTimeout(function() {
callback(null, new Stat(dir));
}, 1);
}

//Mock fs.readdir (https://nodejs.org/api/fs.html#fs_fs_readdir_path_options_callback)
fs.readdir = function(dir, callback) {
var files = [];

if (dir === 'root') {
files = [
'test1.js',
'test2',
'test3.js'
];
}

if (dir === 'root/test2') {
files = ['test4.js'];
}

callback(null, files);
}

//Modify fs.watchFile to calculate files calls for test checks
fs.watchFile = function(file) {
if (!filesCallCount[file]) {
filesCallCount[file] = 1;
}
else {
filesCallCount[file]++;
}
};

fs.unwatchFile = function() {}

describe("watchTree options: ignoreDirectoryPattern", function() {

it("ignoreDirectoryPattern empty", function(done) {
filesCallCount = {};

watch.watchTree('root', function() {});

setTimeout(function() {
//expect(filesCallCount['root']).toBe(1); //Why 2 ???
expect(filesCallCount['root/test2/test4.js']).toBe(1);
expect(filesCallCount['root/test2']).toBe(1);
expect(filesCallCount['root/test1.js']).toBe(1);
expect(filesCallCount['root/test3.js']).toBe(1);
done();
}, 200);

});

it("ignoreDirectoryPattern set", function(done) {
filesCallCount = {};

watch.watchTree('root', { ignoreDirectoryPattern: new RegExp('root/test2') }, function() {});

setTimeout(function() {
//expect(filesCallCount['root']).toBe(1); //Why 2 ???
expect(filesCallCount['root/test2/test4.js']).toBe(undefined);
expect(filesCallCount['root/test2']).toBe(undefined); //Error was here
expect(filesCallCount['root/test1.js']).toBe(1);
expect(filesCallCount['root/test3.js']).toBe(1);
done();
}, 200);

});

});