Skip to content

test 红(Windows 限定):test-suite-lock.test.ts resolveLockPath 断言必失败(path.resolve vs path.join 对 POSIX 路径不一致) #3175

Closed

Description

@YunzhouLi-hub

现象

tests/test-suite-lock.test.ts:65 的 resolveLockPath mirrors paths.ts cacheDir() under XDG_CACHE_HOME 在 Windows 上确定性失败(ubuntu 通过),每个 PR 的 windows test shard 都被它染红。

错误:

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
  + actual - expected
+ actual:   'D:\tmp\x\cache-home\billion-context\test-suite.lock'
- expected: '\tmp\x\cache-home\billion-context\test-suite.lock'

复现

# 任意分支(含干净 master 7f96abe0)
node --import tsx --test tests/test-suite-lock.test.ts
# => tests 16, pass 15, fail 1(resolveLockPath 用例)

根因

tests/test-suite-lock.test.ts:65-70 用一个硬编码 POSIX 绝对路径 /tmp/x/cache-home:

const dir = "/tmp/x/cache-home";
assert.equal(
    lck.resolveLockPath({ XDG_CACHE_HOME: dir } as NodeJS.ProcessEnv),
    join(dir, "billion-context", "test-suite.lock"),
);

scripts/test-suite-lock.mjs:48-53 的实现是 path.resolve(env.XDG_CACHE_HOME)(刻意对齐 src/paths.ts:22 的 xdg() 同样用 path.resolve):

const xdg = env.XDG_CACHE_HOME && env.XDG_CACHE_HOME.length > 0
    ? path.resolve(env.XDG_CACHE_HOME)
    : path.join(homedir(), ".cache");

Windows 上 path.resolve("/tmp/x/cache-home") 会补上当前盘符得到 D:\tmp\x\cache-home,而 join("/tmp/x/cache-home", ...) 保持 \tmp\x\cache-home(无盘符)——两者必然不相等。POSIX 上两者恰好一致,所以只有 Windows 红。

即:被测实现是对的(与 src/paths.ts 语义一致),是测试的期望值写错了。

影响

建议修复

测试侧改为用 path.resolve 的期望值(或改用平台原生临时目录):

const dir = "/tmp/x/cache-home";
assert.equal(
    lck.resolveLockPath({ XDG_CACHE_HOME: dir } as NodeJS.ProcessEnv),
    join(resolve(dir), "billion-context", "test-suite.lock"),
);

(注意 import resolve from "node:path";该文件已 import join。)

发现背景

该测试由 #3112/#3103(test-suite-lock,今天 10:44 合入 master)引入,合入后 master 的 windows CI 未实跑 test lanes(push 时 guards skip),所以合并时没暴露。排查 PR #3169 CI 红灯时定位。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions