Permalink
Browse files

Correct sync behavior. Errors are thrown, not returned

  • Loading branch information...
1 parent 74aa4d3 commit dfb42c711972a1db0caec969a0be7e61e11790e5 @isaacs isaacs committed Mar 28, 2012
Showing with 30 additions and 22 deletions.
  1. +6 −8 index.js
  2. +12 −7 test/sync.js
  3. +12 −7 test/umask_sync.js
View
@@ -46,28 +46,26 @@ mkdirP.sync = function sync (p, mode) {
if (typeof mode === 'string') mode = parseInt(mode, 8);
p = path.resolve(p);
-
+
try {
- fs.mkdirSync(p, mode)
+ fs.mkdirSync(p, mode);
}
catch (err0) {
switch (err0.code) {
case 'ENOENT' :
- var err1 = sync(path.dirname(p), mode)
- if (err1) throw err1;
- else return sync(p, mode);
+ sync(path.dirname(p), mode);
+ sync(p, mode);
break;
-
+
case 'EEXIST' :
var stat;
try {
stat = fs.statSync(p);
}
catch (err1) {
- throw err0
+ throw err0;
}
if (!stat.isDirectory()) throw err0;
- else return null;
break;
default :
throw err0
View
@@ -8,12 +8,17 @@ test('sync', function (t) {
var x = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
var y = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
var z = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
-
+
var file = '/tmp/' + [x,y,z].join('/');
-
- var err = mkdirp.sync(file, 0755);
- if (err) t.fail(err);
- else path.exists(file, function (ex) {
+
+ try {
+ mkdirp.sync(file, 0755);
+ } catch (err) {
+ t.fail(err);
+ return t.end();
+ }
+
+ path.exists(file, function (ex) {
if (!ex) t.fail('file not created')
else fs.stat(file, function (err, stat) {
if (err) t.fail(err)
@@ -22,6 +27,6 @@ test('sync', function (t) {
t.ok(stat.isDirectory(), 'target not a directory');
t.end();
}
- })
- })
+ });
+ });
});
View
@@ -8,12 +8,17 @@ test('umask sync modes', function (t) {
var x = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
var y = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
var z = Math.floor(Math.random() * Math.pow(16,4)).toString(16);
-
+
var file = '/tmp/' + [x,y,z].join('/');
-
- var err = mkdirp.sync(file);
- if (err) t.fail(err);
- else path.exists(file, function (ex) {
+
+ try {
+ mkdirp.sync(file, 0755);
+ } catch (err) {
+ t.fail(err);
+ return t.end();
+ }
+
+ path.exists(file, function (ex) {
if (!ex) t.fail('file not created')
else fs.stat(file, function (err, stat) {
if (err) t.fail(err)
@@ -22,6 +27,6 @@ test('umask sync modes', function (t) {
t.ok(stat.isDirectory(), 'target not a directory');
t.end();
}
- })
- })
+ });
+ });
});

0 comments on commit dfb42c7

Please sign in to comment.