scaffolder-backend: refactor debug actions tests to avoid mock-fs

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-10-05 21:45:36 +02:00
parent e68b9bf850
commit ea14da0777
4 changed files with 28 additions and 40 deletions
@@ -15,44 +15,41 @@
*/
import { getVoidLogger } from '@backstage/backend-common';
import mockFs from 'mock-fs';
import os from 'os';
import { Writable } from 'stream';
import { createDebugLogAction } from './log';
import { join } from 'path';
import yaml from 'yaml';
import { examples } from './log.examples';
import { createMockDirectory } from '@backstage/backend-test-utils';
describe('debug:log examples', () => {
const logStream = {
write: jest.fn(),
} as jest.Mocked<Partial<Writable>> as jest.Mocked<Writable>;
const mockTmpDir = os.tmpdir();
const mockDir = createMockDirectory();
const workspacePath = mockDir.resolve('workspace');
const mockContext = {
input: {},
baseUrl: 'somebase',
workspacePath: mockTmpDir,
workspacePath,
logger: getVoidLogger(),
logStream,
output: jest.fn(),
createTemporaryDirectory: jest.fn().mockResolvedValue(mockTmpDir),
createTemporaryDirectory: jest.fn(),
};
const action = createDebugLogAction();
beforeEach(() => {
mockFs({
[`${mockContext.workspacePath}/README.md`]: '',
[`${mockContext.workspacePath}/a-directory/index.md`]: '',
mockDir.setContent({
[`${workspacePath}/README.md`]: '',
[`${workspacePath}/a-directory/index.md`]: '',
});
jest.resetAllMocks();
});
afterEach(() => {
mockFs.restore();
});
it('should log message', async () => {
const context = {
...mockContext,
@@ -15,43 +15,40 @@
*/
import { getVoidLogger } from '@backstage/backend-common';
import mockFs from 'mock-fs';
import os from 'os';
import { Writable } from 'stream';
import { createDebugLogAction } from './log';
import { join } from 'path';
import yaml from 'yaml';
import { createMockDirectory } from '@backstage/backend-test-utils';
describe('debug:log', () => {
const logStream = {
write: jest.fn(),
} as jest.Mocked<Partial<Writable>> as jest.Mocked<Writable>;
const mockTmpDir = os.tmpdir();
const mockDir = createMockDirectory();
const workspacePath = mockDir.resolve('workspace');
const mockContext = {
input: {},
baseUrl: 'somebase',
workspacePath: mockTmpDir,
workspacePath,
logger: getVoidLogger(),
logStream,
output: jest.fn(),
createTemporaryDirectory: jest.fn().mockResolvedValue(mockTmpDir),
createTemporaryDirectory: jest.fn(),
};
const action = createDebugLogAction();
beforeEach(() => {
mockFs({
mockDir.setContent({
[`${mockContext.workspacePath}/README.md`]: '',
[`${mockContext.workspacePath}/a-directory/index.md`]: '',
});
jest.resetAllMocks();
});
afterEach(() => {
mockFs.restore();
});
it('should do nothing', async () => {
await action.handler(mockContext);
@@ -15,12 +15,11 @@
*/
import { getVoidLogger } from '@backstage/backend-common';
import mockFs from 'mock-fs';
import { createWaitAction } from './wait';
import { Writable } from 'stream';
import os from 'os';
import { examples } from './wait.examples';
import yaml from 'yaml';
import { createMockDirectory } from '@backstage/backend-test-utils';
describe('debug:wait examples', () => {
const action = createWaitAction();
@@ -29,25 +28,23 @@ describe('debug:wait examples', () => {
write: jest.fn(),
} as jest.Mocked<Partial<Writable>> as jest.Mocked<Writable>;
const mockTmpDir = os.tmpdir();
const mockDir = createMockDirectory();
const workspacePath = mockDir.resolve('workspace');
const mockContext = {
input: {},
baseUrl: 'somebase',
workspacePath: mockTmpDir,
workspacePath,
logger: getVoidLogger(),
logStream,
output: jest.fn(),
createTemporaryDirectory: jest.fn().mockResolvedValue(mockTmpDir),
createTemporaryDirectory: jest.fn(),
};
beforeEach(() => {
jest.resetAllMocks();
});
afterEach(() => {
mockFs.restore();
});
it('should wait for specified period of seconds', async () => {
const context = {
...mockContext,
@@ -15,10 +15,9 @@
*/
import { getVoidLogger } from '@backstage/backend-common';
import mockFs from 'mock-fs';
import { createWaitAction } from './wait';
import { Writable } from 'stream';
import os from 'os';
import { createMockDirectory } from '@backstage/backend-test-utils';
describe('debug:wait', () => {
const action = createWaitAction();
@@ -27,25 +26,23 @@ describe('debug:wait', () => {
write: jest.fn(),
} as jest.Mocked<Partial<Writable>> as jest.Mocked<Writable>;
const mockTmpDir = os.tmpdir();
const mockDir = createMockDirectory();
const workspacePath = mockDir.resolve('workspace');
const mockContext = {
input: {},
baseUrl: 'somebase',
workspacePath: mockTmpDir,
workspacePath,
logger: getVoidLogger(),
logStream,
output: jest.fn(),
createTemporaryDirectory: jest.fn().mockResolvedValue(mockTmpDir),
createTemporaryDirectory: jest.fn(),
};
beforeEach(() => {
jest.resetAllMocks();
});
afterEach(() => {
mockFs.restore();
});
it('should wait for specified period of time', async () => {
const context = {
...mockContext,