diff --git a/spec/text-buffer-io-spec.js b/spec/text-buffer-io-spec.js index a41a7f4381..f8da16a851 100644 --- a/spec/text-buffer-io-spec.js +++ b/spec/text-buffer-io-spec.js @@ -249,9 +249,10 @@ describe('TextBuffer IO', () => { describe('.save', () => { let filePath + let tempDir beforeEach(() => { - const tempDir = temp.mkdirSync() + tempDir = temp.mkdirSync() filePath = path.join(tempDir, 'temp.txt') fs.writeFileSync(filePath, '') buffer = new TextBuffer() @@ -430,6 +431,17 @@ describe('TextBuffer IO', () => { done() }, buffer.fileChangeDelay)) }) + + it('passes setPath to the custom File object', (done) => { + const newPath = path.join(tempDir, 'temp2.txt') + fs.writeFileSync(newPath, '') + buffer.setPath(newPath) + buffer.setText('test') + buffer.save().then(() => { + expect(fs.readFileSync(newPath, 'utf8')).toEqual('TEST') + done() + }) + }) }) describe('when a permission error occurs', () => { @@ -1153,6 +1165,10 @@ class ReverseCaseFile { return this.path } + setPath (path) { + this.path = path + } + createReadStream () { return fs.createReadStream(this.path).pipe(new Transform({ transform (chunk, encoding, callback) { diff --git a/src/text-buffer.coffee b/src/text-buffer.coffee index 3e65ecf000..2485c140ca 100644 --- a/src/text-buffer.coffee +++ b/src/text-buffer.coffee @@ -566,6 +566,10 @@ class TextBuffer # * `filePath` A {String} representing the new file path setPath: (filePath) -> return if filePath is @getPath() + if @file? and filePath + @file.setPath filePath + @updateFilePath() + return @setFile(new File(filePath) if filePath) # Experimental: Set a custom {File} object as the buffer's backing store. @@ -588,8 +592,11 @@ class TextBuffer setFile: (file) -> return if file?.getPath() is @getPath() @file = file + @file?.setEncoding?(@getEncoding()) + @updateFilePath() + + updateFilePath: -> if @file? - @file.setEncoding?(@getEncoding()) @subscribeToFile() @emitter.emit 'did-change-path', @getPath()