Merge pull request #248 from humphd/issue247

Fix #247 - sh.cwd() not updating cwd on success, with test.
This commit is contained in:
Alan K 2014-07-28 20:01:59 -04:00
commit 63132bddcb
3 changed files with 68 additions and 23 deletions

View File

@ -36,7 +36,7 @@ function Shell(fs, options) {
* we can access cwd without exposing it externally.
*/
this.cd = function(path, callback) {
path = Path.resolve(this.cwd, path);
path = Path.resolve(cwd, path);
// Make sure the path actually exists, and is a dir
fs.stat(path, function(err, stats) {
if(err) {
@ -78,14 +78,15 @@ function Shell(fs, options) {
*/
Shell.prototype.exec = function(path, args, callback) {
/* jshint evil:true */
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof args === 'function') {
callback = args;
args = [];
}
args = args || [];
callback = callback || function(){};
path = Path.resolve(this.cwd, path);
path = Path.resolve(sh.pwd(), path);
fs.readFile(path, "utf8", function(error, data) {
if(error) {
@ -109,14 +110,15 @@ Shell.prototype.exec = function(path, args, callback) {
* * date - use the provided Date value instead of current date/time
*/
Shell.prototype.touch = function(path, options, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(){};
path = Path.resolve(this.cwd, path);
path = Path.resolve(sh.pwd(), path);
function createFile(path) {
fs.writeFile(path, '', callback);
@ -150,7 +152,8 @@ Shell.prototype.touch = function(path, options, callback) {
* (multiple file paths).
*/
Shell.prototype.cat = function(files, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
var all = '';
callback = callback || function(){};
@ -162,7 +165,7 @@ Shell.prototype.cat = function(files, callback) {
files = typeof files === 'string' ? [ files ] : files;
function append(item, callback) {
var filename = Path.resolve(this.cwd, item);
var filename = Path.resolve(sh.pwd(), item);
fs.readFile(filename, 'utf8', function(error, data) {
if(error) {
callback(error);
@ -200,7 +203,8 @@ Shell.prototype.cat = function(files, callback) {
* the `recursive=true` option.
*/
Shell.prototype.ls = function(dir, options, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
@ -214,7 +218,7 @@ Shell.prototype.ls = function(dir, options, callback) {
}
function list(path, callback) {
var pathname = Path.resolve(this.cwd, path);
var pathname = Path.resolve(sh.pwd(), path);
var result = [];
fs.readdir(pathname, function(error, entries) {
@ -272,7 +276,8 @@ Shell.prototype.ls = function(dir, options, callback) {
* `recursive=true` option.
*/
Shell.prototype.rm = function(path, options, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
@ -286,7 +291,7 @@ Shell.prototype.rm = function(path, options, callback) {
}
function remove(pathname, callback) {
pathname = Path.resolve(this.cwd, pathname);
pathname = Path.resolve(sh.pwd(), pathname);
fs.stat(pathname, function(error, stats) {
if(error) {
callback(error);
@ -343,8 +348,9 @@ Shell.prototype.rm = function(path, options, callback) {
* env.TMP. The callback receives (error, tempDirName).
*/
Shell.prototype.tempDir = function(callback) {
var fs = this.fs;
var tmp = this.env.get('TMP');
var sh = this;
var fs = sh.fs;
var tmp = sh.env.get('TMP');
callback = callback || function(){};
// Try and create it, and it will either work or fail
@ -362,7 +368,8 @@ Shell.prototype.tempDir = function(callback) {
* MIT License
*/
Shell.prototype.mkdirp = function(path, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
callback = callback || function(){};
if(!path) {
@ -428,7 +435,8 @@ Shell.prototype.mkdirp = function(path, callback) {
* filename is used instead. The callback receives (error, path).
*/
Shell.prototype.wget = function(url, options, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
@ -447,7 +455,7 @@ Shell.prototype.wget = function(url, options, callback) {
// i.e. instead of "/foo?bar/" we would expect "/foo?bar%2F"
var path = options.filename || url.split('/').pop();
path = Path.resolve(fs.cwd, path);
path = Path.resolve(sh.pwd(), path);
function onerror() {
callback(new Error('unable to get resource'));
@ -469,8 +477,8 @@ Shell.prototype.wget = function(url, options, callback) {
};
Shell.prototype.unzip = function(zipfile, options, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
@ -483,8 +491,8 @@ Shell.prototype.unzip = function(zipfile, options, callback) {
return;
}
var path = Path.resolve(this.cwd, zipfile);
var destination = Path.resolve(options.destination || this.cwd);
var path = Path.resolve(sh.pwd(), zipfile);
var destination = Path.resolve(options.destination || sh.pwd());
fs.readFile(path, function(err, data) {
if(err) return callback(err);
@ -515,8 +523,8 @@ Shell.prototype.unzip = function(zipfile, options, callback) {
};
Shell.prototype.zip = function(zipfile, paths, options, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
@ -535,7 +543,7 @@ Shell.prototype.zip = function(zipfile, paths, options, callback) {
if(typeof paths === 'string') {
paths = [ paths ];
}
zipfile = Path.resolve(this.cwd, zipfile);
zipfile = Path.resolve(sh.pwd(), zipfile);
function toRelPath(path) {
// Make path relative within the zip
@ -568,7 +576,7 @@ Shell.prototype.zip = function(zipfile, paths, options, callback) {
}
function add(path, callback) {
path = Path.resolve(sh.cwd, path);
path = Path.resolve(sh.pwd(), path);
fs.stat(path, function(err, stats) {
if(err) return callback(err);

36
tests/bugs/issue247.js Normal file
View File

@ -0,0 +1,36 @@
var Filer = require('../..');
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('sh.cd doesn\'t seem to be working from a relative path if I am one or more folders deep, #247', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should properly deal with relative paths missing ./ and ../', function(done) {
var fs = util.fs();
var sh = fs.Shell();
sh.mkdirp('/home/scott', function(err) {
if(err) throw err;
sh.cd('/', function(err) {
if(err) throw err;
expect(sh.pwd()).to.equal('/');
sh.cd('home', function(err) {
if(err) throw err;
expect(sh.pwd()).to.equal('/home');
sh.cd('scott', function(err) {
if(err) throw err;
expect(sh.pwd()).to.equal('/home/scott');
done();
});
});
});
});
});
});

View File

@ -68,4 +68,5 @@ require("./spec/node-js/simple/test-fs-watch-recursive");
require("./bugs/issue105");
require("./bugs/issue106");
require("./bugs/issue239");
require("./bugs/ls-depth-bug");
require("./bugs/ls-depth-bug");
require("./bugs/issue247.js");