Permalink
Browse files

Add fs.mkdirSync()

  • Loading branch information...
1 parent 9601fa7 commit f46e991e42dd075d8d596ab2e825518a0275de80 @vojtajina committed Oct 1, 2012
Showing with 35 additions and 11 deletions.
  1. +25 −11 lib/fs.js
  2. +10 −0 test/fs.spec.coffee
View
@@ -135,22 +135,36 @@ var Mock = function(structure) {
};
this.mkdir = function(directory, callback) {
- predictableNextTick(function() {
- var pointer = getPointer(path.dirname(directory), structure);
- var baseName = path.basename(directory);
+ var mkdirSync = this.mkdirSync;
+ var error = null;
- if (pointer && typeof pointer === 'object' && !(pointer instanceof File)) {
- pointer[baseName] = {};
- callback(null);
- } else {
- var error = new Error(util.format('ENOENT, mkdir "%s"', directory));
- error.code = 'ENOENT';
- error.errno = 34;
- callback(error);
+ predictableNextTick(function() {
+ try {
+ mkdirSync(directory);
+ } catch (e) {
+ error = e;
}
+
+ callback(error);
});
};
+ this.mkdirSync = function(directory) {
+ var pointer = getPointer(path.dirname(directory), structure);
+ var baseName = path.basename(directory);
+
+ if (pointer && typeof pointer === 'object' && !(pointer instanceof File)) {
+ pointer[baseName] = {};
+ return;
+ }
+
+ var error = new Error(util.format('ENOENT, mkdir "%s"', directory));
+ error.code = 'ENOENT';
+ error.errno = 34;
+
+ throw error;
+ };
+
this.readFile = function(path, encoding, callback) {
var readFileSync = this.readFileSync;
callback = callback || encoding;
View
@@ -204,6 +204,16 @@ describe 'fs', ->
# ===========================================================================
+ # fs.mkdirSync()
+ # ===========================================================================
+ describe 'mkdirSync', ->
+
+ it 'should create directory', ->
+ fs.mkdirSync '/home/new'
+ expect(fs.statSync('/home/new').isDirectory()).toBe true
+
+
+ # ===========================================================================
# fs.readFile
# ===========================================================================
describe 'readFile', ->

0 comments on commit f46e991

Please sign in to comment.