fix: scaffolder action catalog:write not working in nested folders

From the design of `catalog:write`, it is meant to support writing to a nested file path, e.g. `backstage/catalog-info.yaml`.

However, if the directory doesn't exists, then `fs.writeFile` will throw an error. This commit fixed the issue by using `fs.outputFile` instead which will create the parent directory if it does not exist: https://github.com/jprichardson/node-fs-extra/blob/master/docs/outputFile.md

The test `'should support a custom filename'` in `write.test.ts` did not catch this issue because `fs` is mocked. It is better to use fake instead of mock, e.g. create a throwaway temp folder for the tests since the file operations are part of the feature. However fixing of the tests as a whole is out of scope for this PR.

Signed-off-by: ShaoWei Teo <tookunokaze@gmail.com>
This commit is contained in:
Teo ShaoWei
2024-07-24 20:11:23 +08:00
committed by ShaoWei Teo
parent bef0282748
commit ef87e06e5b
4 changed files with 14 additions and 9 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---
Fix scaffolder action catalog:write to work in nested folders.
@@ -55,8 +55,8 @@ describe('catalog:write', () => {
input: yaml.parse(examples[0].example).steps[0].input,
});
expect(fsMock.writeFile).toHaveBeenCalledTimes(1);
expect(fsMock.writeFile).toHaveBeenCalledWith(
expect(fsMock.outputFile).toHaveBeenCalledTimes(1);
expect(fsMock.outputFile).toHaveBeenCalledWith(
resolvePath(mockContext.workspacePath, 'catalog-info.yaml'),
yaml.stringify(entity),
);
@@ -59,8 +59,8 @@ describe('catalog:write', () => {
},
});
expect(fsMock.writeFile).toHaveBeenCalledTimes(1);
expect(fsMock.writeFile).toHaveBeenCalledWith(
expect(fsMock.outputFile).toHaveBeenCalledTimes(1);
expect(fsMock.outputFile).toHaveBeenCalledWith(
resolvePath(mockContext.workspacePath, 'catalog-info.yaml'),
yaml.stringify(entity),
);
@@ -88,8 +88,8 @@ describe('catalog:write', () => {
},
});
expect(fsMock.writeFile).toHaveBeenCalledTimes(1);
expect(fsMock.writeFile).toHaveBeenCalledWith(
expect(fsMock.outputFile).toHaveBeenCalledTimes(1);
expect(fsMock.outputFile).toHaveBeenCalledWith(
resolvePath(mockContext.workspacePath, 'some-dir/entity-info.yaml'),
yaml.stringify(entity),
);
@@ -118,8 +118,8 @@ describe('catalog:write', () => {
'backstage.io/source-template': 'template:default/test-skeleton',
};
expect(fsMock.writeFile).toHaveBeenCalledTimes(1);
expect(fsMock.writeFile).toHaveBeenCalledWith(
expect(fsMock.outputFile).toHaveBeenCalledTimes(1);
expect(fsMock.outputFile).toHaveBeenCalledWith(
resolvePath(mockContext.workspacePath, 'catalog-info.yaml'),
yaml.stringify(expectedEntity),
);
@@ -54,7 +54,7 @@ export function createCatalogWriteAction() {
const path = filePath ?? 'catalog-info.yaml';
ctx.logger.info(`Writing ${path}`);
await fs.writeFile(
await fs.outputFile(
resolveSafeChildPath(ctx.workspacePath, path),
yaml.stringify({
...entity,