返回 last30days-skill
test_windows_printing_press_path.py
根目录 / tests / test_windows_printing_press_path.py
1 """Windows Printing Press CLIs off PATH (#1434).
2
3 - The engine appends %LOCALAPPDATA%/Programs/PrintingPress/bin to its own
4 PATH so Digg/arXiv/Techmeme/Yahoo Finance activate without a manual edit.
5 - Off-PATH prescriptions use a Windows-correct command, not bash ``export``.
6 - The setup summary names the "Go is required" cause instead of only telling
7 the user to re-run the command that just failed.
8 """
9
10 from __future__ import annotations
11
12 import os
13 from unittest import mock
14
15 import pytest
16
17 from lib import health, setup_wizard
18
19
20 class _NtOs:
21 """Delegates to the real os module but reports name == 'nt'.
22
23 Patched only in health's namespace so pathlib keeps the host's Path flavour.
24 """
25
26 name = "nt"
27
28 def __getattr__(self, attr):
29 return getattr(os, attr)
30
31
32 class _PosixOs(_NtOs):
33 name = "posix"
34
35
36 def _nt():
37 return mock.patch.object(health, "os", _NtOs())
38
39
40 def _posix():
41 return mock.patch.object(health, "os", _PosixOs())
42
43
44 def _pp_dir(tmp_path):
45 return tmp_path / "Programs" / "PrintingPress" / "bin"
46
47
48 class TestAddManagedBinDirToPath:
49 def test_appends_existing_windows_dir(self, tmp_path):
50 pp_dir = _pp_dir(tmp_path)
51 pp_dir.mkdir(parents=True)
52 with _nt(), mock.patch.dict(os.environ, {"LOCALAPPDATA": str(tmp_path), "PATH": "C:\\first"}):
53 added = health.add_managed_bin_dir_to_path()
54 path = os.environ["PATH"]
55 assert added == pp_dir
56 # Appended, so anything the user already put on PATH keeps precedence.
57 assert path == f"C:\\first{os.pathsep}{pp_dir}"
58
59 def test_idempotent(self, tmp_path):
60 _pp_dir(tmp_path).mkdir(parents=True)
61 with _nt(), mock.patch.dict(os.environ, {"LOCALAPPDATA": str(tmp_path), "PATH": "C:\\first"}):
62 health.add_managed_bin_dir_to_path()
63 assert health.add_managed_bin_dir_to_path() is None
64 assert os.environ["PATH"].count(str(_pp_dir(tmp_path))) == 1
65
66 def test_noop_when_dir_missing(self, tmp_path):
67 with _nt(), mock.patch.dict(os.environ, {"LOCALAPPDATA": str(tmp_path), "PATH": "C:\\first"}):
68 assert health.add_managed_bin_dir_to_path() is None
69 assert os.environ["PATH"] == "C:\\first"
70
71 def test_noop_on_posix(self, tmp_path):
72 _pp_dir(tmp_path).mkdir(parents=True)
73 with _posix(), \
74 mock.patch.dict(os.environ, {"LOCALAPPDATA": str(tmp_path), "PATH": "/usr/bin"}):
75 assert health.add_managed_bin_dir_to_path() is None
76 assert os.environ["PATH"] == "/usr/bin"
77
78
79 class TestPathEditPrescription:
80 def test_windows_uses_powershell_not_export(self):
81 with _nt():
82 text = health.path_edit_prescription(r"C:\Users\u\go\bin", "digg-pp-cli")
83 assert "export PATH" not in text
84 assert "SetEnvironmentVariable('Path'" in text
85 assert r";C:\Users\u\go\bin', 'User')" in text
86 assert "restart your agent host" in text
87
88 def test_posix_keeps_export(self):
89 with _posix():
90 text = health.path_edit_prescription("$HOME/.local/bin", "digg-pp-cli")
91 assert text == (
92 'add $HOME/.local/bin to PATH (e.g. export PATH="$HOME/.local/bin:$PATH") '
93 "so digg-pp-cli resolves"
94 )
95
96
97 _GO_STDERR = (
98 "npm warn something\n"
99 "Go is required to install Printing Press CLIs. Install Go with: winget install GoLang.Go\n"
100 )
101
102
103 class TestSetupSummaryNamesGo:
104 def test_digg_install_failed_names_go(self):
105 text = setup_wizard.get_setup_status_text(
106 {"digg_action": "install_failed", "digg_stderr": _GO_STDERR}
107 )
108 assert "Digg CLI install failed: Go is required" in text
109 assert "winget install GoLang.Go" in text
110 assert setup_wizard.DIGG_INSTALL_CMD in text
111
112 def test_pp_source_install_failed_names_go(self):
113 text = setup_wizard.get_setup_status_text(
114 {"pp_sources": {"arxiv": {"installed": False, "action": "install_failed", "stderr": _GO_STDERR}}}
115 )
116 assert "arXiv CLI install failed: Go is required" in text
117
118 def test_other_failure_keeps_manual_rerun_line(self):
119 text = setup_wizard.get_setup_status_text(
120 {"digg_action": "install_failed", "digg_stderr": "network unreachable"}
121 )
122 assert f"Digg CLI install failed — run `{setup_wizard.DIGG_INSTALL_CMD}` manually" in text
123
124
125 def test_windows_path_command_escapes_apostrophes():
126 """A user folder like O'Brien must not close the PowerShell string early."""
127 with _nt():
128 text = health.path_edit_example(r"C:\Users\O'Brien\go\bin")
129 assert r"';C:\Users\O''Brien\go\bin'" in text
130
131
132 def test_successful_install_puts_the_managed_dir_on_path(tmp_path, monkeypatch):
133 """Fresh Windows setup: the managed dir only exists after the install, so
134 the install itself must put it on PATH before callers check shutil.which,
135 or setup tells the user to edit PATH for no reason."""
136 monkeypatch.setattr(setup_wizard.shutil, "which", lambda name: "npx")
137 monkeypatch.setattr(
138 setup_wizard.subprocess, "run",
139 lambda *a, **k: mock.Mock(returncode=0, stderr=""),
140 )
141 with mock.patch.object(setup_wizard.health, "add_managed_bin_dir_to_path") as add:
142 action, _ = setup_wizard._run_npx_install("digg")
143 assert action == ""
144 add.assert_called_once()
145
146
147 def test_grok_off_path_finding_tells_windows_users_to_restart(tmp_path):
148 from lib import backends
149
150 off = tmp_path / "grok.exe"
151 with _nt(), mock.patch.object(backends, "which", return_value=None), \
152 mock.patch.object(health, "_off_path_binary", return_value=off):
153 finding = backends._probe_grok({})
154 assert "restart your agent host" in finding.prescription
155
156
157 @pytest.mark.parametrize("use_custom_prefix", [False, True])
158 def test_doctor_and_setup_find_off_path_npm_launchers(tmp_path, monkeypatch, use_custom_prefix):
159 from lib import backends
160
161 appdata = tmp_path / "Roaming"
162 default = appdata / "npm"
163 default.mkdir(parents=True)
164 monkeypatch.setenv("APPDATA", str(appdata))
165 monkeypatch.delenv("NPM_CONFIG_PREFIX", raising=False)
166 for name in ("grok", "brightdata"):
167 (default / f"{name}.cmd").touch()
168 prefix = default
169 if use_custom_prefix:
170 prefix = tmp_path / "custom prefix"
171 prefix.mkdir()
172 monkeypatch.setenv("NPM_CONFIG_PREFIX", str(prefix))
173 for name in ("grok", "brightdata"):
174 (prefix / f"{name}.cmd").touch()
175 monkeypatch.setattr(health.shutil, "which", lambda name: None)
176 monkeypatch.setattr(backends, "which", lambda name: None)
177 monkeypatch.setattr(setup_wizard.brightdata, "has_credentials", lambda config: True)
178 with _nt():
179 grok = health._probe_dependency_uncached("grok", 1)
180 backend = backends._probe_grok({})
181 brightdata = setup_wizard.brightdata_status({})
182 assert grok.off_path and grok.status == health.MISSING
183 assert str(prefix / "grok.cmd") in grok.detail
184 assert str(prefix / "grok.cmd") in backend.detail
185 assert "restart your agent host" in backend.prescription
186 assert brightdata["action"] == "installed_off_path"
187 assert brightdata["path"] == str(prefix / "brightdata.cmd")
188 assert not brightdata["engine_active"]
189
190
191 def test_windows_npm_discovery_ignores_non_launchers_and_relative_prefixes(tmp_path, monkeypatch):
192 default = tmp_path / "npm"
193 default.mkdir()
194 (default / "grok").touch()
195 (default / "grok.ps1").touch()
196 relative = tmp_path / "relative"
197 relative.mkdir()
198 (relative / "grok.cmd").touch()
199 monkeypatch.chdir(tmp_path)
200 monkeypatch.setenv("APPDATA", str(tmp_path))
201 monkeypatch.setenv("NPM_CONFIG_PREFIX", "relative")
202 with _nt():
203 assert health.windows_npm_launcher("grok") is None
204 (default / "grok.cmd").touch()
205 with _posix():
206 assert health.windows_npm_launcher("grok") is None
207
207 lines PYTHON