|
1 | 1 | import os from 'node:os' |
2 | 2 | import fs from 'fs-extra' |
3 | 3 | import path from 'pathe' |
4 | | -import { afterEach, beforeEach, describe, expect, it } from 'vitest' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
5 | 5 | import { CacheStore } from '@/cache/store' |
| 6 | +import logger from '@/logger' |
6 | 7 |
|
7 | 8 | let tempDir: string |
8 | 9 |
|
@@ -74,4 +75,62 @@ describe('CacheStore', () => { |
74 | 75 | expect(restored.size).toBe(1) |
75 | 76 | expect(restored.has('foo')).toBe(true) |
76 | 77 | }) |
| 78 | + |
| 79 | + it('ignores ENOENT errors while reading cache files', async () => { |
| 80 | + const cachePath = path.join(tempDir, 'cache.json') |
| 81 | + const store = new CacheStore({ |
| 82 | + enabled: true, |
| 83 | + cwd: tempDir, |
| 84 | + dir: tempDir, |
| 85 | + file: 'cache.json', |
| 86 | + path: cachePath, |
| 87 | + strategy: 'merge', |
| 88 | + }) |
| 89 | + |
| 90 | + const warnSpy = vi.spyOn(logger, 'warn') |
| 91 | + const removeSpy = vi.spyOn(fs, 'remove') |
| 92 | + const pathExistsSpy = vi.spyOn(fs, 'pathExists').mockResolvedValue(true) |
| 93 | + const enoentError = Object.assign(new Error('missing'), { code: 'ENOENT' as NodeJS.ErrnoException['code'] }) |
| 94 | + const readSpy = vi.spyOn(fs, 'readJSON').mockRejectedValue(enoentError) |
| 95 | + |
| 96 | + const restored = await store.read() |
| 97 | + expect(restored.size).toBe(0) |
| 98 | + expect(warnSpy).not.toHaveBeenCalled() |
| 99 | + expect(removeSpy).not.toHaveBeenCalled() |
| 100 | + |
| 101 | + warnSpy.mockRestore() |
| 102 | + removeSpy.mockRestore() |
| 103 | + pathExistsSpy.mockRestore() |
| 104 | + readSpy.mockRestore() |
| 105 | + }) |
| 106 | + |
| 107 | + it('ignores ENOENT errors while reading cache files synchronously', () => { |
| 108 | + const cachePath = path.join(tempDir, 'cache.json') |
| 109 | + const store = new CacheStore({ |
| 110 | + enabled: true, |
| 111 | + cwd: tempDir, |
| 112 | + dir: tempDir, |
| 113 | + file: 'cache.json', |
| 114 | + path: cachePath, |
| 115 | + strategy: 'merge', |
| 116 | + }) |
| 117 | + |
| 118 | + const warnSpy = vi.spyOn(logger, 'warn') |
| 119 | + const removeSpy = vi.spyOn(fs, 'removeSync') |
| 120 | + const pathExistsSpy = vi.spyOn(fs, 'pathExistsSync').mockReturnValue(true) |
| 121 | + const enoentError = Object.assign(new Error('missing'), { code: 'ENOENT' as NodeJS.ErrnoException['code'] }) |
| 122 | + const readSpy = vi.spyOn(fs, 'readJSONSync').mockImplementation(() => { |
| 123 | + throw enoentError |
| 124 | + }) |
| 125 | + |
| 126 | + const restored = store.readSync() |
| 127 | + expect(restored.size).toBe(0) |
| 128 | + expect(warnSpy).not.toHaveBeenCalled() |
| 129 | + expect(removeSpy).not.toHaveBeenCalled() |
| 130 | + |
| 131 | + warnSpy.mockRestore() |
| 132 | + removeSpy.mockRestore() |
| 133 | + pathExistsSpy.mockRestore() |
| 134 | + readSpy.mockRestore() |
| 135 | + }) |
77 | 136 | }) |
0 commit comments