Skip to content
Browse files

refactor Proxy

  • Loading branch information...
1 parent b2d42a9 commit 3791429b64295a98c4193103b8225fa222e18533 @eldargab committed
Showing with 30 additions and 18 deletions.
  1. +27 −18 lib/fake-fs.js
  2. +3 −0 test/fake-fs.js
View
45 lib/fake-fs.js
@@ -29,24 +29,6 @@ Fs.prototype.file = function (path, 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)
- this._fs.dir.apply(this._fs, arguments)
- return this
- },
-
- file: function (p) {
- p = join(path, p)
- this._fs.file.apply(this._fs, arguments)
- return this
- }
- }
-}
-
Fs.prototype._add = function (path, item) {
var segs = path.split('/'); segs.shift()
var dir = this.root
@@ -168,3 +150,30 @@ Fs.prototype.mkdir = function (dir, mode, cb) {
}
cb && cb(err, res)
}
+
+
+Fs.prototype.at = function (path) {
+ return new Proxy(this, path)
+}
+
+function Proxy (fs, path) {
+ this.fs = fs
+ this.path = path
+}
+
+Proxy.prototype.dir = function (p) {
+ p = join(this.path, p)
+ this.fs.dir.apply(this.fs, arguments)
+ return this
+}
+
+Proxy.prototype.file = function (p) {
+ p = join(this.path, p)
+ this.fs.file.apply(this.fs, arguments)
+ return this
+}
+
+Proxy.prototype.at = function (p) {
+ p = join(this.path, p)
+ return this.fs.at.apply(this.fs, arguments)
+}
View
3 test/fake-fs.js
@@ -96,8 +96,11 @@ describe('Fake FS', function () {
fs.at('home')
.file('.gitignore')
.dir('.local')
+ .at('eldar')
+ .dir('dev')
fs.statSync('home/.gitignore').isFile().should.be.true
fs.statSync('home/.local').isDirectory().should.be.true
+ fs.statSync('home/eldar/dev').isDirectory().should.be.true
})
})

0 comments on commit 3791429

Please sign in to comment.
Something went wrong with that request. Please try again.