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 ,
711 getGitRoot ,
812 guardAgainstLocalChanges ,
913 safeCheckout ,
@@ -12,11 +16,14 @@ import {
1216
1317describe ( '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 ( / t m p \/ g i t - t e s t s $ / ) ;
35+ await expect ( getGitRoot ( emptyGit ) ) . resolves . toMatch (
36+ / t m p \/ g i t - t e s t s \/ r e p o $ / ,
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} ) ;
0 commit comments