Permalink
Browse files

adding rmdir

  • Loading branch information...
1 parent f8694dc commit 5f7ab5cc0bc546db2c7fe310359723cd7c56d4a9 @tgeorgiev tgeorgiev committed Jan 24, 2013
Showing with 44 additions and 1 deletion.
  1. +15 −1 lib/fake-fs.js
  2. +29 −0 test/fake-fs.js
View
@@ -3,6 +3,7 @@ var PATH = require('path')
var normalize = PATH.normalize
var join = PATH.join
var dirname = PATH.dirname
+var basename = PATH.basename
function resolve (p) {
return PATH.resolve(p).replace(/\\/g, '/') // Windows support
@@ -106,7 +107,20 @@ Fs.prototype.mkdirSync = function (dir, mode) {
this.dir(dir)
}
-;['readdir', 'stat'].forEach(function (meth) {
+Fs.prototype.rmdirSync = function (path) {
+ if (!this.existsSync(path)) throw FsError('ENOENT');
+
+ var item = this._get(path);
+ if (!item.isDirectory()) throw FsError('ENOTDIR');
+
+ var parent = this._get(dirname(path));
+ if (!parent.isDirectory()) throw FsError('ENOTDIR');
+
+ var itemName = basename(path);
+ delete parent.childs[itemName];
+}
+
+;['readdir', 'stat', 'rmdir'].forEach(function (meth) {
var sync = meth + 'Sync'
Fs.prototype[meth] = function (p, cb) {
var res, err
View
@@ -225,6 +225,35 @@ describe('Fake FS', function () {
})
})
+ describe('.rmdir()', function () {
+ it('Should remove an existing direcory', function () {
+ fs.dir('a/b')
+ fs.rmdirSync('a/b')
+ fs.existsSync('a/b').should.be.false
+ })
+
+ it('Should remove an existing direcory, its subdirectories and files', function () {
+ fs.dir('a/b/c')
+ fs.file('a/b/file.txt')
+
+ fs.rmdirSync('a/b')
+
+ fs.existsSync('a/b/c').should.be.false
+ fs.existsSync('a/b/file.txt').should.be.false
+ fs.existsSync('a/b').should.be.false
+
+ fs.existsSync('a').should.be.true
+ })
+
+ it('Should throw an ENOTDIR error on file', function () {
+ fs.file('a/file.txt')
+
+ fs.rmdir('a/file.txt', cb)
+
+ cb.error('ENOTDIR')
+ })
+ })
+
describe('.readFile()', function () {
it('Should read file contents', function () {
var content = new Buffer([1, 2, 3])

0 comments on commit 5f7ab5c

Please sign in to comment.