-
Notifications
You must be signed in to change notification settings - Fork 57
/
config.test.js
138 lines (120 loc) · 4.7 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
'use strict';
const fs = require('fs');
const mm = require('mm');
const expect = require('expect.js');
const xprofiler = require('../xprofiler');
const testXprofilerConfigKeys = require('./fixtures/cases/config');
const utils = require('./fixtures/utils');
const testKeys = Object.keys(testXprofilerConfigKeys);
function cleanDir(key, dir) {
if ((key === 'log_dir' || key === 'XPROFILER_LOG_DIR') && fs.existsSync(dir)) {
utils.cleanDir(dir);
}
}
describe('xprofiler config', function () {
const message = 'must run "require(\'xprofiler\')()" to set xprofiler config first!';
let error;
before(function () {
mm(process.env, 'XPROFILER_UNIT_TEST_SINGLE_MODULE', 'YES');
});
after(function () {
mm.restore();
});
it(`should throw error if not init config: ${message}`, function () {
try {
xprofiler.getXprofilerConfig();
} catch (err) {
expect(err.message).to.be(message);
error = err.message;
}
expect(error).to.be.ok();
});
it('should be ok after init config', function () {
let defaultConfig;
let error;
try {
xprofiler();
defaultConfig = xprofiler.getXprofilerConfig();
} catch (err) {
error = err;
}
expect(error).not.to.be.ok();
describe('xprofiler config keys', function () {
const configKeys = Object.keys(defaultConfig);
it(`should have these keys: [${configKeys.join(', ')}]`, function () {
expect(utils.arrayEqual(testKeys, configKeys)).to.be.ok();
});
});
for (const testKey of testKeys) {
describe(`xprofiler config.${testKey}`, function () {
const defaultValue = defaultConfig[testKey];
const envTestList = testXprofilerConfigKeys[testKey].env;
const userTestList = testXprofilerConfigKeys[testKey].user;
const assignRule = testXprofilerConfigKeys[testKey].rule;
// test default value
it(`default value should be ${defaultValue}`, function () {
expect(testXprofilerConfigKeys[testKey].defaultValue).to.be(defaultValue);
});
// test env config
for (const envTest of envTestList) {
describe(`set env ${envTest.key}=${envTest.value}`, function () {
let config;
before(function () {
mm(process.env, envTest.key, envTest.value);
mm(process.env, 'XPROFILER_UNIT_TEST_SINGLE_MODULE', 'YES');
xprofiler();
config = xprofiler.getXprofilerConfig();
});
after(function () {
mm.restore();
});
it(`config.${testKey} should be ${envTest.expected}`, function () {
expect(config[testKey]).to.be(envTest.expected);
});
});
}
// test user config
for (const userTest of userTestList) {
const testValue = userTest.value;
const userConfigValue = typeof testValue === 'string' ? `"${testValue}"` : testValue;
describe(`set user config { ${userTest.key}: ${userConfigValue} }`,
function () {
before(function () {
mm(process.env, 'XPROFILER_UNIT_TEST_SINGLE_MODULE', 'YES');
xprofiler({ [userTest.key]: userTest.value });
});
after(function () {
mm.restore();
});
it(`config.${testKey} should be ${userTest.expected}`, function () {
const config = xprofiler.getXprofilerConfig();
expect(config[testKey]).to.be(userTest.expected);
});
});
}
// test config assign rule
const envDescription = `${assignRule.env.key}=${assignRule.env.value}`;
const userAssignRuleValue = assignRule.user.value;
const userConfigDescription = `{ ${assignRule.user.key}: ` +
(typeof userAssignRuleValue === 'string' ? `"${userAssignRuleValue}"` : userAssignRuleValue) + ' }';
describe(`both set env ${envDescription} and user config ${userConfigDescription}`, function () {
let config;
before(function () {
mm(process.env, assignRule.env.key, assignRule.env.value);
mm(process.env, 'XPROFILER_UNIT_TEST_SINGLE_MODULE', 'YES');
xprofiler({ [assignRule.user.key]: assignRule.user.value });
config = xprofiler.getXprofilerConfig();
});
after(function () {
mm.restore();
cleanDir(assignRule.env.key, assignRule.env.value);
cleanDir(assignRule.user.key, assignRule.user.value);
});
it(`config.${testKey} shoule be ${assignRule.expected}`, function () {
expect(config[testKey]).to.be(assignRule.expected);
});
});
});
}
});
});