Skip to content

Refuse to run this tree's tests against another tree's source - #121

Open
getnable wants to merge 1 commit into
mainfrom
test/source-tree-guard
Open

Refuse to run this tree's tests against another tree's source#121
getnable wants to merge 1 commit into
mainfrom
test/source-tree-guard

Conversation

@getnable

Copy link
Copy Markdown
Owner

finops is installed editable against one checkout. A git worktree gets its own tests/ and its own src/, but import finops still resolves to whichever checkout the install points at.

So running pytest from a worktree pairs that worktree's test files with the main checkout's code. Nothing errors. You get a normal-looking run whose result is about neither tree, and the failures it invents are indistinguishable from real ones.

This already cost something

Today it produced two confident, opposite, wrong conclusions about the same test inside an hour:

"test_priority_application_order_is_preserved fails on pristine main."
It does not. The tests were main's; the source was a feature branch.

"Then fix/tag-attribution must have broken it."
It had not. That branch renamed the test to encode a corrected contract, exactly as its commit message said. Running the old name against the new code can only fail.

Both were reported before anyone noticed the trees were crossed, and one reached a merged commit message on main (#119).

The guard

pytest_configure compares where the tests are with where finops resolved, and stops with one line if they disagree, rather than letting thousands of meaningless failures scroll past:

  These tests and the finops they import are from different trees.

    tests come from : .../scratchpad/finops-guard
    finops imports  : /Users/chandan/finops/src/finops
    expected        : .../scratchpad/finops-guard/src/finops

    PYTHONPATH=.../finops-guard/src pytest ...

The message carries the exact fix, because whoever hits this doesn't yet know what's wrong and a description of the problem would leave them to work it out.

FINOPS_TESTS_ALLOW_FOREIGN_SOURCE=1 opts out, for deliberately testing an installed build.

Verified three ways, in a real worktree

Result
The trap (worktree, no PYTHONPATH) caught, with the message above
Correct invocation 10 passed
Escape hatch set reproduces the original misleading failure

That third row is the point: with the guard disabled you get back exactly the phantom failure that caused the two wrong calls, which is the clearest demonstration that it's catching something real rather than being defensive for its own sake.

CI is unaffected

The pytest job installs editable into the checkout it tests, so the two paths already agree. The audit job installs non-editable and does not run pytest.

🤖 Generated with Claude Code

finops is installed editable against ONE checkout. A git worktree gets its own
tests/ and its own src/, but `import finops` still resolves to whichever
checkout the install points at. So running pytest from a worktree pairs that
worktree's test FILES with the main checkout's CODE.

Nothing errors. You get a normal-looking run whose result is about neither tree,
and the failures it invents are indistinguishable from real ones.

That is not hypothetical. Today it produced two confident, opposite, wrong
conclusions about the same test inside an hour:

  "test_priority_application_order_is_preserved fails on pristine main"
      It does not. The tests were main's; the source was a feature branch.

  "then fix/tag-attribution must have broken it"
      It had not. That branch RENAMED the test to encode a corrected contract,
      exactly as its commit message said. Running the old name against the new
      code can only fail.

Both were reported before anyone noticed the trees were crossed, and one of them
reached a merged commit message on main.

pytest_configure compares where the tests are with where finops resolved, and
stops the run with one line if they disagree, rather than letting thousands of
meaningless failures scroll past. The message carries the exact PYTHONPATH that
fixes it, because the person hitting this does not yet know what is wrong and a
description of the problem would leave them to work it out.

FINOPS_TESTS_ALLOW_FOREIGN_SOURCE=1 opts out, for deliberately testing an
installed build rather than the working tree.

Verified three ways, in a real worktree: the trap is caught with the right
message, the correct invocation passes, and with the escape hatch set the run
reproduces the original misleading failure, which is the clearest possible
demonstration that the guard is catching something real.

CI is unaffected: the pytest job installs editable in the checkout it tests, so
the two paths already agree. The audit job installs non-editable and does not
run pytest.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant