Add test fixes

Signed-off-by: Lukas Fruntke (external expert on behalf of DB Netz) <lukas.fruntke-extern@deutschebahn.com>
This commit is contained in:
Lukas Fruntke (external expert on behalf of DB Netz)
2023-04-05 12:37:44 +02:00
parent 19eb8e2066
commit 608b44ef49
10 changed files with 131 additions and 15 deletions
@@ -14,10 +14,30 @@
* limitations under the License.
*/
import { TemplateAction } from '@backstage/plugin-scaffolder-node';
const mockGit = {
init: jest.fn(),
add: jest.fn(),
checkout: jest.fn(),
commit: jest
.fn()
.mockResolvedValue('220f19cc36b551763d157f1b5e4a4b446165dbd6'),
fetch: jest.fn(),
addRemote: jest.fn(),
push: jest.fn(),
};
jest.mock('@backstage/backend-common', () => ({
Git: {
fromAuth() {
return mockGit;
},
},
getVoidLogger: jest.requireActual('@backstage/backend-common').getVoidLogger,
}));
jest.mock('../helpers');
import { TemplateAction } from '@backstage/plugin-scaffolder-node';
import { getVoidLogger } from '@backstage/backend-common';
import { ConfigReader } from '@backstage/config';
import {
@@ -77,6 +97,10 @@ describe('github:repo:push', () => {
beforeEach(() => {
jest.resetAllMocks();
// @ts-ignore TS2339 Jest Mock is not detected by Typescript
initRepoAndPush.mockResolvedValue({ commitHash: 'test123' });
githubCredentialsProvider =
DefaultGithubCredentialsProvider.fromIntegrations(integrations);
action = createGithubRepoPushAction({
@@ -307,7 +307,7 @@ export async function initRepoPushAndProtect(
? gitCommitMessage
: config.getOptionalString('scaffolder.defaultCommitMessage');
const { commitHash } = await initRepoAndPush({
const commitResult = await initRepoAndPush({
dir: getRepoSourceDirectory(workspacePath, sourcePath),
remoteUrl,
defaultBranch,
@@ -347,7 +347,7 @@ export async function initRepoPushAndProtect(
}
}
return { commitHash };
return { commitHash: commitResult.commitHash };
}
function extractCollaboratorName(
@@ -23,9 +23,9 @@ jest.mock('@backstage/backend-common', () => ({
init: jest.fn(),
add: jest.fn(),
checkout: jest.fn(),
commit: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
commit: jest
.fn()
.mockResolvedValue('220f19cc36b551763d157f1b5e4a4b446165dbd6'),
fetch: jest.fn(),
addRemote: jest.fn(),
push: jest.fn(),
@@ -43,6 +43,23 @@ describe('initRepoAndPush', () => {
jest.clearAllMocks();
});
describe('resolves the commitHash properly', () => {
it('resolves the correct hash', () => {
const commitHash = initRepoAndPush({
dir: '/test/repo/dir/',
remoteUrl: 'git@github.com:test/repo.git',
auth: {
username: 'test-user',
password: 'test-password',
},
logger: getVoidLogger(),
});
expect(Promise.resolve(commitHash)).toEqual({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
});
});
});
describe('with minimal parameters', () => {
beforeEach(async () => {
await initRepoAndPush({
@@ -170,6 +187,23 @@ describe('commitAndPushRepo', () => {
jest.clearAllMocks();
});
describe('resolves the commitHash properly', () => {
it('resolves to the commitHash', () => {
const commitHash = commitAndPushRepo({
dir: '/test/repo/dir/',
auth: {
username: 'test-user',
password: 'test-password',
},
logger: getVoidLogger(),
commitMessage: 'commit message',
});
expect(Promise.resolve(commitHash)).toStrictEqual({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
});
});
});
describe('with minimal parameters', () => {
beforeEach(async () => {
await commitAndPushRepo({
@@ -19,7 +19,16 @@ jest.mock('azure-devops-node-api', () => ({
getPersonalAccessTokenHandler: jest.fn().mockReturnValue(() => {}),
}));
jest.mock('../helpers');
jest.mock('../helpers', () => {
return {
initRepoAndPush: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
commitAndPushRepo: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
};
});
import { createPublishAzureAction } from './azure';
import { ScmIntegrations } from '@backstage/integration';
@@ -14,7 +14,16 @@
* limitations under the License.
*/
jest.mock('../helpers');
jest.mock('../helpers', () => {
return {
initRepoAndPush: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
commitAndPushRepo: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
};
});
import { createPublishBitbucketAction } from './bitbucket';
import { rest } from 'msw';
@@ -14,7 +14,16 @@
* limitations under the License.
*/
jest.mock('../helpers');
jest.mock('../helpers', () => {
return {
initRepoAndPush: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
commitAndPushRepo: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
};
});
import { createPublishBitbucketCloudAction } from './bitbucketCloud';
import { rest } from 'msw';
@@ -14,7 +14,16 @@
* limitations under the License.
*/
jest.mock('../helpers');
jest.mock('../helpers', () => {
return {
initRepoAndPush: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
commitAndPushRepo: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
};
});
import { createPublishBitbucketServerAction } from './bitbucketServer';
import { rest } from 'msw';
@@ -14,7 +14,16 @@
* limitations under the License.
*/
jest.mock('../helpers');
jest.mock('../helpers', () => {
return {
initRepoAndPush: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
commitAndPushRepo: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
};
});
import path from 'path';
import { createPublishGerritAction } from './gerrit';
@@ -13,11 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { TemplateAction } from '@backstage/plugin-scaffolder-node';
jest.mock('../helpers');
import { TemplateAction } from '@backstage/plugin-scaffolder-node';
import { getVoidLogger } from '@backstage/backend-common';
import { ConfigReader } from '@backstage/config';
import {
@@ -88,6 +86,12 @@ describe('publish:github', () => {
beforeEach(() => {
jest.resetAllMocks();
// @ts-ignore TS2339 Jest Mock is not detected by Typescript
initRepoAndPush.mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
});
githubCredentialsProvider =
DefaultGithubCredentialsProvider.fromIntegrations(integrations);
action = createPublishGithubAction({
@@ -14,7 +14,16 @@
* limitations under the License.
*/
jest.mock('../helpers');
jest.mock('../helpers', () => {
return {
initRepoAndPush: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
commitAndPushRepo: jest.fn().mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
}),
};
});
import { createPublishGitlabAction } from './gitlab';
import { ScmIntegrations } from '@backstage/integration';