forked from cliftonc/calipso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.core.configuration.js
182 lines (137 loc) · 5.2 KB
/
lib.core.configuration.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Mocha test case for core configuration library
*/
var should = require('should'),
fs = require('fs'),
rootpath = process.cwd() + '/',
path = require('path'),
exec = require('child_process').exec,
Configuration = require('./helpers/require')('core/Configuration'),
defaultConfig = {
"test":"test",
"modules": {
"module":{
"enabled":true
}
}
};
describe('Configuration', function(){
before(function(){
//
});
describe('Negative', function(){
/**
* For some reason this fails in Travis?!?!
it('Invalid configuration type results in an exception', function(done){
var conf = new Config({type:'invalid', env:'invalid1', 'path': path.join(rootpath,'tmp')});
conf.init(function(err) {
err.message.should.equal('Cannot add store with unknown type: invalid');
// Delete the development.json
fs.unlinkSync(path.join(rootpath,'tmp','invalid1.json'));
done();
});
});**/
it('Invalid default configuration results in an exception', function(done){
var conf = new Configuration({env:'invalid2', 'path': path.join(rootpath,'tmp'), 'defaultConfig':'invalidDefault.json'});
conf.init(function(err) {
err.message.should.equal('Unable to load configuration defined in invalid2.json, there may be a problem with the default configuration in invalidDefault.json, reason: ENOENT, no such file or directory \'invalidDefault.json\'')
done()
})
});
});
describe('Positive', function(){
it('I can create a new configuration file based on a default one.', function(done){
mkdir(path.join(rootpath,'tmp'), function() {
fs.writeFileSync(path.join(rootpath,'tmp','default.json'),JSON.stringify(defaultConfig));
var conf = new Configuration({env:'development', 'path':path.join(rootpath,'tmp')});
conf.type.should.equal('file');
conf.file.should.equal(path.join(rootpath,'tmp','development.json'));
done();
});
});
it('I can store and retrieve configuration values', function(done){
var conf = new Configuration({env:'development',path:path.join(rootpath,'tmp')});
conf.init(function(err) {
conf.set('test:v1','v1');
conf.get('test:v1').should.equal('v1');
conf.set('test:v2','v2');
conf.get('test:v2').should.equal('v2');
var test = conf.get('test');
test.v1.should.equal('v1');
test.v2.should.equal('v2');
done();
});
});
it('I can use different environments', function(done){
var confTest = new Configuration({env:'test',path:path.join(rootpath,'tmp')});
confTest.init(function(err) {
confTest.set('test:v1','v1');
confTest.get('test:v1').should.equal('v1');
confTest.save(function(err) {
(fs.existsSync || path.existsSync)(confTest.file);
done();
});
});
});
it('I can use the setSave shortcut', function(done){
var conf = new Configuration({path:path.join(rootpath,'tmp')});
conf.init(function(err) {
conf.setSave('test:setsave','Yah!',function(err) {
var confTest = new Configuration({path:path.join(rootpath,'tmp')});
confTest.init(function(err) {
confTest.get('test:setsave').should.equal('Yah!');
done();
});
});
});
});
});
describe('Modules', function(){
it('I can set and retrieve module configuration', function(done){
var conf = new Configuration({path:path.join(rootpath,'tmp')});
conf.init(function(err) {
conf.setModuleConfig('module','hello','world');
var value = conf.getModuleConfig('module','hello');
value.should.equal('world');
done();
});
});
it('I can set default module configuration', function(done){
var conf = new Configuration({path:path.join(rootpath,'tmp')});
conf.init(function(err) {
var defaultConfig = {
"goodbye": {
"default": "cruel world",
"label": "Example",
"description": "Example"
}
};
conf.setDefaultModuleConfig('module',defaultConfig);
var value = conf.getModuleConfig('module','goodbye');
value.should.equal('cruel world');
done();
});
});
});
after(function() {
try { fs.unlinkSync(path.join(rootpath,'tmp','default.json')); } catch(ex) {};
try { fs.unlinkSync(path.join(rootpath,'tmp','development.json')); } catch(ex) {};
try { fs.unlinkSync(path.join(rootpath,'tmp','test.json')); } catch(ex) {};
try { fs.unlinkSync(path.join(rootpath,'tmp','invalid.json')); } catch(ex) {};
try { fs.unlinkSync(path.join(rootpath,'tmp',process.env.NODE_ENV + '.json')); } catch(ex) {};
})
});
/**
* Mkdir -p.
*
* TODO - these are unix only ...
*
* @param {String} path
* @param {Function} fn
*/
function mkdir(path, fn) {
exec('mkdir -p ' + path, function(err){
if (err) throw err;
fn && fn();
});
}