fix(utils): pass revparse args as array · code-pushup/cli@f912a1f
1-import { mkdir, rm, stat, writeFile } from 'node:fs/promises';
1+import { rm, stat, writeFile } from 'node:fs/promises';
22import path from 'node:path';
33import { 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';
58import { toUnixPath } from '../transform.js';
69import {
10+getGitDefaultBranch,
711getGitRoot,
812guardAgainstLocalChanges,
913safeCheckout,
@@ -12,11 +16,14 @@ import {
12161317describe('git utils in a git repo', () => {
1418const baseDir = path.join(process.cwd(), 'tmp', 'git-tests');
19+const repoDir = path.join(baseDir, 'repo');
1520let emptyGit: SimpleGit;
16211722beforeAll(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});
21282229afterAll(async () => {
@@ -25,13 +32,15 @@ describe('git utils in a git repo', () => {
25322633describe('without a branch and commits', () => {
2734it('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});
31403241describe('with a branch and commits clean', () => {
3342beforeAll(async () => {
34-await writeFile(path.join(baseDir, 'README.md'), '# hello-world\n');
43+await writeFile(path.join(repoDir, 'README.md'), '# hello-world\n');
3544await emptyGit.add('README.md');
3645await emptyGit.commit('Create README');
3746@@ -45,24 +54,24 @@ describe('git utils in a git repo', () => {
4554});
46554756it('should find Git root', async () => {
48-await expect(getGitRoot(emptyGit)).resolves.toBe(toUnixPath(baseDir));
57+await expect(getGitRoot(emptyGit)).resolves.toBe(toUnixPath(repoDir));
4958});
50595160it('should convert absolute path to relative Git path', async () => {
5261await 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});
56655766it('should convert relative Windows path to relative Git path', async () => {
5867await expect(
5968toGitPath(String.raw`Backend\API\Startup.cs`, emptyGit),
60-).resolves.toBe('../../Backend/API/Startup.cs');
69+).resolves.toBe('../../../Backend/API/Startup.cs');
6170});
62716372it('should keep relative Unix path as is (already a Git path)', async () => {
6473await 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});
909991100describe('with a branch and commits dirty', () => {
92-const newFilePath = path.join(baseDir, 'new-file.md');
101+const newFilePath = path.join(repoDir, 'new-file.md');
9310294103beforeAll(async () => {
95-await writeFile(path.join(baseDir, 'README.md'), '# hello-world\n');
104+await writeFile(path.join(repoDir, 'README.md'), '# hello-world\n');
96105await emptyGit.add('README.md');
97106await 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});