Skip to content

[BUG] test_mcp_package_is_removed 断言名不副实:命名空间目录让“已删除”检查假通过 #275

Closed

Description

@codeasier

Bug 描述

tests/test_package.py::TestPackage::test_mcp_package_is_removed 用 pytest.raises(ModuleNotFoundError) 包住 importlib.import_module("pt_snap_cli.mcp"),以此证明 MCP 包已被移除。

判据在命名空间包规则下会失效:只要 src/pt_snap_cli/mcp/ 目录仍然存在(哪怕里面只剩 __pycache__、没有任何 .py),import_module 就会成功返回 <module 'pt_snap_cli.mcp' (namespace)>,__file__ 为 None,断言因此失败。反过来也一样——目录不存在时测试通过,但这不能区分“包被移除”和“目录恰好被清掉”。

工作树里正好有两处这类残留:src/pt_snap_cli/mcp/ 与 src/pt_snap_cli/vendor/memsnapdump/。二者都不在 Git 索引中(git ls-files 为空),只剩 __pycache__ 与被忽略的空目录,因此干净 checkout 永远复现不了,本地开发机必现。

复现步骤

  1. 在保留历史运行痕迹的 checkout(存在 src/pt_snap_cli/mcp/__pycache__)执行 python -m pytest tests/test_package.py -q
  2. 观察 test_mcp_package_is_removed 失败
  3. 执行 python -c "import importlib; m=importlib.import_module('pt_snap_cli.mcp'); print(m, m.__file__)",确认返回的是命名空间模块
  4. 在全新 clone 上重复第 1 步,观察通过

预期行为

该断言能区分“包确实不存在”与“目录恰好不存在”;干净 checkout 与非干净 checkout 得到一致结论,不产生依赖本地残留的假阳性或假阴性。

实际行为

本机 macOS / Python 3.13.11 / pt-snap-cli 0.4.0,执行 python -m pytest -m "not slow and not nightly" -q:3065 passed、1 failed、3 skipped,唯一失败即该用例;同一提交在干净 CI checkout 上无法进入该分支。

操作系统

macOS

Python 版本

3.13.11

pt-snap-cli 版本

0.4.0(HEAD f876665,v0.4.1-4-gf876665)

日志/错误信息

$ python -m pytest tests/test_package.py -q
FAILED tests/test_package.py::TestPackage::test_mcp_package_is_removed
E   Failed: DID NOT RAISE <class 'ModuleNotFoundError'>

$ python -c "import importlib; m=importlib.import_module('pt_snap_cli.mcp'); print(m, m.__file__)"
<module 'pt_snap_cli.mcp' (namespace) from ['.../src/pt_snap_cli/mcp']> None

$ git ls-files src/pt_snap_cli/mcp src/pt_snap_cli/vendor
(无输出)

$ ls src/pt_snap_cli/mcp
__pycache__

补充信息

与已关闭的 #185(重复构建把 build/lib 中已删除的 MCP/vendor 模块混进 wheel)现象相邻但触发面不同:#185 是构建中间产物,这里是 src 树中的命名空间残影,且暴露的是断言判据本身,不只是残留清理。

Activity

  1. codeasier commented on Nov 1, 2026

    @codeasier
    OwnerAuthor

    Implemented by PR #281: removed-package checks tolerate empty/cache-only namespace residue without importing it, while positive/negative regressions reject restored source/assets/importable bytecode and installed-manifest remnants. Existing arbitrary-name packaging audits remain intact.

    Merged main: efd24f8d840e8da5393585b13299e758883dc1a7, tree exactly matching the reviewed pre-merge head. All applicable post-merge checks completed successfully: Test 41318648659 and Platform 41318648477, including Linux 3.10–3.13, required Chromium, all Windows shards and macOS. Scheduled/manual nightly acceptance was event-skipped, not claimed as executed.

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

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions