Skip to content

Commit c2d1663

Browse files
author
probe
committed
fix(test): 修 watcher 测试在 macOS 上空转
`test:capabilities` 在 macOS 上一直是红的,挡住 #1 / #2 / #5 三个 PR 的合并。 根因是路径的两种拼法: - 测试的期望值来自 `mkdtemp(join(tmpdir(), ...))`,在 macOS 上是未解析的 `/var/folders/...` - `createWorkspaceGitWatcher` 内部做了 `realpath`,返回 `/private/var/folders/...` 而 `/var` 是 `/private/var` 的符号链接。 **实现是对的** —— 文件系统 watcher 必须用真实路径,用符号链接那种拼法不保证 指向同一个 inode。错的是测试拿未解析的路径去比。 ## 这个测试此前没有在验任何东西 `mkdtemp` 的返回值用在两处。断言 `directory === workspace` 因此永远不成立; 更要紧的是闭包里 if (directory === workspace) watchWorkspacePath = listener; 也永远不成立,于是 `watchWorkspacePath` 保持 `undefined`,紧接着的 `watchWorkspacePath?.('.git/index')` 变成一次空调用 —— 后面 `expect(onChange).toHaveBeenCalledWith('repository')` 验的是一个从未被触发的 监听器。 所以它不是「因为路径 bug 而失败」,它是**什么都没验就绿了**,直到 `#4` 修好 `typecheck:test`、CI 终于跑到这个门,才把一直没生效的断言暴露出来。 ## 验证 - `capability` 套件 5149 passed / 0 failed(此前 5148 + 1 failed) - 反向验证:去掉 `realpath` 后该测试转红 —— 证明它现在真的在验
1 parent 6c475ab commit c2d1663

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

‎packages/local-runtime-v2/src/service/workspace/snapshot/watcher.test.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventEmitter } from 'node:events';
22
import type { FSWatcher } from 'node:fs';
3-
import { mkdtemp, mkdir, rm } from 'node:fs/promises';
3+
import { mkdtemp, mkdir, realpath, rm } from 'node:fs/promises';
44
import { tmpdir } from 'node:os';
55
import { join } from 'node:path';
66
import { afterEach, describe, expect, it, vi } from 'vitest';
@@ -47,8 +47,19 @@ describe('createWorkspaceGitWatcher', () => {
4747
});
4848

4949
async function createWorkspace(): Promise<string> {
50-
const workspace = await mkdtemp(join(tmpdir(), 'workspace-git-watcher-'));
51-
temporaryDirectories.push(workspace);
50+
const created = await mkdtemp(join(tmpdir(), 'workspace-git-watcher-'));
51+
temporaryDirectories.push(created);
52+
// macOS: `tmpdir()` is `/var/folders/...` and `/var` is a symlink to
53+
// `/private/var`. `mkdtemp` hands back the UNRESOLVED path; the watcher
54+
// realpaths before it watches, which is the correct behaviour — a filesystem
55+
// watch on the symlink spelling is not reliably the same inode.
56+
//
57+
// Comparing the unresolved path against the resolved one is not only a
58+
// failing assertion: the helper closure picks the listener with
59+
// `directory === workspace`, so that comparison never matched, the listener
60+
// stayed undefined, and the `?.()` call after it silently did nothing. The
61+
// test was not failing because of a path bug. It was passing over nothing.
62+
const workspace = await realpath(created);
5263
await mkdir(join(workspace, '.git', 'refs', 'heads'), { recursive: true });
5364
await mkdir(join(workspace, '.git', 'info'), { recursive: true });
5465
return workspace;

0 commit comments

Comments
 (0)