Skip to content

Commit 7caed5c

Browse files
committed
feat(#131): starts test for $extends with env
1 parent 6285b3d commit 7caed5c

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

app-config-cli/src/validation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export async function validateAllConfigVariants({
3535

3636
await Promise.all(
3737
appConfigFiles.map(async (filename) => {
38-
const parsed = await new FileSource(filename).read();
38+
const parsed = await new FileSource(filename).read(undefined, { environmentAliases });
3939

4040
parsed.visitAll((value) => {
4141
const obj = value.asObject();

app-config-extensions/src/index.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,31 @@ describe('$env directive', () => {
843843
const parsed2 = await source.read([envDirective()]);
844844
expect(parsed2.toJSON()).toEqual({ sibling: true, testing: false });
845845
});
846+
847+
it('overrides env', async () => {
848+
await withTempFiles(
849+
{
850+
'test-file.yml': `
851+
foo:
852+
$env:
853+
default: 44
854+
dev: 88
855+
`,
856+
},
857+
async (inDir) => {
858+
const source = new LiteralSource({
859+
$extends: {
860+
path: inDir('test-file.yml'),
861+
env: 'development',
862+
},
863+
});
864+
865+
const parsed = await source.read([envDirective(), extendsDirective()]);
866+
867+
expect(parsed.toJSON()).toEqual({ foo: 88 });
868+
},
869+
);
870+
});
846871
});
847872

848873
/* eslint-disable no-template-curly-in-string */

app-config-extensions/src/index.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -411,26 +411,37 @@ function fileReferenceDirective(keyName: string, meta: ParsedValueMetadata): Par
411411
SchemaBuilder.emptySchema()
412412
.addString('path')
413413
.addBoolean('optional', {}, false)
414-
.addString('select', {}, false),
414+
.addString('select', {}, false)
415+
.addString('env', {}, false),
415416
);
416417

417418
return SchemaBuilder.oneOf(reference, SchemaBuilder.arraySchema(reference));
418419
},
419-
(value) => async (_, __, context, extensions) => {
420-
const retrieveFile = async (filepath: string, subselector?: string, isOptional = false) => {
421-
const resolvedPath = resolveFilepath(context, filepath);
420+
(value, _, context) => async (_, __, originalSource, extensions) => {
421+
const retrieveFile = async (
422+
filepath: string,
423+
subselector?: string,
424+
isOptional = false,
425+
env?: string,
426+
) => {
427+
const resolvedPath = resolveFilepath(originalSource, filepath);
422428

423429
logger.verbose(`Loading file for ${keyName}: ${resolvedPath}`);
424430

425431
const source = new FileSource(resolvedPath);
426432

427-
const parsed = await source.read(extensions).catch((error) => {
428-
if (error instanceof NotFoundError && isOptional) {
429-
return ParsedValue.literal({});
430-
}
433+
const parsed = await source
434+
.read(extensions, {
435+
...context,
436+
environmentOverride: env ?? context.environmentOverride,
437+
})
438+
.catch((error) => {
439+
if (error instanceof NotFoundError && isOptional) {
440+
return ParsedValue.literal({});
441+
}
431442

432-
throw error;
433-
});
443+
throw error;
444+
});
434445

435446
if (subselector) {
436447
const found = parsed.property(subselector.split('.'));
@@ -458,15 +469,15 @@ function fileReferenceDirective(keyName: string, meta: ParsedValueMetadata): Par
458469
if (typeof ext === 'string') {
459470
parsed = ParsedValue.merge(parsed, await retrieveFile(ext));
460471
} else {
461-
const { path, optional, select } = ext;
472+
const { path, optional, select, env } = ext;
462473

463-
parsed = ParsedValue.merge(parsed, await retrieveFile(path, select, optional));
474+
parsed = ParsedValue.merge(parsed, await retrieveFile(path, select, optional, env));
464475
}
465476
}
466477
} else {
467-
const { path, optional, select } = value;
478+
const { path, optional, select, env } = value;
468479

469-
parsed = await retrieveFile(path, select, optional);
480+
parsed = await retrieveFile(path, select, optional, env);
470481
}
471482

472483
return parsed.assignMeta(meta);

0 commit comments

Comments
 (0)