Permalink
Browse files

adding unlink

  • Loading branch information...
1 parent 5f7ab5c commit e91eee1a33636c31b038436dcb3386c39d3a5cea @tgeorgiev tgeorgiev committed Jan 24, 2013
Showing with 32 additions and 1 deletion.
  1. +14 −1 lib/fake-fs.js
  2. +18 −0 test/fake-fs.js
View
@@ -120,7 +120,20 @@ Fs.prototype.rmdirSync = function (path) {
delete parent.childs[itemName];
}
-;['readdir', 'stat', 'rmdir'].forEach(function (meth) {
+Fs.prototype.unlinkSync = function (path) {
+ if (!this.existsSync(path)) throw FsError('ENOENT');
+
+ var item = this._get(path);
+ if (item.isDirectory()) throw FsError('EISDIR');
+
+ var parent = this._get(dirname(path));
+ if (!parent.isDirectory()) throw FsError('ENOTDIR');
+
+ var itemName = basename(path);
+ delete parent.childs[itemName];
+}
+
+;['readdir', 'stat', 'rmdir', 'unlink'].forEach(function (meth) {
var sync = meth + 'Sync'
Fs.prototype[meth] = function (p, cb) {
var res, err
View
@@ -254,6 +254,24 @@ describe('Fake FS', function () {
})
})
+ describe('.unlink()', function () {
+ it('Should remove an existing file', function () {
+ fs.file('a/file.txt')
+
+ fs.unlinkSync('a/file.txt')
+
+ fs.existsSync('a/file.txt').should.be.false
+ })
+
+ it('Should throw an EISDIR error on directory', function () {
+ fs.dir('a/b')
+
+ fs.unlink('a/b', cb)
+
+ cb.error('EISDIR')
+ })
+ })
+
describe('.readFile()', function () {
it('Should read file contents', function () {
var content = new Buffer([1, 2, 3])

0 comments on commit e91eee1

Please sign in to comment.