mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2025-01-07 23:59:22 +03:00
Use cachedContents when checking for an update.
Without this change, we would never get the initial file's change without calling .read() first
This commit is contained in:
parent
261d386809
commit
d8850c2c77
@ -10,112 +10,124 @@ describe 'File', ->
|
|||||||
fsUtils.remove(filePath) if fsUtils.exists(filePath)
|
fsUtils.remove(filePath) if fsUtils.exists(filePath)
|
||||||
fsUtils.writeSync(filePath, "this is old!")
|
fsUtils.writeSync(filePath, "this is old!")
|
||||||
file = new File(filePath)
|
file = new File(filePath)
|
||||||
file.read()
|
|
||||||
|
|
||||||
afterEach ->
|
afterEach ->
|
||||||
file.off()
|
file.off()
|
||||||
fsUtils.remove(filePath) if fsUtils.exists(filePath)
|
fsUtils.remove(filePath) if fsUtils.exists(filePath)
|
||||||
|
|
||||||
describe "when the contents of the file change", ->
|
describe "when the file has not been read", ->
|
||||||
it "triggers 'contents-changed' event handlers", ->
|
describe "when the contents of the file change", ->
|
||||||
changeHandler = null
|
it "triggers 'contents-changed' event handlers", ->
|
||||||
changeHandler = jasmine.createSpy('changeHandler')
|
file.on 'contents-changed', changeHandler = jasmine.createSpy('changeHandler')
|
||||||
file.on 'contents-changed', changeHandler
|
|
||||||
fsUtils.writeSync(file.getPath(), "this is new!")
|
|
||||||
|
|
||||||
waitsFor "change event", ->
|
|
||||||
changeHandler.callCount > 0
|
|
||||||
|
|
||||||
runs ->
|
|
||||||
changeHandler.reset()
|
|
||||||
fsUtils.writeSync(file.getPath(), "this is newer!")
|
|
||||||
|
|
||||||
waitsFor "second change event", ->
|
|
||||||
changeHandler.callCount > 0
|
|
||||||
|
|
||||||
describe "when the file is removed", ->
|
|
||||||
it "triggers 'remove' event handlers", ->
|
|
||||||
removeHandler = null
|
|
||||||
removeHandler = jasmine.createSpy('removeHandler')
|
|
||||||
file.on 'removed', removeHandler
|
|
||||||
fsUtils.remove(file.getPath())
|
|
||||||
|
|
||||||
waitsFor "remove event", ->
|
|
||||||
removeHandler.callCount > 0
|
|
||||||
|
|
||||||
describe "when a file is moved (via the filesystem)", ->
|
|
||||||
newPath = null
|
|
||||||
|
|
||||||
beforeEach ->
|
|
||||||
newPath = path.join(path.dirname(filePath), "atom-file-was-moved-test.txt")
|
|
||||||
|
|
||||||
afterEach ->
|
|
||||||
if fsUtils.exists(newPath)
|
|
||||||
fsUtils.remove(newPath)
|
|
||||||
waitsFor "remove event", (done) -> file.on 'removed', done
|
|
||||||
|
|
||||||
it "it updates its path", ->
|
|
||||||
jasmine.unspy(window, "setTimeout")
|
|
||||||
moveHandler = null
|
|
||||||
moveHandler = jasmine.createSpy('moveHandler')
|
|
||||||
file.on 'moved', moveHandler
|
|
||||||
|
|
||||||
fsUtils.move(filePath, newPath)
|
|
||||||
|
|
||||||
waitsFor "move event", ->
|
|
||||||
moveHandler.callCount > 0
|
|
||||||
|
|
||||||
runs ->
|
|
||||||
expect(file.getPath()).toBe newPath
|
|
||||||
|
|
||||||
it "maintains 'contents-changed' events set on previous path", ->
|
|
||||||
jasmine.unspy(window, "setTimeout")
|
|
||||||
moveHandler = null
|
|
||||||
moveHandler = jasmine.createSpy('moveHandler')
|
|
||||||
file.on 'moved', moveHandler
|
|
||||||
changeHandler = null
|
|
||||||
changeHandler = jasmine.createSpy('changeHandler')
|
|
||||||
file.on 'contents-changed', changeHandler
|
|
||||||
|
|
||||||
fsUtils.move(filePath, newPath)
|
|
||||||
|
|
||||||
waitsFor "move event", ->
|
|
||||||
moveHandler.callCount > 0
|
|
||||||
|
|
||||||
runs ->
|
|
||||||
expect(changeHandler).not.toHaveBeenCalled()
|
|
||||||
fsUtils.writeSync(file.getPath(), "this is new!")
|
fsUtils.writeSync(file.getPath(), "this is new!")
|
||||||
|
|
||||||
waitsFor "change event", ->
|
waitsFor "change event", ->
|
||||||
changeHandler.callCount > 0
|
changeHandler.callCount > 0
|
||||||
|
|
||||||
describe "when a file is deleted and the recreated within a small amount of time (git sometimes does this)", ->
|
describe "when the file has already been read", ->
|
||||||
it "triggers a contents change event if the contents change", ->
|
beforeEach ->
|
||||||
jasmine.unspy(File.prototype, 'detectResurrectionAfterDelay')
|
file.read()
|
||||||
jasmine.unspy(window, "setTimeout")
|
|
||||||
|
|
||||||
changeHandler = jasmine.createSpy("file changed")
|
describe "when the contents of the file change", ->
|
||||||
removeHandler = jasmine.createSpy("file removed")
|
it "triggers 'contents-changed' event handlers", ->
|
||||||
file.on 'contents-changed', changeHandler
|
changeHandler = null
|
||||||
file.on 'removed', removeHandler
|
changeHandler = jasmine.createSpy('changeHandler')
|
||||||
|
file.on 'contents-changed', changeHandler
|
||||||
|
fsUtils.writeSync(file.getPath(), "this is new!")
|
||||||
|
|
||||||
expect(changeHandler).not.toHaveBeenCalled()
|
waitsFor "change event", ->
|
||||||
|
changeHandler.callCount > 0
|
||||||
|
|
||||||
fsUtils.remove(filePath)
|
runs ->
|
||||||
|
changeHandler.reset()
|
||||||
|
fsUtils.writeSync(file.getPath(), "this is newer!")
|
||||||
|
|
||||||
|
waitsFor "second change event", ->
|
||||||
|
changeHandler.callCount > 0
|
||||||
|
|
||||||
|
describe "when the file is removed", ->
|
||||||
|
it "triggers 'remove' event handlers", ->
|
||||||
|
removeHandler = null
|
||||||
|
removeHandler = jasmine.createSpy('removeHandler')
|
||||||
|
file.on 'removed', removeHandler
|
||||||
|
fsUtils.remove(file.getPath())
|
||||||
|
|
||||||
|
waitsFor "remove event", ->
|
||||||
|
removeHandler.callCount > 0
|
||||||
|
|
||||||
|
describe "when a file is moved (via the filesystem)", ->
|
||||||
|
newPath = null
|
||||||
|
|
||||||
|
beforeEach ->
|
||||||
|
newPath = path.join(path.dirname(filePath), "atom-file-was-moved-test.txt")
|
||||||
|
|
||||||
|
afterEach ->
|
||||||
|
if fsUtils.exists(newPath)
|
||||||
|
fsUtils.remove(newPath)
|
||||||
|
waitsFor "remove event", (done) -> file.on 'removed', done
|
||||||
|
|
||||||
|
it "it updates its path", ->
|
||||||
|
jasmine.unspy(window, "setTimeout")
|
||||||
|
moveHandler = null
|
||||||
|
moveHandler = jasmine.createSpy('moveHandler')
|
||||||
|
file.on 'moved', moveHandler
|
||||||
|
|
||||||
|
fsUtils.move(filePath, newPath)
|
||||||
|
|
||||||
|
waitsFor "move event", ->
|
||||||
|
moveHandler.callCount > 0
|
||||||
|
|
||||||
|
runs ->
|
||||||
|
expect(file.getPath()).toBe newPath
|
||||||
|
|
||||||
|
it "maintains 'contents-changed' events set on previous path", ->
|
||||||
|
jasmine.unspy(window, "setTimeout")
|
||||||
|
moveHandler = null
|
||||||
|
moveHandler = jasmine.createSpy('moveHandler')
|
||||||
|
file.on 'moved', moveHandler
|
||||||
|
changeHandler = null
|
||||||
|
changeHandler = jasmine.createSpy('changeHandler')
|
||||||
|
file.on 'contents-changed', changeHandler
|
||||||
|
|
||||||
|
fsUtils.move(filePath, newPath)
|
||||||
|
|
||||||
|
waitsFor "move event", ->
|
||||||
|
moveHandler.callCount > 0
|
||||||
|
|
||||||
|
runs ->
|
||||||
|
expect(changeHandler).not.toHaveBeenCalled()
|
||||||
|
fsUtils.writeSync(file.getPath(), "this is new!")
|
||||||
|
|
||||||
|
waitsFor "change event", ->
|
||||||
|
changeHandler.callCount > 0
|
||||||
|
|
||||||
|
describe "when a file is deleted and the recreated within a small amount of time (git sometimes does this)", ->
|
||||||
|
it "triggers a contents change event if the contents change", ->
|
||||||
|
jasmine.unspy(File.prototype, 'detectResurrectionAfterDelay')
|
||||||
|
jasmine.unspy(window, "setTimeout")
|
||||||
|
|
||||||
|
changeHandler = jasmine.createSpy("file changed")
|
||||||
|
removeHandler = jasmine.createSpy("file removed")
|
||||||
|
file.on 'contents-changed', changeHandler
|
||||||
|
file.on 'removed', removeHandler
|
||||||
|
|
||||||
expect(changeHandler).not.toHaveBeenCalled()
|
|
||||||
waits 20
|
|
||||||
runs ->
|
|
||||||
fsUtils.writeSync(filePath, "HE HAS RISEN!")
|
|
||||||
expect(changeHandler).not.toHaveBeenCalled()
|
expect(changeHandler).not.toHaveBeenCalled()
|
||||||
|
|
||||||
waitsFor "resurrection change event", ->
|
fsUtils.remove(filePath)
|
||||||
changeHandler.callCount == 1
|
|
||||||
|
|
||||||
runs ->
|
expect(changeHandler).not.toHaveBeenCalled()
|
||||||
expect(removeHandler).not.toHaveBeenCalled()
|
waits 20
|
||||||
fsUtils.writeSync(filePath, "Hallelujah!")
|
runs ->
|
||||||
changeHandler.reset()
|
fsUtils.writeSync(filePath, "HE HAS RISEN!")
|
||||||
|
expect(changeHandler).not.toHaveBeenCalled()
|
||||||
|
|
||||||
waitsFor "post-resurrection change event", ->
|
waitsFor "resurrection change event", ->
|
||||||
changeHandler.callCount > 0
|
changeHandler.callCount == 1
|
||||||
|
|
||||||
|
runs ->
|
||||||
|
expect(removeHandler).not.toHaveBeenCalled()
|
||||||
|
fsUtils.writeSync(filePath, "Hallelujah!")
|
||||||
|
changeHandler.reset()
|
||||||
|
|
||||||
|
waitsFor "post-resurrection change event", ->
|
||||||
|
changeHandler.callCount > 0
|
||||||
|
@ -80,7 +80,7 @@ class File
|
|||||||
@setPath(path)
|
@setPath(path)
|
||||||
@trigger "moved"
|
@trigger "moved"
|
||||||
else if eventType is "change"
|
else if eventType is "change"
|
||||||
oldContents = @read()
|
oldContents = @cachedContents
|
||||||
newContents = @read(true)
|
newContents = @read(true)
|
||||||
return if oldContents == newContents
|
return if oldContents == newContents
|
||||||
@trigger 'contents-changed'
|
@trigger 'contents-changed'
|
||||||
|
Loading…
Reference in New Issue
Block a user