Skip to content

Commit 76ac105

Browse files
🧪 Fix tests for git utilities
Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com>
1 parent 90f1c1f commit 76ac105

2 files changed

Lines changed: 42 additions & 40 deletions

File tree

‎src/utils/git.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ export interface CommitInfo {
1111
origin: string;
1212
}
1313

14+
export function getCurrentCommit() {
15+
const result = spawnSync('git', ['rev-parse', 'HEAD']);
16+
if (result.status !== 0) {
17+
throw new Error('Not a git repository');
18+
}
19+
return result.stdout.toString().trim();
20+
}
21+
1422
function findGitRoot(dir = process.cwd()) {
1523
const gitRoot = fs.readdirSync(dir).find((dir) => dir === '.git');
1624
if (gitRoot) {
@@ -49,11 +57,3 @@ export async function getCommitInfo(): Promise<CommitInfo | undefined> {
4957
return;
5058
}
5159
}
52-
53-
export function getCurrentCommit() {
54-
const result = spawnSync('git', ['rev-parse', 'HEAD']);
55-
if (result.status !== 0) {
56-
throw new Error('Not a git repository');
57-
}
58-
return result.stdout.toString().trim();
59-
}

‎tests/git.test.ts‎

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
1-
import { describe, it, expect, mock, afterEach, beforeEach, spyOn } from 'bun:test';
1+
import { describe, expect, it, mock } from 'bun:test';
22

33
// Define the mock functions so we can manipulate them in tests
44
const spawnSyncMock = mock(() => ({
55
status: 0,
6-
stdout: Buffer.from('mock-hash-123\n')
6+
stdout: Buffer.from('mock-hash-123\n'),
77
}));
88

99
mock.module('child_process', () => ({
10-
spawnSync: spawnSyncMock
10+
spawnSync: spawnSyncMock,
1111
}));
1212

13-
const listRemotesMock = mock(async () => [{ remote: 'origin', url: 'https://github.com/test/repo.git' }]);
14-
const logMock = mock(async () => [{
15-
oid: 'mock-commit-hash',
16-
commit: {
17-
message: 'mock commit message',
18-
author: { name: 'Test Author' },
19-
committer: { name: 'Test Committer', timestamp: 1625097600 }
20-
}
21-
}]);
13+
const listRemotesMock = mock(async () => [
14+
{ remote: 'origin', url: 'https://github.com/test/repo.git' },
15+
]);
16+
const logMock = mock(async () => [
17+
{
18+
oid: 'mock-commit-hash',
19+
commit: {
20+
message: 'mock commit message',
21+
author: { name: 'Test Author' },
22+
committer: { name: 'Test Committer', timestamp: 1625097600 },
23+
},
24+
},
25+
]);
2226

2327
mock.module('isomorphic-git', () => ({
2428
default: {
2529
listRemotes: listRemotesMock,
26-
log: logMock
27-
}
30+
log: logMock,
31+
},
2832
}));
2933

30-
import { getCurrentCommit, getCommitInfo } from '../src/utils/git';
31-
32-
describe('git utils', () => {
33-
afterEach(() => {
34-
spawnSyncMock.mockClear();
35-
listRemotesMock.mockClear();
36-
logMock.mockClear();
37-
});
34+
describe('git utils', async () => {
35+
const { getCommitInfo, getCurrentCommit } = await import('../src/utils/git');
3836

3937
describe('getCurrentCommit', () => {
4038
it('should return the commit hash when git command succeeds', () => {
4139
spawnSyncMock.mockImplementationOnce(() => ({
4240
status: 0,
43-
stdout: Buffer.from('abcdef1234567890\n')
41+
stdout: Buffer.from('abcdef1234567890\n'),
4442
}));
4543

4644
const commit = getCurrentCommit();
@@ -52,7 +50,9 @@ describe('git utils', () => {
5250
spawnSyncMock.mockImplementationOnce(() => ({
5351
status: 128,
5452
stdout: Buffer.from(''),
55-
stderr: Buffer.from('fatal: not a git repository (or any of the parent directories): .git\n')
53+
stderr: Buffer.from(
54+
'fatal: not a git repository (or any of the parent directories): .git\n',
55+
),
5656
}));
5757

5858
expect(() => getCurrentCommit()).toThrow('Not a git repository');
@@ -76,14 +76,16 @@ describe('git utils', () => {
7676
});
7777

7878
it('should handle missing author name by falling back to committer name', async () => {
79-
logMock.mockImplementationOnce(async () => [{
80-
oid: 'mock-commit-hash-2',
81-
commit: {
82-
message: 'another message',
83-
author: { name: '' },
84-
committer: { name: 'Fallback Committer', timestamp: 1625098000 }
85-
}
86-
}]);
79+
logMock.mockImplementationOnce(async () => [
80+
{
81+
oid: 'mock-commit-hash-2',
82+
commit: {
83+
message: 'another message',
84+
author: { name: '' },
85+
committer: { name: 'Fallback Committer', timestamp: 1625098000 },
86+
},
87+
},
88+
]);
8789

8890
const info = await getCommitInfo();
8991
expect(info?.author).toBe('Fallback Committer');

0 commit comments

Comments
 (0)