scaffolder-backend: refactor template action example tests to avoid mock-fs

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-10-05 21:48:12 +02:00
parent aefca1e6d0
commit 82458b16cc
@@ -14,10 +14,8 @@
* limitations under the License.
*/
import os from 'os';
import { join as joinPath, sep as pathSep } from 'path';
import fs from 'fs-extra';
import mockFs from 'mock-fs';
import {
getVoidLogger,
resolvePackagePath,
@@ -33,6 +31,7 @@ import {
} from '@backstage/plugin-scaffolder-node';
import { examples } from './template.examples';
import yaml from 'yaml';
import { createMockDirectory } from '@backstage/backend-test-utils';
jest.mock('@backstage/plugin-scaffolder-node', () => ({
...jest.requireActual('@backstage/plugin-scaffolder-node'),
@@ -45,16 +44,6 @@ type FetchTemplateInput = ReturnType<
? U
: never;
const realFiles = Object.fromEntries(
[
resolvePackagePath(
'@backstage/plugin-scaffolder-backend',
'assets',
'nunjucks.js.txt',
),
].map(k => [k, mockFs.load(k)]),
);
const aBinaryFile = fs.readFileSync(
resolvePackagePath(
'@backstage/plugin-scaffolder-backend',
@@ -69,14 +58,8 @@ const mockFetchContents = fetchContents as jest.MockedFunction<
describe('fetch:template examples', () => {
let action: TemplateAction<any>;
const workspacePath = os.tmpdir();
const createTemporaryDirectory: jest.MockedFunction<
ActionContext<FetchTemplateInput>['createTemporaryDirectory']
> = jest.fn(() =>
Promise.resolve(
joinPath(workspacePath, `${createTemporaryDirectory.mock.calls.length}`),
),
);
const mockDir = createMockDirectory();
const workspacePath = mockDir.resolve('workspace');
const logger = getVoidLogger();
@@ -90,24 +73,20 @@ describe('fetch:template examples', () => {
logStream: new PassThrough(),
logger,
workspacePath,
createTemporaryDirectory,
async createTemporaryDirectory() {
return fs.mkdtemp(mockDir.resolve('tmp-'));
},
});
beforeEach(() => {
mockFs({
...realFiles,
});
mockDir.clear();
action = createFetchTemplateAction({
reader: Symbol('UrlReader') as unknown as UrlReader,
integrations: Symbol('Integrations') as unknown as ScmIntegrations,
});
});
afterEach(() => {
mockFs.restore();
});
describe('handler', () => {
describe('with valid input', () => {
let context: ActionContext<FetchTemplateInput>;
@@ -116,13 +95,13 @@ describe('fetch:template examples', () => {
context = mockContext(yaml.parse(examples[0].example).steps[0].input);
mockFetchContents.mockImplementation(({ outputPath }) => {
mockFs({
...realFiles,
mockDir.setContent({
[outputPath]: {
'an-executable.sh': mockFs.file({
content: '#!/usr/bin/env bash',
mode: parseInt('100755', 8),
}),
'an-executable.sh': ctx =>
fs.writeFileSync(ctx.path, '#!/usr/bin/env bash', {
encoding: 'utf8',
mode: parseInt('100755', 8),
}),
'empty-dir-${{ values.count }}': {},
'static.txt': 'static content',
'${{ values.name }}.txt': 'static content',
@@ -132,12 +111,8 @@ describe('fetch:template examples', () => {
},
'.${{ values.name }}': '${{ values.itemList | dump }}',
'a-binary-file.png': aBinaryFile,
symlink: mockFs.symlink({
path: 'a-binary-file.png',
}),
brokenSymlink: mockFs.symlink({
path: './not-a-real-file.txt',
}),
symlink: ctx => ctx.symlink('a-binary-file.png'),
brokenSymlink: ctx => ctx.symlink('./not-a-real-file.txt'),
},
});
@@ -212,7 +187,11 @@ describe('fetch:template examples', () => {
await expect(
fs.realpath(`${workspacePath}/target/symlink`),
).resolves.toBe(joinPath(workspacePath, 'target', 'a-binary-file.png'));
).resolves.toBe(
fs.realpathSync(
joinPath(workspacePath, 'target', 'a-binary-file.png'),
),
);
});
it('copies broken symlinks as-is without processing them', async () => {