Permalink
Browse files

implement .mkdir()

  • Loading branch information...
eldargab committed Sep 12, 2012
1 parent 449f42e commit ddf20c5df71eec80d2a1b09c2f8c19ba11b94534
Showing with 54 additions and 1 deletion.
  1. +21 −0 lib/fake-fs.js
  2. +33 −1 test/fake-fs.js
View
@@ -2,6 +2,7 @@ var stat = require('./stat')
var PATH = require('path')
var normalize = PATH.normalize
var join = PATH.join
+var dirname = PATH.dirname
function resolve (p) {
return PATH.resolve(p).replace('\\', '/') // Windows support
@@ -96,6 +97,13 @@ Fs.prototype.readFileSync = function (filename, encoding) {
return item.read(encoding)
}
+Fs.prototype.mkdirSync = function (dir, mode) {
+ if (this.existsSync(dir)) throw FsError('EEXIST')
+ var parent = this._get(dirname(dir))
+ if (!parent.isDirectory()) throw FsError('ENOTDIR')
+ this.dir(dir)
+}
+
;['readdir', 'stat'].forEach(function (meth) {
var sync = meth + 'Sync'
Fs.prototype[meth] = function (p, cb) {
@@ -126,3 +134,16 @@ Fs.prototype.readFile = function (filename, encoding, cb) {
Fs.prototype.exists = function (path, cb) {
cb && cb(this.existsSync(path))
}
+
+Fs.prototype.mkdir = function (dir, mode, cb) {
+ if (typeof mode == 'function') {
+ cb = mode
+ }
+ var res, err
+ try {
+ res = this.mkdirSync(dir)
+ } catch (e) {
+ err = e
+ }
+ cb && cb(err, res)
+}
View
@@ -2,19 +2,22 @@ var should = require('should')
var Fs = require('..')
function Cb () {
- var err, res
+ var err, res, called = 0
function cb (_err, _res) {
err = _err
res = _res
+ called++
}
cb.result = function () {
+ called.should.equal(1)
should.not.exist(err)
return res
}
cb.error = function (code) {
+ called.should.equal(1)
should.exist(err)
err.should.have.property('code').equal(code)
}
@@ -150,6 +153,35 @@ describe('Fake FS', function () {
})
})
+ describe('.mkdir()', function () {
+ it('Should create dir', function () {
+ fs.dir('.').mkdir('a', cb)
+ cb.result()
+ fs.statSync('a').isDirectory().should.be.true
+ })
+
+ it('Should ignore mode', function () {
+ fs.dir('.').mkdir('a', 0777, cb)
+ cb.result()
+ fs.statSync('a').isDirectory().should.be.true
+ })
+
+ it('Should throw EEXIST on existing item', function () {
+ fs.dir('a').mkdir('a', cb)
+ cb.error('EEXIST')
+ })
+
+ it('Should throw ENOENT on non-existent parent', function () {
+ fs.mkdir('a', cb)
+ cb.error('ENOENT')
+ })
+
+ it('Should throw ENOTDIR on non-dir parent', function () {
+ fs.file('a').mkdir('a/b', cb)
+ cb.error('ENOTDIR')
+ })
+ })
+
describe('.readFile()', function () {
it('Should read file contents', function () {
var content = new Buffer([1, 2, 3])

0 comments on commit ddf20c5

Please sign in to comment.