Skip to content

Commit 3978b22

Browse files
committed
fix(gitService): handle case with no tags
- Improved error handling for `getLatestTag` - Added unit tests for `getLatestTag` - Return error if no tags are found - Added `ignoreReturnCode` option - Updated test cases for new error handling
1 parent 18803ea commit 3978b22

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/services/gitService.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,14 @@ export class GitService {
7979
latestTag += data.toString();
8080
},
8181
},
82+
ignoreReturnCode: true,
8283
};
83-
await this._execGitCommand(['describe', '--tags', '--abbrev=0'], options);
84+
const exitCode = await this._execGitCommand(['describe', '--tags', '--abbrev=0'], options);
85+
86+
if (exitCode !== 0) {
87+
throw new Error('No tags found in the repository. Please create a tag first.');
88+
}
89+
8490
return latestTag.trim();
8591
}
8692

tests/services/gitService.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { GitService } from '../../src/services/gitService';
2+
import * as exec from '@actions/exec';
3+
import { vi } from 'vitest';
4+
5+
describe('GitService', () => {
6+
let gitService: GitService;
7+
8+
beforeEach(() => {
9+
gitService = new GitService();
10+
});
11+
12+
describe('getLatestTag', () => {
13+
it('should throw an error if no tags are found', async () => {
14+
vi.spyOn(exec, 'exec').mockResolvedValue(1); // Simulate git command failure
15+
await expect(gitService.getLatestTag()).rejects.toThrow('No tags found in the repository. Please create a tag first.');
16+
});
17+
18+
it('should return the latest tag if tags are found', async () => {
19+
const execSpy = vi.spyOn(exec, 'exec').mockImplementation(async (command, args, options) => {
20+
if (options && options.listeners && options.listeners.stdout) {
21+
options.listeners.stdout(Buffer.from('v1.0.0'));
22+
}
23+
return 0;
24+
});
25+
const latestTag = await gitService.getLatestTag();
26+
expect(latestTag).toBe('v1.0.0');
27+
execSpy.mockRestore();
28+
});
29+
});
30+
});

0 commit comments

Comments
 (0)