84 lines
3.5 KiB
Python
84 lines
3.5 KiB
Python
"""tools_check: the worker page's Setup checklist in /health — checked in the
|
|
background, cached, never a token."""
|
|
|
|
import time
|
|
|
|
import tools_check as tc
|
|
|
|
|
|
def _reset(monkeypatch, tools=None, at=0.0):
|
|
monkeypatch.setattr(tc, "_state", {"at": at, "tools": tools, "running": False})
|
|
|
|
|
|
def test_public_is_none_first_then_cached(monkeypatch):
|
|
calls = []
|
|
monkeypatch.setattr(tc, "collect", lambda: calls.append(1) or {"gh": {"installed": True}})
|
|
_reset(monkeypatch)
|
|
assert tc.public() is None # never blocks /health
|
|
for _ in range(50):
|
|
if tc._state["tools"] is not None:
|
|
break
|
|
time.sleep(0.02)
|
|
out = tc.public()
|
|
assert out["tools"] == {"gh": {"installed": True}}
|
|
assert tc.public() == out and len(calls) == 1 # fresh: no new check
|
|
|
|
|
|
def test_stale_result_is_served_while_refreshing(monkeypatch):
|
|
started = []
|
|
monkeypatch.setattr(tc.threading, "Thread",
|
|
lambda **kw: type("T", (), {"start": lambda self: started.append(kw)})())
|
|
_reset(monkeypatch, tools={"gh": {"signedIn": False}}, at=time.time() - tc.TTL_S - 1)
|
|
assert tc.public()["tools"] == {"gh": {"signedIn": False}}
|
|
assert tc.public()["tools"] == {"gh": {"signedIn": False}}
|
|
assert len(started) == 1 # one refresh at a time
|
|
|
|
|
|
def test_gh_login_and_no_raw_output(monkeypatch):
|
|
monkeypatch.setattr(tc, "_which", lambda n: "/usr/bin/gh")
|
|
|
|
def run(args, timeout=tc.TIMEOUT_S):
|
|
if args[1] == "--version":
|
|
return 0, "gh version 2.80.0 (2026-01-01)\nhttps://github.com/cli/cli\n"
|
|
return 0, ("github.com\n ✓ Logged in to github.com account octocat (keyring)\n"
|
|
" - Token: gho_************************************\n")
|
|
monkeypatch.setattr(tc, "_run", run)
|
|
out = tc.check_gh()
|
|
assert out == {"installed": True, "path": "/usr/bin/gh",
|
|
"version": "gh version 2.80.0 (2026-01-01)",
|
|
"signedIn": True, "login": "octocat", "managed": False}
|
|
monkeypatch.setattr(tc, "_run", lambda a, timeout=0: (1, "You are not logged into any GitHub hosts."))
|
|
assert tc.check_gh()["signedIn"] is False
|
|
|
|
|
|
def test_missing_tools(monkeypatch):
|
|
monkeypatch.setattr(tc, "_which", lambda n: None)
|
|
assert tc.check_gh() == {"installed": False, "path": None, "signedIn": False}
|
|
|
|
|
|
def test_askform_shim_is_not_the_tool(monkeypatch, tmp_path):
|
|
shim = tmp_path / "ask-form"
|
|
shim.write_text("#!/bin/sh\necho \"ask-form: the ask-form tool isn't installed on this machine.\"\n")
|
|
monkeypatch.setattr(tc, "_which", lambda n: str(shim))
|
|
assert tc._askform_bin() is None
|
|
shim.write_text("\x7fELF binary")
|
|
assert tc._askform_bin() == str(shim)
|
|
|
|
|
|
def test_a_broken_check_reads_as_unknown(monkeypatch):
|
|
def boom():
|
|
raise RuntimeError("nope")
|
|
monkeypatch.setattr(tc, "checks", lambda: {"gh": boom})
|
|
assert tc.collect() == {"gh": {"installed": None, "signedIn": None, "error": "nope"}}
|
|
|
|
|
|
def test_a_driver_without_setup_check_gets_a_plain_row(monkeypatch):
|
|
import tools_install
|
|
monkeypatch.setattr(tools_install, "harness_tools", lambda: [
|
|
{"tool": "zz", "harness": "zz", "order": 1, "guestAllowed": False, "update": False,
|
|
"installerEnv": None, "recipe": "/x/setup.sh", "driver": object()}])
|
|
monkeypatch.setattr(tools_install, "guest_computer", lambda: False)
|
|
monkeypatch.setattr(tc, "_which", lambda n: None)
|
|
assert list(tc.checks()) == ["zz", "gh", "ask-form"]
|
|
assert tc.checks()["zz"]() == {"installed": False, "path": None, "signedIn": None}
|