Permalink
Browse files

Fixed a bug in directory() for empty relative directories. Added tests.

  • Loading branch information...
Kris Kowal
Kris Kowal committed Aug 30, 2011
1 parent 953722a commit 3ed1e567b7c5b4a58d55fddf98ed39e0293f4578
Showing with 72 additions and 4 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 CHANGES
  3. +19 −3 fs-boot.js
  4. +4 −1 package.json
  5. +45 −0 test/directory.js
View
@@ -0,0 +1 @@
+node_modules
View
@@ -1,4 +1,7 @@
+0.0.6
+ - fixed bug in directory for relative directories
+
0.0.4
- removed superfluous "q" package dependency.
View
@@ -1,3 +1,4 @@
+(function (exports) {
// -- kriskowal Kris Kowal Copyright (C) 2009-2010 MIT License
// -- tlrobinson Tom Robinson TODO
@@ -25,7 +26,7 @@ var regExpEscape = function (str) {
var os = typeof process !== "undefined"?
process.platform :
- require("narwhal/engine").os;
+ "unknown"
var isWindows = /\bwind?(nt|ows)\b/i.test(os);
/**
@@ -265,11 +266,25 @@ exports.root = function (path) {
* @returns {String} the parent directory of the given path.
*/
exports.directory = function (path) {
+ path = exports.normal(path);
+ var absolute = exports.isAbsolute(path);
var parts = exports.split(path);
// XXX needs to be sensitive to the root for
// Windows compatibility
- parts.pop();
- return parts.join(exports.SEPARATOR) || exports.ROOT;
+ console.log(parts);
+ if (parts.length) {
+ if (parts[parts.length - 1] == "..") {
+ parts.push("..");
+ } else {
+ parts.pop();
+ }
+ } else {
+ parts.unshift("..");
+ }
+ return parts.join(exports.SEPARATOR) || (
+ exports.isRelative(path) ?
+ "" : exports.ROOT
+ );
};
/**
@@ -301,3 +316,4 @@ exports.extension = function (path) {
return index <= 0 ? "" : path.substring(index);
};
+})(typeof exports !== "undefined" ? exports : FS_BOOT = {});
View
@@ -1,7 +1,7 @@
{
"name": "fs-boot",
+ "version": "0.0.6",
"description": "Pure JavaScript implementation of common file-system API components",
- "version": "0.0.5",
"homepage": "http://github.com/kriskowal/fs-boot/",
"author": "Kris Kowal <kris@cixar.com> (http://github.com/kriskowal/)",
"bugs": {
@@ -19,6 +19,9 @@
"type": "git",
"url": "http://github.com/kriskowal/fs-boot.git"
},
+ "devDependences": {
+ "test": "0.3.0"
+ },
"engines": {
"node": ">=0.2.0"
}
View
@@ -0,0 +1,45 @@
+
+var FS = require("fs-boot");
+
+[
+ {
+ "from": "foo",
+ "to": ""
+ },
+ {
+ "from": "",
+ "to": ".."
+ },
+ {
+ "from": ".",
+ "to": ".."
+ },
+ {
+ "from": "..",
+ "to": "../.."
+ },
+ {
+ "from": "../foo",
+ "to": ".."
+ },
+ {
+ "from": "/foo/bar",
+ "to": "/foo"
+ },
+ {
+ "from": "/foo",
+ "to": "/"
+ },
+ {
+ "from": "/",
+ "to": "/"
+ }
+].forEach(function (test) {
+ exports['test ' + test.from] = function (assert) {
+ assert.equal(FS.directory(test.from), test.to, 'ok');
+ };
+});
+
+if (require.main == module)
+ require("test").run(exports);
+

0 comments on commit 3ed1e56

Please sign in to comment.