From 3c78f9c98540266af5e5b0dc0dae6df020c2538b Mon Sep 17 00:00:00 2001 From: Thomas David Baker Date: Mon, 7 Dec 2015 11:20:57 -0800 Subject: [PATCH] :saveas command and spec test. --- lib/ex.coffee | 20 +++++-- spec/ex-commands-spec.coffee | 102 +++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 5 deletions(-) diff --git a/lib/ex.coffee b/lib/ex.coffee index 684b2f6..cde0aaa 100644 --- a/lib/ex.coffee +++ b/lib/ex.coffee @@ -139,7 +139,7 @@ class Ex buffer.setPath(undefined) buffer.load() - write: (range, filePath) -> + write: (range, filePath, saveas = false) -> if filePath[0] is '!' force = true filePath = filePath[1..] @@ -157,16 +157,23 @@ class Ex if filePath.length isnt 0 fullPath = getFullPath(filePath) if editor.getPath()? and (not fullPath? or editor.getPath() == fullPath) - # Use editor.save when no path is given or the path to the file is given - trySave(-> editor.save()).then(deferred.resolve) - saved = true + if saveas + throw new CommandError("Argument required") + else + # Use editor.save when no path is given or the path to the file is given + trySave(-> editor.save()).then(deferred.resolve) + saved = true else if not fullPath? fullPath = atom.showSaveDialogSync() if not saved and fullPath? if not force and fs.existsSync(fullPath) throw new CommandError("File exists (add ! to override)") - trySave(-> saveAs(fullPath)).then(deferred.resolve) + if saveas + editor = atom.workspace.getActiveTextEditor() + trySave(-> editor.saveAs(fullPath)).then(deferred.resolve) + else + trySave(-> saveAs(fullPath)).then(deferred.resolve) deferred.promise @@ -176,6 +183,9 @@ class Ex wq: (args...) => @write(args...).then => @quit() + saveas: (range, filePath) => + @write(range, filePath, true) + xit: (args...) => @wq(args...) wa: -> diff --git a/spec/ex-commands-spec.coffee b/spec/ex-commands-spec.coffee index a4813b5..5421b8e 100644 --- a/spec/ex-commands-spec.coffee +++ b/spec/ex-commands-spec.coffee @@ -151,6 +151,108 @@ describe "the commands", -> expect(atom.notifications.notifications).toEqual([]) 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", -> pane = null beforeEach ->