Permalink
Browse files

Merge master

  • Loading branch information...
janjongboom committed Jun 1, 2012
2 parents a6f719d + 372663b commit 315a480f6808017ae9146e87e887ebe33325d0bb
@@ -294,12 +294,11 @@ require("util").inherits(RevisionsPlugin, Plugin);
};
if (!exists) {
- fs.mkdir(parentDir, "0755", createRevisionsFile);
+ fs.mkdirP(parentDir, "0755", createRevisionsFile);
}
else {
createRevisionsFile();
}
-
});
});
}
@@ -29,11 +29,8 @@ module.exports = function (projectDir, unixId) {
* Wrapper around Path.exists
*/
this.exists = function (path, callback) {
- var self = this;
-
callback = this.$normalizeCallback(callback);
-
- self.$resolvePath(path, function (err, path) {
+ this.$resolvePath(path, function (err, path) {
if (err) return callback(err);
Path.exists(path, function (exists) {
@@ -169,4 +166,72 @@ module.exports = function (projectDir, unixId) {
this.mkdir = function () {
return this.$chownWrapper.call(this, "mkdir", arguments);
};
-};
+
+ /**
+ * Equivalent to "mkdir - p"
+ * Adapted from https://github.com/substack/node-mkdirp
+ */
+ this.mkdirP = function mkdirP(path, mode, f, made) {
+ var self = this;
+ if (typeof mode === "function" || mode === undefined) {
+ f = mode;
+ mode = 0777 & (~process.umask());
+ }
+
+ if (!made) {
+ made = null;
+ }
+
+ var cb = f || function () {};
+ if (typeof mode === "string") {
+ mode = parseInt(mode, 8);
+ }
+
+ self.mkdir(path, mode, function (er) {
+ if (!er) {
+ made = made || path;
+ return cb(null, made);
+ }
+
+ switch (er.code) {
+ case "ENOENT":
+ mkdirP.call(self, Path.dirname(path), mode, function (er, made) {
+ if (er) {
+ cb(er, made);
+ }
+ else {
+ mkdirP.call(self, path, mode, cb, made);
+ }
+ });
+ break;
+
+ case "EROFS":
+ // a read-only file system.
+ // However, the dir could already exist, in which case
+ // the EROFS error will be obscuring a EEXIST!
+ // Fallthrough to that case.
+ case "EEXIST":
+ Fs.stat(path, function (er2, stat) {
+ // if the stat fails, then that's super weird.
+ // let the original EEXIST be the failure reason.
+ if (er2 || !stat.isDirectory()) {
+ cb(er, made);
+ }
+ else cb(null, made);
+ });
+ break;
+
+ default:
+ cb(er, made);
+ break;
+ }
+ });
+ };
+
+ /**
+ * Wrapper around unlink
+ */
+ this.unlink = function () {
+ return this.$simpleWrapper.call(this, "unlink", arguments);
+ };
+};
@@ -167,7 +167,33 @@ module.exports = {
next();
});
- }
+ },
+
+ "test mkdirP should chown": function (next) {
+ var mkdir = Fs.mkdir = sinon.stub().callsArgWith(2, null);
+ var chown = Fs.chown = sinon.stub().callsArgWith(3, null);
+ this.fs.mkdirP("somefolder/andItsChildren/andItsGrandChildren", "0775", function (err) {
+ assert.equal(err, null);
+ sinon.assert.calledWith(chown, "/usr/jan/1299/somefolder/andItsChildren/andItsGrandChildren", 987, 987);
+
+ next();
+ });
+ },
+
+ "test mkdirP should actually create the dir": function (next) {
+ var fs = new SandboxFs(__dirname, null);
+
+ fs.mkdirP("folder/child/grandchild", "0775", function (err) {
+ assert.equal(err, null);
+
+ fs.exists("folder/child/grandchild", function (err, exists) {
+ assert.equal(err, null);
+ assert.equal(exists, true);
+
+ require("rimraf")(__dirname + "/folder", next);
+ });
+ });
+ }
};
-!module.parent && require("asyncjs").test.testcase(module.exports, "Sandbox.Fs").exec();
+!module.parent && require("asyncjs").test.testcase(module.exports, "Sandbox.Fs").exec();

0 comments on commit 315a480

Please sign in to comment.