Skip to content

Trim down the command snapshot format - #20630

Merged
EliteTK merged 5 commits into
mainfrom
tk/test-compression
Jul 23, 2026
Merged

EliteTK merged 5 commits into
mainfrom
tk/test-compression

Conversation

@EliteTK

@EliteTK EliteTK commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Now, before you run away, hear me out. Locally, this is a small change, but it cuts out redundant information which simply reduces the density of tests. Tests are already meant to be low density, but that's so you can understand them easily. Spreading the code out over more screenfuls makes it harder to follow.

So, I decided to remove information which is strictly redundant from the snapshots. This coincidentally also saves 15K lines.

Note: There's a split of commits to help review, read on...

Test Plan

The changes for all but the last commit were reviewed manually, and they're relatively straightforward.

All but the first commit are fully mechanical in nature, and would just arise from re-generating the snapshots.

To prove that the last commit is correct and that it does not lose information I ran the following script:

import os
import re
import sys
import tempfile
from pathlib import Path


def expand(destination, source):
    state = "outside"
    indent = success = stdout = stderr = hashes = ""
    edited = False

    for line in source:
        next_state = state
        prefix = ""

        if state == "outside" and "uv_snapshot!(" in line.partition("//")[0]:
            next_state = "arguments"
        if next_state == "arguments":
            if opening := re.search(r'@(?:r(?P<hashes>#*))?"\s*$', line):
                hashes = opening.group("hashes") or ""
                next_state = "header"
        elif state == "header":
            if header := re.match(
                r"([ \t]*)exit_code: ([^\n]+) \((success|failure)\)\n",
                line,
            ):
                indent, exit_code, outcome = header.groups()
                success = "true" if outcome == "success" else "false"
                line = f"{indent}exit_code: {exit_code}\n"
                stdout = f"{indent}----- stdout -----\n"
                stderr = f"{indent}----- stderr -----\n"
                next_state = "exit"
            else:
                next_state = "ignored"
        elif state in {"exit", "stdout", "stderr"}:
            if line == stdout:
                next_state = "stdout"
            elif line == stderr:
                next_state = "stderr"
            elif line.strip() == f'"{hashes}':
                next_state = "outside"
            elif closing := re.search(rf'"{hashes}\);[ \t]*(?://.*)?\n?$', line):
                prefix = line[: closing.start()]
                if prefix == indent:
                    prefix = ""
                else:
                    line = line[closing.start() :]
                next_state = "outside"
        elif state == "ignored" and (
            line.strip() == f'"{hashes}' or re.search(rf'"{hashes}\);', line)
        ):
            next_state = "outside"

        insert = {
            ("header", "exit"): f"{indent}success: {success}\n",
            ("exit", "stderr"): f"{stdout}\n",
            ("exit", "outside"): f"{stdout}\n{stderr}{indent if prefix else ''}",
            ("stdout", "outside"): f"\n{stderr}{indent if prefix else ''}",
        }.get((state, next_state), "")
        destination.write(prefix + insert + line)
        edited |= bool(insert)
        state = next_state

    if state in {"exit", "stdout", "stderr"}:
        raise ValueError("Unterminated snapshot")
    return edited


if __name__ == "__main__":
    for filename in sys.argv[1:]:
        path = Path(filename)
        with (
            path.open() as source,
            tempfile.NamedTemporaryFile(mode="w", dir=path.parent) as destination,
        ):
            if expand(destination, source):
                os.fchmod(destination.fileno(), os.fstat(source.fileno()).st_mode)
                destination.flush()
                os.replace(destination.name, path)

If you run:

git diff-tree --no-commit-id --name-only -r -z HEAD | xargs -0 python3 script

And then:

git diff HEAD^

Then there should be no difference. The script is just "decompressing" the "compressed" tests. It's a state machine which re-creates the missing elements if they're not there.

@EliteTK EliteTK added the internal A refactor or improvement that is not user-facing label Jul 23, 2026
@EliteTK EliteTK added area:macos Specific to the macOS platform test:macos Enable macOS tests for a pull request and removed area:macos Specific to the macOS platform labels Jul 23, 2026
@EliteTK
EliteTK marked this pull request as ready for review July 23, 2026 00:14
Base automatically changed from tk/fix-more-filters to main July 23, 2026 11:48
@konstin
konstin requested a review from zanieb July 23, 2026 12:43
@EliteTK
EliteTK force-pushed the tk/test-compression branch from 69b20ab to 3a41e8f Compare July 23, 2026 14:07
@EliteTK
EliteTK merged commit 39e2d2e into main Jul 23, 2026
57 of 58 checks passed
@EliteTK
EliteTK deleted the tk/test-compression branch July 23, 2026 14:15

This branch had an error being deployed

1 failed deployment
automations — 3a41e8f2 Deployed Jul 23, 2026 by EliteTK via review / security review #43111
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal A refactor or improvement that is not user-facing test:macos Enable macOS tests for a pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants