|
| 1 | +import { getCacheOptions, mkCacheDirectory, readCache, writeCache } from '../src/cache' |
| 2 | +import path from 'path' |
| 3 | +import { pkgName } from '../src/constants' |
| 4 | +import fs from 'fs' |
| 5 | +describe('cache', () => { |
| 6 | + it('getCacheOptions', () => { |
| 7 | + expect(getCacheOptions).toBeDefined() |
| 8 | + expect(getCacheOptions().dir).toBe(path.resolve(process.cwd(), './node_modules/.cache', pkgName)) |
| 9 | + }) |
| 10 | + |
| 11 | + it('mkCacheDirectory', () => { |
| 12 | + const dir = path.resolve(__dirname, './fixtures', pkgName) |
| 13 | + expect(mkCacheDirectory(dir)).toBe(dir) |
| 14 | + expect(fs.existsSync(dir)).toBe(true) |
| 15 | + |
| 16 | + fs.rmdirSync(dir) |
| 17 | + expect(fs.existsSync(dir)).toBe(false) |
| 18 | + }) |
| 19 | + |
| 20 | + it('write and read cache default option', () => { |
| 21 | + // const opt = getCacheOptions() |
| 22 | + let cache: Set<string> | undefined |
| 23 | + cache = readCache() |
| 24 | + expect(cache).toBe(undefined) |
| 25 | + writeCache(new Set(['a', 'b', 'c'])) |
| 26 | + cache = readCache() |
| 27 | + expect(cache).toBeDefined() |
| 28 | + if (cache) { |
| 29 | + expect(cache.size).toBe(3) |
| 30 | + } |
| 31 | + }) |
| 32 | + |
| 33 | + it('read broken cache', () => { |
| 34 | + // const opt = getCacheOptions() |
| 35 | + |
| 36 | + const dir = path.resolve(__dirname, './fixtures', pkgName + '-broken') |
| 37 | + const filepath = path.resolve(dir, 'index.json') |
| 38 | + mkCacheDirectory(dir) |
| 39 | + fs.writeFileSync( |
| 40 | + filepath, |
| 41 | + `{ |
| 42 | + [ '2',"fuck you",{s:'12} |
| 43 | + }`, |
| 44 | + 'utf-8' |
| 45 | + ) |
| 46 | + expect(fs.existsSync(filepath)).toBe(true) |
| 47 | + const cache = readCache({ |
| 48 | + dir |
| 49 | + }) |
| 50 | + expect(cache).toBe(undefined) |
| 51 | + expect(fs.existsSync(filepath)).toBe(false) |
| 52 | + }) |
| 53 | +}) |
0 commit comments