|
| 1 | +import { mkdir, rm, writeFile } from 'node:fs/promises'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import { type SimpleGit, simpleGit } from 'simple-git'; |
1 | 4 | import { expect } from 'vitest';
|
2 |
| -import { getLatestCommit } from './git'; |
| 5 | +import { getGitRoot, getLatestCommit, toGitPath } from './git'; |
| 6 | +import { toUnixPath } from './transform'; |
3 | 7 |
|
4 |
| -const gitCommitDateRegex = |
5 |
| - /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2} \d{2}:\d{2}:\d{2} \d{4} [+|-]\d{4}$/; |
| 8 | +describe('git utils', () => { |
| 9 | + const baseDir = join(process.cwd(), 'tmp', 'testing-git-repo'); |
| 10 | + let git: SimpleGit; |
| 11 | + |
| 12 | + beforeAll(async () => { |
| 13 | + await mkdir(baseDir, { recursive: true }); |
| 14 | + await writeFile(join(baseDir, 'README.md'), '# hello-world\n'); |
| 15 | + |
| 16 | + git = simpleGit(baseDir); |
| 17 | + await git.init(); |
| 18 | + |
| 19 | + await git.addConfig('user.name', 'John Doe'); |
| 20 | + await git.addConfig('user.email', '[email protected]'); |
| 21 | + |
| 22 | + await git.add('README.md'); |
| 23 | + await git.commit('Create README'); |
| 24 | + }); |
| 25 | + |
| 26 | + afterAll(async () => { |
| 27 | + await rm(baseDir, { recursive: true, force: true }); |
| 28 | + }); |
6 | 29 |
|
7 |
| -describe('getLatestCommit', () => { |
8 | 30 | it('should log latest commit', async () => {
|
9 |
| - await expect(getLatestCommit()).resolves.toEqual( |
10 |
| - expect.objectContaining({ |
11 |
| - hash: expect.stringMatching(/^[\da-f]{40}$/), |
12 |
| - message: expect.stringMatching(/.+/), |
13 |
| - author: expect.stringMatching(/.+/), |
14 |
| - date: expect.stringMatching(gitCommitDateRegex), |
15 |
| - }), |
| 31 | + const gitCommitDateRegex = |
| 32 | + /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2} \d{2}:\d{2}:\d{2} \d{4} [+|-]\d{4}$/; |
| 33 | + |
| 34 | + await expect(getLatestCommit(git)).resolves.toEqual({ |
| 35 | + hash: expect.stringMatching(/^[\da-f]{40}$/), |
| 36 | + message: 'Create README', |
| 37 | + author: 'John Doe', |
| 38 | + date: expect.stringMatching(gitCommitDateRegex), |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should find Git root', async () => { |
| 43 | + await expect(getGitRoot(git)).resolves.toBe(toUnixPath(baseDir)); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should convert absolute path to relative Git path', async () => { |
| 47 | + await expect( |
| 48 | + toGitPath(join(process.cwd(), 'src', 'utils.ts')), |
| 49 | + ).resolves.toBe('src/utils.ts'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should convert relative Windows path to relative Git path', async () => { |
| 53 | + await expect(toGitPath('Backend\\API\\Startup.cs')).resolves.toBe( |
| 54 | + 'Backend/API/Startup.cs', |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should keep relative Unix path as is (already a Git path)', async () => { |
| 59 | + await expect(toGitPath('Backend/API/Startup.cs')).resolves.toBe( |
| 60 | + 'Backend/API/Startup.cs', |
16 | 61 | );
|
17 | 62 | });
|
18 | 63 | });
|
0 commit comments