Skip to content

Tests: extract setup.sh's fast path without depending on if vs elif - #9579

Merged
danielhanchen merged 2 commits into
mainfrom
fix-setup-fastpath-extraction
Aug 23, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix-setup-fastpath-extraction

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

The red

tests/sh/test_setup_desktop_backend_version_fastpath.sh fails 3 of 6 on main:

fastpath_blk.sh: line 1: syntax error near unexpected token `elif'
fastpath_blk.sh: line 1: `    elif [ -n "$INSTALLED_VER" ] && [ -n "$LATEST_VER" ] && [ "$INSTALLED_VER" = "$LATEST_VER" ]; then'

The test slices the version fast path out of studio/setup.sh with awk and sources the slice. The start anchor matched the condition text but was blind to the keyword in front of it, so the slice began at elif and could not parse.

The other three cases passed vacuously, not correctly. They expect false, and a block that never sources leaves _SKIP_PYTHON_DEPS at the false the test itself set a line earlier.

Where it came from

90644db9e (#8515, "Studio: repair duplicate package metadata during updates") put a duplicate-metadata branch ahead of the fast path, so the fast path became the elif of that chain. Confirmed by running the test either side rather than by reading the diff:

tree result
f1f95f71f (#8515's parent) 6 passed, 0 failed
90644db9e (#8515) 3 passed, 3 failed

The test itself predates that commit (added in dc26127a4, #8670).

The production side is correct

studio/setup.sh is not touched here.

  • bash -n studio/setup.sh is clean, so the shipped script was never broken.
  • The new branch only precedes the fast path. When the probe exit is not 2 the elif is the identical condition the old if had, and when it is 2 the fast path is skipped so the dependency pass repairs the duplicate metadata, which is the intent.
  • Everything the test exercises, the UNSLOTH_DESKTOP_BACKEND_VERSION escape inside the branch, is unchanged.
  • The parity question: studio/setup.ps1 grew the matching elseif ($_InstalledVersionProbeExit -eq 2) in the same commit, and the XPU pin escape the surrounding comment says it mirrors is present on both sides. No follow-up issue needed.

So the defect is entirely in how the test slices, and the fix belongs in the test.

The fix

The extraction matches the condition as a literal without the keyword, checks separately that the keyword is an if or an elif, and re-emits the block as a standalone if. Re-anchoring on the enclosing if was rejected: it reproduces the same defect one line up, and the enclosing branch is different logic.

Every assumption the slice makes now reports itself as drift by name:

  • exactly one line carries the fast-path condition (0 or 2+ is drift)
  • that line is introduced by if or elif
  • the _setup_pin= end anchor still follows it
  • the body is non-empty, still sets _SKIP_PYTHON_DEPS=true, and still consults UNSLOTH_DESKTOP_BACKEND_VERSION
  • bash -n on the extracted fragment, with the numbered fragment and the parser error printed

Next time setup.sh moves, the test says the fast-path extraction no longer matches studio/setup.sh -- ... instead of a cryptic syntax error against a temp file the reader has never heard of.

The cases also stop trusting the default: the runner prints BLOCK_FAILED_TO_RUN if sourcing fails and BLOCK_NOT_ENTERED if the fast path was never taken, so no case can pass on a block that did nothing.

Why not have setup.sh expose the decision as a sourceable function

Considered and rejected, and this is a real tradeoff. setup.sh has no sourced-helper precedent: it is the only .sh under studio/, it sources nothing from the tree, and it runs top to bottom, so a test cannot source it without either a new shipped file (which then has to survive packaging and every install path) or a sourcing guard in an installer that runs on user machines. It is also deliberately mirrored against setup.ps1, which cannot take the same split. And the block is not a pure decision: it runs $VENV_DIR/bin/python probes, emits step/substep output, and mutates several globals. A self-verifying extraction is the accepted outcome, and it is the pattern the sibling tests/sh/test_setup_xpu_fastpath_escape.sh already uses (content assertions plus bash -n on its own slice).

Verification

All 6 cases pass, and the whole tests/sh suite is green.

Mutation testing, each mutation applied to a throwaway copy of setup.sh, shows the test still has teeth and that the failure modes are distinguishable:

mutation result
none (control) 6 passed
plain if instead of elif (pre-#8515 shape) 6 passed, so the fix is keyword-agnostic in both directions
keyword is neither if nor elif drift: "the fast-path condition is no longer introduced by if/elif (leading text: while)"
condition text changed drift: "expected exactly 1 line holding the fast-path condition, found 0"
condition text appears twice drift: "... found 2"
_setup_pin= end anchor renamed drift: "the end anchor (_setup_pin=) no longer follows the fast-path condition"
unbalanced fi inside the block drift: "the extracted block is not valid bash", with the fragment and the bash -n error
the whole desktop-version escape deleted drift: "the extracted block no longer consults UNSLOTH_DESKTOP_BACKEND_VERSION"
escape no longer clears _SKIP_PYTHON_DEPS 3 real FAILs, the behavioural break the test exists to catch
fast path entered but step never runs 6 FAILs reporting BLOCK_NOT_ENTERED, where the old test would have passed 3

The last two rows are the point: a behavioural regression fails as a test failure, and a structural change to setup.sh fails as drift with a name.

tests/sh/test_setup_desktop_backend_version_fastpath.sh slices the
version fast path out of studio/setup.sh with awk. The start anchor
matched the condition text but not the keyword in front of it, so when
#8515 put a duplicate-metadata branch ahead of it and the condition
became an elif, the slice began at `elif` and could not parse. Three of
the six cases failed with a bare "syntax error near unexpected token",
and the other three passed vacuously: a block that never sources leaves
_SKIP_PYTHON_DEPS at the false the test itself set.

studio/setup.sh is unchanged and correct. `bash -n studio/setup.sh` is
clean, the new branch only precedes the fast path, and setup.ps1 grew
the matching elseif in the same commit, so the two stay mirrored.

The extraction now matches the condition as a literal, without the
keyword, checks that the keyword is an if or an elif, and re-emits the
block as a standalone `if`. Every assumption it makes reports itself:
exactly one condition line, an if/elif keyword, the _setup_pin= end
anchor present, a non-empty body that still sets _SKIP_PYTHON_DEPS and
still consults UNSLOTH_DESKTOP_BACKEND_VERSION, and `bash -n` on the
result. A slice that no longer matches setup.sh now says so by name
instead of emitting a syntax error against a temp file. The sibling
test_setup_xpu_fastpath_escape.sh already guards its own slice this way.

The cases also stop trusting the default: the block reports
BLOCK_FAILED_TO_RUN if sourcing fails and BLOCK_NOT_ENTERED if the fast
path was never taken, so no case can pass on a block that did nothing.
@danielhanchen
danielhanchen merged commit 9f25ba3 into main Aug 23, 2026
15 checks passed
@danielhanchen
danielhanchen deleted the fix-setup-fastpath-extraction branch August 23, 2026 13:32
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 23, 2026
…its text (unslothai#9591)

* Read the settings route's contract off its parse tree, not its text

`Repo tests (CPU)` has been red on main since unslothai#9492 (1f475d7) on
test_the_backfill_fills_in_fields_rather_than_skipping_known_keys. The rule the
test guards did not change. unslothai#9492 added a third name to the
`model_dump(exclude = {...})` set in `update_openai_auto_switch_override`, the
formatter then split the set one name per line, and

    assert '"remove", "fill_absent_fields"' in route

no longer matched, because `route` is the raw source text of
studio/backend/routes/settings.py and the two names now have a newline and
sixteen spaces between them. Both names are still excluded. Nothing about the
write-mode-is-not-a-saved-field rule moved.

This is the third failure of this shape on main in as many days (unslothai#9578, unslothai#9579),
so pinning a new literal only moves the next one into the future. A test that
has to be re-edited whenever a set literal is reflowed is not testing the thing
it names.

The frontend greps in this file have no parser to hand. The backend does. Three
helpers read the rule out of settings.py's AST instead:

- `_model_dump_exclusions` returns the names the one `model_dump(exclude = ...)`
  in a given function leaves out. Exactly one such call, or the answer would be
  a union and a name dropped from the call that matters could hide behind
  another.
- `_annotated_field` returns the annotation and literal default of a field on a
  pydantic model, so "declared as a bool defaulting to False" is asserted as
  that rather than as a substring.
- `_forwarded_keywords` returns the keyword arguments passed as
  `name = payload.name`, which is the part the storage layer depends on.

Each raises rather than returning empty when it cannot find what it was pointed
at, so a renamed handler or field reddens here instead of passing vacuously.

Verified by mutation against the repaired test, one at a time on settings.py:

| Mutation                                          | Result |
|---------------------------------------------------|--------|
| unmodified main                                    | pass   |
| `"fill_absent_fields"` dropped from the exclude set| fail   |
| the exclude set reflowed onto one line, reordered  | pass   |
| the payload field defaulted to True                | fail   |
| `fill_absent_fields` no longer forwarded to the db | fail   |
| the route handler renamed                          | fail   |
| the payload field renamed                          | fail   |

The reflow is the case that caused this outage and is now the only one that
stays green; every mutation that actually breaks the contract still reddens.

Local: `tests/studio/test_model_picker_contracts.py` 187 passed, was 186 passed
1 failed. No production code changed.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.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