test_file_utils umask tests assert against a umask the code never reads (import-time _UMASK), so they only pass on a 0o022 host

未关闭 适合新手
#1,071 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@neverbiasu 已经在做这个了。

开始于 2026年11月4日。

  • #1105 来自 @neverbiasu —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
88/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
testing

调研方向

阅读 tests/comfy_cli/test_file_utils.py 和 comfy_cli/file_utils.py 中 _UMASK 捕获附近的代码。更新两个原子写入测试,以控制实现读取的值,并添加对第二种 umask(如 0o077)的覆盖。运行针对 file-utils 的测试;这些测试应验证预期的文件模式,且不改变进程的 umask。

由索引模型根据 Issue 内容生成。

描述

bug

Summary

test_atomic_write_text_new_file_uses_umask_default and test_atomic_write_bytes_new_file_uses_umask_default set a umask at runtime and assert the resulting file mode. file_utils captures the umask once at import, so the os.umask(0o022) the tests call has no effect on the code under test. The assertion therefore only holds on a host whose ambient umask already happens to be 0o022, and fails outright on any other.

Observed on a host with umask 0007:

tests/comfy_cli/test_file_utils.py:163: AssertionError
assert 432 == (438 & ~18)
#      ^ 0o660, i.e. 0o666 & ~0o007 (the AMBIENT umask)
#              ^ 0o644, i.e. 0o666 & ~0o022 (the umask the test set)

Both tests fail the same way; test_atomic_write_bytes_… at :313.

Cause

comfy_cli/file_utils.py:98-105:

# The process umask, captured once at import. os has no getter, so reading it
# ...
# overlapping probes can restore each other's zero and strand the umask at 0.
_UMASK = os.umask(0)
os.umask(_UMASK)

Capturing once is deliberate and the comment gives a good reason for it. The tests simply do not account for it: they manipulate the real process umask, which _atomic_write no longer consults.

So this is a test defect, not a product one. The product behaviour the tests mean to pin — "a new destination gets the umask-derived default, not mkstemp's hardcoded 0600" — is correct and worth keeping pinned; the tests just pin it against the wrong knob.

Suggested fix

Patch the captured value rather than the process umask, so the test controls what the code actually reads:

def test_atomic_write_text_new_file_uses_umask_default(tmp_path, monkeypatch):
    monkeypatch.setattr(file_utils, "_UMASK", 0o022)
    target = tmp_path / "fresh.json"
    atomic_write_text(target, "data")
    assert stat.S_IMODE(os.stat(target).st_mode) == (0o666 & ~0o022)

That also removes the os.umask save/restore, which is process-global state the current tests mutate.

A second case worth adding while in there: assert the same for a non-0o022 umask (0o077, so 0o600), which would have caught this — a single hardcoded umask value cannot distinguish "the code honours the umask" from "the host umask matches the literal in the assertion".

Context

Found while verifying that pre-existing failures were unrelated to a converter change. Two other environment-sensitive failures turned up in the same run and are worth separating:

  • Five tests/comfy_cli/command/test_code_search.py::TestCodeSearchCLI cases fail when the suite runs from inside an AI coding tool, because CLAUDECODE / AI_AGENT in the environment flip detect_caller() and code-search then passes agent_origin=True. Fixed in #1072.
  • test_node_deps.py::test_non_pep440_installed_version_is_unknown_not_a_crash reports mismatch where it expects unknown. Not agent-env related (it fails with those vars unset) and I have not diagnosed it; noting it so it is not assumed covered by the other two.

None of these fail in CI, which is why they have survived: CI's environment happens to satisfy every one of the assumptions.

Task Decomposition

Not decomposed: this is a two-test change in one file (swap os.umask(...) for monkeypatch.setattr(file_utils, "_UMASK", ...), plus one extra parametrised umask value). Stated explicitly per the issue-filing convention.

主要语言
Python
星标
1k
派生
176
平均合并
1 天 11 小时
30 天内合并 PR
87

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Comfy-Org/comfy-cli 的其他 Issue

查看 Comfy-Org/comfy-cli 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。