From cae63134199dfc0204b43ed74594625aabfb5687 Mon Sep 17 00:00:00 2001 From: Livii Iabanzhi Date: Fri, 22 Jun 2018 18:00:09 +0300 Subject: [PATCH] Fixed --ignore-watch flag and written tests for --ignore-watch --- lib/API/Modules/flagWatch.js | 21 ++++++++--- test/programmatic/flagWatch.mocha.js | 55 ++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 test/programmatic/flagWatch.mocha.js diff --git a/lib/API/Modules/flagWatch.js b/lib/API/Modules/flagWatch.js index 6f8041c11..8603c9a63 100644 --- a/lib/API/Modules/flagWatch.js +++ b/lib/API/Modules/flagWatch.js @@ -1,17 +1,26 @@ var fs = require('fs'); -function handleFolders(folder, mas) -{ - if (fs.lstatSync(folder) && fs.lstatSync(folder).isDirectory()) - { +function handleFolders(folder, mas) { + if (!folder || !mas || folder.indexOf("node_modules") !== -1) + return ; + + try { + fs.accessSync(folder, fs.constants.R_OK); + } catch (err) { + return ; + } + + if (fs.statSync(folder) && fs.statSync(folder).isDirectory()) { fs.readdirSync(folder).forEach(file => { - if(fs.existsSync(folder + file + '/')) + if (fs.statSync(folder)["mode"] & 4 === 0) + return ; + if (fs.existsSync(folder + file + '/')) handleFolders(folder + file + '/', mas); else mas.push(folder + file); }); } else { - if (fs.lstatSync(folder).isFile()) { + if (fs.statSync(folder).isFile()) { mas.push(folder); } } diff --git a/test/programmatic/flagWatch.mocha.js b/test/programmatic/flagWatch.mocha.js new file mode 100644 index 000000000..01447b2d3 --- /dev/null +++ b/test/programmatic/flagWatch.mocha.js @@ -0,0 +1,55 @@ + +var should = require('should'); +var f_w = require('../../lib/API/Modules/flagWatch.js'); +var fs = require('fs'); + +describe('Flag --ignore-watch', function() { + + it('should return not empty result', function() { + var res = []; + f_w.handleFolders('./', res); + should(res).be.not.empty(); + }); + it('should not crash', function() { + var res = [] + f_w.handleFolders(); + f_w.handleFolders(res); + f_w.handleFolders(''); + f_w.handleFolders('lsdldmcsdf/amfkdmfk'); + }); + it('should give different results', function() { + var tmp_res = []; + var res = []; + f_w.handleFolders('./lib', res); + f_w.handleFolders('./examples', tmp_res); + should(res).not.equal(tmp_res); + }); + it('should not crash in case, when no access for file or directory by permissions', function() { + var fileStream; + + if (!fs.existsSync("noAccessDir")) + fs.mkdirSync("noAccessDir", 0777); + if (!fs.existsSync("noAccessDir/checkPermissions.txt")) { + fileStream = fs.createWriteStream("noAccessDir/checkPermissions.txt"); + fileStream.write("It's a temporary file for testing flag --ignore-watch in PM2"); + fileStream.end(); + } + fs.chmodSync('noAccessDir/checkPermissions.txt', 0000); + fs.chmodSync('noAccessDir', 0000); + + after(function () { + fs.chmodSync('noAccessDir', 0777); + fs.chmodSync('noAccessDir/checkPermissions.txt', 0777); + fs.unlinkSync('noAccessDir/checkPermissions.txt'); + fs.rmdirSync('noAccessDir/'); + }); + + f_w.handleFolders('noAccessDir/', []); + f_w.handleFolders('noAccessDir/checkPermissions.txt', []); + }); + it('should ignore node_modules folder', function() { + var res = []; + f_w.handleFolders('./node_modules', res); + should(res).be.empty(); + }); +}); \ No newline at end of file