sh.mv complete with tests and documentation

This commit is contained in:
kwkofler 2014-03-27 22:42:34 -04:00
parent 8fad57231a
commit 7063aafe2d
3 changed files with 322 additions and 104 deletions

View File

@ -1131,6 +1131,7 @@ var sh = fs.Shell();
* [sh.touch(path, [options], callback)](#touch) * [sh.touch(path, [options], callback)](#touch)
* [sh.cat(files, callback)](#cat) * [sh.cat(files, callback)](#cat)
* [sh.rm(path, [options], callback)](#rm) * [sh.rm(path, [options], callback)](#rm)
* [sh.mv(source, destination, callback)](#mv)
* [sh.tempDir(callback)](#tempDir) * [sh.tempDir(callback)](#tempDir)
* [sh.mkdirp(path, callback)](#mkdirp) * [sh.mkdirp(path, callback)](#mkdirp)
@ -1297,6 +1298,30 @@ sh.rm('/dir', { recursive: true }, function(err) {
}); });
``` ```
#### sh.mv(source, destination, callback)<a name="mv"></a>
Moves the file or directory located at `source` to `destination`. Overwrites files
which share names by default.
Example
```javascript
sh.mv('./file', './renamed', function(err) {
if(err) throw err;
// ./file has been moved to the same directory under a new name
});
sh.mv('./dir', './otherdir', function(err) {
if(err) throw err;
// ./dir has been moved to ./otherdir/dir
});
sh.mv('./file', './dir', function(err) {
if(err) throw err;
// ./file has been moved to ./dir/file
});
```
#### sh.tempDir(callback)<a name="tempDir"></a> #### sh.tempDir(callback)<a name="tempDir"></a>
Gets the path to the shell's temporary directory, creating it if it Gets the path to the shell's temporary directory, creating it if it

View File

@ -343,6 +343,7 @@ define(function(require) {
*/ */
Shell.prototype.mv = function(source, destination, callback) { Shell.prototype.mv = function(source, destination, callback) {
var fs = this.fs; var fs = this.fs;
var shell = this;
callback = callback || function() {}; callback = callback || function() {};
@ -363,119 +364,96 @@ define(function(require) {
function move(sourcepath, destpath, callback) { function move(sourcepath, destpath, callback) {
sourcepath = Path.resolve(this.cwd, sourcepath); sourcepath = Path.resolve(this.cwd, sourcepath);
destpath = Path.resolve(this.cwd, destpath); destpath = Path.resolve(this.cwd, destpath);
destdir = Path.resolve(this.cwd, destpath.dirname); destdir = Path.resolve(this.cwd, Path.dirname(destpath));
fs.stat(destdir, function(error, stats) { shell.mkdirp(destdir, function(error) {
if(error && error.code === 'ENOENT') { if(error) {
fs.mkdirp(destdir, function(error) { callback(error);
callback(error); return;
throw error;
return;
});
} }
}); });
fs.stat(sourcepath, function(error, stats) { fs.stat(sourcepath, function(error, sourcestats) {
if(error) { if(error) {
callback(error); callback(error);
return; return;
} }
// If the source is a file, stat the destination path fs.stat(destpath, function(error, deststats) {
if(stats.isFile()) { // If there is an error unrelated to the existence of the destination, exit
fs.stat(destpath, function(error, stats) { if(error && error.code !== 'ENOENT') {
// If the destination doesn't exist, relink source and we're done callback(error);
if(error) { return;
fs.link(sourcepath, destpath, callback); }
return;
}
// If the destination is a file, delete the destination, relink source and we're done // If the destination is a directory, new destination is destpath/source.basename
if(stats.isFile()) { if(deststats) {
fs.unlink(destpath, callback); if(deststats.isDirectory()) {
fs.link(sourcepath, destpath, callback); destpath = Path.join(destpath, Path.basename(sourcepath));
return;
} }
fs.unlink(destpath, function(error) {
// If the destination is a dir, check to see if a file with the source name already exists if (error && error.code !== 'ENOENT') {
fs.readdir(destname, function(error, entries) {
if(error) {
callback(error); callback(error);
return; return;
} }
});
}
// If dir is empty, relink source and we're done if(sourcestats.isFile()) {
if(entries.length === 0) { fs.link(sourcepath, destpath, function(error) {
destpath = Path.join(destpath, sourcepath.basename); if (error) {
fs.link(sourcepath, destpath, callback); callback(error);
return; return;
} }
shell.rm(sourcepath, {recursive:true}, function(error) {
// Iterate through dir entries; if a node with the same name exists, unlink it, if (error) {
// relink source and we're done callback(error);
for(var i = 0; i < entries.length; i++) {
if(entries[i].basename === sourcepath.basename) {
destpath = Path.join(destpath, sourcepath.basename);
fs.unlink(destpath, callback);
fs.link(sourcepath, destpath, callback);
return; return;
} }
} callback();
});
// If a matching node can't be found, relink source and we're done
destpath = Path.join(destpath, sourcepath.basename);
fs.link(sourcepath, destpath, callback);
return;
}); });
});
}
// If the source is a directory, stat the destination path
fs.stat(destpath, function(error, stats) {
// If the destination doesn't exist, relink the source and we're done
if(error) {
fs.link(sourcepath, destpath, callback);
return;
} }
else if(sourcestats.isDirectory()) {
// If the destination is a file, delete the destination, relink source and we're done fs.mkdir(destpath, function(error) {
if(stats.type === 'FILE') { if (error) {
fs.unlink(destpath, callback);
fs.link(sourcepath, destpath, callback);
return;
}
// 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); callback(error);
return; return;
} }
// If there are no entries in source, unlink the source and we're done fs.readdir(sourcepath, function(error, entries) {
if(entries.length === 0) { if(error) {
fs.unlink(sourcepath, callback); callback(error);
return; return;
} }
// Iterate through the entries, unlinking destinations and relinking sources async.each(entries,
for(var i = 0; i < entries.length; i++) { function(entry, callback) {
var temppath = Path.join(destpath, sourcepath.basename); move(Path.join(sourcepath, entry), Path.join(destpath, entry), function(error) {
fs.unlink(temppath, callback); if(error) {
fs.link(sourcepath, temppath, callback); callback(error);
} return;
}
// We're done after relinking all callback();
return; });
},
function(error) {
if(error) {
callback(error);
return;
}
shell.rm(sourcepath, {recursive:true}, function(error) {
if (error) {
callback(error);
return;
}
callback();
});
}
);
});
}); });
} }
// 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

@ -34,12 +34,22 @@ define(["Filer", "util"], function(Filer, util) {
}); });
}); });
it('should fail when source argument does not exist', function(done) { it('should fail when arguments are empty strings', function(done) {
var fs = util.fs();
var shell = fs.Shell();
shell.mv('', '', function(error) {
expect(error).to.exist;
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 = fs.Shell();
fs.mkdir('/dir', function(error) { fs.mkdir('/dir', function(error) {
expect(error).not.to.exist; expect(error).to.not.exist;
}); });
shell.mv('/file', '/dir', function(error) { shell.mv('/file', '/dir', function(error) {
@ -53,7 +63,7 @@ define(["Filer", "util"], function(Filer, util) {
var shell = fs.Shell(); var shell = fs.Shell();
fs.mkdir('/dir', function(error) { fs.mkdir('/dir', function(error) {
expect(error).not.to.exist; expect(error).to.not.exist;
}); });
shell.mv('/', '/dir', function(error) { shell.mv('/', '/dir', function(error) {
@ -68,15 +78,17 @@ define(["Filer", "util"], function(Filer, util) {
var contents = "a"; var contents = "a";
fs.writeFile('/file', contents, function(error) { fs.writeFile('/file', contents, function(error) {
expect(error).not.to.exist; expect(error).to.not.exist;
shell.mv('/file', '/newfile', function(error) { shell.mv('/file', '/newfile', function(error) {
expect(error).not.to.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).not.to.exist; expect(stats).to.not.exist;
fs.stat('/newfile', function(error,stats) {
expect(error).not.to.exist; fs.stat('/newfile', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist; expect(stats).to.exist;
done(); done();
}); });
@ -91,15 +103,17 @@ define(["Filer", "util"], function(Filer, util) {
var contents = "a"; var contents = "a";
fs.writeFile('/file', contents, function(error) { fs.writeFile('/file', contents, function(error) {
expect(error).not.to.exist; expect(error).to.not.exist;
shell.mv('/file', '/dir/newfile', function(error) { shell.mv('/file', '/dir/newfile', function(error) {
expect(error).not.to.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).not.to.exist; expect(stats).to.not.exist;
fs.stat('/dir/newfile', function(error, stats) { fs.stat('/dir/newfile', function(error, stats) {
expect(error).not.to.exist; expect(error).to.not.exist;
expect(stats).to.exist; expect(stats).to.exist;
done(); done();
}); });
@ -109,17 +123,218 @@ define(["Filer", "util"], function(Filer, util) {
}); });
it('should move a file into an empty directory', function(done) { it('should move a file into an empty directory', function(done) {
done(); var fs = util.fs();
var shell = fs.Shell();
var contents = "a";
fs.mkdir('/dir', function(error) {
expect(error).to.not.exist;
fs.stat('/dir', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
fs.writeFile('/file', contents, function(error) {
expect(error).to.not.exist;
fs.stat('/file', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
shell.mv('/file', '/dir', 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/file', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
done();
});
});
});
});
});
});
});
}); });
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) {
done(); var fs = util.fs();
var shell = fs.Shell();
var contents = "a";
var contents2 = "b";
fs.mkdir('/dir', function(error) {
expect(error).to.not.exist;
fs.writeFile('/file', contents, function(error) {
expect(error).to.not.exist;
fs.writeFile('/dir/file', contents2, function(error) {
expect(error).to.not.exist;
shell.mv('/file', '/dir/file', 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/file', function(error, stats) {
expect(error).to.not.exist;
expect(stats).to.exist;
fs.readFile('/dir/file', 'utf8', function(error, data) {
expect(error).not.to.exist;
expect(data).to.equal(contents);
done();
});
});
});
});
});
});
});
}); });
it('should move a directory to a destination that does not currently exist', function(done) { it('should move an empty directory to a destination that does not currently exist', function(done) {
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) {
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();
})
});
});
});
});
});
});
});
});
});
}); });
}); });