diff --git a/spec/app/buffer-spec.coffee b/spec/app/buffer-spec.coffee index 78b15ce55..0b6ccb40b 100644 --- a/spec/app/buffer-spec.coffee +++ b/spec/app/buffer-spec.coffee @@ -38,6 +38,33 @@ describe 'Buffer', -> buffer.setPath("moo.text") expect(eventHandler).toHaveBeenCalledWith(buffer) + describe "when the buffer's file is modified (via another process)", -> + path = null + beforeEach -> + path = fs.join(require.resolve('fixtures'), "tmp.txt") + fs.write(path, "first") + + afterEach -> + fs.remove(path) + + describe "when the buffer is in an unmodified", -> + it "triggers 'change' event", -> + buffer = new Buffer(path) + changeHandler = jasmine.createSpy('changeHandler') + buffer.on 'change', changeHandler + fs.write(path, "second") + + expect(changeHandler.callCount).toBe 0 + waitsFor "file to trigger change event", -> + changeHandler.callCount > 0 + + runs -> + [event] = changeHandler.argsForCall[0] + expect(event.oldRange).toEqual [[0, 0], [0, 5]] + expect(event.newRange).toEqual [[0, 0], [0, 6]] + expect(event.oldText).toBe "first" + expect(event.newText).toBe "second" + describe ".isModified()", -> it "returns true when user changes buffer", -> expect(buffer.isModified()).toBeFalsy() diff --git a/src/app/buffer.coffee b/src/app/buffer.coffee index 9d8d6e567..b975152ed 100644 --- a/src/app/buffer.coffee +++ b/src/app/buffer.coffee @@ -1,5 +1,6 @@ _ = require 'underscore' fs = require 'fs' +File = require 'file' Point = require 'point' Range = require 'range' EventEmitter = require 'event-emitter' @@ -10,7 +11,8 @@ class Buffer @idCounter = 1 modified: null lines: null - path: null + file: null + constructor: (path) -> @id = @constructor.idCounter++ @@ -24,7 +26,7 @@ class Buffer @modified = false getPath: -> - @path + @file.getPath() getExtension: -> if @getPath() @@ -33,7 +35,9 @@ class Buffer null setPath: (path) -> - @path = path + @file = new File(path) + @file.on "contents-change", => + @setText(fs.read(@file.getPath())) unless @isModified() @trigger "path-change", this getText: ->