Skip to content

Commit

Permalink
Skip empty file names during scaffolder
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Jacomb <tim.jacomb@hmcts.net>
  • Loading branch information
Tim Jacomb committed Nov 12, 2021
1 parent d0ea59b commit 26eb174
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-spies-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---

Skip empty file names when scaffolding with nunjucks
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ describe('fetch:template', () => {
name: 'test-project',
count: 1234,
itemList: ['first', 'second', 'third'],
showDummyFile: false,
},
});

Expand All @@ -163,6 +164,10 @@ describe('fetch:template', () => {
},
'.${{ values.name }}': '${{ values.itemList | dump }}',
'a-binary-file.png': aBinaryFile,
'{% if values.showDummyFile %}dummy-file.txt{% else %}{% endif %}':
'dummy file',
'${{ "dummy-file2.txt" if values.showDummyFile else "" }}':
'some dummy file',
},
});

Expand All @@ -181,6 +186,18 @@ describe('fetch:template', () => {
);
});

it('skips empty filename', async () => {
await expect(
fs.pathExists(`${workspacePath}/target/dummy-file.txt`),
).resolves.toEqual(false);
});

it('skips empty filename syntax #2', async () => {
await expect(
fs.pathExists(`${workspacePath}/target/dummy-file2.txt`),
).resolves.toEqual(false);
});

it('copies files with no templating in names or content successfully', async () => {
await expect(
fs.readFile(`${workspacePath}/target/static.txt`, 'utf-8'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ export function createFetchTemplateAction(options: {
localOutputPath = templater.renderString(localOutputPath, context);
}
const outputPath = resolvePath(outputDir, localOutputPath);
// variables have been expanded to make an empty file name
// this is due to a conditional like if values.my_condition then file-name.txt else empty string so skip
if (outputDir === outputPath) {
continue;
}

if (!renderContents && !extension) {
ctx.logger.info(
Expand Down

0 comments on commit 26eb174

Please sign in to comment.