Update per PR comments and add two test assertions

Signed-off-by: JP Dhabolt <jdhabolt@agero.com>
This commit is contained in:
JP Dhabolt
2021-04-01 12:00:17 +00:00
parent b593061da3
commit 0efa7ae6a7
4 changed files with 43 additions and 26 deletions
@@ -29,23 +29,21 @@ const env = jest.fn(async (name: string) => {
} as { [name: string]: string })[name];
});
const substitute = jest.fn(
async (
value: JsonValue,
): Promise<{ applied: boolean; value?: JsonValue }> => {
if (typeof value !== 'string') {
return { applied: false };
}
if (value.includes(substituteMe)) {
return {
applied: true,
value: value.replace(substituteMe, mySubstitution),
};
}
const substitute = async (
value: JsonValue,
): Promise<{ applied: boolean; value?: JsonValue }> => {
if (typeof value !== 'string') {
return { applied: false };
},
);
}
if (value.includes(substituteMe)) {
return {
applied: true,
value: value.replace(substituteMe, mySubstitution),
};
}
return { applied: false };
};
const readFile = jest.fn(async (path: string) => {
const content = ({
@@ -55,12 +55,18 @@ export function createIncludeTransform(
}
const rawIncludedValue = input[includeKey];
const substituteResults = await substitute(rawIncludedValue!, baseDir);
if (typeof rawIncludedValue !== 'string') {
throw new Error(`${includeKey} include value is not a string`);
}
const substituteResults = await substitute(rawIncludedValue, baseDir);
const includeValue = substituteResults.applied
? substituteResults.value
: rawIncludedValue;
if (typeof includeValue !== 'string') {
throw new Error(`${includeKey} include value is not a string`);
// The second string check is needed for Typescript to know this is a string.
if (includeValue === undefined || typeof includeValue !== 'string') {
throw new Error(`${includeKey} substitution value was undefined`);
}
switch (includeKey) {
@@ -56,6 +56,12 @@ describe('substituteTransform', () => {
applied: true,
value: undefined,
});
await expect(
substituteTransform('empty substitute ${}', '/'),
).resolves.toEqual({
applied: true,
value: undefined,
});
await expect(
substituteTransform('foo ${MISSING} ${SECRET}', '/'),
).resolves.toEqual({ applied: true, value: undefined });
@@ -65,5 +71,11 @@ describe('substituteTransform', () => {
await expect(
substituteTransform('foo ${SECRET} $$${ESCAPE_ME}', '/'),
).resolves.toEqual({ applied: true, value: 'foo my-secret $${ESCAPE_ME}' });
await expect(
substituteTransform('foo $${ESCAPE_ME} $$${ESCAPE_ME_TOO} $${}', '/'),
).resolves.toEqual({
applied: true,
value: 'foo ${ESCAPE_ME} $${ESCAPE_ME_TOO} ${}',
});
});
});
@@ -28,15 +28,16 @@ export function createSubstitutionTransform(env: EnvFunc): TransformFunc {
return { applied: false };
}
const parts: (string | undefined)[] = input.split(/(?<!\$)\$\{([^{}]+)\}/);
for (let i = 0; i < parts.length; i++) {
if (i % 2 === 0) {
parts[i] = parts[i]!.replace(/\$\${(.*?)\}/, '${$1}');
}
if (i % 2 === 1) {
parts[i] = await env(parts[i]!.trim());
const parts: (string | undefined)[] = input.split(/(\$?\$\{[^{}]*\})/);
for (let i = 1; i < parts.length; i += 2) {
const part = parts[i]!;
if (part.startsWith('$$')) {
parts[i] = part.slice(1);
} else {
parts[i] = await env(part.slice(2, -1).trim());
}
}
if (parts.some(part => part === undefined)) {
return { applied: true, value: undefined };
}