From d181cdedbd49ab2ee32e69a8755127fd6ea1d7a7 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 8 May 2012 15:02:07 -0600 Subject: [PATCH] Handle errors when attempting to add a file from the TreeView --- spec/extensions/tree-view-spec.coffee | 68 ++++++++++++++++------- src/extensions/tree-view/tree-view.coffee | 18 ++++-- 2 files changed, 60 insertions(+), 26 deletions(-) diff --git a/spec/extensions/tree-view-spec.coffee b/spec/extensions/tree-view-spec.coffee index 2434f006d..7091bc3ac 100644 --- a/spec/extensions/tree-view-spec.coffee +++ b/spec/extensions/tree-view-spec.coffee @@ -43,7 +43,7 @@ describe "TreeView", -> expect(rootEntries.find('> .file:contains(sample.js)')).toExist() expect(rootEntries.find('> .file:contains(sample.txt)')).toExist() - describe "when the project has not path", -> + describe "when the project has no path", -> beforeEach -> treeView.deactivate() @@ -442,30 +442,56 @@ describe "TreeView", -> expect(treeView.find('.selected').text()).toBe fs.base(filePath) describe "when the path without a trailing '/' is changed and confirmed", -> - it "add a file, closes the dialog and selects the file in the tree-view", -> - newPath = fs.join(dirPath, "new-test-file.txt") - addDialog.miniEditor.insertText(fs.base(newPath)) - addDialog.trigger 'tree-view:confirm' - expect(fs.exists(newPath)).toBeTruthy() - expect(fs.isFile(newPath)).toBeTruthy() - expect(addDialog.parent()).not.toExist() - expect(rootView.activeEditor().buffer.path).toBe newPath + describe "when no file exists at that location", -> + it "add a file, closes the dialog and selects the file in the tree-view", -> + newPath = fs.join(dirPath, "new-test-file.txt") + addDialog.miniEditor.insertText(fs.base(newPath)) + addDialog.trigger 'tree-view:confirm' + expect(fs.exists(newPath)).toBeTruthy() + expect(fs.isFile(newPath)).toBeTruthy() + expect(addDialog.parent()).not.toExist() + expect(rootView.activeEditor().buffer.path).toBe newPath - waitsFor "tree view to be updated", -> - dirView.entries.find("> .file").length > 1 + waitsFor "tree view to be updated", -> + dirView.entries.find("> .file").length > 1 - runs -> - expect(treeView.find('.selected').text()).toBe fs.base(newPath) + runs -> + expect(treeView.find('.selected').text()).toBe fs.base(newPath) + + describe "when a file already exists at that location", -> + it "shows an error message and does not close the dialog", -> + newPath = fs.join(dirPath, "new-test-file.txt") + fs.write(newPath, '') + addDialog.miniEditor.insertText(fs.base(newPath)) + addDialog.trigger 'tree-view:confirm' + + expect(addDialog.prompt.text()).toContain 'Error' + expect(addDialog.prompt.text()).toContain 'already exists' + expect(addDialog.prompt).toHaveClass('error') + expect(addDialog.hasParent()).toBeTruthy() describe "when the path with a trailing '/' is changed and confirmed", -> - it "adds a directory and closes the dialog", -> - newPath = fs.join(dirPath, "new-dir") - addDialog.miniEditor.insertText("new-dir/") - addDialog.trigger 'tree-view:confirm' - expect(fs.exists(newPath)).toBeTruthy() - expect(fs.isDirectory(newPath)).toBeTruthy() - expect(addDialog.parent()).not.toExist() - expect(rootView.activeEditor().buffer.path).not.toBe newPath + describe "when no file or directory exists at the given path", -> + it "adds a directory and closes the dialog", -> + newPath = fs.join(dirPath, "new-dir") + addDialog.miniEditor.insertText("new-dir/") + addDialog.trigger 'tree-view:confirm' + expect(fs.exists(newPath)).toBeTruthy() + expect(fs.isDirectory(newPath)).toBeTruthy() + expect(addDialog.parent()).not.toExist() + expect(rootView.activeEditor().buffer.path).not.toBe newPath + + describe "when a or directory already exists at the given path", -> + it "shows an error message and does not close the dialog", -> + newPath = fs.join(dirPath, "new-dir") + fs.makeDirectory(newPath) + addDialog.miniEditor.insertText("new-dir/") + addDialog.trigger 'tree-view:confirm' + + expect(addDialog.prompt.text()).toContain 'Error' + expect(addDialog.prompt.text()).toContain 'already exists' + expect(addDialog.prompt).toHaveClass('error') + expect(addDialog.hasParent()).toBeTruthy() describe "when 'tree-view:cancel' is triggered on the add dialog", -> it "removes the dialog and focuses the tree view", -> diff --git a/src/extensions/tree-view/tree-view.coffee b/src/extensions/tree-view/tree-view.coffee index 042977116..bb5b6d64b 100644 --- a/src/extensions/tree-view/tree-view.coffee +++ b/src/extensions/tree-view/tree-view.coffee @@ -188,11 +188,19 @@ class TreeView extends View onConfirm: (relativePath) => endsWithDirectorySeperator = /\/$/.test(relativePath) path = @rootView.project.resolve(relativePath) - if endsWithDirectorySeperator - fs.makeDirectory(path) - else - fs.write(path, "") - @rootView.open(path) + try + if endsWithDirectorySeperator + fs.makeDirectory(path) + else + if fs.exists(path) + dialog.showError("Error: A file already exists at path '#{path}'. Try a different path:") + false + else + fs.write(path, "") + @rootView.open(path) + catch e + dialog.showError("Error: " + e.message + " Try a different path:") + return false @rootView.append(dialog)