|
23 | 23 | posix_only = pytest.mark.skipif(sys.platform == "win32", reason="fake qsv is a POSIX script") |
24 | 24 |
|
25 | 25 |
|
26 | | -def _fake_qsv(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, body: str) -> Path: |
27 | | - """Put a Python script named ``qsv`` first on PATH, ahead of any real qsv.""" |
| 26 | +QSV_CAPABILITIES = { |
| 27 | + "binary": "qsv", |
| 28 | + "version": "25.0.0", |
| 29 | + "commands": ["count", "describegpt", "frequency", "stats"], |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +def _fake_qsv( |
| 34 | + tmp_path: Path, |
| 35 | + monkeypatch: pytest.MonkeyPatch, |
| 36 | + body: str, |
| 37 | + capabilities: dict | None = QSV_CAPABILITIES, |
| 38 | +) -> Path: |
| 39 | + """Put a Python script named ``qsv`` first on PATH, ahead of any real qsv. |
| 40 | +
|
| 41 | + It answers ``--capabilities`` with ``capabilities`` (qsv-client checks that before the |
| 42 | + first run); pass ``None`` to leave it to ``body``, like a qsv older than 25.0.0. |
| 43 | + """ |
28 | 44 | fake = tmp_path / "bin" / "qsv" |
29 | 45 | fake.parent.mkdir(exist_ok=True) |
30 | | - fake.write_text(f"#!{sys.executable}\nimport json, os, sys, time\n{body}") |
| 46 | + caps = ( |
| 47 | + "" |
| 48 | + if capabilities is None |
| 49 | + else f"if sys.argv[1:] == ['--capabilities']:\n" |
| 50 | + f" print({json.dumps(capabilities)!r})\n" |
| 51 | + f" sys.exit(0)\n" |
| 52 | + ) |
| 53 | + fake.write_text(f"#!{sys.executable}\nimport json, os, sys, time\n{caps}{body}") |
31 | 54 | fake.chmod(0o755) |
32 | 55 | monkeypatch.setenv("PATH", f"{fake.parent}:{os.environ['PATH']}") |
33 | 56 | monkeypatch.delenv("QSV_BIN", raising=False) |
@@ -160,22 +183,28 @@ async def test_describegpt_failure_on_a_binary_without_it_says_so(tmp_path, monk |
160 | 183 | _fake_qsv( |
161 | 184 | tmp_path, |
162 | 185 | monkeypatch, |
163 | | - "arg = sys.argv[1]\n" |
164 | | - "if arg == '--version':\n" |
165 | | - " print('qsvlite 24.0.0-standard-16-16; (aarch64-apple-darwin compiled with Rust 1.99)')\n" |
166 | | - "elif arg == '--list':\n" |
167 | | - " print('Installed commands (2):')\n" |
168 | | - " print(' count Count records')\n" |
169 | | - " print(' stats Infer data types')\n" |
170 | | - "else:\n" |
171 | | - " sys.stderr.write('Invalid arguments.\\n')\n" |
172 | | - " sys.exit(1 if arg == '--capabilities' else 2)\n", |
| 186 | + "sys.stderr.write('Invalid arguments.\\n')\nsys.exit(2)\n", |
| 187 | + capabilities={"binary": "qsvlite", "version": "25.0.0", "commands": ["count", "stats"]}, |
173 | 188 | ) |
174 | 189 | out = tmp_path / "qsv_dict.json" |
175 | 190 |
|
176 | 191 | assert await run_qsv_describegpt(tmp_path / "data.csv", KEY, out) is None |
177 | 192 |
|
178 | 193 | printed = capsys.readouterr().out |
179 | 194 | assert "has no describegpt" in printed |
180 | | - assert "qsvlite 24.0.0" in printed |
| 195 | + assert "qsvlite 25.0.0" in printed |
181 | 196 | assert not out.exists() |
| 197 | + |
| 198 | + |
| 199 | +@posix_only |
| 200 | +async def test_too_old_qsv_returns_none_instead_of_raising(tmp_path, monkeypatch, capsys): |
| 201 | + # qsv before 25.0.0 rejects --capabilities, and qsv-client refuses to run it. |
| 202 | + _fake_qsv( |
| 203 | + tmp_path, |
| 204 | + monkeypatch, |
| 205 | + "sys.stderr.write(\"Unknown flag: '--capabilities'\\n\")\nsys.exit(1)\n", |
| 206 | + capabilities=None, |
| 207 | + ) |
| 208 | + |
| 209 | + assert await run_qsv_count(tmp_path / "data.csv") is None |
| 210 | + assert "QsvVersionError" in capsys.readouterr().out |
0 commit comments