From 53d110c055af1cfab2c004a35c1396da92032708 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 5 Oct 2023 21:51:09 +0200 Subject: [PATCH] scaffolder-backend: refactor github action tests to avoid mock-fs Signed-off-by: Patrik Oldsberg --- .../builtin/publish/githubPullRequest.test.ts | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.test.ts index b6987335f9..a1f3712a9d 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.test.ts @@ -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 @@ -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!' }, });