|
1 | | -import { access, mkdir, readdir, rm, stat } from 'node:fs/promises' // 根据实际情况调整导入 |
2 | | -import { dirname, join } from 'node:path' |
3 | | -import { beforeEach, describe, expect, it, vi } from 'vitest' |
4 | | -import { mkdirp, rmrf } from '../../src/node/path' |
| 1 | +import { existsSync } from 'node:fs' |
| 2 | +import { join } from 'node:path' |
| 3 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest' |
| 4 | +import { mkdirp, projectRoot, resolvePath, rmrf } from '../../src/node/path' |
5 | 5 |
|
6 | | -// 模拟 fs/promises 和 path 模块 |
7 | | -vi.mock('fs/promises', async () => { |
8 | | - const actual = await vi.importActual('fs/promises') |
9 | | - return { |
10 | | - ...actual, |
11 | | - access: vi.fn(), |
12 | | - mkdir: vi.fn(), |
13 | | - stat: vi.fn(), |
14 | | - readdir: vi.fn(), |
15 | | - rm: vi.fn(), |
16 | | - } |
17 | | -}) |
18 | | - |
19 | | -// 修改:合并所有对 path 模块的模拟,避免重复定义 |
20 | | -vi.mock('path', () => ({ |
21 | | - dirname: vi.fn(), |
22 | | - join: vi.fn(), |
23 | | - normalize: vi.fn(), |
24 | | -})) |
25 | | - |
26 | | -describe('mkdirp', () => { |
27 | | - let accessMock: any |
28 | | - let mkdirMock: any |
29 | | - let dirnameMock: any |
30 | | - |
31 | | - beforeEach(() => { |
32 | | - accessMock = vi.mocked(access) |
33 | | - mkdirMock = vi.mocked(mkdir) |
34 | | - dirnameMock = vi.mocked(dirname) |
35 | | - }) |
36 | | - |
37 | | - it('should not create directory if it already exists', async () => { |
38 | | - accessMock.mockResolvedValueOnce() |
39 | | - |
40 | | - await mkdirp('/mock/path') |
| 6 | +describe('path Utils', () => { |
| 7 | + const testDir = join(projectRoot, 'test-temp') |
41 | 8 |
|
42 | | - expect(accessMock).toHaveBeenCalledWith('/mock/path') |
43 | | - expect(mkdirMock).not.toHaveBeenCalled() |
| 9 | + // 测试前确保目录不存在 |
| 10 | + beforeEach(async () => { |
| 11 | + await rmrf(testDir).catch(() => {}) |
44 | 12 | }) |
45 | 13 |
|
46 | | - it('should recursively create directories', async () => { |
47 | | - accessMock.mockRejectedValueOnce(new Error('ENOENT')) // 第一次访问失败 |
48 | | - accessMock.mockResolvedValueOnce() // 父目录存在 |
49 | | - dirnameMock.mockReturnValueOnce('/mock') // 第一次 dirname 调用 |
50 | | - |
51 | | - await mkdirp('/mock/path') |
52 | | - |
53 | | - expect(accessMock).toHaveBeenNthCalledWith(1, '/mock/path') |
54 | | - expect(dirnameMock).toHaveBeenCalledWith('/mock/path') |
55 | | - expect(mkdirMock).toHaveBeenCalledWith('/mock/path') |
| 14 | + // 测试后清理 |
| 15 | + afterEach(async () => { |
| 16 | + await rmrf(testDir).catch(() => {}) |
56 | 17 | }) |
57 | | -}) |
58 | | - |
59 | | -describe('rmrf', () => { |
60 | | - let statMock: any |
61 | | - let readdirMock: any |
62 | | - let rmMock: any |
63 | | - let joinMock: any |
64 | 18 |
|
65 | | - beforeEach(() => { |
66 | | - statMock = vi.mocked(stat) |
67 | | - readdirMock = vi.mocked(readdir) |
68 | | - rmMock = vi.mocked(rm) |
69 | | - joinMock = vi.mocked(join) |
| 19 | + it('should resolvePath correctly', () => { |
| 20 | + const path = resolvePath('foo/bar') |
| 21 | + expect(path).toBe(join(projectRoot, 'foo/bar')) |
70 | 22 | }) |
71 | 23 |
|
72 | | - beforeEach(() => { |
73 | | - statMock.mockClear() |
74 | | - readdirMock.mockClear() |
75 | | - rmMock.mockClear() |
76 | | - joinMock.mockClear() |
| 24 | + it('should create directory recursively', async () => { |
| 25 | + const nestedDir = join(testDir, 'a/b/c') |
| 26 | + await mkdirp(nestedDir) |
| 27 | + expect(existsSync(nestedDir)).toBe(true) |
77 | 28 | }) |
78 | 29 |
|
79 | | - it('should delete a file', async () => { |
80 | | - statMock.mockResolvedValueOnce({ isDirectory: () => false }) |
| 30 | + it('should remove directory recursively', async () => { |
| 31 | + const nestedDir = join(testDir, 'x/y/z') |
| 32 | + await mkdirp(nestedDir) |
| 33 | + expect(existsSync(nestedDir)).toBe(true) |
81 | 34 |
|
82 | | - await rmrf('/mock/path/to/file') |
83 | | - |
84 | | - expect(statMock).toHaveBeenCalledWith('/mock/path/to/file') |
85 | | - expect(rmMock).toHaveBeenCalledWith('/mock/path/to/file') |
86 | | - expect(joinMock).not.toHaveBeenCalled() |
87 | | - expect(readdirMock).not.toHaveBeenCalled() |
| 35 | + await rmrf(testDir) |
| 36 | + expect(existsSync(testDir)).toBe(false) |
88 | 37 | }) |
89 | 38 |
|
90 | | - it('should handle errors gracefully', async () => { |
91 | | - const testError = new Error('Test error') |
92 | | - statMock.mockRejectedValue(testError) |
93 | | - |
94 | | - try { |
95 | | - await rmrf('/mock/path/to/error') |
96 | | - } |
97 | | - catch (error) { |
98 | | - expect(error).toBe(testError) |
99 | | - } |
| 39 | + it('rmrf should not throw if path does not exist', async () => { |
| 40 | + await expect(rmrf(join(testDir, 'nonexistent'))).resolves.not.toThrow() |
| 41 | + }) |
100 | 42 |
|
101 | | - expect(statMock).toHaveBeenCalledWith('/mock/path/to/error') |
102 | | - expect(readdirMock).not.toHaveBeenCalled() |
103 | | - expect(rmMock).not.toHaveBeenCalled() |
| 43 | + it('mkdirp should not throw if directory already exists', async () => { |
| 44 | + const dir = join(testDir, 'already') |
| 45 | + await mkdirp(dir) |
| 46 | + await expect(mkdirp(dir)).resolves.not.toThrow() |
104 | 47 | }) |
105 | 48 | }) |
0 commit comments