Skip to content

Commit 9589010

Browse files
committed
refactor: use git setup helper in tests
1 parent a4cb568 commit 9589010

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

packages/utils/src/lib/git/git.integration.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { mkdir, rm, stat, writeFile } from 'node:fs/promises';
22
import path from 'node:path';
33
import { type SimpleGit, simpleGit } from 'simple-git';
44
import { afterAll, beforeAll, beforeEach, describe, expect } from 'vitest';
5+
import { teardownTestFolder } from '@code-pushup/test-setup';
6+
import { initGitRepo } from '@code-pushup/test-utils';
57
import { toUnixPath } from '../transform.js';
68
import {
79
getGitRoot,
@@ -16,14 +18,11 @@ describe('git utils in a git repo', () => {
1618

1719
beforeAll(async () => {
1820
await mkdir(baseDir, { recursive: true });
19-
emptyGit = simpleGit(baseDir);
20-
await emptyGit.init();
21-
await emptyGit.addConfig('user.name', 'John Doe');
22-
await emptyGit.addConfig('user.email', 'john.doe@example.com');
21+
emptyGit = await initGitRepo(simpleGit, { baseDir, baseBranch: 'master' });
2322
});
2423

2524
afterAll(async () => {
26-
await rm(baseDir, { recursive: true, force: true });
25+
await teardownTestFolder(baseDir);
2726
});
2827

2928
describe('without a branch and commits', () => {

testing/test-utils/src/lib/utils/git.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import type {
99
import { vi } from 'vitest';
1010

1111
export type GitConfig = { name: string; email: string };
12-
1312
export async function initGitRepo(
1413
simpleGit: SimpleGitFactory,
15-
opt: { baseDir: string; config?: GitConfig },
14+
opt: {
15+
baseDir: string;
16+
config?: GitConfig;
17+
baseBranch?: string;
18+
},
1619
): Promise<SimpleGit> {
17-
const { baseDir, config } = opt;
20+
const { baseDir, config, baseBranch } = opt;
1821
const { email = 'john.doe@example.com', name = 'John Doe' } = config ?? {};
1922
await mkdir(baseDir, { recursive: true });
2023
const git = simpleGit(baseDir);
@@ -23,7 +26,7 @@ export async function initGitRepo(
2326
await git.addConfig('user.email', email);
2427
await git.addConfig('commit.gpgSign', 'false');
2528
await git.addConfig('tag.gpgSign', 'false');
26-
await git.branch(['-M', 'main']);
29+
await git.branch(['-M', baseBranch ?? 'main']);
2730
return git;
2831
}
2932

0 commit comments

Comments
 (0)