Debian Codegraph @

tests/unit_tests/test_environment.py

raw
1 from __future__ import annotations
2
3 import json
4 import sys
5
6 import pytest
7
8 from harlequin.environment import (
9 UNKNOWN,
10 adapter_facts,
11 install_facts,
12 runtime_report,
13 terminal_facts,
14 )
15
16
17 def test_runtime_report_is_json_serializable() -> None:
18 """A crash report and `--info` both render it as JSON."""
19 report = runtime_report()
20 assert json.loads(json.dumps(report)) == report
21 assert set(report) == {"version", "python", "platform", "terminal", "install"}
22 assert report["python"]["executable"] == sys.executable
23
24
25 def test_runtime_report_imports_no_adapter() -> None:
26 """It runs inside a crash handler, where importing the world is a fresh crash."""
27 loaded_before = set(sys.modules)
28 runtime_report()
29 assert not {name for name in set(sys.modules) - loaded_before if "adapter" in name}
30
31
32 def test_terminal_facts_report_that_a_session_is_remote_and_nothing_more(
33 monkeypatch: pytest.MonkeyPatch,
34 ) -> None:
35 """Whether the session is remote explains a bug; where it came from does not."""
36 monkeypatch.setenv("SSH_CONNECTION", "10.1.2.3 55555 10.1.2.4 22")
37
38 facts = terminal_facts()
39
40 assert facts["ssh"] is True
41 assert "10.1.2.3" not in json.dumps(facts)
42
43
44 def test_terminal_facts_when_the_environment_says_nothing(
45 monkeypatch: pytest.MonkeyPatch,
46 ) -> None:
47 for name in (
48 "TERM",
49 "TERM_PROGRAM",
50 "TERM_PROGRAM_VERSION",
51 "COLORTERM",
52 "SHELL",
53 "COMSPEC",
54 "WSL_DISTRO_NAME",
55 "SSH_CONNECTION",
56 "SSH_TTY",
57 ):
58 monkeypatch.delenv(name, raising=False)
59
60 facts = terminal_facts()
61
62 assert facts["term"] is None
63 assert facts["ssh"] is False
64 assert "x" in facts["size"]
65
66
67 def test_install_facts_answer_the_bug_templates_checkbox() -> None:
68 facts = install_facts()
69 assert facts["installer"] in (None, "pip", "uv", "pipx", "hatch")
70 assert isinstance(facts["in_venv"], bool)
71
72
73 def test_adapter_facts_report_what_an_adapter_declares() -> None:
74 facts = adapter_facts("duckdb")
75
76 assert list(facts) == ["duckdb"]
77 assert facts["duckdb"]["error"] is None
78 assert facts["duckdb"]["capabilities"]["implements_cancel"] is True
79
80
81 def test_adapter_facts_never_guess_false_about_an_adapter_that_will_not_import(
82 monkeypatch: pytest.MonkeyPatch,
83 ) -> None:
84 from harlequin.exception import HarlequinConfigError
85
86 def _raise(name: str) -> None:
87 raise HarlequinConfigError("boom")
88
89 monkeypatch.setattr("harlequin.plugins.load_adapter", _raise)
90
91 facts = adapter_facts("duckdb")
92
93 assert facts["duckdb"]["capabilities"] == UNKNOWN
94 assert facts["duckdb"]["error"] == "boom"