fileOps: Fixed crash on invalid filenames, added test for it

This commit is contained in:
Danielv123 committed 2018-03-29 14:39:39 +02:00
1 parent e1ab5b4d91
commit e55bf71431
2 files changed
+31 -27

No files matched your search

+29 -26
View File
@@ -73,35 +73,38 @@ var deleteFolderRecursiveSync = function(path) {
var deleteFolderRecursive = function(path){ var deleteFolderRecursive = function(path){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.readdir(path, (err, files) => { fs.readdir(path, (err, files) => {
if(err) reject(err); if(err){
let thingsToDelete = files.length; reject(err);
files.forEach(file => { } else {
let curPath = path + "/" + file; let thingsToDelete = files.length;
fs.stat(curPath, (err, stat) => { files.forEach(file => {
if(!err){ let curPath = path + "/" + file;
if(stat.isDirectory()){ fs.stat(curPath, (err, stat) => {
// recurse if(!err){
deleteFolderRecursive(curPath).then(() => { if(stat.isDirectory()){
whenFilesDeleted(); // recurse
}); deleteFolderRecursive(curPath).then(() => {
whenFilesDeleted();
}).catch(e => console.log(e));
} else {
fs.unlink(curPath, () => {
// console.log("Deleted "+curPath);
whenFilesDeleted();
});
}
} else { } else {
fs.unlink(curPath, () => { resolve(false);
// console.log("Deleted "+curPath);
whenFilesDeleted();
});
} }
} else {
resolve(false);
}
});
});
whenFilesDeleted();
function whenFilesDeleted(){
if(!thingsToDelete--){// var-- instead of --var because we are calling the function once too much and need the delay
fs.rmdir(path, () => {
resolve();
// console.log("Deleted "+path);
}); });
});
whenFilesDeleted();
function whenFilesDeleted(){
if(!thingsToDelete--){// var-- instead of --var because we are calling the function once too much and need the delay
fs.rmdir(path, () => {
resolve();
// console.log("Deleted "+path);
});
}
} }
} }
}); });
+2 -1
View File
@@ -115,6 +115,7 @@ describe("fileOps.js", function(){
describe("fileOps.deleteFolderRecursive(path)", function(){ describe("fileOps.deleteFolderRecursive(path)", function(){
it("deletes a folder and all files and folders in it, just like sync version", async function(){ it("deletes a folder and all files and folders in it, just like sync version", async function(){
setupTestingEnv(); setupTestingEnv();
assert(fs.statSync(path.resolve(__dirname, "test")));
let x = await fileOps.deleteFolderRecursive(path.resolve(__dirname, "test")); let x = await fileOps.deleteFolderRecursive(path.resolve(__dirname, "test"));
assert.throws(function(){ assert.throws(function(){
fs.statSync(path.resolve(__dirname, "test")); fs.statSync(path.resolve(__dirname, "test"));
@@ -122,7 +123,7 @@ describe("fileOps.js", function(){
assert(x === undefined, "Promise version of this function does not have a return value upon success"); assert(x === undefined, "Promise version of this function does not have a return value upon success");
return true; return true;
}).timeout(5000); }).timeout(5000);
it("throws like a promise if you give it an invalid path", async function(){ it("throws like a promise does if you give it an invalid path", async function(){
let x = await fileOps.deleteFolderRecursive("Look this path is fake!") let x = await fileOps.deleteFolderRecursive("Look this path is fake!")
.catch(e => { .catch(e => {
assert(e); assert(e);