Permalink
Browse files

support for absolute paths

  • Loading branch information...
1 parent 5912c90 commit 97d03ec2764994749904604cc0d3aa96bd3f8e87 @eldargab committed Sep 12, 2012
Showing with 19 additions and 10 deletions.
  1. +13 −10 lib/fake-fs.js
  2. +6 −0 test/fake-fs.js
View
@@ -1,6 +1,11 @@
var stat = require('./stat')
-var normalize = require('path').normalize
-var join = require('path').join
+var PATH = require('path')
+var normalize = PATH.normalize
+var join = PATH.join
+
+function resolve (p) {
+ return PATH.resolve(p).replace('\\', '/') // Windows support
+}
function FsError (code) {
var err = new Error(code)
@@ -16,33 +21,33 @@ function Fs (paths) {
}
Fs.prototype.dir = function (path, opts) {
- return this._add(path, new stat.Dir(opts))
+ return this._add(resolve(path), new stat.Dir(opts))
}
Fs.prototype.file = function (path, content, encoding) {
- return this._add(path, new stat.File(content, encoding))
+ return this._add(resolve(path), new stat.File(content, encoding))
}
Fs.prototype.at = function (path) {
return {
_fs: this,
dir: function (p) {
- p = join(path, p).replace('\\', '/')
+ p = join(path, p)
this._fs.dir.apply(this._fs, arguments)
return this
},
file: function (p) {
- p = join(path, p).replace('\\', '/')
+ p = join(path, p)
this._fs.file.apply(this._fs, arguments)
return this
}
}
}
Fs.prototype._add = function (path, item) {
- var segs = path.split('/')
+ var segs = path.split('/'); segs.shift()
var dir = this.root
for (var i = 0; i < segs.length - 1; i++) {
dir = dir.childs[segs[i]] || (dir.childs[segs[i]] = new stat.Dir)
@@ -55,10 +60,8 @@ Fs.prototype._add = function (path, item) {
}
Fs.prototype._itemAt = function (path) {
- path = normalize(path).replace('\\', '/') // windows support
- var segs = path.split('/')
+ var segs = resolve(path).split('/'); segs.shift()
var item = this.root
- if (segs[0] == '.') return item
for (var i = 0; i < segs.length; i++) {
item = item.childs && item.childs[segs[i]]
if (!item) return
View
@@ -106,6 +106,12 @@ describe('Fake FS', function () {
fs.stat('undefined', cb)
cb.error('ENOENT')
})
+
+ it('Should support absolute paths', function () {
+ fs.dir('a')
+ fs.stat(process.cwd(), cb)
+ cb.result().should.equal(fs.statSync('.'))
+ })
})
describe('.readdir()', function () {

0 comments on commit 97d03ec

Please sign in to comment.