haraka-config
Advanced tools
Comparing version 1.0.14 to 1.0.15
## 1.0.15 - 2017-09-21 | ||
- additional test for 'missing json loads yaml' | ||
- modify get_path_to_config_dir regex to also match windows paths | ||
- add tests for get_path_to_config_dir | ||
- configs w/o .ext or declared type default to flat | ||
- add test for json/yaml !filename overloads | ||
## 1.0.14 - 2017-09-19 | ||
@@ -3,0 +11,0 @@ |
@@ -38,3 +38,3 @@ 'use strict'; | ||
function get_path_to_config_dir () { | ||
cfreader.get_path_to_config_dir = function () { | ||
if (process.env.HARAKA) { | ||
@@ -53,3 +53,3 @@ // console.log('process.env.HARAKA: ' + process.env.HARAKA); | ||
// these work when this is loaded with require('haraka-config') | ||
if (/node_modules\/haraka-config$/.test(__dirname)) { | ||
if (/node_modules[\\/]haraka-config$/.test(__dirname)) { | ||
config_dir_candidates = [ | ||
@@ -75,3 +75,3 @@ path.join(__dirname, '..', '..', 'config'), // haraka/Haraka/* | ||
} | ||
get_path_to_config_dir(); | ||
exports.get_path_to_config_dir(); | ||
// console.log('cfreader.config_path: ' + cfreader.config_path); | ||
@@ -170,22 +170,18 @@ | ||
cfreader.get_cache_key = function (name, options) { | ||
let result; | ||
// Ignore options etc. if this is an overriden value | ||
if (cfreader._overrides[name]) { | ||
result = name; | ||
} | ||
else if (options) { | ||
if (cfreader._overrides[name]) return name; | ||
if (options) { | ||
// this ordering of objects isn't guaranteed to be consistent, but I've | ||
// heard that it typically is. | ||
result = name + JSON.stringify(options); | ||
return name + JSON.stringify(options); | ||
} | ||
else if (cfreader._read_args[name] && cfreader._read_args[name].options) { | ||
result = name + JSON.stringify(cfreader._read_args[name].options); | ||
if (cfreader._read_args[name] && cfreader._read_args[name].options) { | ||
return name + JSON.stringify(cfreader._read_args[name].options); | ||
} | ||
else { | ||
result = name; | ||
} | ||
return result; | ||
}; | ||
return name; | ||
} | ||
@@ -328,4 +324,8 @@ cfreader.read_config = function (name, type, cb, options) { | ||
cfreader.get_filetype_reader = function (type) { | ||
if (/^(list|value|data)$/.test(type)) { | ||
return require('./readers/flat'); | ||
switch (type) { | ||
case 'list': | ||
case 'value': | ||
case 'data': | ||
case '': | ||
return require('./readers/flat'); | ||
} | ||
@@ -401,5 +401,4 @@ return require('./readers/' + type); | ||
// Allow JSON files to create or overwrite other | ||
// configuration file data by prefixing the | ||
// outer variable name with ! e.g. !smtp.ini | ||
// Allow JSON files to create or overwrite other config file data | ||
// by prefixing the outer variable name with ! e.g. !smtp.ini | ||
const keys = Object.keys(result); | ||
@@ -410,5 +409,5 @@ for (let j=0; j<keys.length; j++) { | ||
// Overwrite the config cache for this filename | ||
console.log('Overriding file ' + fn + ' with config from ' + name); | ||
console.log(`Overriding file ${fn} with config from ${name}`); | ||
cfreader._config_cache[path.join(cp, fn)] = result[keys[j]]; | ||
} | ||
}; |
@@ -6,3 +6,3 @@ { | ||
"description": "Haraka's config file loader", | ||
"version": "1.0.14", | ||
"version": "1.0.15", | ||
"homepage": "http://haraka.github.io", | ||
@@ -9,0 +9,0 @@ "repository": { |
'use strict'; | ||
process.env.NODE_ENV = 'test' | ||
const fs = require('fs'); | ||
@@ -24,2 +22,3 @@ const path = require('path'); | ||
function setUp (done) { | ||
process.env.NODE_ENV = 'test' | ||
process.env.HARAKA = ''; | ||
@@ -379,2 +378,8 @@ clearRequireCache(); | ||
'reloads when file in dir is touched' : function (test) { | ||
if (/darwin/.test(process.platform)) { | ||
// due to differences in fs.watch, this test is not reliable on | ||
// Mac OS X | ||
test.done(); | ||
return; | ||
} | ||
test.expect(6); | ||
@@ -411,1 +416,24 @@ const self = this; | ||
} | ||
exports.jsonOverrides = { | ||
'setUp' : setUp, | ||
'no override for smtpgreeting': function (test) { | ||
test.expect(1); | ||
// console.log(this.config); | ||
test.deepEqual( | ||
this.config.get('smtpgreeting', 'list'), | ||
[] | ||
); | ||
test.done(); | ||
}, | ||
'with smtpgreeting override': function (test) { | ||
test.expect(1); | ||
const main = this.config.get('main.json'); | ||
console.log(main); | ||
test.deepEqual( | ||
this.config.get('smtpgreeting', 'list'), | ||
[ 'this is line one', 'this is line two' ] | ||
); | ||
test.done(); | ||
} | ||
} |
'use strict'; | ||
process.env.NODE_ENV === 'test'; | ||
// const path = require('path'); | ||
const _set_up = function (done) { | ||
function _setUp (done) { | ||
process.env.NODE_ENV === 'test'; | ||
this.cfreader = require('../configfile'); | ||
this.opts = { booleans: ['main.bool_true','main.bool_false'] }; | ||
done(); | ||
}; | ||
} | ||
exports.load_config = { | ||
setUp: _set_up, | ||
setUp: _setUp, | ||
'non-exist.ini empty' : function (test) { | ||
@@ -161,3 +162,3 @@ test.expect(1); | ||
exports.get_filetype_reader = { | ||
setUp: _set_up, | ||
setUp: _setUp, | ||
'binary': function (test) { | ||
@@ -222,3 +223,3 @@ test.expect(2); | ||
exports.non_existing = { | ||
setUp: _set_up, | ||
setUp: _setUp, | ||
@@ -285,3 +286,3 @@ 'empty object for JSON files': function (test) { | ||
exports.get_cache_key = { | ||
setUp: _set_up, | ||
setUp: _setUp, | ||
'no options is the name': function (test) { | ||
@@ -309,3 +310,3 @@ test.expect(1); | ||
exports.regex = { | ||
setUp: _set_up, | ||
setUp: _setUp, | ||
'section': function (test) { | ||
@@ -381,9 +382,50 @@ test.expect(4); | ||
exports.bad_config = { | ||
setUp: _set_up, | ||
setUp: _setUp, | ||
'bad.yaml returns empty' : function (test) { | ||
test.expect(1); | ||
const res = this.cfreader.load_config('test/config/bad.yaml'); | ||
test.deepEqual(res, {}); | ||
test.deepEqual( | ||
this.cfreader.load_config('test/config/bad.yaml'), | ||
{} | ||
); | ||
test.done(); | ||
}, | ||
} | ||
exports.overrides = { | ||
setUp: _setUp, | ||
'missing json loads yaml instead' : function (test) { | ||
test.expect(1); | ||
test.deepEqual( | ||
this.cfreader.load_config('test/config/override.json'), | ||
{ has: { value: true } }); | ||
test.done(); | ||
}, | ||
} | ||
exports.get_path_to_config_dir = { | ||
setUp: _setUp, | ||
'Haraka runtime (env.HARAKA=*)' : function (test) { | ||
test.expect(1); | ||
process.env.HARAKA = '/etc/'; | ||
this.cfreader.get_path_to_config_dir(); | ||
test.ok(/etc.config$/.test(this.cfreader.config_path), this.cfreader.config_path); | ||
delete process.env.HARAKA; | ||
test.done(); | ||
}, | ||
'NODE_ENV=test' : function (test) { | ||
test.expect(1); | ||
process.env.NODE_ENV === 'test'; | ||
this.cfreader.get_path_to_config_dir(); | ||
test.ok(/haraka-config.test.config$/.test(this.cfreader.config_path), this.cfreader.config_path); | ||
delete process.env.NODE_ENV; | ||
test.done(); | ||
}, | ||
'no $ENV defaults to ./config (if present) or ./' : function (test) { | ||
test.expect(1); | ||
delete process.env.HARAKA; | ||
delete process.env.NODE_ENV; | ||
this.cfreader.get_path_to_config_dir(); | ||
test.ok(/haraka-config$/.test(this.cfreader.config_path), this.cfreader.config_path); | ||
test.done(); | ||
}, | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
77501
47
1782
32