:saveas command and spec test.

This commit is contained in:
Thomas David Baker 2015-12-07 11:20:57 -08:00
parent 472ec2140e
commit 3c78f9c985
2 changed files with 117 additions and 5 deletions

View file

@ -139,7 +139,7 @@ class Ex
buffer.setPath(undefined) buffer.setPath(undefined)
buffer.load() buffer.load()
write: (range, filePath) -> write: (range, filePath, saveas = false) ->
if filePath[0] is '!' if filePath[0] is '!'
force = true force = true
filePath = filePath[1..] filePath = filePath[1..]
@ -157,6 +157,9 @@ class Ex
if filePath.length isnt 0 if filePath.length isnt 0
fullPath = getFullPath(filePath) fullPath = getFullPath(filePath)
if editor.getPath()? and (not fullPath? or editor.getPath() == fullPath) if editor.getPath()? and (not fullPath? or editor.getPath() == fullPath)
if saveas
throw new CommandError("Argument required")
else
# Use editor.save when no path is given or the path to the file is given # Use editor.save when no path is given or the path to the file is given
trySave(-> editor.save()).then(deferred.resolve) trySave(-> editor.save()).then(deferred.resolve)
saved = true saved = true
@ -166,6 +169,10 @@ class Ex
if not saved and fullPath? if not saved and fullPath?
if not force and fs.existsSync(fullPath) if not force and fs.existsSync(fullPath)
throw new CommandError("File exists (add ! to override)") throw new CommandError("File exists (add ! to override)")
if saveas
editor = atom.workspace.getActiveTextEditor()
trySave(-> editor.saveAs(fullPath)).then(deferred.resolve)
else
trySave(-> saveAs(fullPath)).then(deferred.resolve) trySave(-> saveAs(fullPath)).then(deferred.resolve)
deferred.promise deferred.promise
@ -176,6 +183,9 @@ class Ex
wq: (args...) => wq: (args...) =>
@write(args...).then => @quit() @write(args...).then => @quit()
saveas: (range, filePath) =>
@write(range, filePath, true)
xit: (args...) => @wq(args...) xit: (args...) => @wq(args...)
wa: -> wa: ->

View file

@ -151,6 +151,108 @@ describe "the commands", ->
expect(atom.notifications.notifications).toEqual([]) expect(atom.notifications.notifications).toEqual([])
expect(fs.readFileSync(existsPath, 'utf-8')).toEqual('abc\ndef') expect(fs.readFileSync(existsPath, 'utf-8')).toEqual('abc\ndef')
describe ":saveas", ->
describe "when editing a new file", ->
beforeEach ->
editor.getBuffer().setText('abc\ndef')
it "opens the save dialog", ->
spyOn(atom, 'showSaveDialogSync')
keydown(':')
submitNormalModeInputText('saveas')
expect(atom.showSaveDialogSync).toHaveBeenCalled()
it "saves when a path is specified in the save dialog", ->
filePath = projectPath('saveas-from-save-dialog')
spyOn(atom, 'showSaveDialogSync').andReturn(filePath)
keydown(':')
submitNormalModeInputText('saveas')
expect(fs.existsSync(filePath)).toBe(true)
expect(fs.readFileSync(filePath, 'utf-8')).toEqual('abc\ndef')
it "saves when a path is specified in the save dialog", ->
spyOn(atom, 'showSaveDialogSync').andReturn(undefined)
spyOn(fs, 'writeFileSync')
keydown(':')
submitNormalModeInputText('saveas')
expect(fs.writeFileSync.calls.length).toBe(0)
describe "when editing an existing file", ->
filePath = ''
i = 0
beforeEach ->
i++
filePath = projectPath("saveas-#{i}")
editor.setText('abc\ndef')
editor.saveAs(filePath)
it "complains if no path given", ->
editor.setText('abc')
keydown(':')
submitNormalModeInputText('saveas')
expect(atom.notifications.notifications[0].message).toEqual(
'Command error: Argument required'
)
describe "with a specified path", ->
newPath = ''
beforeEach ->
newPath = path.relative(dir, "#{filePath}.new")
editor.getBuffer().setText('abc')
keydown(':')
afterEach ->
submitNormalModeInputText("saveas #{newPath}")
newPath = path.resolve(dir, fs.normalize(newPath))
expect(fs.existsSync(newPath)).toBe(true)
expect(fs.readFileSync(newPath, 'utf-8')).toEqual('abc')
expect(editor.isModified()).toBe(false)
fs.removeSync(newPath)
it "saves to the path", ->
it "expands .", ->
newPath = path.join('.', newPath)
it "expands ..", ->
newPath = path.join('..', newPath)
it "expands ~", ->
newPath = path.join('~', newPath)
it "throws an error with more than one path", ->
keydown(':')
submitNormalModeInputText('saveas path1 path2')
expect(atom.notifications.notifications[0].message).toEqual(
'Command error: Only one file name allowed'
)
describe "when the file already exists", ->
existsPath = ''
beforeEach ->
existsPath = projectPath('saveas-exists')
fs.writeFileSync(existsPath, 'abc')
afterEach ->
fs.removeSync(existsPath)
it "throws an error if the file already exists", ->
keydown(':')
submitNormalModeInputText("saveas #{existsPath}")
expect(atom.notifications.notifications[0].message).toEqual(
'Command error: File exists (add ! to override)'
)
expect(fs.readFileSync(existsPath, 'utf-8')).toEqual('abc')
it "writes if forced with :saveas!", ->
keydown(':')
submitNormalModeInputText("saveas! #{existsPath}")
expect(atom.notifications.notifications).toEqual([])
expect(fs.readFileSync(existsPath, 'utf-8')).toEqual('abc\ndef')
describe ":quit", -> describe ":quit", ->
pane = null pane = null
beforeEach -> beforeEach ->