From 50c55ffd8af2497c5b54fea69282cc1f831083a6 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 28 Dec 2023 15:55:53 +0100 Subject: [PATCH] scaffolder-backend-module-confluence-to-markdown: refactor to remove mock-fs Signed-off-by: Patrik Oldsberg --- .../package.json | 1 - .../confluenceToMarkdown.examples.test.ts | 40 ++++++-------- .../confluence/confluenceToMarkdown.test.ts | 53 ++++++++----------- yarn.lock | 1 - 4 files changed, 39 insertions(+), 56 deletions(-) diff --git a/plugins/scaffolder-backend-module-confluence-to-markdown/package.json b/plugins/scaffolder-backend-module-confluence-to-markdown/package.json index cdd39adf52..942bbb1593 100644 --- a/plugins/scaffolder-backend-module-confluence-to-markdown/package.json +++ b/plugins/scaffolder-backend-module-confluence-to-markdown/package.json @@ -44,7 +44,6 @@ "devDependencies": { "@backstage/backend-test-utils": "workspace:^", "@backstage/cli": "workspace:^", - "mock-fs": "^5.2.0", "msw": "^1.0.0" }, "files": [ diff --git a/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/confluenceToMarkdown.examples.test.ts b/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/confluenceToMarkdown.examples.test.ts index e5aeadbf00..f3bf1fef35 100644 --- a/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/confluenceToMarkdown.examples.test.ts +++ b/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/confluenceToMarkdown.examples.test.ts @@ -19,28 +19,16 @@ import { getVoidLogger } from '@backstage/backend-common'; import { UrlReader } from '@backstage/backend-common'; import { ConfigReader } from '@backstage/config'; import { ScmIntegrations } from '@backstage/integration'; -import { setupRequestMockHandlers } from '@backstage/backend-test-utils'; -import mockFs from 'mock-fs'; -import os from 'os'; -import { readFile, writeFile, createWriteStream } from 'fs-extra'; +import { + createMockDirectory, + setupRequestMockHandlers, +} from '@backstage/backend-test-utils'; import { rest } from 'msw'; import { setupServer } from 'msw/node'; import { examples } from './confluenceToMarkdown.examples'; import yaml from 'yaml'; import { ActionContext } from '@backstage/plugin-scaffolder-node'; -jest.mock('fs-extra', () => ({ - mkdirSync: jest.fn(), - readFile: jest.fn().mockResolvedValue('File contents'), - writeFile: jest.fn().mockImplementation(() => { - return Promise.resolve(); - }), - outputFile: jest.fn(), - openSync: jest.fn(), - createWriteStream: jest.fn().mockReturnValue(new PassThrough()), - ensureDir: jest.fn(), -})); - describe('confluence:transform:markdown examples', () => { const baseUrl = `https://confluence.example.com`; const worker = setupServer(); @@ -72,7 +60,8 @@ describe('confluence:transform:markdown examples', () => { const logger = getVoidLogger(); jest.spyOn(logger, 'info'); - const mockTmpDir = os.tmpdir(); + const mockDir = createMockDirectory(); + const workspacePath = mockDir.resolve('workspace'); beforeEach(() => { reader = { @@ -84,17 +73,18 @@ describe('confluence:transform:markdown examples', () => { }; mockContext = { input: yaml.parse(examples[0].example).steps[0].input, - workspacePath: '/tmp', + workspacePath, logger, logStream: new PassThrough(), output: jest.fn(), - createTemporaryDirectory: jest.fn().mockResolvedValue(mockTmpDir), + createTemporaryDirectory: jest.fn(), }; - mockFs({ [`${mockTmpDir}/src/docs`]: {} }); + + mockDir.setContent({ 'workspace/mkdocs.yml': 'File contents' }); }); + afterEach(() => { jest.clearAllMocks(); - mockFs.restore(); }); it('should call confluence to markdown action successfully with results array', async () => { @@ -155,8 +145,10 @@ describe('confluence:transform:markdown examples', () => { `Fetching the mkdocs.yml catalog from https://github.com/organization-name/repo-name/blob/main/mkdocs.yml`, ); expect(logger.info).toHaveBeenCalledTimes(5); - expect(createWriteStream).toHaveBeenCalledTimes(1); - expect(readFile).toHaveBeenCalledTimes(1); - expect(writeFile).toHaveBeenCalledTimes(1); + + expect(mockDir.content({ path: 'workspace/docs' })).toEqual({ + img: { 'testing.pdf': Buffer.from('hello') }, + 'Page-Title.md': 'hello world', + }); }); }); diff --git a/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/confluenceToMarkdown.test.ts b/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/confluenceToMarkdown.test.ts index 4e4f3091f1..5ce8bb2697 100644 --- a/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/confluenceToMarkdown.test.ts +++ b/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/confluenceToMarkdown.test.ts @@ -19,26 +19,14 @@ import { getVoidLogger } from '@backstage/backend-common'; import { UrlReader } from '@backstage/backend-common'; import { ConfigReader } from '@backstage/config'; import { ScmIntegrations } from '@backstage/integration'; -import { setupRequestMockHandlers } from '@backstage/backend-test-utils'; -import mockFs from 'mock-fs'; -import os from 'os'; +import { + createMockDirectory, + setupRequestMockHandlers, +} from '@backstage/backend-test-utils'; import type { ActionContext } from '@backstage/plugin-scaffolder-node'; -import { readFile, writeFile, createWriteStream } from 'fs-extra'; import { rest } from 'msw'; import { setupServer } from 'msw/node'; -jest.mock('fs-extra', () => ({ - mkdirSync: jest.fn(), - readFile: jest.fn().mockResolvedValue('File contents'), - writeFile: jest.fn().mockImplementation(() => { - return Promise.resolve(); - }), - outputFile: jest.fn(), - openSync: jest.fn(), - createWriteStream: jest.fn().mockReturnValue(new PassThrough()), - ensureDir: jest.fn(), -})); - describe('confluence:transform:markdown', () => { const baseUrl = `https://nodomain.confluence.com`; const worker = setupServer(); @@ -70,7 +58,8 @@ describe('confluence:transform:markdown', () => { const logger = getVoidLogger(); jest.spyOn(logger, 'info'); - const mockTmpDir = os.tmpdir(); + const mockDir = createMockDirectory(); + const workspacePath = mockDir.resolve('workspace'); beforeEach(() => { reader = { @@ -85,19 +74,21 @@ describe('confluence:transform:markdown', () => { confluenceUrls: [ 'https://nodomain.confluence.com/display/testing/mkdocs', ], - repoUrl: 'https://notreal.github.com/space/backstage/mkdocs.yml', + repoUrl: + 'https://notreal.github.com/space/backstage/blob/main/mkdocs.yml', }, - workspacePath: '/tmp', + workspacePath, logger, logStream: new PassThrough(), output: jest.fn(), - createTemporaryDirectory: jest.fn().mockResolvedValue(mockTmpDir), + createTemporaryDirectory: jest.fn(), }; - mockFs({ [`${mockTmpDir}/src/docs`]: {} }); + + mockDir.setContent({ 'workspace/mkdocs.yml': 'File contents' }); }); + afterEach(() => { jest.clearAllMocks(); - mockFs.restore(); }); it('should call confluence to markdown action successfully with results array', async () => { @@ -155,12 +146,14 @@ describe('confluence:transform:markdown', () => { await action.handler(mockContext); expect(logger.info).toHaveBeenCalledWith( - `Fetching the mkdocs.yml catalog from https://notreal.github.com/space/backstage/mkdocs.yml`, + `Fetching the mkdocs.yml catalog from https://notreal.github.com/space/backstage/blob/main/mkdocs.yml`, ); expect(logger.info).toHaveBeenCalledTimes(5); - expect(createWriteStream).toHaveBeenCalledTimes(1); - expect(readFile).toHaveBeenCalledTimes(1); - expect(writeFile).toHaveBeenCalledTimes(1); + + expect(mockDir.content({ path: 'workspace/docs' })).toEqual({ + img: { 'testing.pdf': Buffer.from('hello') }, + 'mkdocs.md': 'hello world', + }); }); it('should call confluence to markdown action successfully with empty results array', async () => { @@ -202,13 +195,13 @@ describe('confluence:transform:markdown', () => { await action.handler(mockContext); expect(logger.info).toHaveBeenCalledWith( - `Fetching the mkdocs.yml catalog from https://notreal.github.com/space/backstage/mkdocs.yml`, + `Fetching the mkdocs.yml catalog from https://notreal.github.com/space/backstage/blob/main/mkdocs.yml`, ); expect(logger.info).toHaveBeenCalledTimes(5); - expect(createWriteStream).not.toHaveBeenCalled(); - expect(readFile).toHaveBeenCalledTimes(1); - expect(writeFile).toHaveBeenCalledTimes(1); + expect(mockDir.content({ path: 'workspace/docs' })).toEqual({ + 'mkdocs.md': 'hello world', + }); }); it('should fail on the first fetch call with response.ok set to false', async () => { diff --git a/yarn.lock b/yarn.lock index 9e17153fd0..038822252c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8170,7 +8170,6 @@ __metadata: "@backstage/plugin-scaffolder-node": "workspace:^" fs-extra: 10.1.0 git-url-parse: ^13.1.0 - mock-fs: ^5.2.0 msw: ^1.0.0 node-fetch: ^2.6.7 node-html-markdown: ^1.3.0