From acae9d63cac7b5abd1c338f2a5d306d91d091a67 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 1 May 2012 08:41:35 -0700 Subject: [PATCH] fs.directory does not append a trailing '/' to directories --- spec/app/project-spec.coffee | 15 +++++++++++---- spec/app/root-view-spec.coffee | 8 ++++---- spec/extensions/tree-view-spec.coffee | 4 ++-- spec/stdlib/fs-spec.coffee | 6 +++--- src/app/project.coffee | 2 +- src/extensions/tree-view/add-dialog.coffee | 2 +- src/stdlib/fs.coffee | 4 ++-- 7 files changed, 24 insertions(+), 17 deletions(-) diff --git a/spec/app/project-spec.coffee b/spec/app/project-spec.coffee index a5eababb1..7088bf070 100644 --- a/spec/app/project-spec.coffee +++ b/spec/app/project-spec.coffee @@ -28,17 +28,17 @@ describe "Project", -> describe "when passed the path to a buffer that has already been opened", -> it "returns the previously opened buffer", -> buffer = project.open(absolutePath) - newBufferHandler.reset() - expect(project.open(absolutePath)).toBe buffer + newBufferHandler.reset() + expect(project.open(absolutePath)).toBe buffer expect(project.open('a')).toBe buffer expect(newBufferHandler).not.toHaveBeenCalled() - + describe "when not passed a path", -> it "returns a new buffer and emits a new-buffer event", -> buffer = project.open() expect(buffer.path).toBeUndefined() expect(newBufferHandler).toHaveBeenCalledWith(buffer) - + describe ".getFilePaths()", -> it "returns a promise which resolves to a list of all file paths in the project, recursively", -> expectedPaths = (path.replace(project.path, '') for path in fs.listTree(project.path) when fs.isFile path) @@ -53,3 +53,10 @@ describe "Project", -> expect(project.resolve('a')).toBe absolutePath expect(project.resolve(absolutePath + '/../a')).toBe absolutePath expect(project.resolve('a/../a')).toBe absolutePath + + describe ".relativize(path)", -> + it "returns an relative path based on the project's root", -> + absolutePath = require.resolve('fixtures/dir') + expect(project.relativize(fs.join(absolutePath, "b"))).toBe "b" + expect(project.relativize(fs.join(absolutePath, "b/file.coffee"))).toBe "b/file.coffee" + expect(project.relativize(fs.join(absolutePath, "file.coffee"))).toBe "file.coffee" diff --git a/spec/app/root-view-spec.coffee b/spec/app/root-view-spec.coffee index fbc58a58f..a79ddbf14 100644 --- a/spec/app/root-view-spec.coffee +++ b/spec/app/root-view-spec.coffee @@ -29,7 +29,7 @@ describe "RootView", -> describe "when pathToOpen references a directory", -> it "creates a project for the directory and sets the document.title, but does not open an editor", -> - path = require.resolve 'fixtures/dir/' + path = require.resolve 'fixtures/dir' rootView = new RootView(pathToOpen: path) rootView.focus() @@ -339,7 +339,7 @@ describe "RootView", -> expect(rootView.fileFinder.pathList.children('li').length).toBe paths.length for path in paths - relativePath = path.replace(project.path, '') + relativePath = project.relativize(path) expect(rootView.fileFinder.pathList.find("li:contains(#{relativePath}):not(:contains(#{project.path}))")).toExist() describe "when there is no project", -> @@ -362,7 +362,7 @@ describe "RootView", -> rootView.fileFinder.trigger 'move-down' selectedLi = rootView.fileFinder.find('li:eq(1)') - expectedPath = project.path + selectedLi.text() + expectedPath = fs.join(project.path, selectedLi.text()) expect(editor1.buffer.path).not.toBe expectedPath expect(editor2.buffer.path).not.toBe expectedPath @@ -409,7 +409,7 @@ describe "RootView", -> rootView = new RootView expect(rootView.project.path).toBeUndefined() rootView.activeEditor().buffer.saveAs('/tmp/ignore-me') - expect(rootView.project.path).toBe '/tmp/' + expect(rootView.project.path).toBe '/tmp' describe "when editors are focused", -> it "triggers 'active-editor-path-change' events if the path of the active editor actually changes", -> diff --git a/spec/extensions/tree-view-spec.coffee b/spec/extensions/tree-view-spec.coffee index a9cb7d050..02f791920 100644 --- a/spec/extensions/tree-view-spec.coffee +++ b/spec/extensions/tree-view-spec.coffee @@ -312,10 +312,10 @@ describe "TreeView", -> describe "when a directory is selected", -> it "opens an add dialog with the directory's path populated", -> - treeView.trigger "tree-view:cancel" + addDialog.cancel() dirView.click() - console.log 'boom' treeView.trigger "tree-view:add" + addDialog = rootView.find(".add-dialog").view() expect(addDialog).toExist() expect(addDialog.prompt.text()).toBeTruthy() diff --git a/spec/stdlib/fs-spec.coffee b/spec/stdlib/fs-spec.coffee index 500281ead..4d82f6848 100644 --- a/spec/stdlib/fs-spec.coffee +++ b/spec/stdlib/fs-spec.coffee @@ -4,12 +4,12 @@ describe "fs", -> describe ".directory(path)", -> describe "when called with a file path", -> it "returns the path to the directory", -> - expect(fs.directory(require.resolve('fixtures/dir/a'))).toBe require.resolve('fixtures/dir/') + expect(fs.directory(require.resolve('fixtures/dir/a'))).toBe require.resolve('fixtures/dir') describe "when called with a directory path", -> it "return the path it was given", -> - expect(fs.directory(require.resolve('fixtures/dir'))).toBe require.resolve('fixtures/dir/') - expect(fs.directory(require.resolve('fixtures/dir/'))).toBe require.resolve('fixtures/dir/') + expect(fs.directory(require.resolve('fixtures/dir'))).toBe require.resolve('fixtures/dir') + expect(fs.directory(require.resolve('fixtures/dir/'))).toBe require.resolve('fixtures/dir') describe ".join(paths...)", -> it "concatenates the given paths with the directory seperator", -> diff --git a/src/app/project.coffee b/src/app/project.coffee index 5cf7292a0..9b2a42318 100644 --- a/src/app/project.coffee +++ b/src/app/project.coffee @@ -38,7 +38,7 @@ class Project fs.absolute filePath relativize: (fullPath) -> - fullPath.replace(@path, "") + fullPath.replace(@path, "").replace(/^\//, '') bufferWithId: (id) -> return buffer for buffer in @buffers when buffer.id == id diff --git a/src/extensions/tree-view/add-dialog.coffee b/src/extensions/tree-view/add-dialog.coffee index cdb6e6a3c..69009dc81 100644 --- a/src/extensions/tree-view/add-dialog.coffee +++ b/src/extensions/tree-view/add-dialog.coffee @@ -21,7 +21,7 @@ class AddDialog extends View @miniEditor.on 'focusout', => @remove() directoryPath = if fs.isFile(@path) then fs.directory(@path) else @path - relativePath = @rootView.project.relativize(directoryPath) + relativePath = @rootView.project.relativize(directoryPath) + '/' @miniEditor.setText(relativePath) confirm: -> diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index c24631942..b595b6ec4 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -22,9 +22,9 @@ module.exports = # non-directory component removed. directory: (path) -> if @isDirectory(path) - path.replace(/\/?$/, '/') + path.replace(/\/?$/, '') else - path.replace(new RegExp("/#{@base(path)}$"), '/') + path.replace(new RegExp("/#{@base(path)}$"), '') # Returns true if the file specified by path exists exists: (path) ->