Skip to content

Commit

Permalink
Fixed --ignore-watch flag and written tests for --ignore-watch
Browse files Browse the repository at this point in the history
  • Loading branch information
livankrekh committed Jun 22, 2018
1 parent 76c731f commit cae6313
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/API/Modules/flagWatch.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
Expand Down
55 changes: 55 additions & 0 deletions test/programmatic/flagWatch.mocha.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit cae6313

Please sign in to comment.