diff --git a/lib/fileOps.js b/lib/fileOps.js index 6ce22672..65250fcf 100644 --- a/lib/fileOps.js +++ b/lib/fileOps.js @@ -40,7 +40,7 @@ var getNewestFile = function(dir, files, callback) { } }); } - + // get names of all directories in directory var getDirectoriesSync = function(srcpath) { return fs.readdirSync(srcpath).filter(function (file) { @@ -65,6 +65,9 @@ var deleteFolderRecursiveSync = function(path) { } }); fs.rmdirSync(path); + return true; + } else { + return false; } }; diff --git a/lib/fileOps.spec.js b/lib/fileOps.spec.js index 112e140e..77ffb249 100644 --- a/lib/fileOps.spec.js +++ b/lib/fileOps.spec.js @@ -1,5 +1,27 @@ var assert = require('assert'); var fileOps = require('./fileOps'); +var fs = require('fs'); + +// create folders for testing +if (!fs.existsSync("lib/test")){ + fs.mkdirSync("lib/test"); +} +if (!fs.existsSync("lib/test/folder")){ + fs.mkdirSync("lib/test/folder"); +} +if (!fs.existsSync("lib/test/another folder")){ + fs.mkdirSync("lib/test/another folder"); +} +if (!fs.existsSync("lib/test/file.txt")){ + fs.writeFileSync("lib/test/file.txt", "contents"); +} +if (!fs.existsSync("lib/test/another file.txt")){ + fs.writeFileSync("lib/test/another file.txt", "more contents"); +} +// we need this xfile to test a branch in fileOps.getNewestFile()s propagation +if (!fs.existsSync("lib/test/xfile.txt")){ + fs.writeFileSync("lib/test/xfile.txt", "contents"); +} describe("fileOps.js", function(){ describe("fileOps.getDirectoriesSync()", function(){ @@ -68,4 +90,17 @@ describe("fileOps.js", function(){ }); }); }); + describe("fileOps.deleteFolderRecursiveSync(path)", function(){ + it("deletes a folder and all files and folders in it", function(){ + let x = fileOps.deleteFolderRecursiveSync("lib/test"); + assert.throws(function(){ + fs.statSync("lib/test"); + }, Error); + assert(x); + }); + it("returns false if you give it an invalid path", function(){ + let x = fileOps.deleteFolderRecursiveSync("This isn't a real path"); + assert(!x); + }); + }); }); diff --git a/lib/test/another file.txt b/lib/test/another file.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/lib/test/file.txt b/lib/test/file.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/package.json b/package.json index 0fb72b3f..012ddf23 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "test": "npm run test-unit", "test-unit": "SET NODE_ENV=test && mocha --recursive \"./{,!(node_modules)/**/}*.spec.js\" || echo .", - "start": "echo Run node client.js start [instance] to start slaves && node master.js" + "start": "echo Run node client.js start [instance] to start slaves && node master.js", + "cover": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- lib/*.js -R spec" }, "keywords": [ "factorio" @@ -40,6 +41,7 @@ }, "devDependencies": { "chai": "^3.5.0", + "istanbul": "^0.4.5", "mocha": "^3.4.1", "mock-fs": "^4.3.0" }