From 1777c5f52b3c3539d5be436443d74ae63d03df53 Mon Sep 17 00:00:00 2001 From: Stefan Petrovic Date: Fri, 4 Aug 2023 21:10:37 +0200 Subject: [PATCH] added test classes for delete and rename examples Signed-off-by: Stefan Petrovic --- .../filesystem/delete.examples.test.ts | 64 +++++++++++++ .../filesystem/rename.examples.test.ts | 95 +++++++++++++++++++ .../builtin/filesystem/rename.examples.ts | 3 +- 3 files changed, 161 insertions(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/delete.examples.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/delete.examples.test.ts index 7f0d9bd499..10994e3824 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/delete.examples.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/delete.examples.test.ts @@ -13,3 +13,67 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +import { createFilesystemDeleteAction } from './delete'; +import { getVoidLogger } from '@backstage/backend-common'; +import { PassThrough } from 'stream'; +import { resolve as resolvePath } from 'path'; +import * as os from 'os'; +import mockFs from 'mock-fs'; +import fs from 'fs-extra'; +import yaml from 'yaml'; +import { examples } from './delete.examples'; + +const root = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir'; +const workspacePath = resolvePath(root, 'my-workspace'); + +describe('fs:delete examples', () => { + const action = createFilesystemDeleteAction(); + + const files: string[] = yaml.parse(examples[0].example).steps[0].input.files; + + const mockContext = { + input: { + files: files, + }, + workspacePath, + logger: getVoidLogger(), + logStream: new PassThrough(), + output: jest.fn(), + createTemporaryDirectory: jest.fn(), + }; + + beforeEach(() => { + jest.restoreAllMocks(); + + mockFs({ + [workspacePath]: { + [files[0]]: 'hello', + [files[1]]: 'world', + 'a-folder': { + 'unit-test-in-a-folder.js2': 'content', + }, + }, + }); + }); + + afterEach(() => { + mockFs.restore(); + }); + + it('should call fs.rm with the correct values', async () => { + files.forEach(file => { + const filePath = resolvePath(workspacePath, file); + const fileExists = fs.existsSync(filePath); + expect(fileExists).toBe(true); + }); + + await action.handler(mockContext); + + files.forEach(file => { + const filePath = resolvePath(workspacePath, file); + const fileExists = fs.existsSync(filePath); + expect(fileExists).toBe(false); + }); + }); +}); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/rename.examples.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/rename.examples.test.ts index 7f0d9bd499..d28403fe80 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/rename.examples.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/rename.examples.test.ts @@ -13,3 +13,98 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +import * as os from 'os'; +import mockFs from 'mock-fs'; +import { resolve as resolvePath } from 'path'; +import { createFilesystemRenameAction } from './rename'; +import { getVoidLogger } from '@backstage/backend-common'; +import { PassThrough } from 'stream'; +import fs from 'fs-extra'; +import yaml from 'yaml'; +import { examples } from './rename.examples'; + +const root = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir'; +const workspacePath = resolvePath(root, 'my-workspace'); + +describe('fs:rename examples', () => { + const action = createFilesystemRenameAction(); + const files: { from: string; to: string }[] = yaml.parse(examples[0].example) + .steps[0].input.files; + + const mockContext = { + input: { + files: files, + }, + workspacePath, + logger: getVoidLogger(), + logStream: new PassThrough(), + output: jest.fn(), + createTemporaryDirectory: jest.fn(), + }; + + beforeEach(() => { + jest.restoreAllMocks(); + + mockFs({ + [workspacePath]: { + [files[0].from]: 'hello', + [files[1].from]: 'world', + [files[2].from]: '!!!', + 'file3Renamed.txt': 'I will be overwritten :(', + 'a-folder': { + 'file.md': 'content', + }, + }, + }); + }); + + afterEach(() => { + mockFs.restore(); + }); + + it('should call fs.move with the correct values', async () => { + mockContext.input.files.forEach(file => { + const filePath = resolvePath(workspacePath, file.from); + const fileExists = fs.existsSync(filePath); + expect(fileExists).toBe(true); + }); + + await action.handler(mockContext); + + mockContext.input.files.forEach(file => { + const filePath = resolvePath(workspacePath, file.from); + const fileExists = fs.existsSync(filePath); + expect(fileExists).toBe(false); + }); + }); + + it('should override when requested', async () => { + const sourceFile = files[2].from; + const destFile = files[2].to; + const sourceFilePath = resolvePath(workspacePath, sourceFile); + const destFilePath = resolvePath(workspacePath, destFile); + + const sourceBeforeContent = fs.readFileSync(sourceFilePath, 'utf-8'); + const destBeforeContent = fs.readFileSync(destFilePath, 'utf-8'); + + expect(sourceBeforeContent).not.toEqual(destBeforeContent); + + await action.handler({ + ...mockContext, + input: { + files: [ + { + from: sourceFile, + to: destFile, + overwrite: true, + }, + ], + }, + }); + + const destAfterContent = fs.readFileSync(destFilePath, 'utf-8'); + + expect(sourceBeforeContent).toEqual(destAfterContent); + }); +}); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/rename.examples.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/rename.examples.ts index bceee899fc..59d7483a10 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/rename.examples.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/filesystem/rename.examples.ts @@ -28,8 +28,9 @@ export const examples: TemplateExample[] = [ name: 'Rename files', input: { files: [ - { from: 'file.txt', to: 'fileRenamed.txt', overwrite: true }, { from: 'file1.txt', to: 'file1Renamed.txt' }, + { from: 'file2.txt', to: 'file2Renamed.txt' }, + { from: 'file3.txt', to: 'file3Renamed.txt', overwrite: true }, ], }, },