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';

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 {

12161317

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;

16211722

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

});

21282229

afterAll(async () => {

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

25322633

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

});

31403241

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

});

46554756

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

});

50595160

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

});

56655766

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

});

62716372

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

});

909991100

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');

9310294103

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

});