|
| 1 | +import { existsSync, readFileSync, writeFileSync } from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { expect, test } from 'rstack/test'; |
| 4 | +import { ensureProjectCacheDir, getProjectCacheDir } from '../src/projectCache.ts'; |
| 5 | +import { withTempProject, writeProjectFile } from './fmt/helpers.ts'; |
| 6 | + |
| 7 | +test('creates and repairs an ignored project cache only when requested', async () => { |
| 8 | + await withTempProject(async (rootPath) => { |
| 9 | + const cachePath = getProjectCacheDir(rootPath); |
| 10 | + const ignorePath = path.join(cachePath, '.gitignore'); |
| 11 | + |
| 12 | + expect(cachePath).toBe(path.join(rootPath, '.rstack', 'cache')); |
| 13 | + expect(existsSync(cachePath)).toBe(false); |
| 14 | + |
| 15 | + await expect(ensureProjectCacheDir(rootPath)).resolves.toEqual({ |
| 16 | + status: 'available', |
| 17 | + path: cachePath, |
| 18 | + }); |
| 19 | + expect(readFileSync(ignorePath, 'utf8')).toBe('*\n'); |
| 20 | + |
| 21 | + writeFileSync(ignorePath, 'stale\n'); |
| 22 | + await ensureProjectCacheDir(rootPath); |
| 23 | + expect(readFileSync(ignorePath, 'utf8')).toBe('*\n'); |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +test('reports an unavailable project cache without throwing', async () => { |
| 28 | + await withTempProject(async (rootPath) => { |
| 29 | + writeProjectFile(rootPath, '.rstack', 'not a directory'); |
| 30 | + |
| 31 | + const result = await ensureProjectCacheDir(rootPath); |
| 32 | + |
| 33 | + expect(result).toMatchObject({ |
| 34 | + status: 'unavailable', |
| 35 | + path: getProjectCacheDir(rootPath), |
| 36 | + }); |
| 37 | + }); |
| 38 | +}); |
0 commit comments