From 6c2f31a5cb4d3add6c9c286073befc2ba2a93788 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sun, 6 Jun 2021 05:26:52 -0400 Subject: [PATCH] tests: Add tests for `settings.json` parsing --- src/node/utils/Settings.js | 4 ++ src/tests/backend/specs/settings.js | 61 +++++++++++++++++++++++++++ src/tests/backend/specs/settings.json | 39 +++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 src/tests/backend/specs/settings.js create mode 100644 src/tests/backend/specs/settings.json diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index 482abd730..40576c345 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -825,5 +825,9 @@ exports.reloadSettings = () => { console.log(`Random string used for versioning assets: ${exports.randomVersionString}`); }; +exports.exportedForTestingOnly = { + parseSettings, +}; + // initially load settings exports.reloadSettings(); diff --git a/src/tests/backend/specs/settings.js b/src/tests/backend/specs/settings.js new file mode 100644 index 000000000..e737f4f34 --- /dev/null +++ b/src/tests/backend/specs/settings.js @@ -0,0 +1,61 @@ +'use strict'; + +const assert = require('assert').strict; +const {parseSettings} = require('../../../node/utils/Settings').exportedForTestingOnly; +const path = require('path'); +const process = require('process'); + +describe(__filename, function () { + describe('parseSettings', function () { + let settings; + const envVarSubstTestCases = [ + {name: 'true', val: 'true', var: 'SET_VAR_TRUE', want: true}, + {name: 'false', val: 'false', var: 'SET_VAR_FALSE', want: false}, + {name: 'null', val: 'null', var: 'SET_VAR_NULL', want: null}, + {name: 'undefined', val: 'undefined', var: 'SET_VAR_UNDEFINED', want: undefined}, + {name: 'number', val: '123', var: 'SET_VAR_NUMBER', want: 123}, + {name: 'string', val: 'foo', var: 'SET_VAR_STRING', want: 'foo'}, + {name: 'empty string', val: '', var: 'SET_VAR_EMPTY_STRING', want: ''}, + ]; + + before(async function () { + for (const tc of envVarSubstTestCases) process.env[tc.var] = tc.val; + delete process.env.UNSET_VAR; + settings = parseSettings(path.join(__dirname, 'settings.json'), true); + assert(settings != null); + }); + + describe('environment variable substitution', function () { + describe('set', function () { + for (const tc of envVarSubstTestCases) { + it(tc.name, async function () { + const obj = settings['environment variable substitution'].set; + if (tc.name === 'undefined') { + assert(!(tc.name in obj)); + } else { + assert.equal(obj[tc.name], tc.want); + } + }); + } + }); + + describe('unset', function () { + it('no default', async function () { + const obj = settings['environment variable substitution'].unset; + assert.equal(obj['no default'], null); + }); + + for (const tc of envVarSubstTestCases) { + it(tc.name, async function () { + const obj = settings['environment variable substitution'].unset; + if (tc.name === 'undefined') { + assert(!(tc.name in obj)); + } else { + assert.equal(obj[tc.name], tc.want); + } + }); + } + }); + }); + }); +}); diff --git a/src/tests/backend/specs/settings.json b/src/tests/backend/specs/settings.json new file mode 100644 index 000000000..12b4748c0 --- /dev/null +++ b/src/tests/backend/specs/settings.json @@ -0,0 +1,39 @@ +// line comment +/* + * block comment + */ +{ + "trailing commas": { + "lists": { + "multiple lines": [ + "", + ] + }, + "objects": { + "multiple lines": { + "key": "", + } + } + }, + "environment variable substitution": { + "set": { + "true": "${SET_VAR_TRUE}", + "false": "${SET_VAR_FALSE}", + "null": "${SET_VAR_NULL}", + "undefined": "${SET_VAR_UNDEFINED}", + "number": "${SET_VAR_NUMBER}", + "string": "${SET_VAR_STRING}", + "empty string": "${SET_VAR_EMPTY_STRING}" + }, + "unset": { + "no default": "${UNSET_VAR}", + "true": "${UNSET_VAR:true}", + "false": "${UNSET_VAR:false}", + "null": "${UNSET_VAR:null}", + "undefined": "${UNSET_VAR:undefined}", + "number": "${UNSET_VAR:123}", + "string": "${UNSET_VAR:foo}", + "empty string": "${UNSET_VAR:}" + } + } +}