address review comments and test input validation

Signed-off-by: Axel Hecht <axel@pike.org>
This commit is contained in:
Axel Hecht
2021-08-16 10:48:39 +02:00
parent 013a8742fa
commit 5536c27dbb
2 changed files with 54 additions and 6 deletions
@@ -105,6 +105,49 @@ describe('fetch:template', () => {
).rejects.toThrowError(/copyWithoutRender must be an array/i);
});
it('throws if copyWithoutRender is used with extension', async () => {
await expect(() =>
action.handler(
mockContext({
copyWithoutRender: ['abc'],
extension: true,
}),
),
).rejects.toThrowError(
/input extension incompatible with copyWithoutRender and cookiecutterCompat/,
);
});
it('throws if cookiecutterCompat is used with extension', async () => {
await expect(() =>
action.handler(
mockContext({
cookiecutterCompat: true,
extension: true,
}),
),
).rejects.toThrowError(
/input extension incompatible with copyWithoutRender and cookiecutterCompat/,
);
});
it('throws if extension string lacks a leading dot', async () => {
await expect(() =>
action.handler(
mockContext({
extension: 'njk',
}),
),
).rejects.toThrowError(/extension needs to start with a `.`/);
await expect(() =>
action.handler(
mockContext({
extension: '.',
}),
),
).rejects.toThrowError(/extension needs to start with a `.`/);
});
describe('with valid input', () => {
let context: ActionContext<FetchTemplateInput>;
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { resolve as resolvePath } from 'path';
import { resolve as resolvePath, extname } from 'path';
import { resolveSafeChildPath, UrlReader } from '@backstage/backend-common';
import { InputError } from '@backstage/errors';
import { ScmIntegrations } from '@backstage/integration';
@@ -138,10 +138,15 @@ export function createFetchTemplateAction(options: {
let extension: string | false = false;
if (ctx.input.extension) {
extension =
typeof ctx.input.extension === 'boolean'
? '.njk'
: ctx.input.extension;
extension = ctx.input.extension === true ? '.njk' : ctx.input.extension;
}
if (
extension !== false &&
(extension.length < 2 || !extension.startsWith('.'))
) {
throw new InputError(
'Fetch action input extension needs to start with a `.`',
);
}
await fetchContents({
@@ -224,7 +229,7 @@ export function createFetchTemplateAction(options: {
let localOutputPath = location;
if (extension) {
if (localOutputPath.endsWith(extension)) {
if (extname(localOutputPath) === extension) {
localOutputPath = localOutputPath.slice(0, -extension.length);
} else {
shouldCopyWithoutRender = true;