|
1 | 1 | import { execSync } from 'node:child_process' |
| 2 | +import { existsSync } from 'node:fs' |
2 | 3 | import { glob } from 'glob' |
3 | 4 | import { afterEach, describe, expect, it, vi } from 'vitest' |
4 | | -import { filterByGlob, getAll, normalizeFilePaths } from './source-files.mjs' |
| 5 | +import { filterByGlob, getAll, normalizeFilePaths, validateFilesExist } from './source-files.mjs' |
5 | 6 |
|
6 | 7 | vi.mock('node:child_process', () => ({ |
7 | 8 | execSync: vi.fn(), |
8 | 9 | })) |
9 | 10 |
|
| 11 | +vi.mock('node:fs', () => ({ |
| 12 | + existsSync: vi.fn(), |
| 13 | +})) |
| 14 | + |
10 | 15 | vi.mock('glob', () => ({ |
11 | 16 | glob: { |
12 | 17 | sync: vi.fn(), |
@@ -152,3 +157,27 @@ describe('filterByGlob', () => { |
152 | 157 | expect(result).toEqual(['src/a.ts', 'src/b.tsx']) |
153 | 158 | }) |
154 | 159 | }) |
| 160 | + |
| 161 | +describe('validateFilesExist', () => { |
| 162 | + it('throws error when file does not exist', () => { |
| 163 | + vi.mocked(existsSync).mockReturnValue(false) |
| 164 | + |
| 165 | + expect(() => validateFilesExist(['nonexistent.tsx'])).toThrow( |
| 166 | + 'File not found: nonexistent.tsx', |
| 167 | + ) |
| 168 | + }) |
| 169 | + |
| 170 | + it('does not throw when all files exist', () => { |
| 171 | + vi.mocked(existsSync).mockReturnValue(true) |
| 172 | + |
| 173 | + expect(() => validateFilesExist(['exists.tsx'])).not.toThrow() |
| 174 | + }) |
| 175 | + |
| 176 | + it('throws on first missing file', () => { |
| 177 | + vi.mocked(existsSync).mockImplementation((path) => path === 'exists.tsx') |
| 178 | + |
| 179 | + expect(() => validateFilesExist(['exists.tsx', 'missing.tsx'])).toThrow( |
| 180 | + 'File not found: missing.tsx', |
| 181 | + ) |
| 182 | + }) |
| 183 | +}) |
0 commit comments