b8686aeff0
* set up jest dependencies * add parser option to eslint to support es6 * migrate first test to jest * migrate the argv test to shpec * convert the env and literal store tests * convert the file-store tests * convert the memory-store tests * convert the hierarchy tests * convert the provider-save test * convert the complete test * convert the provider test * convert the conf test * tweak a test that was no longer working (context changed) * replace in place the helpers file * remove vows dependency * update the test invocation to rely on jest * update the argv test to be able to use the jest --verbose option * Some tweaks to the test to have them working * Update node version tested (+10 +12 -9) * Replace const by var until we drop 0.10/0.12/4 node * Replace let by var until we drop 0.10/0.12/4 node
159 lines
4.6 KiB
JavaScript
159 lines
4.6 KiB
JavaScript
/*
|
|
* hierarchy-test.js: Basic tests for hierarchical file stores.
|
|
*
|
|
* (C) 2011, Charlie Robbins and the Contributors.
|
|
*
|
|
*/
|
|
|
|
var fs = require('fs');
|
|
var path = require('path');
|
|
var spawn = require('child_process').spawn;
|
|
var nconf = require('../lib/nconf');
|
|
|
|
var configDir = path.join(__dirname, 'fixtures', 'hierarchy');
|
|
var globalConfig = path.join(configDir, 'global.json');
|
|
var userConfig = path.join(configDir, 'user.json');
|
|
|
|
describe('nconf/hierarchy, When using nconf', () => {
|
|
it("configured with two file stores, should have the appropriate keys present", () => {
|
|
nconf.add('user', {type: 'file', file: userConfig});
|
|
nconf.add('global', {type: 'file', file: globalConfig});
|
|
nconf.load();
|
|
|
|
expect(nconf.get('title')).toEqual('My specific title');
|
|
expect(nconf.get('color')).toEqual('green');
|
|
expect(nconf.get('movie')).toEqual('Kill Bill');
|
|
|
|
});
|
|
it("configured with two file stores using `file` should have the appropriate keys present", () => {
|
|
nconf.file('user', userConfig);
|
|
nconf.file('global', globalConfig);
|
|
nconf.load();
|
|
|
|
expect(nconf.get('title')).toEqual('My specific title');
|
|
expect(nconf.get('color')).toEqual('green');
|
|
expect(nconf.get('movie')).toEqual('Kill Bill');
|
|
|
|
});
|
|
|
|
it("configured with .argv(), .env() and .file() should not persist information passed in to process.env and process.argv to disk",
|
|
done => {
|
|
var configFile = path.join(__dirname, 'fixtures', 'load-save.json');
|
|
var script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-load-save.js');
|
|
var argv = ['--foo', 'foo', '--bar', 'bar'];
|
|
var data = '';
|
|
|
|
try {
|
|
fs.unlinkSync(configFile)
|
|
}
|
|
catch (ex) {
|
|
// No-op
|
|
}
|
|
|
|
var child = spawn('node', [script].concat(argv));
|
|
|
|
child.stdout.on('data', function (d) {
|
|
data += d;
|
|
});
|
|
|
|
child.on('close', function () {
|
|
fs.readFile(configFile, 'utf8', (err, ondisk) => {
|
|
expect(data).toEqual('foo');
|
|
expect(JSON.parse(ondisk)).toEqual({
|
|
database: {
|
|
host: '127.0.0.1',
|
|
port: 5984
|
|
}
|
|
});
|
|
});
|
|
done();
|
|
});
|
|
|
|
});
|
|
|
|
it("configured with .argv(), .file() and invoked with nested command line options, should merge nested objects",
|
|
done => {
|
|
var script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-load-merge.js');
|
|
var argv = ['--candy:something', 'foo', '--candy:something5:second', 'bar'];
|
|
var data = '';
|
|
|
|
var child = spawn('node', [script].concat(argv));
|
|
|
|
child.stdout.on('data', function (d) {
|
|
data += d;
|
|
});
|
|
|
|
child.on('close', function () {
|
|
expect(JSON.parse(data)).toEqual({
|
|
apples: true,
|
|
candy: {
|
|
something: 'foo',
|
|
something1: true,
|
|
something2: true,
|
|
something5: {
|
|
first: 1,
|
|
second: 'bar'
|
|
}
|
|
}
|
|
});
|
|
done();
|
|
});
|
|
});
|
|
it("configured with .argv() and separator, .file() and invoked with nested command line options should merge nested objects", done => {
|
|
|
|
var script = path.join(__dirname, 'fixtures',
|
|
'scripts', 'nconf-hierarchical-load-merge-with-separator.js');
|
|
var argv = ['--candy--something', 'foo', '--candy--something5--second', 'bar'];
|
|
var data = '';
|
|
process.env.candy__bonbon = 'sweet';
|
|
var child = spawn('node', [script].concat(argv));
|
|
delete process.env.candy__bonbon;
|
|
child.stdout.on('data', function (d) {
|
|
data += d;
|
|
});
|
|
|
|
child.on('close', function () {
|
|
console.log(data)
|
|
expect(JSON.parse(data)).toEqual({
|
|
apples: true,
|
|
candy: {
|
|
bonbon: 'sweet',
|
|
something: 'foo',
|
|
something1: true,
|
|
something2: true,
|
|
something5: {
|
|
first: 1,
|
|
second: 'bar'
|
|
}
|
|
}
|
|
});
|
|
done();
|
|
});
|
|
});
|
|
|
|
it("configured with .file(), .defaults() should deep merge objects should merge nested objects ", done => {
|
|
var script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-defaults-merge.js');
|
|
var data = '';
|
|
var child = spawn('node', [script]);
|
|
|
|
child.stdout.on('data', function (d) {
|
|
data += d;
|
|
});
|
|
|
|
child.on('close', function () {
|
|
expect(JSON.parse(data)).toEqual({
|
|
candy: {
|
|
something: 'much better something for you',
|
|
something1: true,
|
|
something2: true,
|
|
something18: 'completely unique',
|
|
something5: {
|
|
first: 1,
|
|
second: 99
|
|
}
|
|
}
|
|
});
|
|
done();
|
|
});
|
|
})
|
|
})
|