diff --git a/src/tail.coffee b/src/tail.coffee index 749094c..adda5f8 100644 --- a/src/tail.coffee +++ b/src/tail.coffee @@ -43,7 +43,12 @@ class Tail extends events.EventEmitter watch: (pos) -> return if @isWatching @isWatching = true - stats = fs.statSync(@filename) + try + stats = fs.statSync(@filename) + catch err + @logger.error("watch for #{@filename} failed: #{@err}") if @logger + @emit("error", "watch for #{@filename} failed: #{@err}") + return @pos = if pos? then pos else stats.size if @logger @@ -59,7 +64,12 @@ class Tail extends events.EventEmitter watchEvent: (e) -> if e is 'change' - stats = fs.statSync(@filename) + try + stats = fs.statSync(@filename) + catch err + @logger.error("'change' event for #{@filename}. #{@err}") if @logger + @emit("error", "'change' event for #{@filename}. #{@err}") + return @pos = stats.size if stats.size < @pos #scenario where texts is not appended but it's actually a w+ if stats.size > @pos @queue.push({start: @pos, end: stats.size}) diff --git a/test/mocha.opts b/test/mocha.opts index 6fa63b1..66e2133 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1 +1 @@ ---compilers mocha --compilers coffee:coffee-script/register \ No newline at end of file +--compilers mocha --compilers coffee:coffee-script/register --timeout 3000 diff --git a/test/tail.coffee b/test/tail.coffee index 4602d09..2016f1f 100644 --- a/test/tail.coffee +++ b/test/tail.coffee @@ -60,4 +60,25 @@ describe 'Tail', -> for l in lines fs.writeSync fd, l+'\n' - fs.closeSync fd \ No newline at end of file + fs.closeSync fd + + it 'should send error event on deletion of file while watching', (done)-> + text = "This is a line\n" + + fd = fs.openSync fileToTest, 'w+' + + tailedFile = new Tail fileToTest, {fsWatchOptions: {interval:100}, logger: console} + + # ensure error gets called when the file is deleted + tailedFile.on 'error', (line) -> + # recreate file so that `afterEach` can cleanup + fd = fs.openSync fileToTest, 'w+' + done() + + tailedFile.on 'line', (line) -> + # delete the file + fs.unlinkSync fileToTest + + fs.writeSync fd, text + + fs.closeSync fd