2021-06-07 08:58:26 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import { spawn } from 'child_process'
|
|
|
|
import { Eltro as t, assert} from 'eltro'
|
|
|
|
import * as helpers from './helpers.mjs'
|
2021-06-22 19:25:00 +00:00
|
|
|
// import nconf from '../lib/nconf-o.mjs'
|
|
|
|
import Nconf from '../lib/nconf.mjs'
|
2021-06-07 08:58:26 +00:00
|
|
|
|
2021-06-22 19:25:00 +00:00
|
|
|
let files = [
|
2021-06-07 08:58:26 +00:00
|
|
|
helpers.fixture('merge/file1.json'),
|
|
|
|
helpers.fixture('merge/file2.json'),
|
|
|
|
];
|
2021-06-22 19:25:00 +00:00
|
|
|
let override = JSON.parse(fs.readFileSync(files[0]), 'utf8');
|
2021-06-07 08:58:26 +00:00
|
|
|
|
|
|
|
function assertSystemConf(options) {
|
|
|
|
return new Promise(function(res, rej) {
|
2021-06-22 19:25:00 +00:00
|
|
|
let env = null;
|
2021-06-07 08:58:26 +00:00
|
|
|
|
|
|
|
if (options.env) {
|
|
|
|
env = {}
|
|
|
|
Object.keys(process.env).forEach(function (key) {
|
|
|
|
env[key] = process.env[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.keys(options.env).forEach(function (key) {
|
|
|
|
env[key] = options.env[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:25:00 +00:00
|
|
|
let child = spawn('node', [options.script].concat(options.argv), {env: env});
|
2021-06-07 08:58:26 +00:00
|
|
|
child.stdout.once('data', data => {
|
|
|
|
res(data.toString())
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-22 19:25:00 +00:00
|
|
|
t.describe('When using nconf', function() {
|
|
|
|
t.test("calling add with the same store type and different options should use the new instance", function() {
|
|
|
|
let nconf = new Nconf()
|
|
|
|
nconf.file({ file: files[0] })
|
|
|
|
let old = nconf.using.get('file');
|
2021-06-07 08:58:26 +00:00
|
|
|
|
2021-06-22 19:25:00 +00:00
|
|
|
assert.ok(old)
|
|
|
|
assert.strictEqual(old.file, files[0]);
|
|
|
|
nconf.file({ file: files[1] });
|
2021-06-07 08:58:26 +00:00
|
|
|
|
2021-06-22 19:25:00 +00:00
|
|
|
let newOne = nconf.using.get('file');
|
|
|
|
assert.notStrictEqual(old, newOne);
|
|
|
|
assert.strictEqual(newOne.file, files[1]);
|
|
|
|
})
|
2021-06-07 08:58:26 +00:00
|
|
|
|
2021-06-22 19:25:00 +00:00
|
|
|
/*
|
2021-06-07 08:58:26 +00:00
|
|
|
t.test("respond with correct arg when 'env' is true", async function() {
|
|
|
|
let result = await assertSystemConf({
|
2021-06-22 19:25:00 +00:00
|
|
|
script: helpers.fixture('scripts/provider-env.mjs'),
|
2021-06-07 08:58:26 +00:00
|
|
|
env: {SOMETHING: 'foobar'}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.strictEqual(result.toString(), 'foobar')
|
|
|
|
});
|
|
|
|
|
|
|
|
t.test("respond with correct arg when 'env' is true and 'parseValues' option is true", function() {
|
2021-06-22 19:25:00 +00:00
|
|
|
let env = {
|
2021-06-07 08:58:26 +00:00
|
|
|
SOMETHING: 'foobar',
|
|
|
|
SOMEBOOL: 'true',
|
|
|
|
SOMENULL: 'null',
|
|
|
|
SOMEUNDEF: 'undefined',
|
|
|
|
SOMEINT: '3600',
|
|
|
|
SOMEFLOAT: '0.5',
|
|
|
|
SOMEBAD: '5.1a'
|
|
|
|
};
|
2021-06-22 19:25:00 +00:00
|
|
|
let oenv = {};
|
2021-06-07 08:58:26 +00:00
|
|
|
Object.keys(env).forEach(function (key) {
|
|
|
|
if (process.env[key]) oenv[key] = process.env[key];
|
|
|
|
process.env[key] = env[key];
|
|
|
|
});
|
2021-06-22 19:25:00 +00:00
|
|
|
let provider = new nconf.Provider().use('env', {parseValues: true});
|
2021-06-07 08:58:26 +00:00
|
|
|
Object.keys(env).forEach(function (key) {
|
|
|
|
delete process.env[key];
|
|
|
|
if (oenv[key]) process.env[key] = oenv[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.strictEqual(provider.get('SOMETHING'), 'foobar');
|
|
|
|
assert.strictEqual(provider.get('SOMEBOOL'), true);
|
|
|
|
assert.notStrictEqual(provider.get('SOMEBOOL'), 'true');
|
|
|
|
assert.strictEqual(provider.get('SOMENULL'), null);
|
|
|
|
assert.strictEqual(provider.get('SOMEUNDEF'), undefined);
|
|
|
|
assert.strictEqual(provider.get('SOMEINT'), 3600);
|
|
|
|
assert.strictEqual(provider.get('SOMEFLOAT'), .5);
|
|
|
|
assert.strictEqual(provider.get('SOMEBAD'), '5.1a');
|
|
|
|
});
|
|
|
|
|
|
|
|
t.describe("an instance of 'nconf.Provider'", function() {
|
|
|
|
t.describe("the merge() method", function() {
|
|
|
|
t.test("should have the result merged in", function() {
|
2021-06-22 19:25:00 +00:00
|
|
|
let provider = new nconf.Provider().use('file', {file: files[1]});
|
2021-06-07 08:58:26 +00:00
|
|
|
provider.load();
|
|
|
|
provider.merge(override);
|
|
|
|
helpers.assertMerged(null, provider.stores.file.store);
|
|
|
|
assert.strictEqual(provider.stores.file.store.candy.something, 'file1');
|
|
|
|
});
|
|
|
|
|
|
|
|
t.test("should merge Objects over null", function() {
|
2021-06-22 19:25:00 +00:00
|
|
|
let provider = new nconf.Provider().use('file', {file: files[1]});
|
2021-06-07 08:58:26 +00:00
|
|
|
provider.load();
|
|
|
|
provider.merge(override);
|
|
|
|
assert.strictEqual(provider.stores.file.store.unicorn.exists, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
})
|
|
|
|
t.describe("the load() method", function() {
|
|
|
|
t.test("should respect the hierarchy when sources are passed in", function() {
|
2021-06-22 19:25:00 +00:00
|
|
|
let provider = new nconf.Provider({
|
2021-06-07 08:58:26 +00:00
|
|
|
sources: {
|
|
|
|
user: {
|
|
|
|
type: 'file',
|
|
|
|
file: files[0]
|
|
|
|
},
|
|
|
|
global: {
|
|
|
|
type: 'file',
|
|
|
|
file: files[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-06-22 19:25:00 +00:00
|
|
|
let merged = provider.load();
|
2021-06-07 08:58:26 +00:00
|
|
|
helpers.assertMerged(null, merged);
|
|
|
|
assert.strictEqual(merged.candy.something, 'file1');
|
|
|
|
})
|
|
|
|
t.test("should respect the hierarchy when multiple stores are used", function() {
|
2021-06-22 19:25:00 +00:00
|
|
|
let provider = new nconf.Provider().overrides({foo: {bar: 'baz'}})
|
2021-06-07 08:58:26 +00:00
|
|
|
.add('file1', {type: 'file', file: files[0]})
|
|
|
|
.add('file2', {type: 'file', file: files[1]});
|
|
|
|
|
2021-06-22 19:25:00 +00:00
|
|
|
let merged = provider.load();
|
2021-06-07 08:58:26 +00:00
|
|
|
|
|
|
|
helpers.assertMerged(null, merged);
|
|
|
|
assert.strictEqual(merged.foo.bar, 'baz');
|
|
|
|
assert.strictEqual(merged.candy.something, 'file1');
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.describe("the .file() method", function() {
|
|
|
|
t.test("should use the correct File store with a single filepath", function() {
|
2021-06-22 19:25:00 +00:00
|
|
|
let provider = new nconf.Provider();
|
2021-06-07 08:58:26 +00:00
|
|
|
provider.file(helpers.fixture('store.json'));
|
|
|
|
assert.strictEqual(typeof(provider.stores.file), 'object');
|
|
|
|
});
|
|
|
|
t.test("should use the correct File store with a name and a filepath", function() {
|
2021-06-22 19:25:00 +00:00
|
|
|
let provider = new nconf.Provider();
|
2021-06-07 08:58:26 +00:00
|
|
|
provider.file('custom', helpers.fixture('store.json'));
|
|
|
|
assert.strictEqual(typeof(provider.stores.custom), 'object');
|
|
|
|
});
|
|
|
|
t.test("should use the correct File store with a single object", function() {
|
2021-06-22 19:25:00 +00:00
|
|
|
let provider = new nconf.Provider();
|
2021-06-07 08:58:26 +00:00
|
|
|
provider.file({
|
|
|
|
dir: helpers.fixture(''),
|
|
|
|
file: 'store.json',
|
|
|
|
search: true
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.strictEqual(typeof(provider.stores.file), 'object');
|
|
|
|
assert.strictEqual(provider.stores.file.file, helpers.fixture('store.json'));
|
|
|
|
});
|
|
|
|
t.test("should use the correct File store with a name and an object", function() {
|
2021-06-22 19:25:00 +00:00
|
|
|
let provider = new nconf.Provider();
|
2021-06-07 08:58:26 +00:00
|
|
|
provider.file('custom', {
|
|
|
|
dir: helpers.fixture(''),
|
|
|
|
file: 'store.json',
|
|
|
|
search: true
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.strictEqual(typeof(provider.stores.custom), 'object');
|
|
|
|
assert.strictEqual(provider.stores.custom.file, helpers.fixture('store.json'));
|
2021-06-22 19:25:00 +00:00
|
|
|
})*/
|
|
|
|
/*t.describe("the any() method", function() {
|
|
|
|
let provider = new nconf.Provider({
|
2021-06-07 08:58:26 +00:00
|
|
|
type: 'literal',
|
|
|
|
store: {
|
|
|
|
key: "getThisValue"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.describe("without a callback", function() {
|
|
|
|
t.test("should respond with the correct value given an array of keys with one matching", function() {
|
|
|
|
assert.strictEqual(provider.any(["notthis", "orthis", "key"]), 'getThisValue');
|
|
|
|
})
|
|
|
|
t.test("should respond with null given an array of keys with no match", function() {
|
|
|
|
assert.strictEqual(provider.any(["notthis", "orthis"]), null);
|
|
|
|
});
|
|
|
|
t.test("should respond with the correct value given a variable argument list of keys with one matching", function() {
|
|
|
|
assert.strictEqual(provider.any("notthis", "orthis", "key"), 'getThisValue');
|
|
|
|
});
|
|
|
|
t.test("should respond with null given no arguments", function() {
|
|
|
|
assert.strictEqual(provider.any(), null);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
t.describe("with a callback", function() {
|
|
|
|
t.test("should respond with the correct value given an array of keys with one matching", function(done) {
|
|
|
|
provider.any(["notthis", "orthis", "key"], (err, value) => {
|
|
|
|
assert.strictEqual(value, 'getThisValue');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
t.test("should respond with an undefined value given an array of keys with no match", function(done) {
|
|
|
|
provider.any(["notthis", "orthis"], (err, value) => {
|
|
|
|
assert.strictEqual(value, undefined)
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
t.test("should respond with the correct value given a variable argument list of keys with one matching", function(done) {
|
|
|
|
provider.any("notthis", "orthis", "key", (err, value) => {
|
|
|
|
assert.strictEqual(value, 'getThisValue');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
t.test("should respond with an undefined value given no keys", function(done) {
|
|
|
|
provider.any((err, value) => {
|
|
|
|
assert.strictEqual(value, undefined)
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
})
|
2021-06-22 19:25:00 +00:00
|
|
|
})*/
|
2021-06-07 08:58:26 +00:00
|
|
|
});
|