| 1 |
"""Run-time guards for the import hygiene the headless CLI depends on. |
| 2 |
|
| 3 |
The import-linter contracts in `pyproject.toml` read the static graph, so they |
| 4 |
cannot tell a module-scope import from one deferred into the function that needs |
| 5 |
it. These tests run the real thing in a subprocess and look at `sys.modules`, |
| 6 |
which is the only way to prove a deferral actually defers. |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
import subprocess |
| 12 |
from pathlib import Path |
| 13 |
from typing import Callable |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
# Importing any of these must not drag in the TUI. They are the modules an |
| 18 |
# adapter -- or a headless front end -- reaches for. |
| 19 |
HEADLESS_IMPORTS = [ |
| 20 |
"import harlequin", |
| 21 |
"import harlequin.adapter", |
| 22 |
"import harlequin.autocomplete", |
| 23 |
"import harlequin.catalog", |
| 24 |
"import harlequin.config", |
| 25 |
"import harlequin.exception", |
| 26 |
"import harlequin.export", |
| 27 |
"import harlequin.hsql", |
| 28 |
"import harlequin.hsql.cli", |
| 29 |
"import harlequin.keymap", |
| 30 |
"import harlequin.layout", |
| 31 |
"import harlequin.navigate", |
| 32 |
"import harlequin.options", |
| 33 |
"import harlequin.plugins", |
| 34 |
"import harlequin.redact", |
| 35 |
"import harlequin.query", |
| 36 |
"import harlequin.ssh", |
| 37 |
"import harlequin.statements", |
| 38 |
"import harlequin.transaction_mode", |
| 39 |
"import harlequin_duckdb", |
| 40 |
"import harlequin_sqlite", |
| 41 |
"from textual_fastdatatable.backend import create_backend", |
| 42 |
] |
| 43 |
|
| 44 |
FORBIDDEN = ("textual", "questionary", "prompt_toolkit", "sqlfmt", "rich") |
| 45 |
"""`rich` is here for the same reason as the rest: nothing headless renders. |
| 46 |
|
| 47 |
It is the one that arrives sideways rather than from an import of ours -- |
| 48 |
`textual_fastdatatable.backend` pulled it in until 0.17.1 deferred it -- so the |
| 49 |
guard is worth more here than the import-linter contracts, which only see this |
| 50 |
repo's own graph. |
| 51 |
""" |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.parametrize("statement", HEADLESS_IMPORTS) |
| 55 |
def test_headless_imports_do_not_load_the_tui( |
| 56 |
statement: str, run_python: Callable[[str], subprocess.CompletedProcess[str]] |
| 57 |
) -> None: |
| 58 |
proc = run_python( |
| 59 |
f"{statement}\n" |
| 60 |
"import sys\n" |
| 61 |
f"print(','.join(m for m in {FORBIDDEN!r} if m in sys.modules))\n" |
| 62 |
) |
| 63 |
leaked = [m for m in proc.stdout.strip().split(",") if m] |
| 64 |
assert not leaked, f"{statement!r} imported {leaked}" |
| 65 |
|
| 66 |
|
| 67 |
def test_importing_the_completers_does_not_parse_sql( |
| 68 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 69 |
) -> None: |
| 70 |
"""Every adapter imports the completers; only the Query Editor parses SQL. |
| 71 |
|
| 72 |
`find_symbols()` defers `harlequin.statements`, so loading the grammar stays |
| 73 |
off the path of everything that never reads a buffer. |
| 74 |
""" |
| 75 |
proc = run_python( |
| 76 |
"import harlequin.autocomplete\n" |
| 77 |
"import sys\n" |
| 78 |
"print(','.join(m for m in ('tree_sitter', 'tree_sitter_sql') " |
| 79 |
"if m in sys.modules))\n" |
| 80 |
) |
| 81 |
assert not proc.stdout.strip(), f"importing the completers loaded {proc.stdout}" |
| 82 |
|
| 83 |
|
| 84 |
@pytest.mark.parametrize("argv", [["--help"], ["--version"]]) |
| 85 |
def test_building_the_hsql_command_imports_no_adapter( |
| 86 |
argv: list[str], run_python: Callable[[str], subprocess.CompletedProcess[str]] |
| 87 |
) -> None: |
| 88 |
"""The point of hsql's two-phase parse, and it regresses quietly. |
| 89 |
|
| 90 |
Neither names an adapter: `--help` renders the adapter-agnostic surface and |
| 91 |
`--version` prints a string that does not depend on one, so neither must pay |
| 92 |
`ep.load()` for every adapter installed to do it. |
| 93 |
""" |
| 94 |
proc = run_python( |
| 95 |
"import sys\n" |
| 96 |
"from harlequin.hsql.cli import build_cli\n" |
| 97 |
f"build_cli({argv!r})\n" |
| 98 |
"print(','.join(m for m in sys.modules if m.startswith('harlequin_')))\n" |
| 99 |
) |
| 100 |
loaded = [m for m in proc.stdout.strip().split(",") if m] |
| 101 |
assert not loaded, f"building `hsql {' '.join(argv)}` imported {loaded}" |
| 102 |
|
| 103 |
|
| 104 |
def test_building_the_ide_command_imports_only_the_named_adapter( |
| 105 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 106 |
) -> None: |
| 107 |
"""The IDE's half of the same two-phase parse. |
| 108 |
|
| 109 |
An invocation opens the IDE with exactly one adapter, so building its |
| 110 |
command imports exactly one. Every other installed adapter used to be |
| 111 |
`ep.load()`ed on the way to a connection that would never use it, which is |
| 112 |
~200ms with four of them and grows with the fifth. |
| 113 |
""" |
| 114 |
proc = run_python( |
| 115 |
"import sys\n" |
| 116 |
"from harlequin.cli import build_cli\n" |
| 117 |
"build_cli(['-a', 'sqlite', ':memory:'])\n" |
| 118 |
"print(','.join(sorted({m.split('.')[0] for m in sys.modules " |
| 119 |
"if m.startswith('harlequin_')})))\n" |
| 120 |
) |
| 121 |
assert proc.stdout.strip() == "harlequin_sqlite" |
| 122 |
|
| 123 |
|
| 124 |
def test_the_ides_help_still_documents_every_adapter( |
| 125 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 126 |
) -> None: |
| 127 |
"""The deliberate exception, and the one path that still pays for all of them. |
| 128 |
|
| 129 |
`harlequin --help` lists what every installed adapter takes, because an |
| 130 |
option a reader cannot discover is one they cannot use. The trade is the |
| 131 |
right way round: help is not the invocation anyone waits on. |
| 132 |
""" |
| 133 |
proc = run_python( |
| 134 |
"import sys\n" |
| 135 |
"from harlequin.cli import build_cli\n" |
| 136 |
"build_cli(['--help'])\n" |
| 137 |
"print(','.join(sorted({m.split('.')[0] for m in sys.modules " |
| 138 |
"if m.startswith('harlequin_')})))\n" |
| 139 |
) |
| 140 |
# a superset: whatever else the machine running this has installed |
| 141 |
assert {"harlequin_duckdb", "harlequin_sqlite"} <= set( |
| 142 |
proc.stdout.strip().split(",") |
| 143 |
) |
| 144 |
|
| 145 |
|
| 146 |
# Not TUI modules, but slow ones on the path every start-up walks, each kept off |
| 147 |
# it by a deferral the static graph cannot see. The cost is the reason: tomlkit |
| 148 |
# parses a 10KB pyproject.toml ~30x slower than tomllib, and wcwidth costs |
| 149 |
# ~25ms to import for a fast path that returns without calling it. |
| 150 |
DEFERRED_ON_STARTUP = ("tomlkit", "wcwidth") |
| 151 |
|
| 152 |
|
| 153 |
def test_reading_config_does_not_import_tomlkit( |
| 154 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 155 |
) -> None: |
| 156 |
"""Reads go through tomllib; tomlkit is for writes, which are rare. |
| 157 |
|
| 158 |
Start-up reads config -- including whatever `pyproject.toml` is in the |
| 159 |
working directory -- so an eager tomlkit here is paid by every invocation. |
| 160 |
""" |
| 161 |
proc = run_python( |
| 162 |
"import sys\n" |
| 163 |
"from harlequin.config import load_config\n" |
| 164 |
"load_config(config_path=None)\n" |
| 165 |
"print('tomlkit' in sys.modules)\n" |
| 166 |
) |
| 167 |
assert proc.stdout.strip() == "False" |
| 168 |
|
| 169 |
|
| 170 |
def test_an_all_ascii_run_defers_the_slow_startup_imports( |
| 171 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 172 |
) -> None: |
| 173 |
"""A full `hsql -c 'select 1'`, end to end, in a clean interpreter.""" |
| 174 |
proc = run_python( |
| 175 |
"import sys\n" |
| 176 |
"sys.argv = ['hsql', '-c', 'select 1']\n" |
| 177 |
"from harlequin.hsql import main\n" |
| 178 |
"try:\n" |
| 179 |
" main()\n" |
| 180 |
"except SystemExit:\n" |
| 181 |
" pass\n" |
| 182 |
f"print(','.join(m for m in {DEFERRED_ON_STARTUP!r} if m in sys.modules), " |
| 183 |
"file=sys.stderr)\n" |
| 184 |
) |
| 185 |
leaked = [m for m in proc.stderr.strip().split(",") if m] |
| 186 |
assert not leaked, f"`hsql -c 'select 1'` imported {leaked}" |
| 187 |
|
| 188 |
|
| 189 |
def test_non_ascii_output_still_measures_correctly( |
| 190 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 191 |
) -> None: |
| 192 |
"""The other half of the wcwidth deferral: it still loads when needed. |
| 193 |
|
| 194 |
A deferral that never fires on the path that needs it would be a silent |
| 195 |
misalignment bug rather than a slow import, so assert the column is padded |
| 196 |
to the width the ideographs actually occupy. |
| 197 |
""" |
| 198 |
proc = run_python( |
| 199 |
"import sys\n" |
| 200 |
"sys.argv = ['hsql', '-c', \"select '\\u4e2d\\u6587' as a, 'ab' as b\"]\n" |
| 201 |
"from harlequin.hsql import main\n" |
| 202 |
"try:\n" |
| 203 |
" main()\n" |
| 204 |
"except SystemExit:\n" |
| 205 |
" pass\n" |
| 206 |
"print('wcwidth' in sys.modules, file=sys.stderr)\n" |
| 207 |
) |
| 208 |
assert proc.stderr.strip() == "True" |
| 209 |
# two ideographs are four cells, so column `a` is four wide and its rule is |
| 210 |
# six. Measured with `len()` instead it would be two wide and rule four -- |
| 211 |
# which is the misalignment this asserts against, so match the whole line. |
| 212 |
assert "------+----" in proc.stdout |
| 213 |
|
| 214 |
|
| 215 |
@pytest.mark.parametrize( |
| 216 |
"mode,forbidden", |
| 217 |
[ |
| 218 |
# a document, so it needs neither the row machinery nor a database. |
| 219 |
# It does write TOML, so tomlkit is not on its list |
| 220 |
("show", ("duckdb", "pyarrow")), |
| 221 |
# rows, so it pays for pyarrow -- and for no database driver, because |
| 222 |
# `ResultSet.text_columns()` returns strings unchanged, and no tomlkit, |
| 223 |
# because a listing is not a TOML document |
| 224 |
("list-profiles", ("duckdb", "tomlkit")), |
| 225 |
], |
| 226 |
) |
| 227 |
def test_the_config_modes_import_no_database( |
| 228 |
mode: str, |
| 229 |
forbidden: tuple[str, ...], |
| 230 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 231 |
) -> None: |
| 232 |
"""Neither mode reaches for a database driver of its own accord. |
| 233 |
|
| 234 |
There is no config file here, so `show` has no profile to redact and no |
| 235 |
adapter to ask about one -- which is the floor this asserts. What it costs |
| 236 |
when there *is* a profile is the test below. |
| 237 |
`--format csv|json|parquet` is the exception a caller asks for by name: |
| 238 |
those are written by `harlequin.export`, which serializes through duckdb or |
| 239 |
pyarrow whatever produced the rows. |
| 240 |
""" |
| 241 |
proc = run_python( |
| 242 |
"import sys\n" |
| 243 |
f"sys.argv = ['hsql', '--config', {mode!r}]\n" |
| 244 |
"from harlequin.hsql import main\n" |
| 245 |
"try:\n" |
| 246 |
" main()\n" |
| 247 |
"except SystemExit:\n" |
| 248 |
" pass\n" |
| 249 |
f"print(','.join(m for m in {forbidden!r} if m in sys.modules), " |
| 250 |
"file=sys.stderr)\n" |
| 251 |
"print(','.join(m for m in sys.modules if m.startswith('harlequin_')), " |
| 252 |
"file=sys.stderr)\n" |
| 253 |
) |
| 254 |
leaked = [m for m in proc.stderr.strip().replace("\n", ",").split(",") if m] |
| 255 |
assert not leaked, f"`hsql --config {mode}` imported {leaked}" |
| 256 |
|
| 257 |
|
| 258 |
def test_the_catalog_mode_imports_only_the_adapter_it_connects_with( |
| 259 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 260 |
) -> None: |
| 261 |
"""A listing is rows that never went through a database, so nothing casts. |
| 262 |
|
| 263 |
`ResultSet.text_columns()` returns strings unchanged, which is what keeps |
| 264 |
duckdb out of a listing from any other adapter -- it is ~85ms, on the one |
| 265 |
mode whose whole job is a fast look at what is there. |
| 266 |
""" |
| 267 |
proc = run_python( |
| 268 |
"import sys\n" |
| 269 |
"sys.argv = ['hsql', '-a', 'sqlite', '--catalog', ':memory:']\n" |
| 270 |
"from harlequin.hsql import main\n" |
| 271 |
"try:\n" |
| 272 |
" main()\n" |
| 273 |
"except SystemExit:\n" |
| 274 |
" pass\n" |
| 275 |
"print(','.join(sorted({m.split('.')[0] for m in sys.modules " |
| 276 |
"if m.startswith('harlequin_')})), file=sys.stderr)\n" |
| 277 |
"print(','.join(m for m in ('duckdb', 'tomlkit') if m in sys.modules), " |
| 278 |
"file=sys.stderr)\n" |
| 279 |
) |
| 280 |
adapters, forbidden = proc.stderr.split("\n")[:2] |
| 281 |
assert adapters == "harlequin_sqlite" |
| 282 |
assert not forbidden |
| 283 |
|
| 284 |
|
| 285 |
def test_the_search_mode_imports_only_the_adapter_it_connects_with( |
| 286 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 287 |
) -> None: |
| 288 |
"""A search is rows that never went through a database, so nothing casts.""" |
| 289 |
proc = run_python( |
| 290 |
"import sys\n" |
| 291 |
"sys.argv = " |
| 292 |
"['hsql', '-a', 'sqlite', '--catalog-search', 'orders', ':memory:']\n" |
| 293 |
"from harlequin.hsql import main\n" |
| 294 |
"try:\n" |
| 295 |
" main()\n" |
| 296 |
"except SystemExit:\n" |
| 297 |
" pass\n" |
| 298 |
"print(','.join(sorted({m.split('.')[0] for m in sys.modules " |
| 299 |
"if m.startswith('harlequin_')})), file=sys.stderr)\n" |
| 300 |
"print(','.join(m for m in ('duckdb', 'tomlkit') if m in sys.modules), " |
| 301 |
"file=sys.stderr)\n" |
| 302 |
) |
| 303 |
adapters, forbidden = proc.stderr.split("\n")[:2] |
| 304 |
assert adapters == "harlequin_sqlite" |
| 305 |
assert not forbidden |
| 306 |
|
| 307 |
|
| 308 |
def test_a_run_does_not_import_the_catalog_walk( |
| 309 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 310 |
) -> None: |
| 311 |
"""The other half of the mode-per-module rule: a query pays for no mode.""" |
| 312 |
proc = run_python( |
| 313 |
"import sys\n" |
| 314 |
"sys.argv = ['hsql', '-c', 'select 1']\n" |
| 315 |
"from harlequin.hsql import main\n" |
| 316 |
"try:\n" |
| 317 |
" main()\n" |
| 318 |
"except SystemExit:\n" |
| 319 |
" pass\n" |
| 320 |
"print(','.join(m for m in sys.modules " |
| 321 |
"if m.startswith('harlequin.hsql.modes.') " |
| 322 |
"or m == 'harlequin.navigate'), file=sys.stderr)\n" |
| 323 |
) |
| 324 |
assert not proc.stderr.strip() |
| 325 |
|
| 326 |
|
| 327 |
def test_config_validate_imports_only_the_adapters_its_profiles_name( |
| 328 |
tmp_path: Path, run_python: Callable[[str], subprocess.CompletedProcess[str]] |
| 329 |
) -> None: |
| 330 |
"""The one mode here that pays for adapters, paying for as few as it can. |
| 331 |
|
| 332 |
Checking a profile's options against the ones its adapter declares takes |
| 333 |
importing that adapter -- and only that one. A mode that imported every |
| 334 |
installed adapter to check a config naming one would cost a caller with |
| 335 |
four of them four times what the answer is worth. It is rows rather than a |
| 336 |
document, so it pays for pyarrow and not for tomlkit, like `list-profiles`. |
| 337 |
|
| 338 |
`tmp_path` is the subprocess's cwd and its home, so this file is the whole |
| 339 |
of the config it discovers. |
| 340 |
""" |
| 341 |
(tmp_path / ".harlequin.toml").write_text( |
| 342 |
'[profiles.lite]\nadapter = "sqlite"\nread_only = true\n' |
| 343 |
) |
| 344 |
proc = run_python( |
| 345 |
"import sys\n" |
| 346 |
"sys.argv = ['hsql', '--config', 'validate']\n" |
| 347 |
"from harlequin.hsql import main\n" |
| 348 |
"try:\n" |
| 349 |
" main()\n" |
| 350 |
"except SystemExit:\n" |
| 351 |
" pass\n" |
| 352 |
"print(','.join(sorted({m.split('.')[0] for m in sys.modules " |
| 353 |
"if m.startswith('harlequin_')})), file=sys.stderr)\n" |
| 354 |
"print(','.join(m for m in ('duckdb', 'tomlkit') if m in sys.modules), " |
| 355 |
"file=sys.stderr)\n" |
| 356 |
) |
| 357 |
# two lines, and the second is empty when nothing forbidden was imported |
| 358 |
adapters, forbidden = proc.stderr.split("\n")[:2] |
| 359 |
assert adapters == "harlequin_sqlite" |
| 360 |
assert not forbidden |
| 361 |
|
| 362 |
|
| 363 |
def test_config_schema_writes_a_document_and_reads_no_file( |
| 364 |
tmp_path: Path, run_python: Callable[[str], subprocess.CompletedProcess[str]] |
| 365 |
) -> None: |
| 366 |
"""The second mode that pays for adapters on purpose, and pays for nothing else. |
| 367 |
|
| 368 |
Describing the options an adapter declares means importing every installed |
| 369 |
adapter, as `--spec` does. It is a document rather than rows, so it never |
| 370 |
reaches the execution core, and it reads no config file, so no tomlkit -- a |
| 371 |
schema says what a config file may hold whether or not this machine has |
| 372 |
one. |
| 373 |
|
| 374 |
`tmp_path` is the subprocess's cwd and its home, so this file is the whole |
| 375 |
of the config it would discover. |
| 376 |
""" |
| 377 |
(tmp_path / ".harlequin.toml").write_text('[profiles.lite]\nadapter = "sqlite"\n') |
| 378 |
proc = run_python( |
| 379 |
"import sys\n" |
| 380 |
"sys.argv = ['hsql', '--config', 'schema']\n" |
| 381 |
"from harlequin.hsql import main\n" |
| 382 |
"try:\n" |
| 383 |
" main()\n" |
| 384 |
"except SystemExit:\n" |
| 385 |
" pass\n" |
| 386 |
"print(','.join(sorted({m.split('.')[0] for m in sys.modules " |
| 387 |
"if m.startswith('harlequin_')})), file=sys.stderr)\n" |
| 388 |
"print(','.join(m for m in ('harlequin.query', 'tomlkit') " |
| 389 |
"if m in sys.modules), file=sys.stderr)\n" |
| 390 |
) |
| 391 |
adapters, forbidden = proc.stderr.split("\n")[:2] |
| 392 |
# both of the bundled ones, where every other mode here costs at most one |
| 393 |
assert {"harlequin_duckdb", "harlequin_sqlite"} <= set(adapters.split(",")) |
| 394 |
assert not forbidden |
| 395 |
|
| 396 |
|
| 397 |
def test_config_init_imports_tomlkit_and_one_adapter( |
| 398 |
tmp_path: Path, run_python: Callable[[str], subprocess.CompletedProcess[str]] |
| 399 |
) -> None: |
| 400 |
"""The one mode that writes a file, and so the one that pays for tomlkit. |
| 401 |
|
| 402 |
It pays for one adapter too, because the options it writes into a profile |
| 403 |
are the ones that adapter declares -- and for nothing else: it writes a |
| 404 |
file rather than rows, and connects to nothing. |
| 405 |
|
| 406 |
`tmp_path` is the subprocess's cwd and its home, so the file it writes is |
| 407 |
the only config there is. |
| 408 |
""" |
| 409 |
proc = run_python( |
| 410 |
"import sys\n" |
| 411 |
"sys.argv = ['hsql', '--config', 'init', '-P', 'lite', '-a', 'sqlite']\n" |
| 412 |
"from harlequin.hsql import main\n" |
| 413 |
"try:\n" |
| 414 |
" main()\n" |
| 415 |
"except SystemExit:\n" |
| 416 |
" pass\n" |
| 417 |
"print(','.join(sorted({m.split('.')[0] for m in sys.modules " |
| 418 |
"if m.startswith('harlequin_')})), file=sys.stderr)\n" |
| 419 |
"print(','.join(m for m in ('duckdb', 'pyarrow') " |
| 420 |
"if m in sys.modules), file=sys.stderr)\n" |
| 421 |
"print('tomlkit' in sys.modules, file=sys.stderr)\n" |
| 422 |
) |
| 423 |
# the last three lines, because this mode says what it wrote on stderr too |
| 424 |
adapters, forbidden, tomlkit = proc.stderr.strip().split("\n")[-3:] |
| 425 |
assert adapters == "harlequin_sqlite" |
| 426 |
assert not forbidden |
| 427 |
# the other half of the deferral: the mode that needs it still gets it |
| 428 |
assert tomlkit == "True" |
| 429 |
assert (tmp_path / ".harlequin.toml").exists() |
| 430 |
|
| 431 |
|
| 432 |
def test_spec_imports_only_the_adapter_it_was_narrowed_to( |
| 433 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 434 |
) -> None: |
| 435 |
"""`--spec` is the one mode that pays for adapters on purpose. |
| 436 |
|
| 437 |
Reading an adapter's options means importing it, so the whole document |
| 438 |
costs every installed adapter -- which is fine for a once-per-task lookup |
| 439 |
and the reason `-a` exists here. Under `-a` it costs one, and it still |
| 440 |
writes a document rather than rows, so no pyarrow and no tomlkit. |
| 441 |
""" |
| 442 |
proc = run_python( |
| 443 |
"import sys\n" |
| 444 |
"sys.argv = ['hsql', '--spec', '-a', 'sqlite']\n" |
| 445 |
"from harlequin.hsql import main\n" |
| 446 |
"try:\n" |
| 447 |
" main()\n" |
| 448 |
"except SystemExit:\n" |
| 449 |
" pass\n" |
| 450 |
"print(','.join(sorted({m.split('.')[0] for m in sys.modules " |
| 451 |
"if m.startswith('harlequin_')})), file=sys.stderr)\n" |
| 452 |
"print(','.join(m for m in ('duckdb', 'pyarrow', 'tomlkit') " |
| 453 |
"if m in sys.modules), file=sys.stderr)\n" |
| 454 |
) |
| 455 |
adapters, forbidden = proc.stderr.split("\n")[:2] |
| 456 |
assert adapters == "harlequin_sqlite" |
| 457 |
assert not forbidden |
| 458 |
|
| 459 |
|
| 460 |
def test_info_imports_only_the_adapter_it_was_narrowed_to( |
| 461 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 462 |
) -> None: |
| 463 |
"""`--info` reads capabilities off the adapter class, so it imports one per |
| 464 |
adapter it reports -- and under `-a`, exactly one. |
| 465 |
|
| 466 |
It opens no connection, and it writes a document rather than rows, so no |
| 467 |
pyarrow and no tomlkit either. |
| 468 |
""" |
| 469 |
proc = run_python( |
| 470 |
"import sys\n" |
| 471 |
"sys.argv = ['hsql', '--info', '-a', 'sqlite']\n" |
| 472 |
"from harlequin.hsql import main\n" |
| 473 |
"try:\n" |
| 474 |
" main()\n" |
| 475 |
"except SystemExit:\n" |
| 476 |
" pass\n" |
| 477 |
"print(','.join(sorted({m.split('.')[0] for m in sys.modules " |
| 478 |
"if m.startswith('harlequin_')})), file=sys.stderr)\n" |
| 479 |
"print(','.join(m for m in ('duckdb', 'pyarrow', 'tomlkit') " |
| 480 |
"if m in sys.modules), file=sys.stderr)\n" |
| 481 |
) |
| 482 |
adapters, forbidden = proc.stderr.split("\n")[:2] |
| 483 |
assert adapters == "harlequin_sqlite" |
| 484 |
assert not forbidden |
| 485 |
|
| 486 |
|
| 487 |
def test_public_names_still_resolve() -> None: |
| 488 |
"""Every name `harlequin/__init__.py` exported before it went lazy. |
| 489 |
|
| 490 |
Each must resolve to the same object as a direct import from its home |
| 491 |
module, and resolve to the *same* object twice (the second lookup is served |
| 492 |
from the module globals, not `__getattr__`). |
| 493 |
""" |
| 494 |
import harlequin |
| 495 |
from harlequin.adapter import ( |
| 496 |
HarlequinAdapter, |
| 497 |
HarlequinConnection, |
| 498 |
HarlequinCursor, |
| 499 |
) |
| 500 |
from harlequin.app import Harlequin |
| 501 |
from harlequin.autocomplete import HarlequinCompletion |
| 502 |
from harlequin.keymap import HarlequinKeyBinding, HarlequinKeyMap |
| 503 |
from harlequin.keys_app import HarlequinKeys |
| 504 |
from harlequin.options import HarlequinAdapterOption, HarlequinCopyFormat |
| 505 |
from harlequin.transaction_mode import HarlequinTransactionMode |
| 506 |
|
| 507 |
expected = { |
| 508 |
"Harlequin": Harlequin, |
| 509 |
"HarlequinAdapter": HarlequinAdapter, |
| 510 |
"HarlequinAdapterOption": HarlequinAdapterOption, |
| 511 |
"HarlequinCompletion": HarlequinCompletion, |
| 512 |
"HarlequinConnection": HarlequinConnection, |
| 513 |
"HarlequinCopyFormat": HarlequinCopyFormat, |
| 514 |
"HarlequinCursor": HarlequinCursor, |
| 515 |
"HarlequinTransactionMode": HarlequinTransactionMode, |
| 516 |
"HarlequinKeys": HarlequinKeys, |
| 517 |
"HarlequinKeyMap": HarlequinKeyMap, |
| 518 |
"HarlequinKeyBinding": HarlequinKeyBinding, |
| 519 |
} |
| 520 |
assert sorted(expected) == sorted(harlequin.__all__) |
| 521 |
for name, obj in expected.items(): |
| 522 |
assert getattr(harlequin, name) is obj |
| 523 |
assert getattr(harlequin, name) is getattr(harlequin, name) |
| 524 |
|
| 525 |
assert sorted(dir(harlequin)) == sorted(harlequin.__all__) |
| 526 |
|
| 527 |
|
| 528 |
def test_unknown_attribute_still_raises_attribute_error() -> None: |
| 529 |
import harlequin |
| 530 |
|
| 531 |
with pytest.raises(AttributeError): |
| 532 |
_ = harlequin.NotAThing |
| 533 |
|
| 534 |
|
| 535 |
def test_config_show_imports_the_adapters_its_profiles_name( |
| 536 |
tmp_path: Path, run_python: Callable[[str], subprocess.CompletedProcess[str]] |
| 537 |
) -> None: |
| 538 |
"""What `show` buys with an import, and what it refuses to pay for. |
| 539 |
|
| 540 |
Which of a profile's values are secret is the adapter's declaration, so a |
| 541 |
mode that prints those values asks the adapter -- one import per adapter |
| 542 |
its profiles name, and not one more. It stays a document either way: no |
| 543 |
pyarrow, whatever it had to import to redact. |
| 544 |
|
| 545 |
`tmp_path` is the subprocess's cwd and its home, so this file is the whole |
| 546 |
of the config it discovers. |
| 547 |
""" |
| 548 |
(tmp_path / ".harlequin.toml").write_text( |
| 549 |
"[profiles.lite]\n" |
| 550 |
'adapter = "sqlite"\n' |
| 551 |
"[profiles.md]\n" |
| 552 |
'adapter = "duckdb"\n' |
| 553 |
'conn_str = [ "md:my_db?motherduck_token=hunter2-and-then-some" ]\n' |
| 554 |
'md_token = "hunter2-and-then-some"\n' |
| 555 |
) |
| 556 |
proc = run_python( |
| 557 |
"import sys\n" |
| 558 |
"sys.argv = ['hsql', '--config', 'show']\n" |
| 559 |
"from harlequin.hsql import main\n" |
| 560 |
"try:\n" |
| 561 |
" main()\n" |
| 562 |
"except SystemExit:\n" |
| 563 |
" pass\n" |
| 564 |
"print(','.join(sorted({m.split('.')[0] for m in sys.modules " |
| 565 |
"if m.startswith('harlequin_')})), file=sys.stderr)\n" |
| 566 |
"print(','.join(m for m in ('pyarrow',) if m in sys.modules), " |
| 567 |
"file=sys.stderr)\n" |
| 568 |
) |
| 569 |
assert "hunter2-and-then-some" not in proc.stdout |
| 570 |
assert "********" in proc.stdout |
| 571 |
# the last two lines this wrote, and the second is empty when nothing |
| 572 |
# forbidden was imported -- so they are indexed rather than unpacked off a |
| 573 |
# stripped string, which would have swallowed the empty one |
| 574 |
lines = proc.stderr.split("\n") |
| 575 |
assert lines[-3] == "harlequin_duckdb,harlequin_sqlite" |
| 576 |
assert not lines[-2] |
| 577 |
|
| 578 |
|
| 579 |
def test_config_show_masks_by_name_when_an_adapter_will_not_import( |
| 580 |
tmp_path: Path, run_python: Callable[[str], subprocess.CompletedProcess[str]] |
| 581 |
) -> None: |
| 582 |
"""The backstop, and the line that says it is standing in. |
| 583 |
|
| 584 |
A profile naming an adapter this machine cannot import has no declaration |
| 585 |
to redact against, so the key's name is all there is -- and a caller whose |
| 586 |
report was masked the weaker way is told which adapter it was. |
| 587 |
""" |
| 588 |
(tmp_path / ".harlequin.toml").write_text( |
| 589 |
'[profiles.pg]\nadapter = "nonesuch"\npassword = "hunter2-and-then-some"\n' |
| 590 |
) |
| 591 |
proc = run_python( |
| 592 |
"import sys\n" |
| 593 |
"sys.argv = ['hsql', '--config', 'show']\n" |
| 594 |
"from harlequin.hsql import main\n" |
| 595 |
"try:\n" |
| 596 |
" main()\n" |
| 597 |
"except SystemExit:\n" |
| 598 |
" pass\n" |
| 599 |
) |
| 600 |
assert "hunter2-and-then-some" not in proc.stdout |
| 601 |
assert 'password = "********"' in proc.stdout |
| 602 |
assert "could not import nonesuch" in proc.stderr |