Skip to content

Commit a92f089

Browse files
committed
Attempt to fix glob tests
1 parent cde757a commit a92f089

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var isGlob = require('is-glob');
99
var isAbsolute = require('path-is-absolute');
1010
var inherits = require('inherits');
1111
var braces = require('braces');
12+
var normalizePath = require('normalize-path')
1213

1314
var NodeFsHandler = require('./lib/nodefs-handler');
1415
var FsEventsHandler = require('./lib/fsevents-handler');
@@ -586,6 +587,7 @@ FSWatcher.prototype._closePath = function(path) {
586587

587588
// Returns an instance of FSWatcher for chaining.
588589
FSWatcher.prototype.add = function(paths, _origAdd, _internal) {
590+
var disableGlobbing = this.options.disableGlobbing;
589591
var cwd = this.options.cwd;
590592
this.closed = false;
591593
paths = flatten(arrify(paths));
@@ -595,12 +597,20 @@ FSWatcher.prototype.add = function(paths, _origAdd, _internal) {
595597
}
596598

597599
if (cwd) paths = paths.map(function(path) {
600+
var absPath;
598601
if (isAbsolute(path)) {
599-
return path;
602+
absPath = path;
600603
} else if (path[0] === '!') {
601-
return '!' + sysPath.join(cwd, path.substring(1));
604+
absPath = '!' + sysPath.join(cwd, path.substring(1));
602605
} else {
603-
return sysPath.join(cwd, path);
606+
absPath = sysPath.join(cwd, path);
607+
}
608+
609+
// Check `path` instead of `absPath` because the cwd portion can't be a glob
610+
if (disableGlobbing || !isGlob(path)) {
611+
return absPath;
612+
} else {
613+
return normalizePath(absPath);
604614
}
605615
});
606616

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"inherits": "^2.0.1",
5252
"is-binary-path": "^1.0.0",
5353
"is-glob": "^4.0.0",
54+
"normalize-path": "^2.1.1",
5455
"path-is-absolute": "^1.0.0",
5556
"readdirp": "^2.0.0"
5657
}

test.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ function getFixturePath (subPath) {
2121
subPath
2222
);
2323
}
24+
function getGlobPath (subPath) {
25+
return upath.join(
26+
__dirname,
27+
'test-fixtures',
28+
subdir && subdir.toString() || '',
29+
subPath
30+
);
31+
}
2432

2533
var watcher,
2634
watcher2,
@@ -516,10 +524,10 @@ function runTests(baseopts) {
516524
before(closeWatchers);
517525
it('should correctly watch and emit based on glob input', function(done) {
518526
var spy = sinon.spy();
519-
var testPath = getFixturePath('*a*.txt');
527+
var watchPath = getGlobPath('*a*.txt');
520528
var addPath = getFixturePath('add.txt');
521529
var changePath = getFixturePath('change.txt');
522-
watcher = chokidar.watch(testPath, options)
530+
watcher = chokidar.watch(watchPath, options)
523531
.on('all', spy)
524532
.on('ready', function() {
525533
spy.should.have.been.calledWith('add', changePath);
@@ -538,10 +546,10 @@ function runTests(baseopts) {
538546
});
539547
it('should respect negated glob patterns', function(done) {
540548
var spy = sinon.spy();
541-
var testPath = getFixturePath('*');
542-
var negatedPath = '!' + getFixturePath('*a*.txt');
549+
var watchPath = getGlobPath('*');
550+
var negatedWatchPath = '!' + getGlobPath('*a*.txt');
543551
var unlinkPath = getFixturePath('unlink.txt');
544-
watcher = chokidar.watch([testPath, negatedPath], options)
552+
watcher = chokidar.watch([watchPath, negatedWatchPath], options)
545553
.on('all', spy)
546554
.on('ready', function() {
547555
spy.should.have.been.calledOnce;
@@ -556,7 +564,7 @@ function runTests(baseopts) {
556564
});
557565
it('should traverse subdirs to match globstar patterns', function(done) {
558566
var spy = sinon.spy();
559-
var watchPath = getFixturePath('../../test-*/' + subdir + '/**/a*.txt');
567+
var watchPath = getGlobPath('../../test-*/' + subdir + '/**/a*.txt');
560568
fs.mkdirSync(getFixturePath('subdir'), 0x1ed);
561569
fs.mkdirSync(getFixturePath('subdir/subsub'), 0x1ed);
562570
fs.writeFileSync(getFixturePath('subdir/a.txt'), 'b');
@@ -587,12 +595,12 @@ function runTests(baseopts) {
587595
});
588596
it('should resolve relative paths with glob patterns', function(done) {
589597
var spy = sinon.spy();
590-
var testPath = 'test-*/' + subdir + '/*a*.txt';
598+
var watchPath = 'test-*/' + subdir + '/*a*.txt';
591599
// getFixturePath() returns absolute paths, so use sysPath.join() instead
592600
var addPath = sysPath.join('test-fixtures', subdir.toString(), 'add.txt');
593601
var changePath = sysPath.join('test-fixtures', subdir.toString(), 'change.txt');
594602
var unlinkPath = sysPath.join('test-fixtures', subdir.toString(), 'unlink.txt');
595-
watcher = chokidar.watch(testPath, options)
603+
watcher = chokidar.watch(watchPath, options)
596604
.on('all', spy)
597605
.on('ready', function() {
598606
spy.should.have.been.calledWith('add', changePath);
@@ -615,7 +623,7 @@ function runTests(baseopts) {
615623
var changePath = getFixturePath('change.txt');
616624
var unlinkPath = getFixturePath('unlink.txt');
617625
var addPath = getFixturePath('add.txt');
618-
var watchPaths = [getFixturePath('change*'), getFixturePath('unlink*')];
626+
var watchPaths = [getGlobPath('change*'), getGlobPath('unlink*')];
619627
watcher = chokidar.watch(watchPaths, options)
620628
.on('all', spy)
621629
.on('ready', function() {
@@ -639,7 +647,7 @@ function runTests(baseopts) {
639647
it('should correctly handle intersecting glob patterns', function(done) {
640648
var spy = sinon.spy();
641649
var changePath = getFixturePath('change.txt');
642-
var watchPaths = [getFixturePath('cha*'), getFixturePath('*nge.*')];
650+
var watchPaths = [getGlobPath('cha*'), getGlobPath('*nge.*')];
643651
watcher = chokidar.watch(watchPaths, options)
644652
.on('all', spy)
645653
.on('ready', function() {
@@ -674,10 +682,11 @@ function runTests(baseopts) {
674682
var spy = sinon.spy();
675683
var filePath = getFixturePath('nota[glob]/a.txt');
676684
var watchPath = getFixturePath('nota[glob]');
685+
var testDir = getFixturePath('nota[glob]');
677686
var matchingDir = getFixturePath('notag');
678687
var matchingFile = getFixturePath('notag/b.txt');
679688
var matchingFile2 = getFixturePath('notal');
680-
fs.mkdirSync(watchPath, 0x1ed);
689+
fs.mkdirSync(testDir, 0x1ed);
681690
fs.writeFileSync(filePath, 'b');
682691
fs.mkdirSync(matchingDir, 0x1ed);
683692
fs.writeFileSync(matchingFile, 'c');
@@ -700,6 +709,7 @@ function runTests(baseopts) {
700709
options.disableGlobbing = true;
701710
var spy = sinon.spy();
702711
var filePath = getFixturePath('nota[glob]');
712+
// This isn't using getGlobPath because it isn't treated as a glob
703713
var watchPath = getFixturePath('nota[glob]');
704714
var matchingDir = getFixturePath('notag');
705715
var matchingFile = getFixturePath('notag/a.txt');
@@ -725,7 +735,7 @@ function runTests(baseopts) {
725735
it('should not prematurely filter dirs against complex globstar patterns', function(done) {
726736
var spy = sinon.spy();
727737
var deepFile = getFixturePath('subdir/subsub/subsubsub/a.txt');
728-
var watchPath = getFixturePath('../../test-*/' + subdir + '/**/subsubsub/*.txt');
738+
var watchPath = getGlobPath('../../test-*/' + subdir + '/**/subsubsub/*.txt');
729739
fs.mkdirSync(getFixturePath('subdir'), 0x1ed);
730740
fs.mkdirSync(getFixturePath('subdir/subsub'), 0x1ed);
731741
fs.mkdirSync(getFixturePath('subdir/subsub/subsubsub'), 0x1ed);
@@ -744,7 +754,7 @@ function runTests(baseopts) {
744754
it('should emit matching dir events', function(done) {
745755
var spy = sinon.spy();
746756
// test with and without globstar matches
747-
var watchPaths = [getFixturePath('*'), getFixturePath('subdir/subsub/**/*')];
757+
var watchPaths = [getGlobPath('*'), getGlobPath('subdir/subsub/**/*')];
748758
var deepDir = getFixturePath('subdir/subsub/subsubsub');
749759
var deepFile = sysPath.join(deepDir, 'a.txt');
750760
fs.mkdirSync(getFixturePath('subdir'), 0x1ed);
@@ -770,7 +780,7 @@ function runTests(baseopts) {
770780
});
771781
it('should correctly handle glob with braces', function(done) {
772782
var spy = sinon.spy();
773-
var watchPath = upath.normalizeSafe(getFixturePath('{subdir/*,subdir1/subsub1}/subsubsub/*.txt'));
783+
var watchPath = upath.normalizeSafe(getGlobPath('{subdir/*,subdir1/subsub1}/subsubsub/*.txt'));
774784
var deepFileA = getFixturePath('subdir/subsub/subsubsub/a.txt');
775785
var deepFileB = getFixturePath('subdir1/subsub1/subsubsub/a.txt');
776786
fs.mkdirSync(getFixturePath('subdir'), 0x1ed);
@@ -966,8 +976,8 @@ function runTests(baseopts) {
966976
var dirSpy = sinon.spy(function dirSpy(){});
967977
var addSpy = sinon.spy(function addSpy(){});
968978
// test with relative path to ensure proper resolution
969-
var watchDir = sysPath.relative(process.cwd(), linkedDir);
970-
watcher = chokidar.watch(sysPath.join(watchDir, '**/*'), options)
979+
var watchDir = upath.relative(process.cwd(), linkedDir);
980+
watcher = chokidar.watch(upath.join(watchDir, '**/*'), options)
971981
.on('addDir', dirSpy)
972982
.on('add', addSpy)
973983
.on('ready', function() {
@@ -1413,7 +1423,7 @@ function runTests(baseopts) {
14131423
var options2 = {};
14141424
Object.keys(options).forEach(function(key) { options2[key] = options[key] });
14151425
options2.cwd = getFixturePath('subdir');
1416-
watcher = chokidar.watch(getFixturePath('**'), options)
1426+
watcher = chokidar.watch(getGlobPath('**'), options)
14171427
.on('all', spy1)
14181428
.on('ready', w(function() {
14191429
watcher2 = chokidar.watch(fixturesPath, options2)
@@ -1754,8 +1764,8 @@ function runTests(baseopts) {
17541764
.on('all', spy)
17551765
.on('ready', w(function() {
17561766
// test with both relative and absolute paths
1757-
var subdirRel = sysPath.relative(process.cwd(), getFixturePath('subdir'));
1758-
watcher.unwatch([subdirRel, getFixturePath('unl*')]);
1767+
var subdirRel = upath.relative(process.cwd(), getFixturePath('subdir'));
1768+
watcher.unwatch([subdirRel, getGlobPath('unl*')]);
17591769
w(function() {
17601770
fs.unlink(getFixturePath('unlink.txt'), simpleCb);
17611771
fs.writeFile(getFixturePath('subdir/add.txt'), Date.now(), simpleCb);

0 commit comments

Comments
 (0)