scaffolder-backend: refactor github action tests to avoid mock-fs
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
+28
-28
@@ -24,21 +24,17 @@ import {
|
||||
ActionContext,
|
||||
TemplateAction,
|
||||
} from '@backstage/plugin-scaffolder-node';
|
||||
import mockFs from 'mock-fs';
|
||||
import os from 'os';
|
||||
import { resolve as resolvePath } from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import { Writable } from 'stream';
|
||||
import {
|
||||
createPublishGithubPullRequestAction,
|
||||
OctokitWithPullRequestPluginClient,
|
||||
} from './githubPullRequest';
|
||||
import { createMockDirectory } from '@backstage/backend-test-utils';
|
||||
|
||||
// Make sure root logger is initialized ahead of FS mock
|
||||
createRootLogger();
|
||||
|
||||
const root = os.platform() === 'win32' ? 'C:\\root' : '/root';
|
||||
const workspacePath = resolvePath(root, 'my-workspace');
|
||||
|
||||
type GithubPullRequestActionInput = ReturnType<
|
||||
typeof createPublishGithubPullRequestAction
|
||||
> extends TemplateAction<infer U>
|
||||
@@ -54,7 +50,12 @@ describe('createPublishGithubPullRequestAction', () => {
|
||||
};
|
||||
};
|
||||
|
||||
const mockDir = createMockDirectory();
|
||||
const workspacePath = mockDir.resolve('workspace');
|
||||
|
||||
beforeEach(() => {
|
||||
mockDir.clear();
|
||||
|
||||
const integrations = ScmIntegrations.fromConfig(new ConfigReader({}));
|
||||
fakeClient = {
|
||||
createPullRequest: jest.fn(async (_: any) => {
|
||||
@@ -92,7 +93,6 @@ describe('createPublishGithubPullRequestAction', () => {
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mockFs.restore();
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
@@ -132,7 +132,7 @@ describe('createPublishGithubPullRequestAction', () => {
|
||||
draft: true,
|
||||
};
|
||||
|
||||
mockFs({
|
||||
mockDir.setContent({
|
||||
[workspacePath]: { 'file.txt': 'Hello there!' },
|
||||
});
|
||||
|
||||
@@ -197,7 +197,7 @@ describe('createPublishGithubPullRequestAction', () => {
|
||||
draft: true,
|
||||
};
|
||||
|
||||
mockFs({
|
||||
mockDir.setContent({
|
||||
[workspacePath]: { 'file.txt': 'Hello there!' },
|
||||
});
|
||||
|
||||
@@ -261,7 +261,7 @@ describe('createPublishGithubPullRequestAction', () => {
|
||||
sourcePath: 'source',
|
||||
};
|
||||
|
||||
mockFs({
|
||||
mockDir.setContent({
|
||||
[workspacePath]: {
|
||||
source: { 'foo.txt': 'Hello there!' },
|
||||
irrelevant: { 'bar.txt': 'Nothing to see here' },
|
||||
@@ -323,7 +323,7 @@ describe('createPublishGithubPullRequestAction', () => {
|
||||
description: 'This PR is really good',
|
||||
};
|
||||
|
||||
mockFs({
|
||||
mockDir.setContent({
|
||||
[workspacePath]: { 'file.txt': 'Hello there!' },
|
||||
});
|
||||
|
||||
@@ -385,7 +385,7 @@ describe('createPublishGithubPullRequestAction', () => {
|
||||
teamReviewers: ['team-foo'],
|
||||
};
|
||||
|
||||
mockFs({ [workspacePath]: {} });
|
||||
mockDir.setContent({ [workspacePath]: {} });
|
||||
|
||||
ctx = {
|
||||
createTemporaryDirectory: jest.fn(),
|
||||
@@ -437,7 +437,7 @@ describe('createPublishGithubPullRequestAction', () => {
|
||||
description: 'This PR is really good',
|
||||
};
|
||||
|
||||
mockFs({ [workspacePath]: {} });
|
||||
mockDir.setContent({ [workspacePath]: {} });
|
||||
|
||||
ctx = {
|
||||
createTemporaryDirectory: jest.fn(),
|
||||
@@ -469,11 +469,9 @@ describe('createPublishGithubPullRequestAction', () => {
|
||||
description: 'This PR is really good',
|
||||
};
|
||||
|
||||
mockFs({
|
||||
mockDir.setContent({
|
||||
[workspacePath]: {
|
||||
Makefile: mockFs.symlink({
|
||||
path: '../../nothing/yet',
|
||||
}),
|
||||
Makefile: c => c.symlink('../../nothing/yet'),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -523,12 +521,13 @@ describe('createPublishGithubPullRequestAction', () => {
|
||||
description: 'This PR is really good',
|
||||
};
|
||||
|
||||
mockFs({
|
||||
mockDir.setContent({
|
||||
[workspacePath]: {
|
||||
'hello.sh': mockFs.file({
|
||||
content: 'echo Hello there!',
|
||||
mode: 0o100755,
|
||||
}),
|
||||
'hello.sh': c =>
|
||||
fs.writeFileSync(c.path, 'echo Hello there!', {
|
||||
encoding: 'utf8',
|
||||
mode: 0o100755,
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -588,12 +587,13 @@ describe('createPublishGithubPullRequestAction', () => {
|
||||
description: 'This PR is really good',
|
||||
};
|
||||
|
||||
mockFs({
|
||||
mockDir.setContent({
|
||||
[workspacePath]: {
|
||||
'hello.sh': mockFs.file({
|
||||
content: 'echo Hello there!',
|
||||
mode: 0o100775,
|
||||
}),
|
||||
'hello.sh': c =>
|
||||
fs.writeFileSync(c.path, 'echo Hello there!', {
|
||||
encoding: 'utf8',
|
||||
mode: 0o100775,
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -654,7 +654,7 @@ describe('createPublishGithubPullRequestAction', () => {
|
||||
commitMessage: 'Create my new app, but in the commit message',
|
||||
};
|
||||
|
||||
mockFs({
|
||||
mockDir.setContent({
|
||||
[workspacePath]: { 'file.txt': 'Hello there!' },
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user