copyWithoutRender, extensions inputs for fetch:cookiecutter

as discussed in this GitHub issue:
https://github.com/backstage/backstage/issues/4920

Signed-off-by: Vinay P <mail@vinayvinay.com>
This commit is contained in:
Vinay P
2021-03-15 12:45:06 +00:00
committed by Vinay P
parent 38fcac8133
commit b1dc16baa1
3 changed files with 111 additions and 1 deletions
@@ -108,6 +108,56 @@ describe('fetch:cookiecutter', () => {
});
});
it('should execute the cookiecutter templater with optional inputs if they are present and valid', async () => {
await action.handler({
...mockContext,
input: {
...mockContext.input,
copyWithoutRender: ['goreleaser.yml'],
extensions: [
'jinja2_custom_filters_extension.string_filters_extension.StringFilterExtension',
],
},
});
expect(cookiecutterTemplater.run).toHaveBeenCalledWith({
workspacePath: mockTmpDir,
dockerClient: mockDockerClient,
logStream: mockContext.logStream,
values: {
...mockContext.input.values,
_copy_without_render: ['goreleaser.yml'],
_extensions: [
'jinja2_custom_filters_extension.string_filters_extension.StringFilterExtension',
],
},
});
});
it('should throw if copyWithoutRender is not an Array', async () => {
await expect(
action.handler({
...mockContext,
input: {
...mockContext.input,
copyWithoutRender: 'xyz',
},
}),
).rejects.toThrow(/copyWithoutRender must be an Array/);
});
it('should throw if extensions is not an Array', async () => {
await expect(
action.handler({
...mockContext,
input: {
...mockContext.input,
extensions: 'xyz',
},
}),
).rejects.toThrow(/extensions must be an Array/);
});
it('should throw if there is no cookiecutter templater initialized', async () => {
const templatersWithoutCookiecutter = new Templaters();
@@ -37,6 +37,8 @@ export function createFetchCookiecutterAction(options: {
url: string;
targetPath?: string;
values: JsonObject;
copyWithoutRender?: string[];
extensions?: string[];
}>({
id: 'fetch:cookiecutter',
description:
@@ -63,6 +65,24 @@ export function createFetchCookiecutterAction(options: {
description: 'Values to pass on to cookiecutter for templating',
type: 'object',
},
copyWithoutRender: {
title: 'Copy Without Render',
description:
'Avoid rendering directories and files in the template',
type: 'array',
items: {
type: 'string',
},
},
extensions: {
title: 'Template Extensions',
description:
"Jinja2 extensions to add filters, tests, globals or extend the parser. Extensions must be installed in the container or on the host where Cookiecutter executes. See the contrib directory in Backstage's repo for more information",
type: 'array',
items: {
type: 'string',
},
},
},
},
},
@@ -76,6 +96,18 @@ export function createFetchCookiecutterAction(options: {
);
const resultDir = resolvePath(workDir, 'result');
if (
ctx.input.copyWithoutRender &&
!Array.isArray(ctx.input.copyWithoutRender)
) {
throw new InputError(
'Fetch action input copyWithoutRender must be an Array',
);
}
if (ctx.input.extensions && !Array.isArray(ctx.input.extensions)) {
throw new InputError('Fetch action input extensions must be an Array');
}
await fetchContents({
reader,
integrations,
@@ -85,13 +117,18 @@ export function createFetchCookiecutterAction(options: {
});
const cookiecutter = templaters.get('cookiecutter');
const values = {
...(ctx.input.values as TemplaterValues),
_copy_without_render: ctx.input.copyWithoutRender,
_extensions: ctx.input.extensions,
};
// Will execute the template in ./template and put the result in ./result
await cookiecutter.run({
workspacePath: workDir,
dockerClient,
logStream: ctx.logStream,
values: ctx.input.values as TemplaterValues,
values,
});
// Finally move the template result into the task workspace
@@ -168,6 +168,29 @@ describe('CookieCutter Templater', () => {
});
});
it('should run the docker container mentioned in configs, overriding the default', async () => {
const values = {
owner: 'blobby',
storePath: 'https://github.com/org/repo',
imageName: 'foo/cookiecutter-image-with-extensions',
};
jest.spyOn(fs, 'readdir').mockResolvedValueOnce(['newthing'] as any);
const templater = new CookieCutter();
await templater.run({
workspacePath: 'tempdir',
values,
dockerClient: mockDocker,
});
expect(runDockerContainer).toHaveBeenCalledWith(
expect.objectContaining({
imageName: 'foo/cookiecutter-image-with-extensions',
}),
);
});
it('should pass through the streamer to the run docker helper', async () => {
const stream = new PassThrough();