feat(scaffolder): add description and tests for directories

Signed-off-by: Andrea Falzetti <afalzetti@gmail.com>
This commit is contained in:
Andrea Falzetti
2021-07-05 13:37:03 +01:00
parent 62c2f10f7d
commit 00f590ad53
4 changed files with 32 additions and 9 deletions
@@ -46,6 +46,9 @@ describe('fs:delete', () => {
[workspacePath]: {
'unit-test-a.js': 'hello',
'unit-test-b.js': 'world',
'a-folder': {
'unit-test-in-a-folder.js2': 'content',
},
},
});
});
@@ -90,14 +93,18 @@ describe('fs:delete', () => {
...mockContext,
input: { files: ['/foo/../../../index.js'] },
}),
).rejects.toThrow(/Relative path is not allowed to refer to a directory outside its parent/);
).rejects.toThrow(
/Relative path is not allowed to refer to a directory outside its parent/,
);
await expect(
action.handler({
...mockContext,
input: { files: ['../../../index.js'] },
}),
).rejects.toThrow(/Relative path is not allowed to refer to a directory outside its parent/);
).rejects.toThrow(
/Relative path is not allowed to refer to a directory outside its parent/,
);
});
it('should call fs.rm with the correct values', async () => {
@@ -21,6 +21,7 @@ import fs from 'fs-extra';
export const createFilesystemDeleteAction = () => {
return createTemplateAction<{ files: string[] }>({
id: 'fs:delete',
description: 'Deletes files and directories from the workspace',
schema: {
input: {
required: ['files'],
@@ -28,7 +29,7 @@ export const createFilesystemDeleteAction = () => {
properties: {
files: {
title: 'Files',
description: 'A list of files or directories that will be deleted',
description: 'A list of files and directories that will be deleted',
type: 'array',
items: {
type: 'string',
@@ -37,6 +37,10 @@ describe('fs:rename', () => {
from: 'unit-test-b.js',
to: 'new-b.js',
},
{
from: 'a-folder',
to: 'brand-new-folder',
},
];
const mockContext = {
input: {
@@ -57,6 +61,9 @@ describe('fs:rename', () => {
'unit-test-a.js': 'hello',
'unit-test-b.js': 'world',
'unit-test-c.js': 'i will be overwritten :-(',
'a-folder': {
'file.md': 'content',
},
},
});
});
@@ -124,14 +131,18 @@ describe('fs:rename', () => {
...mockContext,
input: { files: [{ from: 'index.js', to: '/core/../../../index.js' }] },
}),
).rejects.toThrow(/Relative path is not allowed to refer to a directory outside its parent/);
).rejects.toThrow(
/Relative path is not allowed to refer to a directory outside its parent/,
);
await expect(
action.handler({
...mockContext,
input: { files: [{ from: '/core/../../../index.js', to: 'index.js' }] },
}),
).rejects.toThrow(/Relative path is not allowed to refer to a directory outside its parent/);
).rejects.toThrow(
/Relative path is not allowed to refer to a directory outside its parent/,
);
});
it('should throw is trying to override by mistake', async () => {
@@ -184,7 +195,6 @@ describe('fs:rename', () => {
const destBeforeContent = fs.readFileSync(destFilePath, 'utf-8');
expect(sourceBeforeContent).not.toEqual(destBeforeContent);
await action.handler({
...mockContext,
@@ -28,6 +28,7 @@ interface FilesToRename extends JsonObject {
export const createFilesystemRenameAction = () => {
return createTemplateAction<{ files: FilesToRename }>({
id: 'fs:rename',
description: 'Renames files and directories within the workspace',
schema: {
input: {
required: ['files'],
@@ -35,7 +36,8 @@ export const createFilesystemRenameAction = () => {
properties: {
files: {
title: 'Files',
description: 'A list of file names that will be renamed',
description:
'A list of file and directory names that will be renamed',
type: 'array',
items: {
type: 'object',
@@ -70,12 +72,15 @@ export const createFilesystemRenameAction = () => {
throw new InputError('each file must have a from and to property');
}
const sourceFilepath = resolveSafeChildPath(ctx.workspacePath, file.from);
const sourceFilepath = resolveSafeChildPath(
ctx.workspacePath,
file.from,
);
const destFilepath = resolveSafeChildPath(ctx.workspacePath, file.to);
try {
await fs.move(sourceFilepath, destFilepath, {
overwrite: file.overwrite || false,
overwrite: file.overwrite ?? false,
});
ctx.logger.info(
`File ${sourceFilepath} renamed to ${destFilepath} successfully`,