Skip to content

Commit

Permalink
WIP: eslint-no-var (#29)
Browse files Browse the repository at this point in the history
* extend lint tests to readers/*.js
* extend test coverage a little more
  • Loading branch information
msimerson committed Sep 21, 2017
1 parent fced0c4 commit ce7243a
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 90 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
},
"scripts": {
"test": "./run_tests",
"lint": "./node_modules/.bin/eslint *.js test/*.js test/*/*.js",
"lintfix": "./node_modules/.bin/eslint --fix *.js test/*.js test/*/*.js",
"lint": "./node_modules/.bin/eslint *.js readers/*.js test/*.js test/*/*.js",
"lintfix": "./node_modules/.bin/eslint --fix *.js readers/*.js test/*.js test/*/*.js",
"cover": "./node_modules/.bin/istanbul cov run_tests"
}
}
2 changes: 1 addition & 1 deletion readers/binary.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var fs = require('fs');
const fs = require('fs');

exports.load = function (name) {
return fs.readFileSync(name);
Expand Down
25 changes: 12 additions & 13 deletions readers/flat.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
'use strict';

var fs = require('fs');
const fs = require('fs');

exports.load = function (name, type, options, regex) {
var result = [];
let result = [];

var data = fs.readFileSync(name, 'UTF-8');
let data = fs.readFileSync(name, 'UTF-8');
if (type === 'data') {
while (data.length > 0) {
var match = data.match(/^([^\r\n]*)\r?\n?/);
const match = data.match(/^([^\r\n]*)\r?\n?/);
result.push(match[1]);
data = data.slice(match[0].length);
}
return result;
}

data.split(/\r\n|\r|\n/).forEach( function(line) {
var line_data;
data.split(/\r\n|\r|\n/).forEach( function (line) {
if (regex.comment.test(line)) { return; }
if (regex.blank.test(line)) { return; }

line_data = regex.line.exec(line);
if (!line_data) { return; }
const line_data = regex.line.exec(line);
if (!line_data) return;

result.push(line_data[1].trim());
});

if (result.length && type !== 'list' && type !== 'data') {
result = result[0];
if (options && in_array(result, options.booleans)) {
result = regex.is_truth.test(result);
return regex.is_truth.test(result);
}
else if (regex.is_integer.test(result)) {
result = parseInt(result, 10);
if (regex.is_integer.test(result)) {
return parseInt(result, 10);
}
else if (regex.is_float.test(result)) {
result = parseFloat(result);
if (regex.is_float.test(result)) {
return parseFloat(result);
}
return result;
}
Expand Down
136 changes: 68 additions & 68 deletions readers/ini.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

var fs = require('fs');
const fs = require('fs');

exports.load = function (name, options, regex) {
var result = { main: {} };
var current_sect = result.main;
var current_sect_name = 'main';
let result = { main: {} };
let current_sect = result.main;
let current_sect_name = 'main';
this.bool_matches = [];
if (options && options.booleans) {
this.bool_matches = options.booleans.slice();
Expand All @@ -14,72 +14,72 @@ exports.load = function (name, options, regex) {
// Initialize any booleans
result = this.init_booleans(options, result);

var match;
var setter;
var pre = '';
let match;
let setter;
let pre = '';

fs.readFileSync(name, 'UTF-8')
.split(/\r\n|\r|\n/)
.forEach(function(line) {
if (regex.comment.test(line)) { return; }
if (regex.blank.test(line) ) { return; }

match = regex.section.exec(line);
if (match) {
if (!result[match[1]]) result[match[1]] = {};
current_sect = result[match[1]];
current_sect_name = match[1];
return;
}

if (regex.continuation.test(line)) {
pre += line.replace(regex.continuation, '');
return;
}

line = pre + line;
pre = '';

match = regex.param.exec(line);
if (!match) {
exports.logger(
.split(/\r\n|\r|\n/)
.forEach(function (line) {
if (regex.comment.test(line)) return;
if (regex.blank.test(line) ) return;

match = regex.section.exec(line);
if (match) {
if (!result[match[1]]) result[match[1]] = {};
current_sect = result[match[1]];
current_sect_name = match[1];
return;
}

if (regex.continuation.test(line)) {
pre += line.replace(regex.continuation, '');
return;
}

line = pre + line;
pre = '';

match = regex.param.exec(line);
if (!match) {
exports.logger(
'Invalid line in config file \'' + name + '\': ' + line);
return;
}

var is_array_match = regex.is_array.exec(match[1]);
if (is_array_match) {
setter = function(key, value) {
key = key.replace('[]', '');
if (! current_sect[key]) current_sect[key] = [];
current_sect[key].push(value);
};
}
else {
setter = function (key, value) { current_sect[key] = value; };
}

if (options && Array.isArray(options.booleans) &&
return;
}

const is_array_match = regex.is_array.exec(match[1]);
if (is_array_match) {
setter = function (key, value) {
key = key.replace('[]', '');
if (! current_sect[key]) current_sect[key] = [];
current_sect[key].push(value);
};
}
else {
setter = function (key, value) { current_sect[key] = value; };
}

if (options && Array.isArray(options.booleans) &&
(
exports.bool_matches.indexOf(current_sect_name + '.' + match[1]) !== -1
||
exports.bool_matches.indexOf('*.' + match[1]) !== -1
)) {
current_sect[match[1]] = regex.is_truth.test(match[2]);
current_sect[match[1]] = regex.is_truth.test(match[2]);
// var msg = 'Using boolean ' + current_sect[match[1]] +
// ' for ' + current_sect_name + '.' + match[1] + '=' + match[2];
// exports.logger(msg, 'logdebug');
}
else if (regex.is_integer.test(match[2])) {
setter(match[1], parseInt(match[2], 10));
}
else if (regex.is_float.test(match[2])) {
setter(match[1], parseFloat(match[2]));
}
else {
setter(match[1], match[2]);
}
});
}
else if (regex.is_integer.test(match[2])) {
setter(match[1], parseInt(match[2], 10));
}
else if (regex.is_float.test(match[2])) {
setter(match[1], parseFloat(match[2]));
}
else {
setter(match[1], match[2]);
}
});

return result;
};
Expand All @@ -94,19 +94,19 @@ exports.init_booleans = function (options, result) {
if (!Array.isArray(options.booleans)) return result;

// console.log(options.booleans);
for (var i=0; i<options.booleans.length; i++) {
var m = /^(?:([^\. ]+)\.)?(.+)/.exec(options.booleans[i]);
for (let i=0; i<options.booleans.length; i++) {
const m = /^(?:([^. ]+)\.)?(.+)/.exec(options.booleans[i]);
if (!m) continue;

var section = m[1] || 'main';
var key = m[2];
let section = m[1] || 'main';
let key = m[2];

var bool_default = section[0] === '+' ? true
: key[0] === '+' ? true
: false;
const bool_default = section[0] === '+' ? true
: key[0] === '+' ? true
: false;

if (section.match(/^(\-|\+)/)) section = section.substr(1);
if ( key.match(/^(\-|\+)/)) key = key.substr(1);
if (section.match(/^(-|\+)/)) section = section.substr(1);
if ( key.match(/^(-|\+)/)) key = key.substr(1);

if (section === '*') continue; // wildcard, don't initialize

Expand Down
4 changes: 2 additions & 2 deletions readers/json.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var fs = require('fs');
const fs = require('fs');

exports.load = function(name) {
exports.load = function (name) {
return JSON.parse(fs.readFileSync(name));
};

Expand Down
6 changes: 3 additions & 3 deletions readers/yaml.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var fs = require('fs');
var yaml = require('js-yaml');
const fs = require('fs');
const yaml = require('js-yaml');

exports.load = function(name) {
exports.load = function (name) {
return yaml.safeLoad(fs.readFileSync(name, 'utf8'));
};

Expand Down
29 changes: 28 additions & 1 deletion test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function clearRequireCache () {
function setUp (done) {
process.env.NODE_ENV = 'test'
process.env.HARAKA = '';
process.env.WITHOUT_CONFIG_CACHE = '1';
clearRequireCache();
this.config = require('../config');
done();
Expand Down Expand Up @@ -218,6 +219,7 @@ exports.get = {
_test_get(test, 'test', null, null, null, null);
},
'test (non-existing, cached)' : function (test) {
process.env.WITHOUT_CONFIG_CACHE= '';
test.expect(1);
const cfg = this.config.get('test', null, null);
test.deepEqual(cfg, null);
Expand All @@ -238,7 +240,31 @@ exports.get = {
intlist: [ '123', '456', '789' ],
},
'foo.com': { is_bool: 'true' },
'bar.com': { is_bool: 'false' }
'bar.com': { is_bool: 'false' },
has_nums: { integer: 454, float: 10.5 },
});
},

'test.ini, opts' : function (test) {
_test_get(test, 'test.ini', 'ini', null, {
booleans: [
'*.bool_true',
'*.bool_false',
]
}, {
main: { bool_true: true, bool_false: false, str_true: 'true', str_false: 'false' },
sect1: { bool_true: true, bool_false: false, str_true: 'true', str_false: 'false' },
whitespace: { str_no_trail: 'true', str_trail: 'true' },
funnychars: { 'results.auth/auth_base.fail': 'fun' },
empty_values: { first: undefined, second: undefined },
has_ipv6: { '2605:ae00:329::2': undefined },
array_test: {
hostlist: [ 'first_host', 'second_host', 'third_host' ],
intlist: [ '123', '456', '789' ],
},
'foo.com': { is_bool: 'true' },
'bar.com': { is_bool: 'false' },
has_nums: { integer: 454, float: 10.5 },
});
},

Expand Down Expand Up @@ -425,6 +451,7 @@ exports.jsonOverrides = {
},
'with smtpgreeting override': function (test) {
test.expect(1);
process.env.WITHOUT_CONFIG_CACHE='';
const main = this.config.get('main.json');
console.log(main);
test.deepEqual(
Expand Down
4 changes: 4 additions & 0 deletions test/config/test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ is_bool=true

[bar.com]
is_bool=false

[has_nums]
integer = 454
float=10.5

0 comments on commit ce7243a

Please sign in to comment.