Skip to content

Commit

Permalink
Update to use current APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
kuychaco committed Apr 15, 2016
1 parent 7876d55 commit 6988546
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 118 deletions.
156 changes: 67 additions & 89 deletions spec/pane-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -917,103 +917,81 @@ describe "Pane", ->
expect(item1.save).not.toHaveBeenCalled()
expect(pane.isDestroyed()).toBe false

it "does not destroy the pane if save fails and user clicks cancel", ->
pane = new Pane(items: [new Item("A"), new Item("B")])
[item1, item2] = pane.getItems()

item1.shouldPromptToSave = -> true
item1.getURI = -> "/test/path"

item1.save = jasmine.createSpy("save").andCallFake ->
error = new Error("EACCES, permission denied '/test/path'")
error.path = '/test/path'
error.code = 'EACCES'
throw error

confirmations = 0
spyOn(atom, 'confirm').andCallFake ->
confirmations++
if confirmations is 1
return 0
else
return 1

pane.close()

expect(atom.confirm).toHaveBeenCalled()
expect(confirmations).toBe(2)
expect(item1.save).toHaveBeenCalled()
expect(pane.isDestroyed()).toBe false

it "does destroy the pane if the user saves the file under a new name", ->
pane = new Pane(items: [new Item("A"), new Item("B")])
[item1, item2] = pane.getItems()

item1.shouldPromptToSave = -> true
item1.getURI = -> "/test/path"

item1.save = jasmine.createSpy("save").andCallFake ->
error = new Error("EACCES, permission denied '/test/path'")
error.path = '/test/path'
error.code = 'EACCES'
throw error

item1.saveAs = jasmine.createSpy("saveAs").andReturn(true)

confirmations = 0
spyOn(atom, 'confirm').andCallFake ->
confirmations++
return 0

spyOn(atom, 'showSaveDialogSync').andReturn("new/path")
describe "when item fails to save", ->
[pane, item1, item2] = []

pane.close()

expect(atom.confirm).toHaveBeenCalled()
expect(confirmations).toBe(2)
expect(atom.showSaveDialogSync).toHaveBeenCalled()
expect(item1.save).toHaveBeenCalled()
expect(item1.saveAs).toHaveBeenCalled()
expect(pane.isDestroyed()).toBe true

it "asks again if the saveAs also fails", ->
pane = new Pane(items: [new Item("A"), new Item("B")])
[item1, item2] = pane.getItems()

item1.shouldPromptToSave = -> true
item1.getURI = -> "/test/path"

item1.save = jasmine.createSpy("save").andCallFake ->
error = new Error("EACCES, permission denied '/test/path'")
error.path = '/test/path'
error.code = 'EACCES'
throw error
beforeEach ->
pane = new Pane({items: [new Item("A"), new Item("B")], applicationDelegate: atom.applicationDelegate, config: atom.config})
[item1, item2] = pane.getItems()

item1.saveAs = jasmine.createSpy("saveAs").andCallFake ->
error = new Error("EACCES, permission denied '/test/path'")
error.path = '/test/path'
error.code = 'EACCES'
throw error
item1.shouldPromptToSave = -> true
item1.getURI = -> "/test/path"

item1.save = jasmine.createSpy("save").andCallFake ->
error = new Error("EACCES, permission denied '/test/path'")
error.path = '/test/path'
error.code = 'EACCES'
throw error

confirmations = 0
spyOn(atom, 'confirm').andCallFake ->
confirmations++
if confirmations < 3
return 0
return 2
it "does not destroy the pane if save fails and user clicks cancel", ->
confirmations = 0
confirm.andCallFake ->
confirmations++
if confirmations is 1
return 0 # click save
else
return 1 # click cancel

pane.close()

expect(atom.applicationDelegate.confirm).toHaveBeenCalled()
expect(confirmations).toBe(2)
expect(item1.save).toHaveBeenCalled()
expect(pane.isDestroyed()).toBe false

it "does destroy the pane if the user saves the file under a new name", ->
item1.saveAs = jasmine.createSpy("saveAs").andReturn(true)

confirmations = 0
confirm.andCallFake ->
confirmations++
return 0 # save and then save as

showSaveDialog.andReturn("new/path")

pane.close()

expect(atom.applicationDelegate.confirm).toHaveBeenCalled()
expect(confirmations).toBe(2)
expect(atom.applicationDelegate.showSaveDialog).toHaveBeenCalled()
expect(item1.save).toHaveBeenCalled()
expect(item1.saveAs).toHaveBeenCalled()
expect(pane.isDestroyed()).toBe true

it "asks again if the saveAs also fails", ->
item1.saveAs = jasmine.createSpy("saveAs").andCallFake ->
error = new Error("EACCES, permission denied '/test/path'")
error.path = '/test/path'
error.code = 'EACCES'
throw error

spyOn(atom, 'showSaveDialogSync').andReturn("new/path")
confirmations = 0
confirm.andCallFake ->
confirmations++
if confirmations < 3
return 0 # save, save as, save as
return 2 # don't save

pane.close()
showSaveDialog.andReturn("new/path")

expect(atom.confirm).toHaveBeenCalled()
expect(confirmations).toBe(3)
expect(atom.showSaveDialogSync).toHaveBeenCalled()
expect(item1.save).toHaveBeenCalled()
expect(item1.saveAs).toHaveBeenCalled()
expect(pane.isDestroyed()).toBe true
pane.close()

expect(atom.applicationDelegate.confirm).toHaveBeenCalled()
expect(confirmations).toBe(3)
expect(atom.applicationDelegate.showSaveDialog).toHaveBeenCalled()
expect(item1.save).toHaveBeenCalled()
expect(item1.saveAs).toHaveBeenCalled()
expect(pane.isDestroyed()).toBe true

describe "::destroy()", ->
[container, pane1, pane2] = []
Expand Down
32 changes: 3 additions & 29 deletions src/pane.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,8 @@ class Pane extends Model

saveError = (error) =>
if error
chosen = atom.confirm
message: @getSaveErrorMessage(error)
chosen = @applicationDelegate.confirm
message: @getMessageForErrorCode(error)
detailedMessage: "Your changes will be lost if you close this item without saving."
buttons: ["Save as", "Cancel", "Don't save"]
switch chosen
Expand Down Expand Up @@ -657,7 +657,7 @@ class Pane extends Model
nextAction?()
catch error
if nextAction
nextAction()
nextAction(error)
else
@handleSaveError(error, item)

Expand Down Expand Up @@ -851,7 +851,6 @@ class Pane extends Model
return false unless @promptToSaveItem(item)
true

# <<<<<<< HEAD
handleSaveError: (error, item) ->
itemPath = error.path ? item?.getPath?()
addWarningWithPath = (message, options) =>
Expand All @@ -868,31 +867,6 @@ class Pane extends Model
else if errorMatch = /ENOTDIR, not a directory '([^']+)'/.exec(error.message)
fileName = errorMatch[1]
@notificationManager.addWarning("Unable to save file: A directory in the path '#{fileName}' could not be written to")
# =======
# # Translate an error object to a human readable string
# getSaveErrorMessage: (error) ->
# if error.code is 'EISDIR' or error.message.endsWith('is a directory')
# "Unable to save file: #{error.message}."
# else if error.code is 'EACCES' and error.path?
# "Unable to save file: Permission denied '#{error.path}'."
# else if error.code in ['EPERM', 'EBUSY', 'UNKNOWN', 'EEXIST'] and error.path?
# "Unable to save file '#{error.path}': #{error.message}."
# else if error.code is 'EROFS' and error.path?
# "Unable to save file: Read-only file system '#{error.path}'."
# else if error.code is 'ENOSPC' and error.path?
# "Unable to save file: No space left on device '#{error.path}'."
# else if error.code is 'ENXIO' and error.path?
# "Unable to save file: No such device or address '#{error.path}'."
# else if errorMatch = /ENOTDIR, not a directory '([^']+)'/.exec(error.message)
# fileName = errorMatch[1]
# "Unable to save file: A directory in the path '#{fileName}' could not be written to."
#
# # Display a popup warning to the user
# handleSaveError: (error) ->
# errorMessage = Pane::getSaveErrorMessage(error)
# if errorMessage?
# atom.notifications.addWarning(errorMessage)
# >>>>>>> b5c9a90ae00ec44e91782b0d6e30be7ca2fea11c
else
throw error

Expand Down

0 comments on commit 6988546

Please sign in to comment.