|
1 | 1 | import { expect, test, vi, describe, beforeEach, afterEach } from 'vitest'; |
2 | | -import { compileGenericGitHostConfig_file } from './repoCompileUtils'; |
| 2 | +import { compileGenericGitHostConfig_file, compileGenericGitHostConfig_url } from './repoCompileUtils'; |
3 | 3 |
|
4 | 4 | // Mock the git module |
5 | 5 | vi.mock('./git.js', () => ({ |
6 | 6 | isPathAValidGitRepoRoot: vi.fn(), |
7 | 7 | getOriginUrl: vi.fn(), |
| 8 | + isUrlAValidGitRepo: vi.fn(), |
8 | 9 | })); |
9 | 10 |
|
10 | 11 | // Mock the glob module |
11 | 12 | vi.mock('glob', () => ({ |
12 | 13 | glob: vi.fn(), |
13 | 14 | })); |
14 | 15 |
|
15 | | -import { isPathAValidGitRepoRoot, getOriginUrl } from './git.js'; |
| 16 | +import { isPathAValidGitRepoRoot, getOriginUrl, isUrlAValidGitRepo } from './git.js'; |
16 | 17 | import { glob } from 'glob'; |
17 | 18 |
|
18 | 19 | const mockedGlob = vi.mocked(glob); |
19 | 20 | const mockedIsPathAValidGitRepoRoot = vi.mocked(isPathAValidGitRepoRoot); |
20 | 21 | const mockedGetOriginUrl = vi.mocked(getOriginUrl); |
| 22 | +const mockedIsUrlAValidGitRepo = vi.mocked(isUrlAValidGitRepo); |
21 | 23 |
|
22 | 24 | describe('compileGenericGitHostConfig_file', () => { |
23 | 25 | beforeEach(() => { |
@@ -122,3 +124,71 @@ describe('compileGenericGitHostConfig_file', () => { |
122 | 124 | expect(result.warnings[0]).toContain('not a git repository'); |
123 | 125 | }); |
124 | 126 | }); |
| 127 | + |
| 128 | +describe('compileGenericGitHostConfig_url', () => { |
| 129 | + beforeEach(() => { |
| 130 | + vi.clearAllMocks(); |
| 131 | + }); |
| 132 | + |
| 133 | + afterEach(() => { |
| 134 | + vi.resetAllMocks(); |
| 135 | + }); |
| 136 | + |
| 137 | + test('should return warning when url is not a valid git repo', async () => { |
| 138 | + mockedIsUrlAValidGitRepo.mockResolvedValue(false); |
| 139 | + |
| 140 | + const config = { |
| 141 | + type: 'git' as const, |
| 142 | + url: 'https://example.com/not-a-repo', |
| 143 | + }; |
| 144 | + |
| 145 | + const result = await compileGenericGitHostConfig_url(config, 1); |
| 146 | + |
| 147 | + expect(result.repoData).toHaveLength(0); |
| 148 | + expect(result.warnings).toHaveLength(1); |
| 149 | + expect(result.warnings[0]).toContain('not a git repository'); |
| 150 | + }); |
| 151 | + |
| 152 | + test('should successfully compile with gitConfig when valid git repo url is found', async () => { |
| 153 | + mockedIsUrlAValidGitRepo.mockResolvedValue(true); |
| 154 | + |
| 155 | + const config = { |
| 156 | + type: 'git' as const, |
| 157 | + url: 'https://git.kernel.org/pub/scm/bluetooth/bluez.git', |
| 158 | + }; |
| 159 | + |
| 160 | + const result = await compileGenericGitHostConfig_url(config, 1); |
| 161 | + |
| 162 | + expect(result.repoData).toHaveLength(1); |
| 163 | + expect(result.warnings).toHaveLength(0); |
| 164 | + expect(result.repoData[0].cloneUrl).toBe('https://git.kernel.org/pub/scm/bluetooth/bluez.git'); |
| 165 | + expect(result.repoData[0].name).toBe('git.kernel.org/pub/scm/bluetooth/bluez'); |
| 166 | + |
| 167 | + // Verify gitConfig is set properly (this is the key fix for SOU-218) |
| 168 | + const metadata = result.repoData[0].metadata as { gitConfig?: Record<string, string> }; |
| 169 | + expect(metadata.gitConfig).toBeDefined(); |
| 170 | + expect(metadata.gitConfig!['zoekt.name']).toBe('git.kernel.org/pub/scm/bluetooth/bluez'); |
| 171 | + expect(metadata.gitConfig!['zoekt.web-url']).toBe('https://git.kernel.org/pub/scm/bluetooth/bluez.git'); |
| 172 | + expect(metadata.gitConfig!['zoekt.display-name']).toBe('git.kernel.org/pub/scm/bluetooth/bluez'); |
| 173 | + expect(metadata.gitConfig!['zoekt.archived']).toBe('0'); |
| 174 | + expect(metadata.gitConfig!['zoekt.fork']).toBe('0'); |
| 175 | + expect(metadata.gitConfig!['zoekt.public']).toBe('1'); |
| 176 | + }); |
| 177 | + |
| 178 | + test('should handle url with trailing .git correctly', async () => { |
| 179 | + mockedIsUrlAValidGitRepo.mockResolvedValue(true); |
| 180 | + |
| 181 | + const config = { |
| 182 | + type: 'git' as const, |
| 183 | + url: 'https://github.com/test/repo.git', |
| 184 | + }; |
| 185 | + |
| 186 | + const result = await compileGenericGitHostConfig_url(config, 1); |
| 187 | + |
| 188 | + expect(result.repoData).toHaveLength(1); |
| 189 | + expect(result.repoData[0].name).toBe('github.com/test/repo'); |
| 190 | + |
| 191 | + const metadata = result.repoData[0].metadata as { gitConfig?: Record<string, string> }; |
| 192 | + expect(metadata.gitConfig!['zoekt.name']).toBe('github.com/test/repo'); |
| 193 | + }); |
| 194 | +}); |
0 commit comments