Skip to content

Commit f912a1f

Browse files
committed
fix(utils): pass revparse args as array
1 parent a945296 commit f912a1f

File tree

3 files changed

+65
-17
lines changed

3 files changed

+65
-17
lines changed

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { mkdir, rm, stat, writeFile } from 'node:fs/promises';
1+
import { rm, stat, writeFile } from 'node:fs/promises';
22
import path from 'node:path';
33
import { type SimpleGit, simpleGit } from 'simple-git';
4-
import { initGitRepo, teardownTestFolder } from '@code-pushup/test-utils';
4+
import {
5+
initGitRepoWithRemote,
6+
teardownTestFolder,
7+
} from '@code-pushup/test-utils';
58
import { toUnixPath } from '../transform.js';
69
import {
10+
getGitDefaultBranch,
711
getGitRoot,
812
guardAgainstLocalChanges,
913
safeCheckout,
@@ -12,11 +16,14 @@ import {
1216

1317
describe('git utils in a git repo', () => {
1418
const baseDir = path.join(process.cwd(), 'tmp', 'git-tests');
19+
const repoDir = path.join(baseDir, 'repo');
1520
let emptyGit: SimpleGit;
1621

1722
beforeAll(async () => {
18-
await mkdir(baseDir, { recursive: true });
19-
emptyGit = await initGitRepo(simpleGit, { baseDir, baseBranch: 'master' });
23+
emptyGit = await initGitRepoWithRemote(simpleGit, {
24+
baseDir,
25+
baseBranch: 'master',
26+
});
2027
});
2128

2229
afterAll(async () => {
@@ -25,13 +32,15 @@ describe('git utils in a git repo', () => {
2532

2633
describe('without a branch and commits', () => {
2734
it('getGitRoot should return git root in a set up repo', async () => {
28-
await expect(getGitRoot(emptyGit)).resolves.toMatch(/tmp\/git-tests$/);
35+
await expect(getGitRoot(emptyGit)).resolves.toMatch(
36+
/tmp\/git-tests\/repo$/,
37+
);
2938
});
3039
});
3140

3241
describe('with a branch and commits clean', () => {
3342
beforeAll(async () => {
34-
await writeFile(path.join(baseDir, 'README.md'), '# hello-world\n');
43+
await writeFile(path.join(repoDir, 'README.md'), '# hello-world\n');
3544
await emptyGit.add('README.md');
3645
await emptyGit.commit('Create README');
3746

@@ -45,24 +54,24 @@ describe('git utils in a git repo', () => {
4554
});
4655

4756
it('should find Git root', async () => {
48-
await expect(getGitRoot(emptyGit)).resolves.toBe(toUnixPath(baseDir));
57+
await expect(getGitRoot(emptyGit)).resolves.toBe(toUnixPath(repoDir));
4958
});
5059

5160
it('should convert absolute path to relative Git path', async () => {
5261
await expect(
53-
toGitPath(path.join(baseDir, 'src', 'utils.ts'), emptyGit),
62+
toGitPath(path.join(repoDir, 'src', 'utils.ts'), emptyGit),
5463
).resolves.toBe('src/utils.ts');
5564
});
5665

5766
it('should convert relative Windows path to relative Git path', async () => {
5867
await expect(
5968
toGitPath(String.raw`Backend\API\Startup.cs`, emptyGit),
60-
).resolves.toBe('../../Backend/API/Startup.cs');
69+
).resolves.toBe('../../../Backend/API/Startup.cs');
6170
});
6271

6372
it('should keep relative Unix path as is (already a Git path)', async () => {
6473
await expect(toGitPath('Backend/API/Startup.cs', emptyGit)).resolves.toBe(
65-
'../../Backend/API/Startup.cs',
74+
'../../../Backend/API/Startup.cs',
6675
);
6776
});
6877

@@ -89,10 +98,10 @@ describe('git utils in a git repo', () => {
8998
});
9099

91100
describe('with a branch and commits dirty', () => {
92-
const newFilePath = path.join(baseDir, 'new-file.md');
101+
const newFilePath = path.join(repoDir, 'new-file.md');
93102

94103
beforeAll(async () => {
95-
await writeFile(path.join(baseDir, 'README.md'), '# hello-world\n');
104+
await writeFile(path.join(repoDir, 'README.md'), '# hello-world\n');
96105
await emptyGit.add('README.md');
97106
await emptyGit.commit('Create README');
98107

@@ -179,4 +188,10 @@ describe('git utils in a git repo', () => {
179188
);
180189
});
181190
});
191+
192+
describe('getGitDefaultBranch', () => {
193+
it('should resolve the default branch name from origin/HEAD', async () => {
194+
await expect(getGitDefaultBranch(emptyGit)).resolves.toBe('master');
195+
});
196+
});
182197
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function getGitRoot(git = simpleGit()): Promise<string> {
1010

1111
export async function getGitDefaultBranch(git = simpleGit()): Promise<string> {
1212
try {
13-
const head = await git.revparse('--abbrev-ref origin/HEAD');
13+
const head = await git.revparse(['--abbrev-ref', 'origin/HEAD']);
1414
return head.replace(/^origin\//, '');
1515
} catch (error) {
1616
logger.warn(

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,50 @@ export async function initGitRepo(
1818
baseBranch?: string;
1919
},
2020
): Promise<SimpleGit> {
21-
const { baseDir, config, baseBranch } = opt;
22-
const { email = 'john.doe@example.com', name = 'John Doe' } = config ?? {};
21+
const { baseDir, baseBranch } = opt;
2322
await mkdir(baseDir, { recursive: true });
2423
const git = simpleGit(baseDir);
2524
await git.init();
25+
await git.branch(['-M', baseBranch ?? 'main']);
26+
await configureGitUser(git, opt.config);
27+
return git;
28+
}
29+
30+
/** Like {@link initGitRepo}, but with a simulated remote origin. Working directory is `<baseDir>/repo`. */
31+
export async function initGitRepoWithRemote(
32+
simpleGit: SimpleGitFactory,
33+
opt: {
34+
baseDir: string;
35+
config?: GitConfig;
36+
baseBranch?: string;
37+
},
38+
): Promise<SimpleGit> {
39+
const { baseDir, baseBranch = 'main' } = opt;
40+
const originDir = path.join(baseDir, 'origin.git');
41+
const repoDir = path.join(baseDir, 'repo');
42+
43+
await mkdir(originDir, { recursive: true });
44+
await simpleGit(originDir).init(true, ['--initial-branch', baseBranch]);
45+
await simpleGit(baseDir).clone(originDir, repoDir);
46+
47+
const git = simpleGit(repoDir);
48+
await configureGitUser(git, opt.config);
49+
await commitFile(git, { baseDir: repoDir });
50+
await git.push('origin', baseBranch);
51+
await git.remote(['set-head', 'origin', baseBranch]);
52+
53+
return git;
54+
}
55+
56+
async function configureGitUser(
57+
git: SimpleGit,
58+
config?: GitConfig,
59+
): Promise<void> {
60+
const { email = 'john.doe@example.com', name = 'John Doe' } = config ?? {};
2661
await git.addConfig('user.name', name);
2762
await git.addConfig('user.email', email);
2863
await git.addConfig('commit.gpgSign', 'false');
2964
await git.addConfig('tag.gpgSign', 'false');
30-
await git.branch(['-M', baseBranch ?? 'main']);
31-
return git;
3265
}
3366

3467
export async function commitFile(

0 commit comments

Comments
 (0)