/
chain-class.js
100 lines (93 loc) · 3.13 KB
/
chain-class.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
var test = require('tap').test
var CC = require('../index.js').ConfigChain
var env = { foo_blaz : 'blzaa', foo_env : 'myenv' }
var jsonObj = { blaz: 'json', json: true }
var iniObj = { 'x.y.z': 'xyz', blaz: 'ini' }
var fs = require('fs')
var ini = require('ini')
fs.writeFileSync('/tmp/config-chain-class.json', JSON.stringify(jsonObj))
fs.writeFileSync('/tmp/config-chain-class.ini', ini.stringify(iniObj))
var http = require('http')
var reqs = 0
http.createServer(function (q, s) {
if (++reqs === 2) this.close()
if (q.url === '/json') {
// make sure that the requests come back from the server
// out of order. they should still be ordered properly
// in the resulting config object set.
setTimeout(function () {
s.setHeader('content-type', 'application/json')
s.end(JSON.stringify({
blaz: 'http',
http: true,
json: true
}))
}, 200)
} else {
s.setHeader('content-type', 'application/ini')
s.end(ini.stringify({
blaz: 'http',
http: true,
ini: true,
json: false
}))
}
}).listen(1337)
test('basic class test', function (t) {
var cc = new CC()
var expectlist =
[ { blaz: 'json', json: true },
{ 'x.y.z': 'xyz', blaz: 'ini' },
{ blaz: 'blzaa', env: 'myenv' },
{ blaz: 'http', http: true, json: true },
{ blaz: 'http', http: true, ini: true, json: false } ]
cc.addFile('/tmp/config-chain-class.json')
.addFile('/tmp/config-chain-class.ini')
.addEnv('foo_', env)
.addUrl('http://localhost:1337/json')
.addUrl('http://localhost:1337/ini')
.on('load', function () {
t.same(cc.list, expectlist)
t.same(cc.snapshot, { blaz: 'json',
json: true,
'x.y.z': 'xyz',
env: 'myenv',
http: true,
ini: true })
cc.del('blaz', '/tmp/config-chain-class.json')
t.same(cc.snapshot, { blaz: 'ini',
json: true,
'x.y.z': 'xyz',
env: 'myenv',
http: true,
ini: true })
cc.del('blaz')
t.same(cc.snapshot, { json: true,
'x.y.z': 'xyz',
env: 'myenv',
http: true,
ini: true })
cc.shift()
t.same(cc.snapshot, { 'x.y.z': 'xyz',
env: 'myenv',
http: true,
json: true,
ini: true })
cc.shift()
t.same(cc.snapshot, { env: 'myenv',
http: true,
json: true,
ini: true })
cc.shift()
t.same(cc.snapshot, { http: true,
json: true,
ini: true })
cc.shift()
t.same(cc.snapshot, { http: true,
ini: true,
json: false })
cc.shift()
t.same(cc.snapshot, {})
t.end()
})
})