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.

6.0 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
"""Vendor installers exit on unknown options, so a dropped speed-only flag must not turn CI red."""

from __future__ import annotations

import re
import shlex
import shutil
import subprocess
import sys
from pathlib import Path

import pytest

REPO_ROOT = Path(__file__).resolve().parents[2]
SCRIPT = REPO_ROOT / ".github" / "scripts" / "agent-guides-install.sh"
HERMES_URL = "https://raw.githubusercontent.com/NousResearch/hermes-agent/main/scripts/install.sh"

pytestmark = pytest.mark.skipif(
    sys.platform == "win32" or not shutil.which("bash") or not shutil.which("curl"),
    reason = "runs the installer helpers under bash with a file:// installer",
)


def _source() -> str:
    return SCRIPT.read_text(encoding = "utf-8")


def _function(name: str) -> str:
    match = re.search(rf"^{name}\(\) \{{\n.*?^\}}\n", _source(), re.M | re.S)
    assert match, f"{name} is gone from {SCRIPT.name}"
    return match.group(0)


def _hermes_args() -> list[str]:
    """The options the hermes recipe passes, exactly as the script writes them."""
    match = re.search(
        rf'curl_bash "{re.escape(HERMES_URL)}" \\\n(.*?)\\\n',
        _source(),
        re.S,
    )
    assert match, "the hermes recipe no longer calls curl_bash on the vendor installer"
    return shlex.split(match.group(1))


def _installer(
    tmp_path: Path,
    known: list[str],
    preamble: str = "",
) -> Path:
    """A vendor installer: records its arguments, and exits on any it does not parse."""
    record = tmp_path / "argv"
    labels = "|".join(known)
    script = tmp_path / "install.sh"
    # Recorded before parsing, so a rejected run still shows what it was given.
    script.write_text(
        preamble + f"printf '%s\\n' \"$@\" > {shlex.quote(str(record))}\n"
        "while [ $# -gt 0 ]; do\n"
        '    case "$1" in\n'
        f"        {labels}) shift ;;\n"
        '        *) echo "unknown option: $1" >&2; exit 1 ;;\n'
        "    esac\n"
        "done\n",
        encoding = "utf-8",
    )
    return script


def _curl_bash(tmp_path: Path, installer: Path, args: list[str]) -> subprocess.CompletedProcess:
    helpers = tmp_path / "helpers.sh"
    # sleep is the retry backoff; stub it or three rejected attempts cost a minute.
    helpers.write_text(
        "sleep() { :; }\n" + _function("installer_args") + _function("curl_bash"),
        encoding = "utf-8",
    )
    command = (
        "set -uo pipefail\n"
        f"LOG={shlex.quote(str(tmp_path / 'install.log'))}\n"
        f". {shlex.quote(str(helpers))}\n"
        f"curl_bash {shlex.quote(installer.as_uri())} {' '.join(shlex.quote(a) for a in args)}\n"
    )
    return subprocess.run(["bash", "-c", command], capture_output = True, text = True, timeout = 60)


def _received(tmp_path: Path) -> list[str]:
    return (tmp_path / "argv").read_text(encoding = "utf-8").split()


def test_the_hermes_recipe_installs_once_the_vendor_drops_no_skills(tmp_path: Path) -> None:
    installer = _installer(
        tmp_path,
        ["--non-interactive", "--skip-setup", "--skip-browser|--no-playwright|-SkipBrowser"],
    )
    result = _curl_bash(tmp_path, installer, _hermes_args())
    assert result.returncode == 0, (tmp_path / "install.log").read_text(encoding = "utf-8")
    assert _received(tmp_path) == ["--non-interactive", "--skip-setup", "--skip-browser"]
    assert "no longer takes --no-skills" in (tmp_path / "install.log").read_text(encoding = "utf-8")


def test_an_optional_flag_the_installer_still_parses_is_passed(tmp_path: Path) -> None:
    installer = _installer(tmp_path, ["--non-interactive", "--no-skills"])
    result = _curl_bash(tmp_path, installer, ["--non-interactive", "?--no-skills"])
    assert result.returncode == 0
    assert _received(tmp_path) == ["--non-interactive", "--no-skills"]


def test_a_flag_is_not_kept_for_a_longer_one_that_starts_with_it(tmp_path: Path) -> None:
    installer = _installer(tmp_path, ["--non-interactive", "--skip-setup"])
    result = _curl_bash(tmp_path, installer, ["--non-interactive", "?--skip"])
    assert result.returncode == 0
    assert _received(tmp_path) == ["--non-interactive"]


def test_a_required_flag_the_installer_dropped_still_fails(tmp_path: Path) -> None:
    """Only the ?-marked niceties are forgiven: losing one we depend on is real drift."""
    installer = _installer(tmp_path, ["--skip-setup"])
    result = _curl_bash(tmp_path, installer, ["--non-interactive", "?--skip-setup"])
    assert result.returncode != 0
    assert _received(tmp_path) == ["--non-interactive", "--skip-setup"]


def test_a_removed_flag_still_named_outside_the_parser_is_not_kept(tmp_path: Path) -> None:
    """A comment, help text or migration note naming the old option is not a case label."""
    preamble = (
        "# removed --no-skills) in favour of always syncing skills\n"
        'usage() { echo "  --no-skills)  gone: skills always sync"; }\n'
        "NOTE='--no-skills|--quiet) were retired'\n"
    )
    installer = _installer(tmp_path, ["--non-interactive"], preamble)
    result = _curl_bash(tmp_path, installer, ["--non-interactive", "?--no-skills"])
    assert result.returncode == 0, (tmp_path / "install.log").read_text(encoding = "utf-8")
    assert _received(tmp_path) == ["--non-interactive"]


def test_a_flag_among_alternatives_is_kept(tmp_path: Path) -> None:
    installer = _installer(
        tmp_path, ["--non-interactive", "--no-playwright|--skip-browser|-SkipBrowser"]
    )
    result = _curl_bash(tmp_path, installer, ["--non-interactive", "?--skip-browser"])
    assert result.returncode == 0
    assert _received(tmp_path) == ["--non-interactive", "--skip-browser"]


def test_the_helpers_run_on_the_bash_macos_ships() -> None:
    """bash 3.2 has neither mapfile nor readarray."""
    for name in ("installer_args", "curl_bash"):
        assert not re.search(r"\b(mapfile|readarray)\b", _function(name)), name


def test_hermes_still_passes_non_interactive_as_required() -> None:
    assert "--non-interactive" in _hermes_args()