-
Notifications
You must be signed in to change notification settings - Fork 252
/
complete-test.js
137 lines (133 loc) · 3.98 KB
/
complete-test.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* complete-test.js: Complete test for multiple stores.
*
* (C) 2011, Charlie Robbins and the Contributors.
*
*/
var fs = require('fs'),
path = require('path'),
vows = require('vows'),
assert = require('assert'),
nconf = require('../lib/nconf'),
data = require('./fixtures/data').data,
helpers = require('./helpers');
var completeTest = helpers.fixture('complete-test.json'),
complete = helpers.fixture('complete.json');
// prime the process.env
process.env['NCONF_foo'] = 'bar';
process.env.FOO = 'bar';
process.env.BAR = 'zalgo';
process.env.NODE_ENV = 'debug';
process.env.FOOBAR = 'should not load';
vows.describe('nconf/multiple-stores').addBatch({
"When using the nconf with multiple providers": {
topic: function () {
var that = this;
helpers.cp(complete, completeTest, function () {
nconf.env({
// separator: '__',
match: /^NCONF_/,
whitelist: ['NODE_ENV', 'FOO', 'BAR']
});
nconf.file({ file: completeTest });
nconf.use('argv', { type: 'literal', store: data });
that.callback();
});
},
"should have the correct `stores`": function () {
assert.isObject(nconf.stores.env);
assert.isObject(nconf.stores.argv);
assert.isObject(nconf.stores.file);
},
"env vars": {
"are present": function () {
['NODE_ENV', 'FOO', 'BAR', 'NCONF_foo'].forEach(function (key) {
assert.equal(nconf.get(key), process.env[key]);
});
}
},
"json vars": {
topic: function () {
fs.readFile(complete, 'utf8', this.callback);
},
"are present": function (err, data) {
assert.isNull(err);
data = JSON.parse(data);
Object.keys(data).forEach(function (key) {
assert.deepEqual(nconf.get(key), data[key]);
});
}
},
"literal vars": {
"are present": function () {
Object.keys(data).forEach(function (key) {
assert.deepEqual(nconf.get(key), data[key]);
});
}
},
"and saving *synchronously*": {
topic: function () {
nconf.set('weebls', 'stuff');
return nconf.save();
},
"correct return value": function (topic) {
Object.keys(topic).forEach(function (key) {
assert.deepEqual(topic[key], nconf.get(key));
});
},
"the file": {
topic: function () {
fs.readFile(completeTest, 'utf8', this.callback);
},
"saved correctly": function (err, data) {
data = JSON.parse(data);
Object.keys(data).forEach(function (key) {
assert.deepEqual(data[key], nconf.get(key));
});
assert.equal(nconf.get('weebls'), 'stuff');
}
}
},
teardown: function () {
// remove the file so that we can test saving it async
fs.unlinkSync(completeTest);
}
}
}).addBatch({
// Threw this in it's own batch to make sure it's run separately from the
// sync check
"When using the nconf with multiple providers": {
"and saving *asynchronously*": {
topic: function () {
nconf.set('weebls', 'crap');
nconf.save(this.callback);
},
"correct return value": function (err, data) {
assert.isNull(err);
Object.keys(data).forEach(function (key) {
assert.deepEqual(data[key], nconf.get(key));
});
},
"the file": {
topic: function () {
fs.readFile(completeTest, 'utf8', this.callback);
},
"saved correctly": function (err, data) {
assert.isNull(err);
data = JSON.parse(data);
Object.keys(data).forEach(function (key) {
assert.deepEqual(nconf.get(key), data[key]);
});
assert.equal(nconf.get('weebls'), 'crap');
}
}
},
teardown: function () {
fs.unlinkSync(completeTest);
nconf.remove('file');
nconf.remove('memory');
nconf.remove('argv');
nconf.remove('env');
}
}
}).export(module);