scaffolder-backend-module-confluence-to-markdown: refactor to remove mock-fs

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-12-28 15:55:53 +01:00
parent e02efdf8e6
commit 50c55ffd8a
4 changed files with 39 additions and 56 deletions
@@ -44,7 +44,6 @@
"devDependencies": {
"@backstage/backend-test-utils": "workspace:^",
"@backstage/cli": "workspace:^",
"mock-fs": "^5.2.0",
"msw": "^1.0.0"
},
"files": [
@@ -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',
});
});
});
@@ -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 () => {
-1
View File
@@ -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