From 629c1ef239b17542e08214ad9a79805f3b08ecec Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 4 Jan 2012 19:07:55 -0700 Subject: [PATCH] If passed a directory path, fs.directory returns the path it is given (not the parent dir) --- spec/stdlib/fs-spec.coffee | 11 +++++++++++ src/stdlib/fs.coffee | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/spec/stdlib/fs-spec.coffee b/spec/stdlib/fs-spec.coffee index d7854d97c..66ce34e1b 100644 --- a/spec/stdlib/fs-spec.coffee +++ b/spec/stdlib/fs-spec.coffee @@ -1,6 +1,17 @@ fs = require 'fs' 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/') + + 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/') + + describe ".async", -> describe ".listFiles(directoryPath, recursive)", -> directoryPath = null diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index 3d4e0ae6f..0950232e9 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -25,7 +25,11 @@ module.exports = # Return the dirname of the given path. That is the path with any trailing # non-directory component removed. directory: (path) -> - @absolute(path).replace(new RegExp(@base(path) + '$'), '') + absPath = @absolute(path) + if @isDirectory(absPath) + absPath.replace(/\/?$/, '/') + else + absPath.replace(new RegExp("/#{@base(path)}$"), '/') # Returns true if the file specified by path exists exists: (path) ->