Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

16.7 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""guard installer shell-suite discovery, path triggers, and parallel result reporting."""

import os
import re
import shutil
import subprocess
from pathlib import Path

import pytest
import yaml


REPO_ROOT = Path(__file__).resolve().parents[2]

_WORKFLOWS = REPO_ROOT / ".github" / "workflows"
_BACKEND_CI = _WORKFLOWS / "studio-backend-ci.yml"
_PARITY_CI = _WORKFLOWS / "cross-platform-parity-ci.yml"
_RUN_ALL = REPO_ROOT / "tests" / "run_all.sh"
_SH_DIR = REPO_ROOT / "tests" / "sh"

_EXPECTED_CI_SKIPS = {
    "test_install_rollback_lifecycle.sh": "runs on both platforms in cross-platform-parity-ci.yml",
}


def _backend_ci() -> dict:
    return yaml.safe_load(_BACKEND_CI.read_text(encoding = "utf-8"))


def _shell_step_script() -> str:
    """The `run:` body of the shell-installer step, located by name through the
    parsed YAML rather than by slicing the raw file."""
    for job in _backend_ci()["jobs"].values():
        for step in job.get("steps", []):
            if step.get("name") == "Shell installer tests":
                return step["run"]
    raise AssertionError("Backend CI has no 'Shell installer tests' step")


def _shell_test_files():
    files = sorted(p.name for p in _SH_DIR.glob("test_*.sh"))
    assert files, "tests/sh has no test_*.sh files -- did the directory move?"
    return files


def _skip_list(source: str) -> set[str]:
    """The skip= / SH_SKIP= line from a discovery loop."""
    m = re.search(r"^\s*(?:skip|SH_SKIP)=\"([^\"]*)\"", source, re.MULTILINE)
    assert m, "no skip list found; the discovery loop must declare one (even if empty)"
    return {name for name in m.group(1).split() if name}


class TestBackendCiRunsEveryShellTest:
    def test_step_discovers_the_directory_instead_of_listing_files(self):
        """Matches the glob in the parsed step script rather than a verbatim line, so reformatting
        the loop keeps CI green; only a hardcoded list turns it red."""
        script = _shell_step_script()
        assert re.search(r"for\s+\w+\s+in\s+tests/sh/test_\*\.sh", script), (
            "Backend CI must glob tests/sh; a hardcoded list is how the ROCm WSL "
            f"suite went unrun for months. Step script was:\n{script}"
        )
        listed = re.findall(r"tests/sh/test_[a-z0-9_]+\.sh", script)
        assert not listed, f"Backend CI still names individual shell tests: {sorted(set(listed))}"

    def test_step_fails_loudly_if_discovery_finds_nothing(self):
        """A moved directory must break the build, not pass vacuously."""
        assert "no shell tests discovered under tests/sh" in _shell_step_script()

    def test_every_shell_test_runs_or_is_a_known_skip(self):
        skips = _skip_list(_shell_step_script())
        unexpected = skips - set(_EXPECTED_CI_SKIPS)
        assert not unexpected, (
            f"Backend CI skips {sorted(unexpected)} without a reason recorded in "
            "_EXPECTED_CI_SKIPS; add one or stop skipping it"
        )
        for name in _shell_test_files():
            assert name not in skips or name in _EXPECTED_CI_SKIPS, name

    def test_skip_entries_are_not_stale(self):
        """A skip for a deleted file quietly widens next time a name is reused."""
        existing = set(_shell_test_files())
        for name in _skip_list(_shell_step_script()):
            assert name in existing, f"{name} is skipped but no longer exists in tests/sh"

    def test_each_skip_is_documented_in_the_workflow(self):
        source = _BACKEND_CI.read_text(encoding = "utf-8")
        for name in _EXPECTED_CI_SKIPS:
            assert (
                source.count(name) >= 2
            ), f"{name} is skipped in Backend CI without a comment explaining why"

    def test_rollback_lifecycle_really_does_run_elsewhere(self):
        """The one skip justified by 'another workflow covers it' must be true."""
        assert "tests/sh/test_install_rollback_lifecycle.sh" in _PARITY_CI.read_text(
            encoding = "utf-8"
        )

    def test_rocm_shell_suite_is_in_scope(self):
        """The suite whose absence prompted this file: it must exist and be
        picked up (i.e. not skipped)."""
        assert "test_strixhalo_wsl_reroute.sh" in _shell_test_files()
        assert "test_strixhalo_wsl_reroute.sh" not in _skip_list(_shell_step_script())


class TestRunAllMatchesCi:
    """tests/run_all.sh is what a contributor runs before pushing. If it and CI
    disagree, one of them is lying about the state of the tree."""

    def test_run_all_discovers_the_directory(self):
        source = _RUN_ALL.read_text(encoding = "utf-8")
        assert 'for _t in "$TESTS_DIR"/sh/test_*.sh; do' in source

    def test_run_all_invokes_the_tests_with_bash(self):
        """tests/sh must run under bash: /bin/sh is dash on Debian/Ubuntu, where bashisms fail the suite."""
        source = _RUN_ALL.read_text(encoding = "utf-8")
        assert 'bash "$_t"' in source, "tests/run_all.sh must run tests/sh/ with bash"
        assert 'sh "$_t"' not in source.replace(
            'bash "$_t"', ""
        ), "tests/run_all.sh still invokes a discovered test with sh"
        assert 'bash "$s"' in _shell_step_script(), "Backend CI must run tests/sh/ with bash"

    def test_run_all_skips_are_a_subset_of_ci_skips(self):
        local = _skip_list(_RUN_ALL.read_text(encoding = "utf-8"))
        unexpected = local - set(_EXPECTED_CI_SKIPS)
        assert not unexpected, (
            f"tests/run_all.sh skips {sorted(unexpected)} that CI still runs: a "
            "contributor would see green locally and red on the PR"
        )


class TestBackendCiPathFilters:
    """The workflow has to fire on the files its tests assert against."""

    def _paths(self) -> set[str]:
        """Read the real trigger through the YAML parser. `on:` is a YAML 1.1
        boolean, so pyyaml keys it as True."""
        wf = _backend_ci()
        triggers = wf.get("on", wf.get(True))
        assert triggers, "Backend CI has no trigger block"
        paths = triggers["pull_request"]["paths"]
        assert paths, "Backend CI pull_request trigger has no paths filter"
        return set(paths)

    @pytest.mark.parametrize(
        "path,why",
        [
            ("install.sh", "tests/sh/* and tests/studio/install/* assert against it"),
            ("install.ps1", "the Windows/ROCm arch tables and pin allowlist live here"),
            ("studio/**", "covers studio/setup.sh, studio/setup.ps1, install_python_stack.py"),
            ("tests/**", "test-only changes must run the tests they touch"),
        ],
    )
    def test_trigger_covers(self, path, why):
        assert path in self._paths(), f"Backend CI does not run when {path} changes ({why})"

    def test_installer_change_would_trigger_the_workflow(self):
        """End to end: the exact filenames the ROCm fixes edit."""
        paths = self._paths()
        for changed in ("install.sh", "install.ps1"):
            assert changed in paths
        for changed in ("studio/setup.ps1", "studio/setup.sh", "studio/install_python_stack.py"):
            assert any(
                changed.startswith(pattern.rstrip("*").rstrip("/"))
                for pattern in paths
                if pattern.endswith("/**")
            ), f"nothing in the path filter matches {changed}"


if __name__ == "__main__":
    pytest.main([__file__, "-v"])


def _github_path_matcher(pattern: str) -> re.Pattern:
    """GitHub path filters: ** crosses directories, * and ? do not."""
    out, i = [], 0
    while i < len(pattern):
        c = pattern[i]
        if pattern.startswith("**", i):
            out.append(".*")
            i += 2
        elif c == "*":
            out.append("[^/]*")
            i += 1
        elif c == "?":
            out.append("[^/]")
            i += 1
        else:
            out.append(re.escape(c))
            i += 1
    return re.compile("^" + "".join(out) + "$")


def _workflows_running_powershell_tests():
    """Every workflow that invokes a tests/**.ps1 file, with its PR path filter."""
    found = {}
    for workflow in sorted(_WORKFLOWS.glob("*.yml")):
        text = workflow.read_text(encoding = "utf-8")
        invoked = sorted(set(re.findall(r"pwsh -NoProfile -File (tests/[^\s`\"']+\.ps1)", text)))
        if not invoked:
            continue
        parsed = yaml.safe_load(text)
        triggers = parsed.get(True, parsed.get("on", {})) or {}
        paths = (triggers.get("pull_request") or {}).get("paths")
        found[workflow.name] = (invoked, paths)
    return found


class TestGithubPathMatcher:
    """The guard below is only as good as this matcher, since a wrong one would pass everything silently."""

    @pytest.mark.parametrize(
        "pattern,path,expected",
        [
            ("tests/studio/*.ps1", "tests/studio/test_x.ps1", True),
            ("tests/studio/*.ps1", "tests/studio/nested/test_x.ps1", False),
            ("tests/studio/*.ps1", "tests/studio/test_x.py", False),
            ("tests/studio/**", "tests/studio/nested/test_x.ps1", True),
            ("studio/**", "studio/setup.ps1", True),
            ("studio/**", "tests/studio/setup.ps1", False),
            (
                "tests/studio/test_uninstall_*.ps1",
                "tests/studio/test_uninstall_arg_guard.ps1",
                True,
            ),
            ("tests/studio/test_uninstall_*.ps1", "tests/studio/test_node_decision.ps1", False),
            ("install.ps1", "install.ps1", True),
            ("install.ps1", "studio/install.ps1", False),
        ],
    )
    def test_matcher_semantics(self, pattern, path, expected):
        assert bool(_github_path_matcher(pattern).match(path)) is expected


class TestPowerShellTestsRunOnAPr:
    """A workflow's path filter must match the tests it runs, or a PR touching one never runs them."""

    def test_some_workflow_runs_powershell_tests(self):
        assert (
            _workflows_running_powershell_tests()
        ), "no workflow invokes a tests/*.ps1 file; did the invocation form change?"

    def test_every_invoked_powershell_test_triggers_its_workflow(self):
        unguarded = []
        for name, (invoked, paths) in _workflows_running_powershell_tests().items():
            if paths is None:
                continue
            matchers = [_github_path_matcher(p) for p in paths]
            for test in invoked:
                if not any(m.match(test) for m in matchers):
                    unguarded.append(f"{name} runs {test} but its paths filter never matches it")
        assert not unguarded, (
            "these PowerShell tests can break without any PR running them; add the "
            f"path (or a scoped glob) to the workflow's paths filter: {unguarded}"
        )

    def test_multi_test_steps_propagate_each_exit_code(self):
        """pwsh keeps only the last command's exit code, so check $LASTEXITCODE after each test."""
        offenders = []
        for workflow in sorted(_WORKFLOWS.glob("*.yml")):
            for block in re.findall(
                r"run: \|\n(.*?)(?=\n      [-a-zA-Z]|\Z)",
                workflow.read_text(encoding = "utf-8"),
                re.S,
            ):
                invocations = re.findall(
                    r"pwsh -NoProfile -File (tests/[^\s`\"']+\.ps1)[^\n]*\n(.*?)(?=pwsh -NoProfile -File|\Z)",
                    block,
                    re.S,
                )
                if len(invocations) < 2:
                    continue
                for test, following in invocations:
                    if "$LASTEXITCODE" not in following:
                        offenders.append(f"{workflow.name}: {test} runs without an exit-code check")
        assert not offenders, (
            "these tests can fail without failing their step; add "
            f"`if ($LASTEXITCODE) {{ exit $LASTEXITCODE }}` after each: {offenders}"
        )

    def test_every_invoked_powershell_test_exists(self):
        missing = [
            f"{name} -> {test}"
            for name, (invoked, _) in _workflows_running_powershell_tests().items()
            for test in invoked
            if not (REPO_ROOT / test).is_file()
        ]
        assert not missing, f"workflows invoke PowerShell tests that do not exist: {missing}"


class TestWindowsPowerShellStepsAreGated:
    """Steps that run powershell on a multi-OS job need an if: guard, as it is absent on Linux runners."""

    @staticmethod
    def _multi_os_jobs(doc):
        for name, job in (doc.get("jobs") or {}).items():
            runs_on = job.get("runs-on")
            labels = runs_on if isinstance(runs_on, list) else [runs_on]
            if all(
                isinstance(l, str) and l.startswith("windows-") for l in labels if l is not None
            ):
                continue
            yield name, job

    @staticmethod
    def _windows_powershell_steps(job):
        for step in job.get("steps") or []:
            if not isinstance(step, dict):
                continue
            run = str(step.get("run") or "")
            if "powershell -NoProfile" not in run:
                continue
            if "pwsh -NoProfile" in run:
                continue
            yield step

    def test_every_windows_powershell_step_is_gated_on_windows(self):
        for workflow in sorted(_WORKFLOWS.glob("*.yml")):
            doc = yaml.safe_load(workflow.read_text(encoding = "utf-8"))
            if not isinstance(doc, dict):
                continue
            for job_name, job in self._multi_os_jobs(doc):
                for step in self._windows_powershell_steps(job):
                    condition = str(step.get("if") or "")
                    assert "runner.os == 'Windows'" in condition, (
                        f"{workflow.name}: job {job_name}, step {step.get('name')!r} runs "
                        f"powershell.exe on a job that is not pinned to a Windows runner, with no "
                        f"`if: runner.os == 'Windows'`. That executable does not exist on the "
                        f"Linux and macOS legs."
                    )

    def test_the_rule_finds_the_steps_it_is_about(self):
        """prevent a vacuous pass when workflow traversal finds no matching steps."""
        found = 0
        for workflow in sorted(_WORKFLOWS.glob("*.yml")):
            doc = yaml.safe_load(workflow.read_text(encoding = "utf-8"))
            if not isinstance(doc, dict):
                continue
            for _, job in self._multi_os_jobs(doc):
                found += len(list(self._windows_powershell_steps(job)))
        assert found >= 5, (
            f"only {found} Windows PowerShell steps found on multi-OS jobs; the walk above is "
            f"looking in the wrong place"
        )


@pytest.mark.skipif(
    os.name != "posix" or not all(shutil.which(x) for x in ("bash", "xargs", "timeout")),
    reason = "the Linux shell-suite runner needs bash, xargs and timeout",
)
class TestParallelShellResults:
    def run_step(self, tmp_path, suites):
        directory = tmp_path / "tests" / "sh"
        directory.mkdir(parents = True)
        for name, body in suites.items():
            (directory / name).write_text(body)
        return subprocess.run(
            ["bash", "-c", _shell_step_script()],
            cwd = tmp_path,
            env = {**os.environ, "RUNNER_TEMP": str(tmp_path)},
            capture_output = True,
            text = True,
            timeout = 15,
        )

    def test_a_failure_does_not_hide_other_suite_results(self, tmp_path):
        result = self.run_step(
            tmp_path,
            {
                "test_a.sh": "echo first-failure; exit 7",
                "test_b.sh": "echo second-success",
                "test_c with spaces.sh": "echo third-success",
                "test_install_rollback_lifecycle.sh": "echo should-not-run; exit 99",
            },
        )
        assert result.returncode == 1
        assert "first-failure" in result.stdout
        assert "second-success" in result.stdout
        assert "third-success" in result.stdout
        assert "test_a.sh exited 7" in result.stdout
        assert "should-not-run" not in result.stdout
        assert "ran 3 shell installer test files" in result.stdout

    def test_empty_discovery_fails(self, tmp_path):
        result = self.run_step(tmp_path, {})
        assert result.returncode != 0
        assert "no shell tests discovered under tests/sh" in result.stdout

    def test_four_suites_can_reach_a_barrier_together(self, tmp_path):
        body = """touch "${0##*/}.ready"
for attempt in $(seq 1 100); do
    count=$(find . -maxdepth 1 -name '*.ready' | wc -l)
    [ "$count" -eq 4 ] && exit 0
    sleep 0.02
done
echo "the other suites never reached the barrier"
exit 1
"""
        result = self.run_step(tmp_path, {f"test_{i}.sh": body for i in range(4)})
        assert result.returncode == 0, result.stdout + result.stderr
        assert "ran 4 shell installer test files" in result.stdout