Skip to content

refactor(cli): product code holds no test accommodations, and Ruff and ast-grep ban them in src #2512

Closed

Description

@dan-petty

Problem or Motivation

docs/TESTING.md says that product code holds nothing for tests. On origin/main (2026-10-28) src/devops_cli has 9 test accommodations:

  • unittest.mock imports: security/base.py:18, ai/review/pipeline.py:657;
  • the string PYTEST_CURRENT_TEST: commands/ci.py:418, :422, core/process.py:109;
  • a mock attribute probed by name: sandbox/host.py:101 (_mock_return_value);
  • a __module__ value compared with "subprocess": core/process.py:670, :1080, sandbox/host.py:99.

Nothing stops a new one. #1309 planned a per-file ratchet for them. It was closed as not planned, because newer owner decisions add no new ratchet and no baseline file (#1016 and the rigid-rules decision Q4, 2026-10-22; #423, 2026-10-24). With only 9 sites, the sites can be removed and the forms banned at zero, with no baseline.

Proposed Solution & Architectural Design

  • Remove the 9 sites. Each one is replaced by the seam or fake that the test needs, as docs/TESTING.md describes. A test that relied on a site doubles the process edge instead.
  • Ban unittest and unittest.mock imports in src/ with Ruff's banned-api (TID251), which pyproject.toml already configures, and a per-file ignore for tests/.
  • Ban the string PYTEST_CURRENT_TEST, a NonCallableMock attribute name that starts with assert_ or _mock_, and a comparison of a __module__ value with a standard-library module name, in src/, with ast-grep rules and their rule tests.
  • No ceiling table, no baseline file and no lowering command.

Target Subsystem

src/devops_cli (security/base.py, ai/review/pipeline.py, commands/ci.py, core/process.py, sandbox/host.py), pyproject.toml, the ast-grep rules, docs/TESTING.md.

Existing Tool Considered

Ruff banned-api (TID251) for imports, and ast-grep for the other forms. Both already run in devops ci and in the pre-commit hooks. Nothing new is built.

Acceptance criteria

  • src/devops_cli holds none of the 9 sites.
  • Ruff reports TID251 for a fixture src/ module that imports unittest.mock, and not for a module under tests/.
  • Each ast-grep rule has a rule test with one failing and one passing case, including the getattr(..., "__module__") form and a __module__ read that is not compared with a standard-library name.
  • docs/TESTING.md names the bans and drops the ratchet for test accommodations from its target list.
  • uv run devops ci passes.

Related

#1309 (closed as not planned), #1307, #1336, #1016, #423.

Source: #1309

Activity

  1. added
    source/agent
    type/refactor
    status/new
    value/medium
    effort/medium
    on Oct 28, 2026
  2. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/refactor. The change removes existing code and adds static-analysis bans without altering user-facing behaviour.
    • Priority: P2-Medium. It prevents a known class of test accommodation from growing but does not block a release or fix a defect.
    • Value: Medium. It enforces the TESTING.md rule that product code holds no test hooks, reducing future coupling.
    • Effort: Medium. Nine sites to replace, one Ruff rule to activate, and three ast-grep rules with passing and failing tests to write.
    • Related: refactor(security): the unused run_subprocess re-import in security/ruff.py and the private _prepare_cmd alias go, and a ci row fails on a new noqa F401 re-import in src #2166, which asks for a different change. Both remove test accommodations from src/ and add a check to block new ones, but #2166 targets two specific F401 re-imports and a private alias while the candidate targets nine sites of mock imports, PYTEST_CURRENT_TEST, mock attributes, an
  3. added
    priority/p2-medium
    on Oct 28, 2026
  4. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    This item cannot be built as written in one pull request. Each of its 9 sites and its bans is already in the acceptance criteria of an older open item. No code was written.

    Evidence (origin/main 0137f23, 2026-10-28):

    Site Item that deletes it
    security/base.py:18 (NonCallableMock), with _is_mocked, the three mock-aware wrappers and the 58 patch sites that rely on them #1332 (Ready, P1), "Accommodations (Q27)"
    ai/review/pipeline.py:657 (NonCallableMock) #1328, "Mock detection is gone"
    sandbox/host.py:99, :101 (__module__, _mock_return_value in _is_mock_subprocess) #2149 (Ready), and #1335 criterion 6
    commands/ci.py:418, :422 (PYTEST_CURRENT_TEST) #1325, "This item deletes the two PYTEST_CURRENT_TEST reads"
    core/process.py:109 (PYTEST_CURRENT_TEST pass-through), :670, :1080 (async reroute on subprocess.run.__module__) #1333, criterion 8 and step 7 ("async reroute, PYTEST_CURRENT_TEST")
    Bans of the four kinds at zero #1336, "Bans": "test accommodations in src, of the four kinds #1309 counts"

    A site cannot go before the tests that rely on it move to the fakes. For example, removing the security/base.py site needs the rewrite of the 58 patch sites that #1332 owns. So this item, built as written, does the work of #1325, #1328, #1332, #1333 and #2149 in one pull request.

    One part of this item is new: the bans use Ruff banned-api (TID251) for unittest imports and ast-grep rules with rule tests for the other three kinds, instead of hand-written invariant tests. That fits the rigid-rules decision Q4 (2026-10-22: a tool owns its configuration). It is a design choice for #1336, which adds the bans after #1321 to #1335 bring each count to zero.

    Proposed next step for a person: close this item as a duplicate of #1325, #1328, #1332, #1333, #2149 and #1336, and add the tool choice for the bans to #1336.

  5. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Closing as duplicate.

  6. removed
    status/new
    on Oct 30, 2026
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

    effort/mediumpriority/p2-mediumsource/agenttype/refactorvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions