Skip to content

Commit

Permalink
Merge pull request #43 from Dignifiedquire/fix-empty-dir
Browse files Browse the repository at this point in the history
Fix ignoreInitial option.
  • Loading branch information
paulmillr committed Mar 19, 2013
2 parents 84162ce + a807466 commit a10b905
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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.
Expand Down
47 changes: 45 additions & 2 deletions test/chokidar-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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', ->
Expand All @@ -15,8 +17,6 @@ describe 'chokidar', ->

describe 'watch', ->
options = {}
delay = (fn) =>
setTimeout fn, 205

beforeEach (done) ->
@watcher = chokidar.watch fixturesPath, options
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit a10b905

Please sign in to comment.