deer-flow/backend/tests/test_setup_wizard_script.py
NanHan Qing 2baadff446
fix(scripts): make check.py and setup_wizard.py --help print usage (#8152)
Both entry points ignored their arguments entirely, so the first thing a
contributor tries ran the real work: `python scripts/check.py --help` ran the
full dependency probe (and, on a host with a missing tool, exited 1), and
`python scripts/setup_wizard.py --help` entered the wizard -- which on an
existing config.yaml asks "Do you want to reconfigure?" and then rewrites
config.yaml and .env. Unknown flags were silently ignored the same way.

Parse arguments in the __main__ block, following scripts/doctor.py: main()
stays directly callable for the in-process unit tests, --help prints usage and
exits 0 before any probe or prompt, and an unknown flag exits 2 with
"unrecognized arguments". The no-argument invocations behind `make check` and
`make setup` are unchanged.

Adds subprocess CLI-contract cases in a disposable checkout next to the
existing script tests, modelled on backend/tests/test_doctor_script.py.
2026-11-12 06:15:52 +08:00

64 lines
2.0 KiB
Python

"""CLI contract for scripts/setup_wizard.py, exercised in a disposable checkout."""
from __future__ import annotations
import os
import shutil
import subprocess
import sys
from pathlib import Path
import pytest
REPO_ROOT = Path(__file__).resolve().parents[2]
@pytest.fixture
def wizard_checkout(tmp_path: Path) -> Path:
checkout = tmp_path / "checkout"
scripts_dir = checkout / "scripts"
scripts_dir.mkdir(parents=True)
shutil.copyfile(REPO_ROOT / "scripts" / "setup_wizard.py", scripts_dir / "setup_wizard.py")
return checkout
def _run_wizard(checkout: Path, *args: str) -> subprocess.CompletedProcess[str]:
env = {name: value for name, value in os.environ.items() if not name.startswith("DEER_FLOW_") and name not in {"PYTHONPATH", "PYTHONHOME"}}
return subprocess.run(
[sys.executable, str(checkout / "scripts" / "setup_wizard.py"), *args],
cwd=checkout,
env=env,
stdin=subprocess.DEVNULL,
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
check=False,
timeout=30,
)
def test_help_prints_usage_without_starting_the_wizard(wizard_checkout: Path):
result = _run_wizard(wizard_checkout, "--help")
assert result.returncode == 0, result.stderr
assert "usage:" in result.stdout
assert "Welcome to DeerFlow Setup!" not in result.stdout
assert "Non-interactive environment detected" not in result.stdout
def test_unknown_flag_exits_2_with_usage_error(wizard_checkout: Path):
result = _run_wizard(wizard_checkout, "--verbos")
assert result.returncode == 2
assert "unrecognized arguments: --verbos" in result.stderr
assert "Welcome to DeerFlow Setup!" not in result.stdout
def test_no_arguments_still_enters_the_wizard(wizard_checkout: Path):
result = _run_wizard(wizard_checkout)
# The subprocess has no tty, so the wizard reports that instead of prompting.
assert result.returncode == 1, result.stderr
assert "Non-interactive environment detected" in result.stdout