forked from finos/SymphonyElectron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.test.js
322 lines (251 loc) · 9.31 KB
/
config.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
const { clearCachedConfigs, getConfigField, updateConfigField, configFileName, saveUserConfig } = require('../js/config');
const fs = require('fs');
const path = require('path');
const os = require('os');
// mock required so getConfig reads config from correct path
jest.mock('../js/utils/misc.js', function() {
return {
isDevEnv: false,
isMac: false
}
});
let globalConfigDir;
let userConfigDir;
jest.mock('electron', function() {
return {
app: {
getPath: mockedGetPath,
getVersion: mockedGetVersion
}
}
});
function mockedGetVersion() {
return '3.1.0';
}
function mockedGetPath(type) {
if (type === 'exe') {
return globalConfigDir;
}
if (type === 'userData') {
return userConfigDir
}
return '';
}
describe('read/write config tests', function() {
beforeEach(function() {
/// reset module vars between running tests.
globalConfigDir = null;
userConfigDir = null;
// reset module values so each test starts clean.
clearCachedConfigs();
});
afterEach(function() {
// clean up temp files creating during tests
if (globalConfigDir) {
fs.unlinkSync(path.join(globalConfigDir, configFileName));
fs.rmdirSync(globalConfigDir);
fs.rmdirSync(path.join(globalConfigDir, '..'));
}
if (userConfigDir) {
fs.unlinkSync(path.join(userConfigDir, configFileName));
fs.rmdirSync(userConfigDir);
}
});
function createTempConfigFile(filePath, config) {
fs.writeFileSync(filePath, JSON.stringify(config));
}
function createTempUserConfig(config) {
const tmpDir = os.tmpdir();
userConfigDir = fs.mkdtempSync(path.join(tmpDir, 'config-'));
return createTempConfigFile(path.join(userConfigDir, configFileName), config);
}
function createTempGlobalConfig(config) {
const tmpDir = os.tmpdir();
globalConfigDir = path.join(fs.mkdtempSync(path.join(tmpDir, 'config-')), 'config');
fs.mkdirSync(globalConfigDir);
return createTempConfigFile(path.join(globalConfigDir, configFileName), config);
}
function removeTempConfigFile(filePath) {
fs.unlinkSync(filePath);
}
describe('getConfigField tests', function() {
it('should fail when field not present in either user or global config', function() {
const userConfig = {
url: 'something'
};
createTempUserConfig(userConfig);
const globalConfig = {
url: 'something-else'
};
createTempGlobalConfig(globalConfig);
return getConfigField('noturl').catch(function(err) {
expect(err).toBeTruthy();
});
});
it('should succeed when field only present in user config', function() {
const userConfig = {
url: 'something'
};
createTempUserConfig(userConfig);
return getConfigField('url').then(function(url) {
expect(url).toBe('something');
});
});
it('should succeed when field only present in global config', function() {
const globalConfig = {
url: 'something-else'
};
createTempGlobalConfig(globalConfig);
return getConfigField('url').then(function(url) {
expect(url).toBe('something-else');
});
});
it('should succeed and return user config field when value is in both', function() {
const userConfig = {
url: 'something'
};
createTempUserConfig(userConfig);
const globalConfig = {
url: 'something-else'
};
createTempGlobalConfig(globalConfig);
return getConfigField('url').then(function(url) {
expect(url).toBe('something');
});
});
it('should fail when global config path is invalid', function() {
const globalConfig = {
url: 'something-else'
};
createTempGlobalConfig(globalConfig);
let correctConfigDir = globalConfigDir;
globalConfigDir = '//';
return getConfigField('url').catch(function(err) {
globalConfigDir = correctConfigDir;
expect(err).toBeTruthy();
});
});
it('should fail when user config path is invalid', function() {
const userConfig = {
url: 'something'
};
createTempUserConfig(userConfig);
let correctConfigDir = userConfigDir;
userConfigDir = '//';
return getConfigField('url').catch(function(err) {
userConfigDir = correctConfigDir;
expect(err).toBeTruthy();
});
});
it('should read cached user config value rather than reading file from disk again', function(done) {
const userConfig = {
url: 'qa4.symphony.com'
};
createTempUserConfig(userConfig);
const userConfig2 = {
url: 'qa5.symphony.com'
};
return getConfigField('url')
.then(function() {
createTempUserConfig(userConfig2);
})
.then(function() {
return getConfigField('url')
})
.then(function(url) {
expect(url).toBe('qa4.symphony.com');
done();
});
});
it('should read cache global config value rather than reading file from disk again', function(done) {
const globalConfig = {
url: 'qa8.symphony.com'
};
createTempGlobalConfig(globalConfig);
const globalConfig2 = {
url: 'qa9.symphony.com'
};
return getConfigField('url')
.then(function() {
createTempGlobalConfig(globalConfig2);
})
.then(function() {
return getConfigField('url')
})
.then(function(url) {
expect(url).toBe('qa8.symphony.com');
done();
});
});
});
describe('updateConfigField tests', function() {
it('should succeed and overwrite existing field', function() {
const userConfig = {
url: 'something'
};
createTempUserConfig(userConfig);
return updateConfigField('url', 'hello world')
.then(function(newConfig) {
expect(newConfig).toEqual({
url: 'hello world'
});
});
});
it('should succeed and add new field', function() {
const userConfig = {
url: 'something'
};
createTempUserConfig(userConfig);
return updateConfigField('url2', 'hello world')
.then(function(newConfig) {
expect(newConfig).toEqual({
url: 'something',
url2: 'hello world'
});
});
});
it('should fail to update if invalid field name', function() {
const userConfig = {
url: 'something'
};
createTempUserConfig(userConfig);
return updateConfigField('', 'hello word').catch(function (err) {
expect(err.message).toBe('can not save config, invalid input');
});
});
it('should throw error if path is not defined', function() {
userConfigDir = null;
return updateConfigField('url2', 'hello world')
.catch(function (err) {
expect(err.message).toBe('The "path" argument must be of type string. Received type object');
});
});
it('should throw error if fieldName is not defined', function() {
const userConfig = {
url: 'something'
};
createTempUserConfig(userConfig);
return saveUserConfig(undefined, 'something', 'oldConfig')
.catch(function (reject) {
expect(reject.message).toBe('can not save config, invalid input');
});
});
it('should throw error if config is not defined', function() {
const userConfig = {
url: 'something'
};
createTempUserConfig(userConfig);
return saveUserConfig('url2', 'something', undefined)
.catch(function (reject) {
expect(reject.message).toBe('can not save config, invalid input');
});
});
it('should throw error if path is not defined for saveUserConfig()', function() {
userConfigDir = null;
return saveUserConfig('url2', 'hello world')
.catch(function (err) {
expect(err.message).toBe('The "path" argument must be of type string. Received type object');
});
});
});
});