|
7 | 7 | ifDirective,
|
8 | 8 | eqDirective,
|
9 | 9 | envDirective,
|
| 10 | + envVarDirective, |
10 | 11 | extendsDirective,
|
11 | 12 | extendsSelfDirective,
|
12 | 13 | overrideDirective,
|
@@ -898,6 +899,18 @@ describe('$substitute directive', () => {
|
898 | 899 | expect(parsed.toJSON()).toEqual({ foo: 'qa' });
|
899 | 900 | });
|
900 | 901 |
|
| 902 | + it('reads special case name APP_CONFIG_ENV', async () => { |
| 903 | + process.env.NODE_ENV = 'qa'; |
| 904 | + |
| 905 | + const source = new LiteralSource({ |
| 906 | + foo: { $subs: { name: 'APP_CONFIG_ENV' } }, |
| 907 | + }); |
| 908 | + |
| 909 | + const parsed = await source.read([substituteDirective()]); |
| 910 | + |
| 911 | + expect(parsed.toJSON()).toEqual({ foo: 'qa' }); |
| 912 | + }); |
| 913 | + |
901 | 914 | it('reads object with $name', async () => {
|
902 | 915 | process.env.FOO = 'foo';
|
903 | 916 |
|
@@ -1211,6 +1224,195 @@ describe('$substitute directive', () => {
|
1211 | 1224 | });
|
1212 | 1225 | });
|
1213 | 1226 |
|
| 1227 | +describe('$envVar directive', () => { |
| 1228 | + it('fails with non-string values', async () => { |
| 1229 | + const source = new LiteralSource({ $envVar: {} }); |
| 1230 | + await expect(source.read([envVarDirective()])).rejects.toThrow(); |
| 1231 | + }); |
| 1232 | + |
| 1233 | + it('does simple environment variable substitution', async () => { |
| 1234 | + process.env.FOO = 'foo'; |
| 1235 | + process.env.BAR = 'bar'; |
| 1236 | + |
| 1237 | + const source = new LiteralSource({ |
| 1238 | + foo: { $envVar: 'FOO' }, |
| 1239 | + bar: { $envVar: 'BAR' }, |
| 1240 | + }); |
| 1241 | + |
| 1242 | + const parsed = await source.read([envVarDirective()]); |
| 1243 | + |
| 1244 | + expect(parsed.toJSON()).toEqual({ foo: 'foo', bar: 'bar' }); |
| 1245 | + }); |
| 1246 | + |
| 1247 | + it('reads object with $name', async () => { |
| 1248 | + process.env.FOO = 'foo'; |
| 1249 | + |
| 1250 | + const source = new LiteralSource({ |
| 1251 | + foo: { $envVar: { name: 'FOO' } }, |
| 1252 | + }); |
| 1253 | + |
| 1254 | + const parsed = await source.read([envVarDirective()]); |
| 1255 | + |
| 1256 | + expect(parsed.toJSON()).toEqual({ foo: 'foo' }); |
| 1257 | + }); |
| 1258 | + |
| 1259 | + it('fails with $name when not defined', async () => { |
| 1260 | + const source = new LiteralSource({ |
| 1261 | + foo: { $envVar: { name: 'FOO' } }, |
| 1262 | + }); |
| 1263 | + |
| 1264 | + await expect(source.read([envVarDirective()])).rejects.toThrow(); |
| 1265 | + }); |
| 1266 | + |
| 1267 | + it('uses $name when $fallback is defined', async () => { |
| 1268 | + process.env.FOO = 'foo'; |
| 1269 | + |
| 1270 | + const source = new LiteralSource({ |
| 1271 | + foo: { $envVar: { name: 'FOO', fallback: 'bar' } }, |
| 1272 | + }); |
| 1273 | + |
| 1274 | + const parsed = await source.read([envVarDirective()]); |
| 1275 | + |
| 1276 | + expect(parsed.toJSON()).toEqual({ foo: 'foo' }); |
| 1277 | + }); |
| 1278 | + |
| 1279 | + it('uses $fallback when $name was not found', async () => { |
| 1280 | + const source = new LiteralSource({ |
| 1281 | + foo: { $envVar: { name: 'FOO', fallback: 'bar' } }, |
| 1282 | + }); |
| 1283 | + |
| 1284 | + const parsed = await source.read([envVarDirective()]); |
| 1285 | + |
| 1286 | + expect(parsed.toJSON()).toEqual({ foo: 'bar' }); |
| 1287 | + }); |
| 1288 | + |
| 1289 | + it('allows null value when $allowNull', async () => { |
| 1290 | + const source = new LiteralSource({ |
| 1291 | + foo: { $envVar: { name: 'FOO', fallback: null, allowNull: true } }, |
| 1292 | + }); |
| 1293 | + |
| 1294 | + const parsed = await source.read([envVarDirective()]); |
| 1295 | + |
| 1296 | + expect(parsed.toJSON()).toEqual({ foo: null }); |
| 1297 | + }); |
| 1298 | + |
| 1299 | + it('does not allow number even when $allowNull', async () => { |
| 1300 | + const source = new LiteralSource({ |
| 1301 | + foo: { $envVar: { name: 'FOO', fallback: 42, allowNull: true } }, |
| 1302 | + }); |
| 1303 | + |
| 1304 | + await expect(source.read([envVarDirective()])).rejects.toThrow(); |
| 1305 | + }); |
| 1306 | + |
| 1307 | + it('parses ints', async () => { |
| 1308 | + process.env.FOO = '11'; |
| 1309 | + |
| 1310 | + const source = new LiteralSource({ |
| 1311 | + $envVar: { name: 'FOO', parseInt: true }, |
| 1312 | + }); |
| 1313 | + |
| 1314 | + expect(await source.readToJSON([envVarDirective()])).toEqual(11); |
| 1315 | + }); |
| 1316 | + |
| 1317 | + it('fails when int is invalid', async () => { |
| 1318 | + process.env.FOO = 'not a number'; |
| 1319 | + |
| 1320 | + const source = new LiteralSource({ |
| 1321 | + $envVar: { name: 'FOO', parseInt: true }, |
| 1322 | + }); |
| 1323 | + |
| 1324 | + await expect(source.read([envVarDirective()])).rejects.toThrow(); |
| 1325 | + }); |
| 1326 | + |
| 1327 | + it('parses float', async () => { |
| 1328 | + process.env.FOO = '11.2'; |
| 1329 | + |
| 1330 | + const source = new LiteralSource({ |
| 1331 | + $envVar: { name: 'FOO', parseFloat: true }, |
| 1332 | + }); |
| 1333 | + |
| 1334 | + expect(await source.readToJSON([envVarDirective()])).toEqual(11.2); |
| 1335 | + }); |
| 1336 | + |
| 1337 | + it('fails when float is invalid', async () => { |
| 1338 | + process.env.FOO = 'not a number'; |
| 1339 | + |
| 1340 | + const source = new LiteralSource({ |
| 1341 | + $envVar: { name: 'FOO', parseFloat: true }, |
| 1342 | + }); |
| 1343 | + |
| 1344 | + await expect(source.read([envVarDirective()])).rejects.toThrow(); |
| 1345 | + }); |
| 1346 | + |
| 1347 | + it('parses boolean = true', async () => { |
| 1348 | + process.env.FOO = 'true'; |
| 1349 | + |
| 1350 | + const source = new LiteralSource({ |
| 1351 | + $envVar: { name: 'FOO', parseBool: true }, |
| 1352 | + }); |
| 1353 | + |
| 1354 | + expect(await source.readToJSON([envVarDirective()])).toEqual(true); |
| 1355 | + }); |
| 1356 | + |
| 1357 | + it('parses boolean = 1', async () => { |
| 1358 | + process.env.FOO = '1'; |
| 1359 | + |
| 1360 | + const source = new LiteralSource({ |
| 1361 | + $envVar: { name: 'FOO', parseBool: true }, |
| 1362 | + }); |
| 1363 | + |
| 1364 | + expect(await source.readToJSON([envVarDirective()])).toEqual(true); |
| 1365 | + }); |
| 1366 | + |
| 1367 | + it('parses boolean = 0', async () => { |
| 1368 | + process.env.FOO = '0'; |
| 1369 | + |
| 1370 | + const source = new LiteralSource({ |
| 1371 | + $envVar: { name: 'FOO', parseBool: true }, |
| 1372 | + }); |
| 1373 | + |
| 1374 | + expect(await source.readToJSON([envVarDirective()])).toEqual(false); |
| 1375 | + }); |
| 1376 | + |
| 1377 | + it('parses boolean = false', async () => { |
| 1378 | + process.env.FOO = 'false'; |
| 1379 | + |
| 1380 | + const source = new LiteralSource({ |
| 1381 | + $envVar: { name: 'FOO', parseBool: true }, |
| 1382 | + }); |
| 1383 | + |
| 1384 | + expect(await source.readToJSON([envVarDirective()])).toEqual(false); |
| 1385 | + }); |
| 1386 | + |
| 1387 | + it('doesnt visit fallback if name is defined', async () => { |
| 1388 | + const failDirective = forKey('$fail', () => () => { |
| 1389 | + throw new Error(); |
| 1390 | + }); |
| 1391 | + |
| 1392 | + process.env.FOO = 'foo'; |
| 1393 | + |
| 1394 | + const source = new LiteralSource({ |
| 1395 | + foo: { $envVar: { name: 'FOO', fallback: { fail: true } } }, |
| 1396 | + }); |
| 1397 | + |
| 1398 | + const parsed = await source.read([envVarDirective(), failDirective]); |
| 1399 | + |
| 1400 | + expect(parsed.toJSON()).toEqual({ foo: 'foo' }); |
| 1401 | + }); |
| 1402 | + |
| 1403 | + it('reads special case name APP_CONFIG_ENV', async () => { |
| 1404 | + process.env.NODE_ENV = 'qa'; |
| 1405 | + |
| 1406 | + const source = new LiteralSource({ |
| 1407 | + foo: { $envVar: { name: 'APP_CONFIG_ENV' } }, |
| 1408 | + }); |
| 1409 | + |
| 1410 | + const parsed = await source.read([envVarDirective()]); |
| 1411 | + |
| 1412 | + expect(parsed.toJSON()).toEqual({ foo: 'qa' }); |
| 1413 | + }); |
| 1414 | +}); |
| 1415 | + |
1214 | 1416 | describe('$timestamp directive', () => {
|
1215 | 1417 | it('uses the current date', async () => {
|
1216 | 1418 | const now = new Date();
|
|
0 commit comments