|
1 | | -import { afterEach, describe, expect, it } from 'vitest' |
| 1 | +import { mkdtempSync } from 'node:fs' |
| 2 | +import os from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | + |
| 5 | +import { |
| 6 | + afterAll, |
| 7 | + afterEach, |
| 8 | + beforeAll, |
| 9 | + beforeEach, |
| 10 | + describe, |
| 11 | + expect, |
| 12 | + it, |
| 13 | +} from 'vitest' |
2 | 14 |
|
3 | 15 | import * as cacache from '../../registry/dist/lib/cacache.js' |
| 16 | +import { trash } from '../../scripts/utils/fs.mjs' |
4 | 17 |
|
5 | 18 | // Test key for cache operations. |
6 | 19 | const TEST_KEY = 'test-cacache-key' |
7 | 20 | const TEST_DATA = 'test data content' |
8 | 21 |
|
9 | 22 | describe('cacache module', () => { |
| 23 | + let testCacheDir: string |
| 24 | + let originalEnv: string | undefined |
| 25 | + |
| 26 | + beforeAll(() => { |
| 27 | + // Create a temporary directory for test-specific cacache. |
| 28 | + testCacheDir = mkdtempSync(path.join(os.tmpdir(), 'cacache-test-')) |
| 29 | + // Override cacache directory for tests. |
| 30 | + originalEnv = process.env['SOCKET_CACACHE_DIR'] |
| 31 | + process.env['SOCKET_CACACHE_DIR'] = testCacheDir |
| 32 | + }) |
| 33 | + |
| 34 | + afterAll(async () => { |
| 35 | + // Restore original environment. |
| 36 | + if (originalEnv === undefined) { |
| 37 | + delete process.env['SOCKET_CACACHE_DIR'] |
| 38 | + } else { |
| 39 | + process.env['SOCKET_CACACHE_DIR'] = originalEnv |
| 40 | + } |
| 41 | + // Clean up test cache directory. |
| 42 | + await trash(testCacheDir) |
| 43 | + }) |
| 44 | + |
| 45 | + beforeEach(async () => { |
| 46 | + // Remove test key before each test for defensive cleanup. |
| 47 | + await cacache.remove(TEST_KEY).catch(() => { |
| 48 | + // Ignore cleanup errors. |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
10 | 52 | afterEach(async () => { |
11 | 53 | // Clean up test entries after each test. |
12 | 54 | await cacache.remove(TEST_KEY).catch(() => { |
|
0 commit comments