Refactor to fix bugs and update unit tests

This commit is contained in:
Kieran Sedgwick 2015-01-15 14:41:16 -05:00
parent 384f571c9b
commit 5d6e7bb018
2 changed files with 281 additions and 418 deletions

View File

@ -4,7 +4,7 @@ var Environment = require('./environment.js');
var async = require('../../lib/async.js'); var async = require('../../lib/async.js');
var Encoding = require('../encoding.js'); var Encoding = require('../encoding.js');
var minimatch = require('minimatch'); var minimatch = require('minimatch');
var Constants = require('src/constants'); var ROOT_DIRECTORY_NAME = require('../constants').ROOT_DIRECTORY_NAME;
function Shell(fs, options) { function Shell(fs, options) {
options = options || {}; options = options || {};
@ -429,9 +429,9 @@ Shell.prototype.mkdirp = function(path, callback) {
}; };
/** /**
* Moves the file or directory at the `source` path to the * Renames or moves `source` to `destination`, or moves each path
* `destination` path by relinking the source to the destination * in the `source` array into `destination`. Overwrites files in
* path. * `destination` that have the same name(s) as `source` by default.
*/ */
Shell.prototype.mv = function(source, destination, callback) { Shell.prototype.mv = function(source, destination, callback) {
var fs = this.fs; var fs = this.fs;
@ -440,16 +440,17 @@ Shell.prototype.mv = function(source, destination, callback) {
callback = callback || function() {}; callback = callback || function() {};
if(!source) { if(!source) {
callback(new Errors.EINVAL('missing source path argument')); callback(new Errors.EINVAL('Missing source path argument'));
return; return;
} }
else if(source === Constants.ROOT_DIRECTORY_NAME) {
callback(new Errors.EINVAL('the root directory is not a valid source argument')); if(source === ROOT_DIRECTORY_NAME) {
callback(new Errors.EINVAL('The root directory is not a valid source argument'));
return; return;
} }
if(!destination) { if(!destination) {
callback(new Errors.EINVAL('missing destination path argument')); callback(new Errors.EINVAL('Missing destination path argument'));
return; return;
} }
@ -458,15 +459,16 @@ Shell.prototype.mv = function(source, destination, callback) {
destpath = Path.resolve(this.cwd, destpath); destpath = Path.resolve(this.cwd, destpath);
var destdir = Path.dirname(destpath); var destdir = Path.dirname(destpath);
// Recursively create any directories on the destination path which do not exist // If there is no node at the source path, error and quit
shell.mkdirp(destdir, function(error) { fs.lstat(sourcepath, function(error, sourcestats) {
if(error) { if(error) {
callback(error); callback(error);
return; return;
} }
// If there is no node at the source path, error and quit // If the destination's parent directory doesn't exist, error and quit
fs.lstat(sourcepath, function(error, sourcestats) { fs.lstat(destdir, function(error) {
// If there is an error unrelated to the existence of the destination, exit
if(error) { if(error) {
callback(error); callback(error);
return; return;
@ -480,9 +482,13 @@ Shell.prototype.mv = function(source, destination, callback) {
} }
if(deststats) { if(deststats) {
// If the destination is a directory, new destination is destpath/source.basename
if (deststats.isDirectory()) { if (deststats.isDirectory()) {
// If the destination is a directory, new destination is destpath/source.basename
destpath = Path.join(destpath, Path.basename(sourcepath)); destpath = Path.join(destpath, Path.basename(sourcepath));
} else if(sourcestats.isDirectory()) {
// If the source is a folder, but the destination isn't, error and exit
callback(new Errors.EINVAL("Destination is not a directory"));
return;
} }
} }
@ -492,14 +498,15 @@ Shell.prototype.mv = function(source, destination, callback) {
callback(error); callback(error);
return; return;
} }
// If the source is a file, link it to destination and remove the source, then done
// If the source isn't a directory, link it to destination and remove the source, then done
if(sourcestats.isFile() || sourcestats.isSymbolicLink()) { if(sourcestats.isFile() || sourcestats.isSymbolicLink()) {
fs.link(sourcepath, destpath, function(error) { fs.link(sourcepath, destpath, function(error) {
if (error) { if (error) {
callback(error); callback(error);
return; return;
} }
shell.rm(sourcepath, {recursive:true}, function(error) { fs.unlink(sourcepath, function(error) {
if (error) { if (error) {
callback(error); callback(error);
return; return;
@ -507,10 +514,12 @@ Shell.prototype.mv = function(source, destination, callback) {
callback(); callback();
}); });
}); });
return;
} }
// If the source is a directory, create a directory at destination and then recursively // If the source is a directory, create a directory at destination and then recursively
// move every dir entry. // move every dir entry.
else if(sourcestats.isDirectory()) { if(sourcestats.isDirectory()) {
fs.mkdir(destpath, function(error) { fs.mkdir(destpath, function(error) {
if (error) { if (error) {
callback(error); callback(error);
@ -523,8 +532,8 @@ Shell.prototype.mv = function(source, destination, callback) {
return; return;
} }
// Asychronously applies move to all nodes in the source directory // Apply the move to all nodes in the directory
async.each(entries, async.eachSeries(entries,
function(entry, callback) { function(entry, callback) {
move(Path.join(sourcepath, entry), Path.join(destpath, entry), function(error) { move(Path.join(sourcepath, entry), Path.join(destpath, entry), function(error) {
if(error) { if(error) {

View File

@ -1,4 +1,6 @@
define(["Filer", "util"], function(Filer, util) { var Filer = require('../../..');
var util = require('../../lib/test-utils.js');
var expect = require('chai').expect;
describe('FileSystemShell.mv', function() { describe('FileSystemShell.mv', function() {
beforeEach(util.setup); beforeEach(util.setup);
@ -11,7 +13,7 @@ define(["Filer", "util"], function(Filer, util) {
it('should fail when source argument is absent', function(done) { it('should fail when source argument is absent', function(done) {
var fs = util.fs(); var fs = util.fs();
var shell = fs.Shell(); var shell = new fs.Shell();
shell.mv(null, null, function(error) { shell.mv(null, null, function(error) {
expect(error).to.exist; expect(error).to.exist;
@ -21,7 +23,7 @@ define(["Filer", "util"], function(Filer, util) {
it('should fail when destination argument is absent', function(done) { it('should fail when destination argument is absent', function(done) {
var fs = util.fs(); var fs = util.fs();
var shell = fs.Shell(); var shell = new fs.Shell();
var contents = "a"; var contents = "a";
fs.writeFile('/file', contents, function(error) { fs.writeFile('/file', contents, function(error) {
@ -34,62 +36,94 @@ define(["Filer", "util"], function(Filer, util) {
}); });
}); });
it('should fail when arguments are empty strings', function(done) { it('should fail when source argument is an empty string', function(done) {
var fs = util.fs(); var fs = util.fs();
var shell = fs.Shell(); var shell = new fs.Shell();
shell.mv('', '', function(error) { shell.mv('', '/file', function(error) {
expect(error).to.exist; expect(error).to.exist;
done(); done();
}); });
}); });
it('should fail when destination argument is an empty string', function(done) {
var fs = util.fs();
var shell = new fs.Shell();
var contents = 'a';
fs.writeFile('/file', contents, function(error) {
if(error) throw error;
shell.mv('/file', '', function(error) {
expect(error).to.exist;
done();
});
});
});
it('should fail when the node at source path does not exist', function(done) { it('should fail when the node at source path does not exist', function(done) {
var fs = util.fs(); var fs = util.fs();
var shell = fs.Shell(); var shell = new fs.Shell();
fs.mkdir('/dir', function(error) { fs.mkdir('/dir', function(error) {
expect(error).to.not.exist; if(error) throw error;
});
shell.mv('/file', '/dir', function(error) { shell.mv('/file', '/dir', function(error) {
expect(error).to.exist; expect(error).to.exist;
done(); done();
}); });
}); });
});
it('should fail when root is provided as source argument', function(done) { it('should fail when root is provided as source argument', function(done) {
var fs = util.fs(); var fs = util.fs();
var shell = fs.Shell(); var shell = new fs.Shell();
fs.mkdir('/dir', function(error) { fs.mkdir('/dir', function(error) {
expect(error).to.not.exist; if(error) throw error;
});
shell.mv('/', '/dir', function(error) { shell.mv('/', '/dir', function(error) {
expect(error).to.exist; expect(error).to.exist;
done(); done();
}); });
}); });
});
it('should fail when node at source path is a folder, but node at destination is a file', function(done) {
var fs = util.fs();
var shell = new fs.Shell();
var contents = "a";
fs.mkdir ('/dir', function(error) {
if(error) throw error;
fs.writeFile('/file', contents, function(error) {
if(error) throw error;
shell.mv('/dir', '/file', function(error) {
expect(error).to.exist;
expect(error.code).to.equal('EINVAL');
done();
});
});
});
});
it('should rename a file which is moved to the same directory under a different name', function(done) { it('should rename a file which is moved to the same directory under a different name', function(done) {
var fs = util.fs(); var fs = util.fs();
var shell = fs.Shell(); var shell = new fs.Shell();
var contents = "a"; var contents = "a";
fs.writeFile('/file', contents, function(error) { fs.writeFile('/file', contents, function(error) {
expect(error).to.not.exist; if(error) throw error;
shell.mv('/file', '/newfile', function(error) { shell.mv('/file', '/newfile', function(error) {
expect(error).to.not.exist; if(error) throw error;
fs.stat('/file', function(error, stats) { fs.stat('/file', function(error, stats) {
expect(error).to.exist; expect(error).to.exist;
expect(stats).to.not.exist; expect(stats).not.to.exist;
fs.stat('/newfile', function(error, stats) { fs.readFile('/newfile', 'utf8', function(error, data) {
expect(error).to.not.exist; expect(error).not.to.exist;
expect(stats).to.exist; expect(data).to.equal(contents);
done(); done();
}); });
}); });
@ -99,29 +133,27 @@ define(["Filer", "util"], function(Filer, util) {
it('should rename a symlink which is moved to the same directory under a different name', function(done) { it('should rename a symlink which is moved to the same directory under a different name', function(done) {
var fs = util.fs(); var fs = util.fs();
var shell = fs.Shell(); var shell = new fs.Shell();
var contents = "a"; var contents = "a";
fs.writeFile('/file', contents, function(error) { fs.writeFile('/file', contents, function(error) {
expect(error).to.not.exist; if(error) throw error;
fs.symlink('/file', '/newfile', function(error) { fs.symlink('/file', '/newfile', function(error) {
expect(error).to.not.exist; if(error) throw error;
shell.mv('/newfile', '/newerfile', function(error) { shell.mv('/newfile', '/newerfile', function(error) {
expect(error).to.not.exist; expect(error).not.to.exist;
fs.stat('/file', function(error, stats) { fs.stat('/file', function(error, stats) {
expect(error).to.not.exist; expect(error).not.to.exist;
expect(stats).to.exist;
fs.stat('/newfile', function(error, stats) { fs.stat('/newfile', function(error, stats) {
expect(error).to.exist; expect(error).to.exist;
expect(stats).to.not.exist;
fs.stat('/newerfile', function(error, stats) { fs.readFile('/newerfile', 'utf8', function(error, data) {
expect(error).to.not.exist; expect(error).not.to.exist;
expect(stats).to.exist; expect(data).to.equal(contents);
done(); done();
}); });
}); });
@ -131,60 +163,27 @@ define(["Filer", "util"], function(Filer, util) {
}); });
}); });
it('should move a file to a directory which does not currently exist', function(done) {
var fs = util.fs();
var shell = fs.Shell();
var contents = "a";
fs.writeFile('/file', contents, function(error) {
expect(error).to.not.exist;
shell.mv('/file', '/dir/newfile', function(error) {
expect(error).to.not.exist;
fs.stat('/file', function(error, stats) {
expect(error).to.exist;
expect(stats).to.not.exist;
fs.stat('/dir/newfile', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
done();
});
});
});
});
});
it('should move a file into an empty directory', function(done) { it('should move a file into an empty directory', function(done) {
var fs = util.fs(); var fs = util.fs();
var shell = fs.Shell(); var shell = new fs.Shell();
var contents = "a"; var contents = "a";
fs.mkdir('/dir', function(error) { fs.mkdir('/dir', function(error) {
expect(error).to.not.exist; if(error) throw error;
fs.stat('/dir', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
fs.writeFile('/file', contents, function(error) { fs.writeFile('/file', contents, function(error) {
expect(error).to.not.exist; if(error) throw error;
fs.stat('/file', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
shell.mv('/file', '/dir', function(error) { shell.mv('/file', '/dir', function(error) {
expect(error).to.not.exist; expect(error).to.not.exist;
fs.stat('/file', function(error, stats) { fs.stat('/file', function(error, stats) {
expect(error).to.exist; expect(error).to.exist;
expect(stats).to.not.exist; expect(error.code).to.equal('ENOENT');
fs.stat('/dir/file', function(error, stats) { fs.readFile('/dir/file', 'utf8', function(error, data) {
expect(error).to.not.exist; expect(error).not.to.exist;
expect(stats).to.exist; expect(data).to.equal(contents);
done(); done();
}); });
}); });
@ -192,34 +191,28 @@ define(["Filer", "util"], function(Filer, util) {
}); });
}); });
}); });
});
});
it('should move a file into a directory that has a file of the same name', function(done) { it('should move a file into a directory that has a file of the same name', function(done) {
var fs = util.fs(); var fs = util.fs();
var shell = fs.Shell(); var shell = new fs.Shell();
var contents = "a"; var contents = "a";
var contents2 = "b"; var contents2 = "b";
fs.mkdir('/dir', function(error) { fs.mkdir('/dir', function(error) {
expect(error).to.not.exist; if(error) throw error;
fs.writeFile('/file', contents, function(error) { fs.writeFile('/file', contents, function(error) {
expect(error).to.not.exist; if(error) throw error;
fs.writeFile('/dir/file', contents2, function(error) { fs.writeFile('/dir/file', contents2, function(error) {
expect(error).to.not.exist; if(error) throw error;
shell.mv('/file', '/dir/file', function(error) { shell.mv('/file', '/dir/file', function(error) {
expect(error).to.not.exist; expect(error).to.not.exist;
fs.stat('/file', function(error, stats) { fs.stat('/file', function(error, stats) {
expect(error).to.exist; expect(error).to.exist;
expect(stats).to.not.exist; expect(error.code).to.equal('ENOENT');
fs.stat('/dir/file', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
fs.readFile('/dir/file', 'utf8', function(error, data) { fs.readFile('/dir/file', 'utf8', function(error, data) {
expect(error).not.to.exist; expect(error).not.to.exist;
@ -232,48 +225,23 @@ define(["Filer", "util"], function(Filer, util) {
}); });
}); });
}); });
});
it('should move an empty directory to a destination that does not currently exist', function(done) {
var fs = util.fs();
var shell = fs.Shell();
fs.mkdir('/dir', function(error) {
expect(error).to.not.exist;
shell.mv('/dir', '/newdir', function(error) {
expect(error).to.not.exist;
fs.stat('/dir', function(error, stats) {
expect(error).to.exist;
expect(stats).to.not.exist;
fs.stat('/newdir', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
done();
});
});
});
});
});
it('should move an empty directory to another empty directory', function(done) { it('should move an empty directory to another empty directory', function(done) {
var fs = util.fs(); var fs = util.fs();
var shell = fs.Shell(); var shell = new fs.Shell();
fs.mkdir('/dir', function(error) { fs.mkdir('/dir', function(error) {
expect(error).to.not.exist; if(error) throw error;
fs.mkdir('/otherdir', function(error) { fs.mkdir('/otherdir', function(error) {
expect(error).to.not.exist; if(error) throw error;
shell.mv('/dir', '/otherdir', function(error) { shell.mv('/dir', '/otherdir', function(error) {
expect(error).to.not.exist; expect(error).to.not.exist;
fs.stat('/dir', function(error, stats) { fs.stat('/dir', function(error, stats) {
expect(error).to.exist; expect(error).to.exist;
expect(stats).to.not.exist; expect(error.code).to.equal('ENOENT');
fs.stat('/otherdir/dir', function(error, stats) { fs.stat('/otherdir/dir', function(error, stats) {
expect(error).to.not.exist; expect(error).to.not.exist;
@ -288,24 +256,24 @@ define(["Filer", "util"], function(Filer, util) {
it('should move an empty directory to a populated directory', function(done) { it('should move an empty directory to a populated directory', function(done) {
var fs = util.fs(); var fs = util.fs();
var shell = fs.Shell(); var shell = new fs.Shell();
var contents = "a"; var contents = "a";
fs.mkdir('/dir', function(error) { fs.mkdir('/dir', function(error) {
expect(error).to.not.exist; if(error) throw error;
fs.mkdir('/otherdir', function(error) { fs.mkdir('/otherdir', function(error) {
expect(error).to.not.exist; if(error) throw error;
fs.writeFile('/otherdir/file', contents, function(error) { fs.writeFile('/otherdir/file', contents, function(error) {
expect(error).to.not.exist; if(error) throw error;
shell.mv('/dir', '/otherdir', function(error) { shell.mv('/dir', '/otherdir', function(error) {
expect(error).to.not.exist; expect(error).to.not.exist;
fs.stat('/dir', function(error, stats) { fs.stat('/dir', function(error, stats) {
expect(error).to.exist; expect(error).to.exist;
expect(stats).to.not.exist; expect(error.code).to.equal('ENOENT');
fs.stat('/otherdir/file', function(error, stats) { fs.stat('/otherdir/file', function(error, stats) {
expect(error).to.not.exist; expect(error).to.not.exist;
@ -326,152 +294,40 @@ define(["Filer", "util"], function(Filer, util) {
it('should move a populated directory to a populated directory', function(done) { it('should move a populated directory to a populated directory', function(done) {
var fs = util.fs(); var fs = util.fs();
var shell = fs.Shell(); var shell = new fs.Shell();
var contents = "a"; var contents = "a";
var contents2 = "b";
fs.mkdir('/dir', function(error) { fs.mkdir('/dir', function(error) {
expect(error).to.not.exist; if(error) throw error;
fs.mkdir('/otherdir', function(error) { fs.mkdir('/otherdir', function(error) {
expect(error).to.not.exist; if(error) throw error;
fs.writeFile('/otherdir/file', contents, function(error) { fs.writeFile('/otherdir/file', contents, function(error) {
expect(error).to.not.exist; if(error) throw error;
fs.writeFile('/dir/file', contents, function(error) { fs.writeFile('/dir/file', contents2, function(error) {
expect(error).to.not.exist; if(error) throw error;
shell.mv('/dir', '/otherdir', function(error) { shell.mv('/dir', '/otherdir', function(error) {
expect(error).to.not.exist; expect(error).to.not.exist;
fs.stat('/dir', function(error, stats) { fs.stat('/dir', function(error, stats) {
expect(error).to.exist; expect(error).to.exist;
expect(stats).to.not.exist; expect(error.code).to.equal('ENOENT');
fs.stat('/otherdir/file', function(error, stats) { fs.readFile('/otherdir/file', 'utf8', function(error, data) {
expect(error).to.not.exist; expect(error).to.not.exist;
expect(stats).to.exist; expect(data).to.equal(contents);
fs.stat('/otherdir/dir', function(error, stats) { fs.stat('/otherdir/dir', function(error, stats) {
expect(error).to.not.exist; expect(error).to.not.exist;
expect(stats).to.exist; expect(stats).to.exist;
fs.stat('/otherdir/dir/file', function(error, stats) { fs.readFile('/otherdir/dir/file', 'utf8', function(error, data) {
expect(error).to.not.exist; expect(error).to.not.exist;
expect(stats).to.exist; expect(data).to.equal(contents2);
done();
})
});
});
});
});
});
});
});
});
});
it('should move an empty directory to another empty directory', function(done) {
var fs = util.fs();
var shell = fs.Shell();
fs.mkdir('/dir', function(error) {
expect(error).to.not.exist;
fs.mkdir('/otherdir', function(error) {
expect(error).to.not.exist;
shell.mv('/dir', '/otherdir', function(error) {
expect(error).to.not.exist;
fs.stat('/dir', function(error, stats) {
expect(error).to.exist;
expect(stats).to.not.exist;
fs.stat('/otherdir/dir', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
done();
});
});
});
});
});
});
it('should move an empty directory to a populated directory', function(done) {
var fs = util.fs();
var shell = fs.Shell();
var contents = "a";
fs.mkdir('/dir', function(error) {
expect(error).to.not.exist;
fs.mkdir('/otherdir', function(error) {
expect(error).to.not.exist;
fs.writeFile('/otherdir/file', contents, function(error) {
expect(error).to.not.exist;
shell.mv('/dir', '/otherdir', function(error) {
expect(error).to.not.exist;
fs.stat('/dir', function(error, stats) {
expect(error).to.exist;
expect(stats).to.not.exist;
fs.stat('/otherdir/file', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
fs.stat('/otherdir/dir', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
done();
});
});
});
});
});
});
});
});
it('should move a populated directory to a populated directory', function(done) {
var fs = util.fs();
var shell = fs.Shell();
var contents = "a";
fs.mkdir('/dir', function(error) {
expect(error).to.not.exist;
fs.mkdir('/otherdir', function(error) {
expect(error).to.not.exist;
fs.writeFile('/otherdir/file', contents, function(error) {
expect(error).to.not.exist;
fs.writeFile('/dir/file', contents, function(error) {
expect(error).to.not.exist;
shell.mv('/dir', '/otherdir', function(error) {
expect(error).to.not.exist;
fs.stat('/dir', function(error, stats) {
expect(error).to.exist;
expect(stats).to.not.exist;
fs.stat('/otherdir/file', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
fs.stat('/otherdir/dir', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
fs.stat('/otherdir/dir/file', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
done(); done();
}) })
}); });
@ -484,5 +340,3 @@ define(["Filer", "util"], function(Filer, util) {
}); });
}); });
}); });
});