From a86db5d0d15f736ec25229ddde62859ea15f306e Mon Sep 17 00:00:00 2001 From: Andrei Listochkin Date: Wed, 11 May 2022 13:22:58 +0100 Subject: iterative dependency solver First, we go through every environment variable key and record all cases where there are reference to other variables / dependencies. We track two sets of variables - resolved and yet-to-be-resolved. We pass over a list of variables over and over again and when all variable's dependencies were resolved during previous passes we perform a replacement for that variable, too. Over time the size of `toResolve` set should go down to zero, however circular dependencies may prevent that. We track the size of `toResolve` between iterations to avoid infinite looping. At the end we produce an object of the same size and shape as the original, but with the values replace with resolved versions. --- editors/code/tests/unit/settings.test.ts | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 editors/code/tests/unit/settings.test.ts (limited to 'editors/code/tests') diff --git a/editors/code/tests/unit/settings.test.ts b/editors/code/tests/unit/settings.test.ts new file mode 100644 index 00000000000..12734d15667 --- /dev/null +++ b/editors/code/tests/unit/settings.test.ts @@ -0,0 +1,41 @@ +import * as assert from 'assert'; +import { Context } from '.'; +import { substituteVariablesInEnv } from '../../src/config'; + +export async function getTests(ctx: Context) { + await ctx.suite('Server Env Settings', suite => { + suite.addTest('Replacing Env Variables', async () => { + const envJson = { + USING_MY_VAR: "${env:MY_VAR} test ${env:MY_VAR}", + MY_VAR: "test" + }; + const expectedEnv = { + USING_MY_VAR: "test test test", + MY_VAR: "test" + }; + const actualEnv = await substituteVariablesInEnv(envJson); + assert.deepStrictEqual(actualEnv, expectedEnv); + }); + + suite.addTest('Circular dependencies remain as is', async () => { + const envJson = { + A_USES_B: "${env:B_USES_A}", + B_USES_A: "${env:A_USES_B}", + C_USES_ITSELF: "${env:C_USES_ITSELF}", + D_USES_C: "${env:C_USES_ITSELF}", + E_IS_ISOLATED: "test", + F_USES_E: "${env:E_IS_ISOLATED}" + }; + const expectedEnv = { + A_USES_B: "${env:B_USES_A}", + B_USES_A: "${env:A_USES_B}", + C_USES_ITSELF: "${env:C_USES_ITSELF}", + D_USES_C: "${env:C_USES_ITSELF}", + E_IS_ISOLATED: "test", + F_USES_E: "test" + }; + const actualEnv = await substituteVariablesInEnv(envJson); + assert.deepStrictEqual(actualEnv, expectedEnv); + }); + }); +} -- cgit 1.4.1-3-g733a5