diff --git a/test/add_test.js b/test/add_test.js index 6949ac9..105d387 100644 --- a/test/add_test.js +++ b/test/add_test.js @@ -1,14 +1,20 @@ 'use strict'; -var Gaze = require('../lib/gaze.js').Gaze; +var Gaze = require('../lib/gaze.js'); var path = require('path'); var fs = require('fs'); +var gaze; exports.add = { setUp: function(done) { process.chdir(path.resolve(__dirname, 'fixtures')); done(); }, + tearDown: function(done) { + gaze.close(); + gaze = null; + done(); + }, addToWatched: function(test) { test.expect(1); var files = [ @@ -27,20 +33,19 @@ exports.add = { 'nested/': ['one.js', 'three.js', 'sub/'], 'nested/sub/': ['two.js'] }; - var gaze = new Gaze('addnothingtowatch'); + gaze = Gaze('thiswillnevermatchanyfile'); gaze._addToWatched(files); test.deepEqual(gaze.relative(null, true), expected); test.done(); }, addLater: function(test) { test.expect(3); - new Gaze('sub/one.js', function(err, watcher) { + gaze = Gaze('sub/one.js', function(err, watcher) { test.deepEqual(watcher.relative('sub'), ['one.js']); watcher.add('sub/*.js', function() { test.deepEqual(watcher.relative('sub'), ['one.js', 'two.js']); watcher.on('changed', function(filepath) { test.equal('two.js', path.basename(filepath)); - watcher.close(); test.done(); }); fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'two.js'), 'var two = true;'); diff --git a/test/api_test.js b/test/api_test.js index 42d258e..72db067 100644 --- a/test/api_test.js +++ b/test/api_test.js @@ -1,37 +1,40 @@ 'use strict'; -var gaze = require('../lib/gaze.js'); +var Gaze = require('../lib/gaze.js'); var path = require('path'); +var gaze; exports.api = { setUp: function(done) { process.chdir(path.resolve(__dirname, 'fixtures')); done(); }, + tearDown: function(done) { + gaze.close(); + gaze = null; + done(); + }, newGaze: function(test) { test.expect(2); - new gaze.Gaze('**/*', {}, function() { + gaze = new Gaze.Gaze('**/*', {}, function() { var result = this.relative(null, true); test.deepEqual(result['.'], ['Project (LO)/', 'nested/', 'one.js', 'sub/']); test.deepEqual(result['sub/'], ['one.js', 'two.js']); - this.close(); test.done(); }); }, func: function(test) { test.expect(1); - var g = gaze('**/*', function(err, watcher) { + gaze = Gaze('**/*', function(err, watcher) { test.deepEqual(watcher.relative('sub', true), ['one.js', 'two.js']); - g.close(); test.done(); }); }, ready: function(test) { test.expect(1); - var g = new gaze.Gaze('**/*'); - g.on('ready', function(watcher) { + gaze = new Gaze.Gaze('**/*'); + gaze.on('ready', function(watcher) { test.deepEqual(watcher.relative('sub', true), ['one.js', 'two.js']); - this.close(); test.done(); }); } diff --git a/test/matching_test.js b/test/matching_test.js index b9ac7de..c8193b2 100644 --- a/test/matching_test.js +++ b/test/matching_test.js @@ -1,56 +1,57 @@ 'use strict'; -var gaze = require('../lib/gaze.js'); +var Gaze = require('../lib/gaze.js'); var path = require('path'); +var gaze; exports.matching = { setUp: function(done) { process.chdir(path.resolve(__dirname, 'fixtures')); done(); }, + tearDown: function(done) { + gaze.close(); + gaze = null; + done(); + }, globAll: function(test) { test.expect(2); - gaze('**/*', function() { - var result = this.relative(null, true); + gaze = Gaze('**/*', function(err, watcher) { + var result = watcher.relative(null, true); test.deepEqual(result['.'], ['Project (LO)/', 'nested/', 'one.js', 'sub/']); test.deepEqual(result['sub/'], ['one.js', 'two.js']); - this.close(); test.done(); }); }, relativeDir: function(test) { test.expect(1); - gaze('**/*', function() { - test.deepEqual(this.relative('sub', true), ['one.js', 'two.js']); - this.close(); + gaze = Gaze('**/*', function(err, watcher) { + test.deepEqual(watcher.relative('sub', true), ['one.js', 'two.js']); test.done(); }); }, globArray: function(test) { test.expect(2); - gaze(['*.js', 'sub/*.js'], function() { - var result = this.relative(null, true); + gaze = Gaze(['*.js', 'sub/*.js'], function(err, watcher) { + var result = watcher.relative(null, true); test.deepEqual(result['.'], ['one.js']); test.deepEqual(result['sub/'], ['one.js', 'two.js']); - this.close(); test.done(); }); }, globArrayDot: function(test) { - test.expect(1); - gaze(['./sub/*.js'], function() { - var result = this.relative(null, true); - test.deepEqual(result['sub/'], ['one.js', 'two.js']); - this.close(); - test.done(); - }); - }, + test.expect(1); + gaze = Gaze(['sub/*.js'], function(err, watcher) { + var result = watcher.relative(null, true); + test.deepEqual(result['sub/'], ['one.js', 'two.js']); + test.done(); + }); + }, oddName: function(test) { test.expect(1); - gaze(['Project (LO)/*.js'], function() { - var result = this.relative(null, true); + gaze = Gaze(['Project (LO)/*.js'], function(err, watcher) { + var result = watcher.relative(null, true); test.deepEqual(result['Project (LO)/'], ['one.js']); - this.close(); test.done(); }); } diff --git a/test/relative_test.js b/test/relative_test.js index 52e5a57..2fa3c60 100644 --- a/test/relative_test.js +++ b/test/relative_test.js @@ -1,28 +1,30 @@ 'use strict'; -var Gaze = require('../lib/gaze.js').Gaze; +var Gaze = require('../lib/gaze.js'); var path = require('path'); +var gaze; exports.relative = { setUp: function(done) { process.chdir(path.resolve(__dirname, 'fixtures')); done(); }, + tearDown: function(done) { + gaze.close(); + gaze = null; + done(); + }, relative: function(test) { test.expect(1); - var files = [ - 'Project (LO)/', - 'Project (LO)/one.js', - 'nested/', - 'nested/one.js', - 'nested/three.js', - 'nested/sub/', - 'nested/sub/two.js', - 'one.js' - ]; - var gaze = new Gaze('addnothingtowatch'); - gaze._addToWatched(files); - test.deepEqual(gaze.relative('.', true), ['Project (LO)/', 'nested/', 'one.js']); - test.done(); + gaze = Gaze(['**/*.js'], function(err, watcher) { + test.deepEqual(watcher.relative(null, true), { + 'Project (LO)/': ['one.js'], + 'nested/': ['one.js', 'three.js'], + 'nested/sub/': ['two.js'], + '.': ['one.js'], + 'sub/': ['one.js', 'two.js'] + }); + test.done(); + }); } }; diff --git a/test/rename_test.js b/test/rename_test.js index 028b344..552f1e4 100644 --- a/test/rename_test.js +++ b/test/rename_test.js @@ -1,14 +1,14 @@ 'use strict'; -var gaze = require('../lib/gaze.js'); +var Gaze = require('../lib/gaze.js'); var path = require('path'); var fs = require('fs'); - +var gaze; // Node v0.6 compat fs.existsSync = fs.existsSync || path.existsSync; // Clean up helper to call in setUp and tearDown -function cleanUp(done) { +function deleteFiles(done) { [ 'sub/rename.js', 'sub/renamed.js' @@ -22,22 +22,28 @@ function cleanUp(done) { exports.watch = { setUp: function(done) { process.chdir(path.resolve(__dirname, 'fixtures')); - cleanUp(done); + deleteFiles(done); + }, + tearDown: function(done) { + gaze.close(); + gaze = null; + deleteFiles(done); }, - tearDown: cleanUp, rename: function(test) { test.expect(2); + var oldPath = path.join(__dirname, 'fixtures', 'sub', 'rename.js'); var newPath = path.join(__dirname, 'fixtures', 'sub', 'renamed.js'); - fs.writeFileSync(oldPath, 'var rename = true;'); - gaze('**/*', function(err, watcher) { + + gaze = Gaze('**/*.js', function(err, watcher) { watcher.on('renamed', function(newFile, oldFile) { test.equal(newFile, newPath); test.equal(oldFile, oldPath); - watcher.close(); test.done(); }); fs.renameSync(oldPath, newPath); }); + + fs.writeFileSync(oldPath, 'var rename = true;'); } }; diff --git a/test/safewrite_test.js b/test/safewrite_test.js index 9200f11..33a44af 100644 --- a/test/safewrite_test.js +++ b/test/safewrite_test.js @@ -1,14 +1,15 @@ 'use strict'; -var gaze = require('../lib/gaze.js'); +var Gaze = require('../lib/gaze.js'); var path = require('path'); var fs = require('fs'); +var gaze; // Node v0.6 compat fs.existsSync = fs.existsSync || path.existsSync; // Clean up helper to call in setUp and tearDown -function cleanUp(done) { +function deleteFiles(done) { [ 'safewrite.js' ].forEach(function(d) { @@ -21,9 +22,13 @@ function cleanUp(done) { exports.safewrite = { setUp: function(done) { process.chdir(path.resolve(__dirname, 'fixtures')); - cleanUp(done); + deleteFiles(done); + }, + tearDown: function(done) { + gaze.close(); + gaze = null; + deleteFiles(done); }, - tearDown: cleanUp, safewrite: function(test) { test.expect(4); @@ -39,15 +44,14 @@ exports.safewrite = { times++; } - gaze('**/*', function() { - this.on('all', function(action, filepath) { + gaze = Gaze('**/*', function(err, watcher) { + watcher.on('all', function(action, filepath) { test.equal(action, 'changed'); test.equal(path.basename(filepath), 'safewrite.js'); if (times < 2) { setTimeout(simSafewrite, 1000); } else { - this.close(); test.done(); } }); @@ -55,7 +59,6 @@ exports.safewrite = { setTimeout(function() { simSafewrite(); }, 1000); - }); } }; diff --git a/test/watch_test.js b/test/watch_test.js index 44dc23f..5da417e 100644 --- a/test/watch_test.js +++ b/test/watch_test.js @@ -1,19 +1,21 @@ 'use strict'; -var gaze = require('../lib/gaze.js'); +var Gaze = require('../lib/gaze.js'); var grunt = require('grunt'); var path = require('path'); var fs = require('fs'); +var gaze; // Node v0.6 compat fs.existsSync = fs.existsSync || path.existsSync; // Clean up helper to call in setUp and tearDown -function cleanUp(done) { +function deleteFiles(done) { [ 'sub/tmp.js', 'sub/tmp', 'sub/renamed.js', + 'sub/added.js', 'added.js', 'nested/added.js', 'nested/sub/added.js' @@ -27,115 +29,136 @@ function cleanUp(done) { exports.watch = { setUp: function(done) { process.chdir(path.resolve(__dirname, 'fixtures')); - cleanUp(done); + deleteFiles(done); + }, + tearDown: function(done) { + gaze.close(); + gaze = null; + deleteFiles(done); }, - tearDown: cleanUp, remove: function(test) { test.expect(2); - gaze('**/*', function() { - this.remove(path.resolve(__dirname, 'fixtures', 'sub', 'two.js')); - this.remove(path.resolve(__dirname, 'fixtures')); - var result = this.relative(null, true); + gaze = Gaze('**/*.js', function(err, watcher) { + watcher.remove(path.resolve(__dirname, 'fixtures', 'sub', 'two.js')); + //Two options here, require the user to add a seperator as done below to remove + //a directory. + //Or, we could do stat checks in the remove function to test if it is a directory. + watcher.remove(path.resolve(__dirname, 'fixtures') + path.sep); + var result = watcher.relative(null, true); test.deepEqual(result['sub/'], ['one.js']); test.notDeepEqual(result['.'], ['one.js']); - this.close(); test.done(); }); }, changed: function(test) { test.expect(1); - gaze('**/*', function(err, watcher) { + + gaze = Gaze('**/*.js', function(err, watcher) { watcher.on('changed', function(filepath) { var expected = path.relative(process.cwd(), filepath); test.equal(path.join('sub', 'one.js'), expected); - watcher.close(); + test.done(); }); - this.on('added', function() { test.ok(false, 'added event should not have emitted.'); }); - this.on('deleted', function() { test.ok(false, 'deleted event should not have emitted.'); }); + watcher.on('added', function() { test.ok(false, 'added event should not have emitted.'); }); + watcher.on('deleted', function() { test.ok(false, 'deleted event should not have emitted.'); }); + fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;'); - watcher.on('end', test.done); }); }, added: function(test) { test.expect(1); - gaze('**/*', function(err, watcher) { + + gaze = Gaze('**/*.js', function(err, watcher) { watcher.on('added', function(filepath) { var expected = path.relative(process.cwd(), filepath); test.equal(path.join('sub', 'tmp.js'), expected); - watcher.close(); }); - this.on('changed', function() { test.ok(false, 'changed event should not have emitted.'); }); - this.on('deleted', function() { test.ok(false, 'deleted event should not have emitted.'); }); - fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp.js'), 'var tmp = true;'); - watcher.on('end', test.done); + watcher.on('changed', function() { test.ok(false, 'changed event should not have emitted.'); }); + watcher.on('deleted', function() { test.ok(false, 'deleted event should not have emitted.'); }); + + fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp.js'), 'var tmp = true;'); + + setTimeout(function() { + //are two events firing for this add? + //as well gives the file system a break to ensure accurate tests. + test.done(); + }, 2000) }); }, dontAddUnmatchedFiles: function(test) { test.expect(2); - gaze('**/*.js', function(err, watcher) { - setTimeout(function() { - test.ok(true, 'Ended without adding a file.'); - watcher.close(); - }, 1000); + gaze = Gaze('**/*.js', function(err, watcher) { this.on('added', function(filepath) { test.equal(path.relative(process.cwd(), filepath), path.join('sub', 'tmp.js')); }); fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp'), 'Dont add me!'); fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp.js'), 'add me!'); - watcher.on('end', test.done); + + setTimeout(function() { + test.ok(true, 'Ended without adding the non.js file.'); + test.done(); + }, 1000); }); }, deleted: function(test) { test.expect(1); var tmpfile = path.resolve(__dirname, 'fixtures', 'sub', 'deleted.js'); fs.writeFileSync(tmpfile, 'var tmp = true;'); - gaze('**/*', function(err, watcher) { + gaze = Gaze('**/*.js', function(err, watcher) { watcher.on('deleted', function(filepath) { test.equal(path.join('sub', 'deleted.js'), path.relative(process.cwd(), filepath)); - watcher.close(); + test.done(); }); this.on('changed', function() { test.ok(false, 'changed event should not have emitted.'); }); this.on('added', function() { test.ok(false, 'added event should not have emitted.'); }); fs.unlinkSync(tmpfile); - watcher.on('end', test.done); }); }, nomark: function(test) { test.expect(1); - gaze('**/*', {mark:false}, function(err, watcher) { + + gaze = Gaze('**/*.js', {mark:false}, function(err, watcher) { watcher.on('changed', function(filepath) { var expected = path.relative(process.cwd(), filepath); test.equal(path.join('sub', 'one.js'), expected); - watcher.close(); + test.done(); }); + fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;'); - watcher.on('end', test.done); }); }, dontEmitTwice: function(test) { test.expect(2); - gaze('**/*', function(err, watcher) { + gaze = Gaze('**/*.js', function(err, watcher) { + watcher.on('all', function(status, filepath) { var expected = path.relative(process.cwd(), filepath); + test.equal(path.join('sub', 'one.js'), expected); test.equal(status, 'changed'); + fs.readFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js')); + setTimeout(function() { fs.readFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js')); }, 1000); + // Give some time to accidentally emit before we close - setTimeout(function() { watcher.close(); }, 5000); + setTimeout(function() { test.done() }, 3000); + }); + setTimeout(function() { fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;'); }, 1000); - watcher.on('end', test.done); + }); }, emitTwice: function(test) { test.expect(2); var times = 0; - gaze('**/*', function(err, watcher) { + + gaze = Gaze('**/*.js', function(err, watcher) { watcher.on('all', function(status, filepath) { test.equal(status, 'changed'); times++; @@ -143,19 +166,17 @@ exports.watch = { if (times < 2) { fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;'); } else { - watcher.close(); + test.done(); } }, 1000); }); - setTimeout(function() { - fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;'); - }, 1000); - watcher.on('end', test.done); + + fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;'); }); }, nonExistent: function(test) { test.expect(1); - gaze('non/existent/**/*', function(err, watcher) { + gaze = Gaze('non/existent/**/*', function(err, watcher) { test.ok(true); test.done(); }); @@ -163,39 +184,35 @@ exports.watch = { differentCWD: function(test) { test.expect(1); var cwd = path.resolve(__dirname, 'fixtures', 'sub'); - gaze('two.js', { + + gaze = Gaze('two.js', { cwd: cwd }, function(err, watcher) { watcher.on('changed', function(filepath) { test.deepEqual(this.relative(), {'.':['two.js']}); - watcher.close(); + test.done(); }); + fs.writeFileSync(path.resolve(cwd, 'two.js'), 'var two = true;'); - watcher.on('end', test.done); }); }, - addedEmitInSubFolders: function(test) { - test.expect(4); - var adds = [ - { pattern: '**/*', file: path.resolve(__dirname, 'fixtures', 'nested', 'sub', 'added.js') }, - { pattern: '**/*', file: path.resolve(__dirname, 'fixtures', 'added.js') }, - { pattern: 'nested/**/*', file: path.resolve(__dirname, 'fixtures', 'nested', 'added.js') }, - { pattern: 'nested/sub/*.js', file: path.resolve(__dirname, 'fixtures', 'nested', 'sub', 'added.js') }, - ]; - grunt.util.async.forEachSeries(adds, function(add, next) { - new gaze.Gaze(add.pattern, function(err, watcher) { - watcher.on('added', function(filepath) { - test.equal('added.js', path.basename(filepath)); - fs.unlinkSync(filepath); - watcher.close(); - next(); - }); - watcher.on('changed', function() { test.ok(false, 'changed event should not have emitted.'); }); - watcher.on('deleted', function() { test.ok(false, 'deleted event should not have emitted.'); }); - fs.writeFileSync(add.file, 'var added = true;'); + dontEmitInSpecificSubFolders: function(test) { + test.expect(3); + + gaze = Gaze(['**/*.js', 'nested/**/*.js', '!nested/sub/*.js'], function(err, watcher) { + watcher.on('added', function(filepath) { + test.equal('added.js', path.basename(filepath)); }); - }, function() { - test.done(); + watcher.on('changed', function() { test.ok(false, 'changed event should not have emitted.'); }); + + fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'nested', 'sub', 'added.js'), 'var added = true;'); + fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'nested', 'added.js'), 'var added = true;'); + fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'added.js'), 'var added = true;'); + fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'added.js'), 'var added = true;'); + + setTimeout(function() { + test.done(); + }, 2000); }); }, };