Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1639 from ajaxorg/sandbox_rename_doesnt_resolve_s…

…econd_param

A rename should also resolve arg[1]
  • Loading branch information...
commit ded4d5c97a6f503e465ad2d1c4d32305a787fec6 2 parents 372663b + 315a480
@zefhemel zefhemel authored
View
90 plugins-server/cloud9.sandbox.fs/fs.js
@@ -2,29 +2,29 @@ var Path = require("path");
var Fs = require("fs");
module.exports = function (projectDir, unixId) {
-
+
this.$normalizeCallback = function (fn) {
if (typeof fn !== "function") {
return function () {};
}
-
+
return fn;
};
-
+
/**
* Resolves the path given a path relative to a project folder
*/
this.$resolvePath = function (relativePath, callback) {
var path = Path.join(projectDir, relativePath);
path = Path.normalize(path);
-
+
if (path.indexOf(projectDir) !== 0) {
return callback("Can't step out project dir... " + path);
}
-
+
callback(null, path);
};
-
+
/**
* Wrapper around Path.exists
*/
@@ -32,116 +32,134 @@ module.exports = function (projectDir, unixId) {
callback = this.$normalizeCallback(callback);
this.$resolvePath(path, function (err, path) {
if (err) return callback(err);
-
+
Path.exists(path, function (exists) {
callback(null, exists);
});
});
};
-
- this.$simpleWrapper = function (name, fnArgs) {
+
+ this.$simpleWrapper = function (name, fnArgs) {
var path = fnArgs[0];
var callback = this.$normalizeCallback(fnArgs[fnArgs.length - 1]);
-
+
this.$resolvePath(path, function (err, path) {
if (err) return callback(err);
-
+
// first copy the array with slice(0), then remove the first arg
var args = [].slice.call(fnArgs).slice(1);
Fs[name].apply(Fs, [path].concat(args));
- });
+ });
};
-
+
this.$chownWrapper = function (name, fnArgs) {
var path = fnArgs[0];
var callback = this.$normalizeCallback(fnArgs[fnArgs.length - 1]);
-
+
this.$resolvePath(path, function (err, path) {
if (err) return callback(err);
-
+
// first copy the array with slice(0), then remove the first arg
var args = [].slice.call(fnArgs).slice(1);
-
+
// pop the last argument cause we want to override it (if its a function of course)
var lastArg = args.pop();
if (typeof lastArg === "function") {
lastArg = function (err) {
if (err) return callback(err);
-
+
if (!unixId) {
return callback();
}
-
+
Fs.chown(path, unixId, unixId, function (err) {
if (err) return callback(err);
-
+
// @todo, do a chmod here?
-
+
callback();
});
};
}
-
+
Fs[name].apply(Fs, [path].concat(args).concat([lastArg]));
- });
+ });
};
-
+
/**
* Wrapper around fs.readFile
*/
this.readFile = function () {
return this.$simpleWrapper.call(this, "readFile", arguments);
};
-
+
/**
* Wrapper around fs.writeFile
*/
this.writeFile = function () {
- return this.$chownWrapper.call(this, "writeFile", arguments);
+ return this.$chownWrapper.call(this, "writeFile", arguments);
};
-
+
/**
* Wrapper around chmod
*/
this.chmod = function () {
return this.$simpleWrapper.call(this, "chmod", arguments);
};
-
+
/**
* Wrapper around rename
*/
this.rename = function () {
- return this.$simpleWrapper.call(this, "rename", arguments);
- };
-
+ var self = this;
+ var args = arguments;
+ var callback = self.$normalizeCallback(args[args.length - 1]);
+
+ // so the first argument will auto resolve
+ // the second parameter doesn't but it's required for a rename
+ if (args.length > 1 && typeof args[1] === "string") {
+ self.$resolvePath(args[1], function (err, renamePath) {
+ if (err) return callback(err);
+
+ // swap arg[1]
+ args[1] = renamePath;
+
+ self.$simpleWrapper.call(self, "rename", args);
+ });
+ }
+ else {
+ self.$simpleWrapper.call(self, "rename", args);
+ }
+ };
+
/**
* Wrapper around readdir
*/
this.readdir = function () {
- return this.$simpleWrapper.call(this, "readdir", arguments);
+ return this.$simpleWrapper.call(this, "readdir", arguments);
};
-
+
/**
* Wrapper around open
*/
this.open = function () {
- return this.$simpleWrapper.call(this, "open", arguments);
+ return this.$simpleWrapper.call(this, "open", arguments);
};
-
+
/**
* Wrapper around write
*/
this.write = function () {
Fs.write.apply(this, arguments);
};
-
+
/**
* Wrapper around close
*/
this.close = function () {
Fs.close.apply(this, arguments);
};
-
+
/**
* Wrapper around mkdir
*/
View
15 plugins-server/cloud9.sandbox.fs/fs_test.js
@@ -158,6 +158,17 @@ module.exports = {
});
},
+ "test rename should resolve first and second parameter": function (next) {
+ var rename = Fs.rename = sinon.stub().callsArgWith(2, null);
+
+ this.fs.rename("hello.js", "pietje.js", function (err) {
+ assert.equal(err, null);
+ sinon.assert.calledWith(rename, "/usr/jan/1299/hello.js", "/usr/jan/1299/pietje.js");
+
+ 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);
@@ -182,7 +193,7 @@ module.exports = {
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();
Please sign in to comment.
Something went wrong with that request. Please try again.