Permalink
Browse files

Merge pull request #43 from Dignifiedquire/fix-empty-dir

Fix ignoreInitial option.
  • Loading branch information...
2 parents 84162ce + a807466 commit a10b9055877fdc75369fd0bba478ccc5e969d5fb @paulmillr committed Mar 19, 2013
Showing with 53 additions and 10 deletions.
  1. +8 −8 src/index.coffee
  2. +45 −2 test/chokidar-test.coffee
View
@@ -138,8 +138,8 @@ exports.FSWatcher = class FSWatcher extends EventEmitter
# directory - string, fs path.
#
# Returns nothing.
- _handleDir: (directory) =>
- read = (directory) =>
+ _handleDir: (directory, initialAdd) =>
+ read = (directory, initialAdd) =>
fs.readdir directory, (error, current) =>
return @emit 'error', error if error?
return unless current
@@ -161,18 +161,18 @@ exports.FSWatcher = class FSWatcher extends EventEmitter
.filter (file) =>
previous.indexOf(file) is -1
.forEach (file) =>
- @_handle sysPath.join(directory, file), previous.length is 0
+ @_handle sysPath.join(directory, file), initialAdd
- read directory
- @_watch directory, 'directory', read
+ read directory, initialAdd
+ @_watch directory, 'directory', (dir) -> read dir, no
# Private: Handle added file or directory.
# Delegates call to _handleFile / _handleDir after checks.
#
# item - string, path to file or directory.
#
# Returns nothing.
- _handle: (item, initialAdd = no) =>
+ _handle: (item, initialAdd) =>
# Don't handle invalid files, dotfiles etc.
return if @_ignored item
@@ -187,7 +187,7 @@ exports.FSWatcher = class FSWatcher extends EventEmitter
return
@_handleFile item, stats, initialAdd if stats.isFile()
- @_handleDir item if stats.isDirectory()
+ @_handleDir item, initialAdd if stats.isDirectory()
emit: (event, args...) ->
super
@@ -204,7 +204,7 @@ exports.FSWatcher = class FSWatcher extends EventEmitter
# Returns an instance of FSWatcher for chaning.
add: (files) =>
files = [files] unless Array.isArray files
- files.forEach @_handle
+ files.forEach (file) => @_handle file, true
this
# Public: Remove all listeners from watched files.
View
@@ -7,6 +7,8 @@ getFixturePath = (subPath) ->
sysPath.join __dirname, 'fixtures', subPath
fixturesPath = getFixturePath ''
+delay = (fn) =>
+ setTimeout fn, 205
describe 'chokidar', ->
it 'should expose public API methods', ->
@@ -15,8 +17,6 @@ describe 'chokidar', ->
describe 'watch', ->
options = {}
- delay = (fn) =>
- setTimeout fn, 205
beforeEach (done) ->
@watcher = chokidar.watch fixturesPath, options
@@ -114,6 +114,49 @@ describe 'chokidar', ->
spy.should.have.been.calledWith testPath
done()
+
+ describe 'watch options', ->
+ describe 'ignoreInitial', ->
+ options = { ignoreInitial: yes }
+
+ before (done) ->
+ try fs.unlinkSync getFixturePath('subdir/add.txt')
+ try fs.rmdirSync getFixturePath('subdir')
+ done()
+
+ after (done) ->
+ try fs.unlinkSync getFixturePath('subdir/add.txt')
+ try fs.rmdirSync getFixturePath('subdir')
+ done()
+
+ it 'should ignore inital add events', (done) ->
+ spy = sinon.spy()
+ watcher = chokidar.watch fixturesPath, options
+ watcher.on 'add', spy
+ delay ->
+ spy.should.not.have.been.called
+ watcher.close()
+ done()
+
+ it 'should notice when a file appears in an empty directory', (done) ->
+ spy = sinon.spy()
+ testDir = getFixturePath 'subdir'
+ testPath = getFixturePath 'subdir/add.txt'
+
+ watcher = chokidar.watch fixturesPath, options
+ watcher.on 'add', spy
+
+ delay ->
+ spy.should.not.have.been.called
+ fs.mkdirSync testDir, 0o755
+ watcher.add testDir
+
+ fs.writeFileSync testPath, 'hello'
+ delay ->
+ spy.should.have.been.calledOnce
+ spy.should.have.been.calledWith testPath
+ done()
+
describe 'is-binary', ->
it 'should be a function', ->
isBinary.should.be.a 'function'

0 comments on commit a10b905

Please sign in to comment.