tests for fileOps.getNewestFile()

This commit is contained in:
Danielv123 committed 2017-05-22 10:59:39 +02:00
1 parent 2695f0a443
commit 3d4fbfb3fa
3 files changed
+76 -36

No files matched your search

+5 -4
View File
@@ -82,14 +82,14 @@ if (!command || command == "help" || command == "--help") {
console.error("node client.js download"); console.error("node client.js download");
process.exit(1); process.exit(1);
} else if (command == "list") { } else if (command == "list") {
console.dir(fileOps.getDirectories("./instances/")); console.dir(fileOps.getDirectoriesSync("./instances/"));
process.exit(1); process.exit(1);
} else if (command == "delete") { } else if (command == "delete") {
if (!process.argv[3]) { if (!process.argv[3]) {
console.error("Usage: node client.js delete [instance]"); console.error("Usage: node client.js delete [instance]");
process.exit(1); process.exit(1);
} else if (typeof process.argv[3] == "string" && fs.existsSync("./instances/" + process.argv[3]) && process.argv[3] != "/" && process.argv[3] != "") { } else if (typeof process.argv[3] == "string" && fs.existsSync("./instances/" + process.argv[3]) && process.argv[3] != "/" && process.argv[3] != "") {
fileOps.deleteFolderRecursive("./instances/" + process.argv[3]); fileOps.deleteFolderRecursiveSync("./instances/" + process.argv[3]);
console.log("Deleted instance " + process.argv[3]); console.log("Deleted instance " + process.argv[3]);
process.exit(1); process.exit(1);
} else { } else {
@@ -216,7 +216,8 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\
// Spawn factorio server // Spawn factorio server
//var serverprocess = child_process.exec(commandline); //var serverprocess = child_process.exec(commandline);
fileOps.getNewestFile(instancedirectory + "/saves/", fs.readdirSync(instancedirectory + "/saves/"),function(latestSave) { fileOps.getNewestFile(instancedirectory + "/saves/", fs.readdirSync(instancedirectory + "/saves/"),function(err, latestSave) {
if(err) throw err;
// implicit global // implicit global
serverprocess = child_process.spawn( serverprocess = child_process.spawn(
'./' + config.factorioDirectory + '/bin/x64/factorio', [ './' + config.factorioDirectory + '/bin/x64/factorio', [
@@ -301,7 +302,7 @@ _.once(instanceManagement);
function instanceManagement() { function instanceManagement() {
console.log("Started instanceManagement();"); console.log("Started instanceManagement();");
// load plugins and execute onLoad event // load plugins and execute onLoad event
let pluginDirectories = fileOps.getDirectories("./sharedPlugins/"); let pluginDirectories = fileOps.getDirectoriesSync("./sharedPlugins/");
let plugins = []; let plugins = [];
for(i=0; i<pluginDirectories.length; i++) { for(i=0; i<pluginDirectories.length; i++) {
let I = i let I = i
+34 -23
View File
@@ -4,51 +4,62 @@ var path = require("path");
// gets newest file in a directory // gets newest file in a directory
// dir is directory // dir is directory
// files is array of filenames // files is array of filenames
// callback(filename string); // callback(err, filename string);
var getNewestFile = function(dir, files, callback) { var getNewestFile = function(dir, files, callback) {
if (!callback) return; if (!callback) throw new Error("Missing callback");
if (!files || (files && files.length === 0)) { if (!files || (files && files.length === 0)) {
callback(); callback(new Error("No files"));
return;
}
// we are appending the file name to the directory, without the
// "/" we would get paths like /folderfile.txt instead of /folder/file.txt
if(dir[dir.length-1] != "/"){
dir += "/";
} }
var newest = { file: files[0] }; var newest = { file: files[0] };
var checked = 0; var checked = 0;
fs.stat(dir + newest.file, function(err, stats) { fs.stat(dir + newest.file, function(err, stats) {
newest.mtime = stats.mtime; if(err) {
for (var i = 0; i < files.length; i++) { callback(err);
var file = files[i]; } else {
(function(file) { newest.mtime = stats.mtime;
fs.stat(file, function(err, stats) { for (var i = 0; i < files.length; i++) {
++checked; var file = files[i];
if (stats.mtime.getTime() > newest.mtime.getTime()) { (function(file) {
newest = { file : file, mtime : stats.mtime }; fs.stat(file, function(err, stats) {
} ++checked;
if (checked == files.length) { if (stats.mtime.getTime() > newest.mtime.getTime()) {
callback(newest); newest = { file : file, mtime : stats.mtime };
} }
}); if (checked == files.length) {
})(dir + file); callback(undefined,newest);
}
});
})(dir + file);
}
} }
}); });
} }
// get all directories in folder // get names of all directories in directory
var getDirectories = function(srcpath) { var getDirectoriesSync = function(srcpath) {
return fs.readdirSync(srcpath).filter(function (file) { return fs.readdirSync(srcpath).filter(function (file) {
return fs.statSync(path.join(srcpath, file)).isDirectory(); return fs.statSync(path.join(srcpath, file)).isDirectory();
}); });
} }
// get names of all files in a directory
var getFileNamesSync = function(srcpath) { var getFileNamesSync = function(srcpath) {
return fs.readdirSync(srcpath).filter(function (file) { return fs.readdirSync(srcpath).filter(function (file) {
return !fs.statSync(path.join(srcpath, file)).isDirectory(); return !fs.statSync(path.join(srcpath, file)).isDirectory();
}); });
} }
// does what it says on the tin // does what it says on the tin
var deleteFolderRecursive = function(path) { var deleteFolderRecursiveSync = function(path) {
if (fs.existsSync(path)) { if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file, index) { fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + "/" + file; var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath); deleteFolderRecursiveSync(curPath);
} else { // delete file } else { // delete file
fs.unlinkSync(curPath); fs.unlinkSync(curPath);
} }
@@ -59,7 +70,7 @@ var deleteFolderRecursive = function(path) {
module.exports = { module.exports = {
getNewestFile:getNewestFile, getNewestFile:getNewestFile,
getDirectories:getDirectories, getDirectoriesSync:getDirectoriesSync,
deleteFolderRecursive:deleteFolderRecursive, deleteFolderRecursiveSync:deleteFolderRecursiveSync,
getFileNamesSync:getFileNamesSync, getFileNamesSync:getFileNamesSync,
} }
+37 -9
View File
@@ -2,9 +2,9 @@ var assert = require('assert');
var fileOps = require('./fileOps'); var fileOps = require('./fileOps');
describe("fileOps.js", function(){ describe("fileOps.js", function(){
describe("fileOps.getDirectories()", function(){ describe("fileOps.getDirectoriesSync()", function(){
it("gets an array of directory names from a path", function(){ it("gets an array of directory names from a path", function(){
let directories = fileOps.getDirectories("lib/test"); let directories = fileOps.getDirectoriesSync("lib/test");
// because there are no arrays for some reason // because there are no arrays for some reason
assert(typeof directories == "object"); assert(typeof directories == "object");
@@ -13,11 +13,16 @@ describe("fileOps.js", function(){
assert(directories[0] == "another folder" && directories[1] == "folder"); assert(directories[0] == "another folder" && directories[1] == "folder");
}); });
it("does not return any filenames", function(){ it("does not return any filenames", function(){
let directories = fileOps.getDirectories("lib/test"); let directories = fileOps.getDirectoriesSync("lib/test");
// this is a function to return directories, not files. So yeah. // this is a function to return directories, not files. So yeah.
assert(!directories.includes("file.txt") && !directories.includes("another file.txt")); assert(!directories.includes("file.txt") && !directories.includes("another file.txt"));
}); });
it("throws when lacking first argument", function(){
assert.throws(function(){
let files = fileOps.getDirectoriesSync();
}, Error);
});
}); });
describe("fileOps.getFileNamesSync(path)", function(){ describe("fileOps.getFileNamesSync(path)", function(){
it("gets names of files in directory", function(){ it("gets names of files in directory", function(){
@@ -31,13 +36,36 @@ describe("fileOps.js", function(){
}, Error); }, Error);
}); });
}); });
/*describe("fileOps.getNewestFile()", function(){ describe("fileOps.getNewestFile()", function(){
it("gets the newest file in a directory async", function(done){ it("gets the newest file in a directory async", function(done){
fileOps.getNewestFile("lib/test", fileOps.getFileNames("lib/test"), function(newest){ fileOps.getNewestFile("lib/test", fileOps.getFileNamesSync("lib/test"), function(err, newest){
assert(newest); assert(newest && typeof newest.file == "string");
done(); done();
}) });
}); });
});*/ it("throws if callback is missing", function(){
assert.throws(function(){
fileOps.getNewestFile("lib/test", fileOps.getFileNamesSync("lib/test"));
}, Error);
});
it("executes callback(err) if no files are provided", function(done){
fileOps.getNewestFile("lib/test", [], function(err, newest){
assert(err && typeof err == "object");
done();
});
});
it("executes callback(err) if launched without a directory", function(done){
fileOps.getNewestFile("", fileOps.getFileNamesSync("lib/test"), function(err, newest){
assert(err);
done();
});
});
it("executes callback(err) when you do stupid shit", function(done){
fileOps.getNewestFile({thisWontWork: "but not crash either"}, {lolz:"yeah..."}, function(err, newest){
assert(err);
// ENOENT: no such file or directory, stat 'C:\**\factorioClusterio\lib\test\undefined'
done();
});
});
});
}); });