From ca1f66d197527e9e76bf5d84128f7c1d7bc833df Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 7 Jan 2015 16:01:23 -0800 Subject: [PATCH] Post a notification when the user cannot access a file --- spec/workspace-spec.coffee | 20 ++++++++++++++++++++ src/project.coffee | 5 +++++ src/workspace.coffee | 2 ++ 3 files changed, 27 insertions(+) diff --git a/spec/workspace-spec.coffee b/spec/workspace-spec.coffee index bb5c662ba..2c232c956 100644 --- a/spec/workspace-spec.coffee +++ b/spec/workspace-spec.coffee @@ -297,6 +297,26 @@ describe "Workspace", -> expect(notification.getMessage()).toContain 'No such file' expect(notification.getMessage()).toContain 'not-a-file.md' + describe "when the user does not have access to the file", -> + beforeEach -> + spyOn(fs, 'openSync').andCallFake (path)-> + error = new Error("EACCES, permission denied '#{path}'") + error.path = path + error.code = 'EACCES' + throw error + + it "creates a notification", -> + waitsForPromise -> + workspace.open('file1', workspace.getActivePane()) + + runs -> + expect(notificationSpy).toHaveBeenCalled() + notification = notificationSpy.mostRecentCall.args[0] + expect(notification.getType()).toBe 'warning' + expect(notification.getMessage()).toContain 'Permission denied' + expect(notification.getMessage()).toContain 'file1' + + describe "::reopenItem()", -> it "opens the uri associated with the last closed pane that isn't currently open", -> pane = workspace.getActivePane() diff --git a/src/project.coffee b/src/project.coffee index f972c2695..d822cbd76 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -218,6 +218,11 @@ class Project extends Model # Returns a promise that resolves to an {TextEditor}. open: (filePath, options={}) -> filePath = @resolvePath(filePath) + + # Make sure we have permissions + fileDescriptor = fs.openSync(filePath, 'r+') + fs.closeSync(fileDescriptor) + @bufferForPath(filePath).then (buffer) => @buildEditorForBuffer(buffer, options) diff --git a/src/workspace.coffee b/src/workspace.coffee index bd6aee8fa..14854a81e 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -455,6 +455,8 @@ class Workspace extends Model atom.notifications.addWarning(error.message) else if error.code is 'ENOENT' and error.path? atom.notifications.addWarning("No such file '#{error.path}'") + else if error.code is 'EACCES' and error.path? + atom.notifications.addWarning("Permission denied '#{error.path}'") return Q() Q(item)