hierarchy-test.js
3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* hierarchy-test.js: Basic tests for hierarchical file stores.
*
* (C) 2011, Charlie Robbins and the Contributors.
*
*/
var assert = require('assert'),
fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn,
vows = require('vows'),
nconf = require('../lib/nconf');
var configDir = path.join(__dirname, 'fixtures', 'hierarchy'),
globalConfig = path.join(configDir, 'global.json'),
userConfig = path.join(configDir, 'user.json');
vows.describe('nconf/hierarchy').addBatch({
"When using nconf": {
"configured with two file stores": {
topic: function () {
nconf.add('user', { type: 'file', file: userConfig });
nconf.add('global', { type: 'file', file: globalConfig });
nconf.load();
return nconf;
},
"should have the appropriate keys present": function () {
assert.equal(nconf.get('title'), 'My specific title');
assert.equal(nconf.get('color'), 'green');
assert.equal(nconf.get('movie'), 'Kill Bill');
}
},
"configured with two file stores using `file`": {
topic: function () {
nconf.file('user', userConfig);
nconf.file('global', globalConfig);
nconf.load();
return nconf;
},
"should have the appropriate keys present": function () {
assert.equal(nconf.get('title'), 'My specific title');
assert.equal(nconf.get('color'), 'green');
assert.equal(nconf.get('movie'), 'Kill Bill');
}
},
"configured with .argv(), .env() and .file()": {
topic: function () {
var configFile = path.join(__dirname, 'fixtures', 'load-save.json'),
script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-load-save.js'),
argv = ['--foo', 'foo', '--bar', 'bar'],
that = this,
data = '',
child;
try { fs.unlinkSync(configFile) }
catch (ex) { }
child = spawn('node', [script].concat(argv));
child.stdout.on('data', function (d) {
data += d;
});
child.on('exit', function () {
fs.readFile(configFile, 'utf8', that.callback.bind(null, null, data));
});
},
"should not persist information passed in to process.env and process.argv to disk ": function (_, data, _, ondisk){
assert.equal(data, 'foo');
assert.deepEqual(JSON.parse(ondisk), {
database: {
host: '127.0.0.1',
port: 5984
}
});
}
},
"configured with .argv(), .file() and invoked with nested command line options": {
topic: function () {
var script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-load-merge.js'),
argv = ['--candy:something', 'foo', '--candy:something5:second', 'bar'],
that = this,
data = '',
child;
child = spawn('node', [script].concat(argv));
child.stdout.on('data', function (d) {
data += d;
});
child.on('exit', function() {
that.callback(null, data);
});
},
"should merge nested objects ": function (err, data) {
assert.deepEqual(JSON.parse(data), {
apples: true,
candy: {
something: 'foo',
something1: true,
something2: true,
something5: {
first: 1,
second: 'bar'
}
}
});
}
}
}
}).export(module);