Finished draft of mv, started writing tests

This commit is contained in:
kwkofler 2014-03-11 11:09:45 -04:00
parent 10e6c86a48
commit d07dd0991d
3 changed files with 43 additions and 4 deletions

View File

@ -339,8 +339,8 @@ define(function(require) {
/**
* Moves the file or directory at the `source` path to the
* `destination` path by relinking the source to the destination
* path. Also currently there are no options, but it might be nice
* to implement
* path. Currently there are no options, but it might be nice
* to implement an interactive mode at some point.
*/
Shell.prototype.mv = function(source, destination, options, callback) {
var fs = this.fs;
@ -436,11 +436,35 @@ define(function(require) {
// If the destination is a dir, compare basenames for equality
if(sourcepath.basename === destpath.basename) {
// If they're the same, attempt to relink each source entry to the destination
fs.readdir(sourcepath, function(error, entries) {
if(error) {
callback(error);
return;
}
// If there are no entries in source, unlink the source and we're done
if(entries.length === 0) {
fs.unlink(sourcepath, callback);
return;
}
// Iterate through the entries, unlinking destinations and relinking sources
for(var i = 0; i < entries.length; i++) {
var temppath = Path.join(destpath, sourcepath.basename);
fs.unlink(temppath, callback);
fs.link(sourcepath, temppath, callback);
}
// We're done after relinking all
return;
}
}
// If they're different, link the source as a subdir of the destination
}
destpath = Path.join(destpath, sourcepath.basename);
fs.link(sourcepath, destpath, callback);
return;
});
});
}

View File

@ -0,0 +1,14 @@
define(["Filer", "util"], function(Filer, util) {
describe('FileSystemShell.mv'), function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
var shell = util.shell();
expect(shell.mv).to.be.a('function');
});
});
});

View File

@ -55,6 +55,7 @@ define([
"spec/shell/cat.spec",
"spec/shell/ls.spec",
"spec/shell/rm.spec",
"spec/shell/mv.spec",
"spec/shell/env.spec",
"spec/shell/mkdirp.spec",