Skip to content

Commit

Permalink
more testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored and fengmk2 committed May 7, 2015
1 parent 73a1b90 commit 5b7e022
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
2 changes: 0 additions & 2 deletions README.md
Expand Up @@ -24,8 +24,6 @@ wt
[download-image]: https://img.shields.io/npm/dm/wt.svg?style=flat-square
[download-url]: https://npmjs.org/package/wt

![logo](https://raw.github.com/node-modules/wt/master/logo.png)

wt: Simple dir watcher, including all subdirectories, support events and multi dirs:

* `all`: every change event
Expand Down
14 changes: 9 additions & 5 deletions index.js
@@ -1,11 +1,11 @@
/**!
* wt - index.js
*
* Copyright(c) fengmk2 and other contributors.
* Copyright(c) node-modules and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
*/

'use strict';
Expand All @@ -20,7 +20,6 @@ var fs = require('fs');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var ndir = require('ndir');
var pedding = require('pedding');

module.exports = Watcher;

Expand Down Expand Up @@ -64,11 +63,16 @@ Watcher.watch = function (dirs, options, done) {
done = function() {};
}

var len = Array.isArray(dirs) ? dirs.length : 1;
var count = Array.isArray(dirs) ? dirs.length : 1;
return new Watcher(options)
.watch(dirs)
.once('error', done)
.on('watch', pedding(len, done).bind(null, null));
.on('watch', function () {
count--;
if (count === 0) {
done();
}
});
};

util.inherits(Watcher, EventEmitter);
Expand Down
Binary file removed logo.png
Binary file not shown.
38 changes: 37 additions & 1 deletion test/index.test.js
Expand Up @@ -26,11 +26,12 @@ describe('index.test.js', function () {
this.watcher = wt.watch(fixtures, done);
});

afterEach(function () {
afterEach(function (done) {
try {
fs.rmdirSync(path.join(fixtures, '.createdir'));
} catch (err) {}
this.watcher.close();
setTimeout(done, 100);
});

it('should watch file change', function (done) {
Expand Down Expand Up @@ -69,6 +70,20 @@ describe('index.test.js', function () {
setTimeout(done, 200);
});

it('should not ignore hidden dir change with ignoreHidden option', function (done) {
done = pedding(2, done);

this.watcher.close();
this.watcher = wt.watch(fixtures, {ignoreHidden: false}, function() {
var dirpath = path.join(fixtures, '.createdir');
fs.mkdir(dirpath, done);

this.watcher.on('dir', function () {
done();
});
}.bind(this));
});

it('should watch subdir file change', function (done) {
done = pedding(2, done);
var filepath = path.join(fixtures, 'subdir', 'subfoo.txt');
Expand Down Expand Up @@ -175,4 +190,25 @@ describe('index.test.js', function () {
done();
});
});

it('should emit watch event twice', function(done) {
done = pedding(2, done);

this.watcher.close();
this.watcher = wt.watch([
path.join(fixtures, 'subdir'),
path.join(fixtures, 'subdir2')
]).on('watch', done.bind(null, null));
});

it('should emit watch-error event', function(done) {
var filepath = path.join(fixtures, 'not-exist');

this.watcher.close();
this.watcher = wt.watch(filepath)
.on('watch-error', function(err) {
err.dir.should.eql(filepath);
done();
});
});
});

0 comments on commit 5b7e022

Please sign in to comment.