Skip to content

Commit 36e2c24

Browse files
committed
watcher: correctly test if source is in the cwd
Make sure the path starts with ../ (after platform-specific slashes have been corrected). Clarify existing test and add a case where the source starts with two dots but is still in the current working directory.
1 parent 21bdcc5 commit 36e2c24

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/watcher.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,15 @@ function makeSourceMatcher(sources) {
320320
}
321321

322322
return function (path) {
323+
path = matchable(path);
324+
323325
// Ignore paths outside the current working directory. They can't be matched
324326
// to a pattern.
325-
if (/^\.\./.test(path)) {
327+
if (/^\.\.\//.test(path)) {
326328
return false;
327329
}
328330

329-
return multimatch(matchable(path), patterns).length === 1;
331+
return multimatch(path, patterns).length === 1;
330332
};
331333
}
332334

test/watcher.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -859,16 +859,31 @@ group('chokidar is installed', function (beforeEach, test, group) {
859859
});
860860

861861
test('ignores dependencies outside of the current working directory', function (t) {
862-
t.plan(2);
863-
seed();
862+
t.plan(4);
863+
seed(['**/*.js', '..foo.js']);
864864

865865
emitDependencies(path.join('test', '1.js'), [path.resolve('../outside.js')]);
866+
emitDependencies(path.join('test', '2.js'), [path.resolve('..foo.js')]);
866867
// Pretend Chokidar detected a change to verify (normally Chokidar would
867868
// also be ignoring this file but hey).
868869
change(path.join('..', 'outside.js'));
870+
871+
api.run.returns(Promise.resolve());
869872
return debounce().then(function () {
870873
t.ok(api.run.calledTwice);
874+
// If ../outside.js was tracked as a dependency of test/1.js this would
875+
// have caused test/1.js to be rerun. Instead expect all tests to be
876+
// rerun. This is somewhat artifical: normally changes to ../outside.js
877+
// wouldn't even be picked up. However this lets us test dependency
878+
// tracking without directly inspecting the internal state of the
879+
// watcher.
871880
t.same(api.run.secondCall.args, [files, {runOnlyExclusive: false}]);
881+
882+
change('..foo.js');
883+
return debounce();
884+
}).then(function () {
885+
t.ok(api.run.calledThrice);
886+
t.same(api.run.thirdCall.args, [[path.join('test', '2.js')], {runOnlyExclusive: false}]);
872887
});
873888
});
874889

0 commit comments

Comments
 (0)