Files
apes-sidebar/tests/fs-write-relative-path.spec.ts
T
Menghuan1918 57fe6e657d test: 临时目录清理统一走 removeTree(修 Windows 上的 ENOTEMPTY 竞态)
CI 在 windows lane 上红在**清理**而不是断言:
`rmSync(scratch, { recursive: true, force: true })` 抛
`ENOTEMPTY: rmdir …archives-pvpAWR`——刚被 kill 的 zip/tar 子进程(以及刚关闭的
fs.watch / 刚结束的 git 子进程)还没被系统释放句柄,删除于是失败在一个**已经通过**
的用例的 afterEach 里(那一次 2466 个用例全过,只有清理挂掉,整个 lane 变红)。

`maxRetries` + `retryDelay` 正是 Node 为这种场景提供的对策,因此新增
`tests/temp-tree.ts` 的 `removeTree()`,把 30 份 spec(175 处 `{ recursive, force }`
写法)统一换过去,并清掉随之不再使用的 `rmSync` 导入。POSIX 上首次即成功、不触发
重试,行为不变。

本地:typecheck / lint 干净,全量 210 文件 / 2524 通过。
2026-10-16 00:51:11 +08:00

52 lines
2.1 KiB
TypeScript

import { mkdirSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { afterEach, describe, expect, it } from 'vitest'
import { ensureWorkspaceWritePath } from '../src/path-security.ts'
import { removeTree } from './temp-tree.ts'
const dirs: string[] = []
afterEach(() => { for (const d of dirs.splice(0)) removeTree(d) })
describe('fs.write resolves session-relative paths (#646)', () => {
it('resolves a relative write path against the session cwd', async () => {
const dir = join(tmpdir(), 'dshm-646-a')
dirs.push(dir)
mkdirSync(join(dir, 'pastes'), { recursive: true })
const result = await ensureWorkspaceWritePath(dir, 'pastes/test.txt', false)
expect(result).toContain('pastes')
expect(result).toContain('test.txt')
})
it('keeps an absolute write path unchanged', async () => {
const dir = join(tmpdir(), 'dshm-646-b')
dirs.push(dir)
mkdirSync(dir, { recursive: true })
const abs = join(dir, 'abs.txt')
const result = await ensureWorkspaceWritePath(dir, abs, false)
expect(result).toContain('abs.txt')
})
it('keeps a write outside the workspace (containment was removed with the path-security rewrite)', async () => {
const dir = join(tmpdir(), 'dshm-646-c')
dirs.push(dir)
mkdirSync(join(dir, 'inside'), { recursive: true })
const outside = join(tmpdir(), 'dshm-646-outside')
mkdirSync(outside, { recursive: true })
dirs.push(outside)
const target = join(outside, 'x.txt')
// The fence argument is ignored by design now; the path resolves lexically.
await expect(ensureWorkspaceWritePath(dir, target, true)).resolves.toBe(target)
})
it('expands a ~ home path instead of joining it under the cwd (#713)', async () => {
const dir = join(tmpdir(), 'dshm-713')
dirs.push(dir)
mkdirSync(dir, { recursive: true })
const { homedir } = await import('node:os')
const result = await ensureWorkspaceWritePath(dir, '~/notes/x.txt', false)
expect(result.startsWith(homedir())).toBe(true)
expect(result).toContain(join('notes', 'x.txt'))
})
})