Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Update setPath to work with custom files
Browse files Browse the repository at this point in the history
Contributed under CC0.
  • Loading branch information
hansonw committed Nov 1, 2017
1 parent 8294d3f commit b619fce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
18 changes: 17 additions & 1 deletion spec/text-buffer-io-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class SimpleFile {
return this.path
}

setPath (path) {
this.path = path
}

createReadStream () {
return fs.createReadStream(this.path)
}
Expand Down Expand Up @@ -149,9 +153,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()
Expand Down Expand Up @@ -257,6 +262,17 @@ describe('TextBuffer IO', () => {
done()
})
})

it('passes setPath to the custom File object', (done) => {
const newPath = path.join(tempDir, 'temp2.txt')
buffer.setPath(newPath)
buffer.setText('test')
buffer.save().then(() => {
expect(fs.readFileSync(newPath, 'utf8')).toEqual(buffer.getText())
expect(file.createWriteStream).toHaveBeenCalled()
done()
})
})
})
})

Expand Down
9 changes: 8 additions & 1 deletion src/text-buffer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,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.
Expand All @@ -607,8 +611,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()

Expand Down

0 comments on commit b619fce

Please sign in to comment.