1 | const assert = require('assert'),
|
2 | common = require('./common'),
|
3 | path = require('path');
|
4 |
|
5 | const { _getDeploymentConfig,
|
6 | _getIndexConfig,
|
7 | clone,
|
8 | get,
|
9 | getAllConfigs,
|
10 | getConfig,
|
11 | merge,
|
12 | mergeWith } = require('../src/lib');
|
13 |
|
14 | const { compressEnv,
|
15 | _defaultMerge,
|
16 | _getMerger,
|
17 | _isEnvProp,
|
18 | _isValueType,
|
19 | _parseKey } = require('../src/lib/object');
|
20 |
|
21 | const configPath = path.join(__dirname, './config');
|
22 | const options = { path: configPath };
|
23 |
|
24 | describe('lib module', function() {
|
25 |
|
26 | describe('"dir"', function() {
|
27 | const dir = require('../src/lib/dir');
|
28 |
|
29 | it('rootPath()', function() {
|
30 | assert.strictEqual(dir.appRoot(), path.join(__dirname, '..'));
|
31 | });
|
32 | });
|
33 |
|
34 | describe('"env"', function() {
|
35 | const env = require('../src/lib/env');
|
36 |
|
37 | it('_isNpmEnvSet()', function() {
|
38 | process.env.npm_ = "set for testing";
|
39 | assert.strictEqual(env._isNpmEnvSet(), true);
|
40 | delete process.env.npm_;
|
41 | assert.ok(!process.env.npm_);
|
42 | });
|
43 |
|
44 | it('_getPackageConfigPath()', function() {
|
45 | let dir = path.join(__dirname, '../');
|
46 | let packageConfigPath = env._getPackageConfigPath(dir);
|
47 | assert.ok(packageConfigPath);
|
48 | assert.strictEqual(
|
49 | packageConfigPath,
|
50 | './test/config');
|
51 | });
|
52 |
|
53 | it('_getEnvConfigPath()', function() {
|
54 | const APP_CONFIG_PATH = "CONFIG_PATH";
|
55 | process.env[APP_CONFIG_PATH] = './test/config';
|
56 | let envConfigPath = env._getEnvConfigPath();
|
57 | assert.ok(envConfigPath);
|
58 | assert.strictEqual(
|
59 | envConfigPath,
|
60 | './test/config');
|
61 | delete process.env[APP_CONFIG_PATH];
|
62 | assert.ok(!process.env[APP_CONFIG_PATH]);
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | });
|
75 |
|
76 | it('getConfigPath()', function() {
|
77 | assert.strictEqual(
|
78 | env.getConfigPath(),
|
79 | configPath);
|
80 |
|
81 |
|
82 | });
|
83 | });
|
84 |
|
85 | describe('file', function() {
|
86 |
|
87 |
|
88 | describe('_getIndexConfig', function() {
|
89 | let indexConfig = _getIndexConfig(options);
|
90 |
|
91 | it('gets index config object', function() {
|
92 | assert.ok(indexConfig);
|
93 | });
|
94 |
|
95 | it('all values are as expected', function() {
|
96 | assert.strictEqual(indexConfig.logging, false);
|
97 |
|
98 | assert.strictEqual(
|
99 | get(indexConfig, "db.username"), null);
|
100 | assert.strictEqual(indexConfig.db.password, null);
|
101 | assert.strictEqual(indexConfig.db.host, null);
|
102 | assert.strictEqual(indexConfig.db.port, 27017);
|
103 | assert.strictEqual(indexConfig.db.database, null);
|
104 | });
|
105 | });
|
106 |
|
107 |
|
108 | describe('_getDeploymentConfig', function() {
|
109 | let saved = {};
|
110 |
|
111 | before(function() {
|
112 | saved.NODE_ENV = process.env.NODE_ENV;
|
113 | saved.TEST = process.env.TEST;
|
114 | });
|
115 |
|
116 | describe('development values', function() {
|
117 | let dev;
|
118 |
|
119 | before(function() {
|
120 | process.env.NODE_ENV = "development";
|
121 | delete process.env.TEST;
|
122 |
|
123 | dev = _getDeploymentConfig("development", options);
|
124 | });
|
125 |
|
126 | it('NODE_ENV = "development"', function() {
|
127 | assert.strictEqual(process.env.NODE_ENV, "development");
|
128 | });
|
129 |
|
130 | it('TEST = undefined', function() {
|
131 | assert.strictEqual(process.env.hasOwnProperty("TEST"), false);
|
132 | assert.strictEqual(process.env.TEST, undefined);
|
133 | });
|
134 |
|
135 | it('logging ok', function() {
|
136 | assert.strictEqual(dev.logging, true);
|
137 | });
|
138 |
|
139 | it('username ok', function() {
|
140 | assert.strictEqual(dev.db.username, "app");
|
141 | });
|
142 |
|
143 | it('password ok', function() {
|
144 | assert.strictEqual(dev.db.password, "password123");
|
145 | });
|
146 |
|
147 | it('host ok', function() {
|
148 | assert.strictEqual(dev.db.host, "localhost");
|
149 | });
|
150 |
|
151 | it('port ok', function() {
|
152 | assert.strictEqual(dev.db.port, 27017);
|
153 | });
|
154 |
|
155 | it('database ok', function() {
|
156 | assert.strictEqual(dev.db.database, "dev");
|
157 | });
|
158 | });
|
159 |
|
160 | describe('testing values', function() {
|
161 | let test;
|
162 |
|
163 | before(function() {
|
164 | process.env.NODE_ENV = "development";
|
165 | process.env.TEST = "TEST";
|
166 | test = _getDeploymentConfig("development", options);
|
167 | });
|
168 |
|
169 | it('NODE_ENV = "development"', function() {
|
170 | assert.strictEqual(process.env.NODE_ENV, "development");
|
171 | });
|
172 |
|
173 | it('TEST = "TEST"', function() {
|
174 | assert.strictEqual(process.env.TEST, "TEST");
|
175 | });
|
176 |
|
177 | it('logging ok', function() {
|
178 | assert.strictEqual(test.logging, false);
|
179 | });
|
180 |
|
181 | it('db username ok', function() {
|
182 | assert.strictEqual(test.db.username, "app");
|
183 | });
|
184 |
|
185 | it('db password ok', function() {
|
186 | assert.strictEqual(test.db.password, "password123");
|
187 | });
|
188 |
|
189 | it('db host ok', function() {
|
190 | assert.strictEqual(test.db.host, "127.0.0.1");
|
191 | });
|
192 |
|
193 | it('db port ok', function() {
|
194 | assert.strictEqual(test.db.port, 27017);
|
195 | });
|
196 |
|
197 | it('db database ok', function() {
|
198 | assert.strictEqual(test.db.database, "test");
|
199 | });
|
200 | });
|
201 |
|
202 | after(function() {
|
203 | process.env.NODE_ENV = saved.NODE_ENV;
|
204 | process.env.TEST = saved.TEST;
|
205 | });
|
206 |
|
207 | });
|
208 |
|
209 |
|
210 | describe('getConfig', function() {
|
211 | let saved = {};
|
212 |
|
213 | before(function() {
|
214 | saved.NODE_ENV = process.env.NODE_ENV;
|
215 | saved.TEST = process.env.TEST;
|
216 | });
|
217 |
|
218 | beforeEach(function() {
|
219 | delete process.env.NODE_ENV;
|
220 | delete process.env.TEST;
|
221 | });
|
222 |
|
223 | it('default config validates', function() {
|
224 | let config = getConfig();
|
225 |
|
226 | assert.strictEqual(config('#env'), 'development');
|
227 | common.assert_development_config(config);
|
228 | });
|
229 |
|
230 | it('development config validates', function() {
|
231 | let config = getConfig('development');
|
232 |
|
233 | common.assert_development_config(config);
|
234 | });
|
235 |
|
236 | it('development+TEST config validates', function() {
|
237 | process.env.TEST = "TEST";
|
238 | let config = getConfig('development');
|
239 |
|
240 | common.assert_development_plus_TEST_config(config);
|
241 | });
|
242 |
|
243 | it('staging config validates', function() {
|
244 | process.env.NODE_ENV = 'staging';
|
245 | let config = getConfig('staging', options);
|
246 |
|
247 | common.assert_staging_config(config);
|
248 | });
|
249 |
|
250 | after(function() {
|
251 | process.env.NODE_ENV = saved.NODE_ENV;
|
252 | process.env.TEST = saved.TEST;
|
253 | });
|
254 | });
|
255 |
|
256 |
|
257 | describe('getAllConfigs', function() {
|
258 |
|
259 |
|
260 | let dev = getConfig("development", options);
|
261 | let staging = getConfig("staging", options);
|
262 | let configs = getAllConfigs(options);
|
263 |
|
264 | it('gets 2 configs', function() {
|
265 | assert.strictEqual(Object.keys(configs).length, 2);
|
266 | });
|
267 |
|
268 | it('development config validates', function() {
|
269 | common.assert_development_plus_TEST_config(dev);
|
270 | });
|
271 |
|
272 | it('staging config validates', function() {
|
273 | common.assert_staging_config(staging);
|
274 | });
|
275 |
|
276 | it('from all: development config validates', function() {
|
277 | common.assert_development_plus_TEST_config(configs.development);
|
278 | });
|
279 |
|
280 | it('from all: staging config validates', function() {
|
281 | common.assert_staging_config(configs.staging);
|
282 | });
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | });
|
294 | });
|
295 | }); |
\ | No newline at end of file |