Skip to content

Commit 76ce3dc

Browse files
committed
feat: more tests for $extends with env override
1 parent 38e6e9f commit 76ce3dc

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

app-config-extensions/src/env-var-directive.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ export function envVarDirective(
1818
return named(
1919
'$envVar',
2020
forKey('$envVar', (value, key, parentKeys, context) => async (parse) => {
21-
const environment = currentEnvFromContext(
22-
context,
23-
asEnvOptions(environmentOverride, aliases, environmentSourceNames),
24-
);
25-
2621
let name: string;
2722
let parseInt = false;
2823
let parseFloat = false;
@@ -98,7 +93,12 @@ export function envVarDirective(
9893

9994
let resolvedValue = process.env[name];
10095

101-
if (!resolvedValue && name === 'APP_CONFIG_ENV') {
96+
if (name === 'APP_CONFIG_ENV') {
97+
const environment = currentEnvFromContext(
98+
context,
99+
asEnvOptions(environmentOverride, aliases, environmentSourceNames),
100+
);
101+
102102
resolvedValue = environment;
103103
}
104104

app-config-extensions/src/extends-directive.test.ts

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { withTempFiles } from '@app-config/test-utils';
22
import { LiteralSource, NotFoundError } from '@app-config/core';
33
import { FileSource } from '@app-config/node';
44
import { extendsDirective, extendsSelfDirective, overrideDirective } from './extends-directive';
5-
import { envDirective } from './env-directive';
5+
import { envDirective, envVarDirective, substituteDirective } from './index';
66

77
describe('$extends directive', () => {
88
it('fails if file is missing', async () => {
@@ -417,14 +417,20 @@ describe('$override directive', () => {
417417
await withTempFiles(
418418
{
419419
'test-file.yml': `
420-
foo:
420+
bar:
421421
$env:
422422
default: 44
423423
dev: 88
424424
`,
425425
},
426426
async (inDir) => {
427427
const source = new LiteralSource({
428+
foo: {
429+
$env: {
430+
default: 44,
431+
dev: 88,
432+
},
433+
},
428434
$extends: {
429435
path: inDir('test-file.yml'),
430436
env: 'development',
@@ -433,7 +439,61 @@ describe('$override directive', () => {
433439

434440
const parsed = await source.read([envDirective(), extendsDirective()]);
435441

436-
expect(parsed.toJSON()).toEqual({ foo: 88 });
442+
expect(parsed.toJSON()).toEqual({ foo: 44, bar: 88 });
443+
},
444+
);
445+
});
446+
447+
it('overrides env and $envVar', async () => {
448+
await withTempFiles(
449+
{
450+
'test-file.yml': `
451+
bar:
452+
$envVar: APP_CONFIG_ENV
453+
`,
454+
},
455+
async (inDir) => {
456+
process.env.APP_CONFIG_ENV = 'test';
457+
const source = new LiteralSource({
458+
foo: {
459+
$envVar: 'APP_CONFIG_ENV',
460+
},
461+
$extends: {
462+
path: inDir('test-file.yml'),
463+
env: 'development',
464+
},
465+
});
466+
467+
const parsed = await source.read([extendsDirective(), envVarDirective()]);
468+
469+
expect(parsed.toJSON()).toEqual({ foo: 'test', bar: 'development' });
470+
},
471+
);
472+
});
473+
474+
it('overrides env and $substitute', async () => {
475+
await withTempFiles(
476+
{
477+
'test-file.yml': `
478+
bar:
479+
$substitute: '$APP_CONFIG_ENV'
480+
`,
481+
},
482+
async (inDir) => {
483+
process.env.APP_CONFIG_ENV = 'test';
484+
const source = new LiteralSource({
485+
foo: {
486+
$substitute: '$APP_CONFIG_ENV',
487+
},
488+
$extends: {
489+
path: inDir('test-file.yml'),
490+
env: 'development',
491+
},
492+
});
493+
494+
const parsed = await source.read([extendsDirective(), substituteDirective()]);
495+
496+
expect(parsed.toJSON()).toEqual({ foo: 'test', bar: 'development' });
437497
},
438498
);
439499
});

app-config-extensions/src/substitute-directive.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function substituteDirective(
102102

103103
let resolvedValue = process.env[name];
104104

105-
if (!resolvedValue && name === 'APP_CONFIG_ENV') {
105+
if (name === 'APP_CONFIG_ENV') {
106106
resolvedValue = environment;
107107
}
108108

@@ -165,18 +165,18 @@ function performAllSubstitutions(text: string, envType?: string): string {
165165
if (varName) {
166166
const env = process.env[varName];
167167

168-
if (env !== undefined) {
169-
output = output.replace(fullMatch, env);
170-
} else if (fallback !== undefined) {
171-
// we'll recurse again, so that ${FOO:-${FALLBACK}} -> ${FALLBACK} -> value
172-
output = performAllSubstitutions(output.replace(fullMatch, fallback), envType);
173-
} else if (varName === 'APP_CONFIG_ENV') {
168+
if (varName === 'APP_CONFIG_ENV') {
174169
if (!envType) {
175170
throw new AppConfigError(`Could not find environment variable ${varName}`);
176171
}
177172

178173
// there's a special case for APP_CONFIG_ENV, which is always the envType
179174
output = output.replace(fullMatch, envType);
175+
} else if (env !== undefined) {
176+
output = output.replace(fullMatch, env);
177+
} else if (fallback !== undefined) {
178+
// we'll recurse again, so that ${FOO:-${FALLBACK}} -> ${FALLBACK} -> value
179+
output = performAllSubstitutions(output.replace(fullMatch, fallback), envType);
180180
} else {
181181
throw new AppConfigError(`Could not find environment variable ${varName}`);
182182
}

0 commit comments

Comments
 (0)