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
+77 -37

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");
process.exit(1);
} else if (command == "list") {
console.dir(fileOps.getDirectories("./instances/"));
console.dir(fileOps.getDirectoriesSync("./instances/"));
process.exit(1);
} else if (command == "delete") {
if (!process.argv[3]) {
console.error("Usage: node client.js delete [instance]");
process.exit(1);
} 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]);
process.exit(1);
} else {
@@ -216,7 +216,8 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\
// Spawn factorio server
//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
serverprocess = child_process.spawn(
'./' + config.factorioDirectory + '/bin/x64/factorio', [
@@ -301,7 +302,7 @@ _.once(instanceManagement);
function instanceManagement() {
console.log("Started instanceManagement();");
// load plugins and execute onLoad event
let pluginDirectories = fileOps.getDirectories("./sharedPlugins/");
let pluginDirectories = fileOps.getDirectoriesSync("./sharedPlugins/");
let plugins = [];
for(i=0; i<pluginDirectories.length; i++) {
let I = i
+35 -24
View File
@@ -4,51 +4,62 @@ var path = require("path");
// gets newest file in a directory
// dir is directory
// files is array of filenames
// callback(filename string);
// callback(err, filename string);
var getNewestFile = function(dir, files, callback) {
if (!callback) return;
if (!callback) throw new Error("Missing callback");
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 checked = 0;
fs.stat(dir + newest.file, function(err, stats) {
newest.mtime = stats.mtime;
for (var i = 0; i < files.length; i++) {
var file = files[i];
(function(file) {
fs.stat(file, function(err, stats) {
++checked;
if (stats.mtime.getTime() > newest.mtime.getTime()) {
newest = { file : file, mtime : stats.mtime };
}
if (checked == files.length) {
callback(newest);
}
});
})(dir + file);
if(err) {
callback(err);
} else {
newest.mtime = stats.mtime;
for (var i = 0; i < files.length; i++) {
var file = files[i];
(function(file) {
fs.stat(file, function(err, stats) {
++checked;
if (stats.mtime.getTime() > newest.mtime.getTime()) {
newest = { file : file, mtime : stats.mtime };
}
if (checked == files.length) {
callback(undefined,newest);
}
});
})(dir + file);
}
}
});
}
// get all directories in folder
var getDirectories = function(srcpath) {
// get names of all directories in directory
var getDirectoriesSync = function(srcpath) {
return fs.readdirSync(srcpath).filter(function (file) {
return fs.statSync(path.join(srcpath, file)).isDirectory();
});
}
// get names of all files in a directory
var getFileNamesSync = function(srcpath) {
return fs.readdirSync(srcpath).filter(function (file) {
return !fs.statSync(path.join(srcpath, file)).isDirectory();
});
}
// does what it says on the tin
var deleteFolderRecursive = function(path) {
var deleteFolderRecursiveSync = function(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
deleteFolderRecursiveSync(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
@@ -59,7 +70,7 @@ var deleteFolderRecursive = function(path) {
module.exports = {
getNewestFile:getNewestFile,
getDirectories:getDirectories,
deleteFolderRecursive:deleteFolderRecursive,
getDirectoriesSync:getDirectoriesSync,
deleteFolderRecursiveSync:deleteFolderRecursiveSync,
getFileNamesSync:getFileNamesSync,
}
}
+37 -9
View File
@@ -2,9 +2,9 @@ var assert = require('assert');
var fileOps = require('./fileOps');
describe("fileOps.js", function(){
describe("fileOps.getDirectories()", function(){
describe("fileOps.getDirectoriesSync()", 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
assert(typeof directories == "object");
@@ -13,11 +13,16 @@ describe("fileOps.js", function(){
assert(directories[0] == "another folder" && directories[1] == "folder");
});
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.
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(){
it("gets names of files in directory", function(){
@@ -31,13 +36,36 @@ describe("fileOps.js", function(){
}, Error);
});
});
/*describe("fileOps.getNewestFile()", function(){
describe("fileOps.getNewestFile()", function(){
it("gets the newest file in a directory async", function(done){
fileOps.getNewestFile("lib/test", fileOps.getFileNames("lib/test"), function(newest){
assert(newest);
fileOps.getNewestFile("lib/test", fileOps.getFileNamesSync("lib/test"), function(err, newest){
assert(newest && typeof newest.file == "string");
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();
});
});
});
});