Debian Codegraph @

tests/unit_tests/test_cli.py

raw
1 import re
2 import shlex
3 import sqlite3
4 from pathlib import Path
5 from typing import Any
6 from unittest.mock import MagicMock
7
8 import pytest
9 from click.testing import CliRunner, Result
10
11 from harlequin import Harlequin
12 from harlequin.cli import (
13 DEFAULT_KEYMAP_NAMES,
14 DEFAULT_THEME,
15 DEFAULT_VIEWER_MAX_ROWS,
16 HEADLESS_DOCS_URL,
17 build_cli,
18 hsql_profile_keys,
19 hsql_spellings,
20 )
21 from harlequin.config import Config, Provenance, _merge
22 from harlequin_duckdb import DUCKDB_OPTIONS, DuckDbAdapter
23 from harlequin_sqlite import SQLITE_OPTIONS, HarlequinSqliteAdapter
24
25
26 def invoke(runner: CliRunner, args: str | list[str] = "", **kwargs: Any) -> Result:
27 """Build the command for these arguments, then run it against them.
28
29 The two are one step for a caller -- `harlequin()` does exactly this with
30 `sys.argv` -- because the command is built from the arguments: which
31 adapter's connection options it carries is what the first pass over them
32 settles.
33 """
34 argv = shlex.split(args) if isinstance(args, str) else args
35 return runner.invoke(build_cli(argv), args=argv, **kwargs)
36
37
38 @pytest.fixture()
39 def mock_adapter(monkeypatch: pytest.MonkeyPatch) -> MagicMock:
40 mock_adapter = MagicMock(name="mock_duckdb_adapter", spec=DuckDbAdapter)
41 mock_adapter.ADAPTER_OPTIONS = DUCKDB_OPTIONS
42 mock_adapter.profile_name = None
43 mock_entrypoint = MagicMock(name="mock_entrypoint")
44 mock_entrypoint.name = "duckdb"
45 mock_entrypoint.load.return_value = mock_adapter
46 mock_entry_points = MagicMock()
47 mock_entry_points.return_value = [mock_entrypoint]
48 monkeypatch.setattr("harlequin.plugins.entry_points", mock_entry_points)
49 return mock_adapter
50
51
52 @pytest.fixture()
53 def mock_sqlite_adapter(monkeypatch: pytest.MonkeyPatch) -> MagicMock:
54 mock_adapter = MagicMock(name="mock_sqlite_adapter", spec=HarlequinSqliteAdapter)
55 mock_adapter.ADAPTER_OPTIONS = SQLITE_OPTIONS
56 mock_entrypoint = MagicMock(name="mock_entrypoint")
57 mock_entrypoint.name = "sqlite"
58 mock_entrypoint.load.return_value = mock_adapter
59 mock_entry_points = MagicMock()
60 mock_entry_points.return_value = [mock_entrypoint]
61 monkeypatch.setattr("harlequin.plugins.entry_points", mock_entry_points)
62 return mock_adapter
63
64
65 @pytest.fixture()
66 def mock_harlequin(monkeypatch: pytest.MonkeyPatch) -> MagicMock:
67 mock = MagicMock(spec=Harlequin)
68 monkeypatch.setattr("harlequin.cli.Harlequin", mock)
69 return mock
70
71
72 @pytest.fixture()
73 def mock_empty_config(no_discovered_config: None) -> None:
74 """No config file anywhere. See tests/conftest.py."""
75
76
77 @pytest.fixture()
78 def mock_load_config(monkeypatch: pytest.MonkeyPatch) -> Config:
79 """A merged config, in place of whatever is on the machine running this.
80
81 Merged by the real `_merge()` rather than handed over as a literal, because
82 a caller that asks for a `Provenance` gets one filled in -- and a double
83 that filled it in itself would be a second copy of the merge's bookkeeping.
84 """
85 config = Config(profiles={"test-profile": {"theme": "fruity"}})
86
87 def load_config(
88 config_path: Path | None = None, provenance: Provenance | None = None
89 ) -> Config:
90 merged = Config()
91 _merge(config, into=merged, source=Path("test.toml"), provenance=provenance)
92 return merged
93
94 monkeypatch.setattr("harlequin.config.load_config", load_config)
95 return config
96
97
98 def test_help(mock_adapter: MagicMock, mock_empty_config: None) -> None:
99 """--help has to actually render.
100
101 It is the one command that exercises rich-click's help rendering, so a
102 rich-click that is out of step with click's API breaks it and nothing else.
103 """
104 runner = CliRunner()
105 # the option groups are keyed on the program name, so this has to match the
106 # name the console script is installed under
107 res = invoke(runner, "--help", prog_name="harlequin")
108 assert res.exception is None, f"--help raised {res.exception!r}"
109 assert res.exit_code == 0
110 # the option groups configured at the top of cli.py, including the ones
111 # built dynamically from the installed adapters
112 assert "Harlequin Options" in res.output
113 assert "duckdb Adapter Options" in res.output
114 # an option's help text, and a metavar appended to it
115 assert "--theme" in res.output
116 assert "(TEXT)" in res.output
117
118
119 @pytest.mark.parametrize("harlequin_args", ["", ":memory:"])
120 def test_default(
121 mock_harlequin: MagicMock,
122 mock_adapter: MagicMock,
123 harlequin_args: str,
124 mock_empty_config: None,
125 ) -> None:
126 runner = CliRunner()
127 res = invoke(runner, harlequin_args)
128 assert res.exit_code == 0
129 expected_conn_str = (harlequin_args,) if harlequin_args else tuple()
130 mock_adapter.assert_called_once_with(conn_str=expected_conn_str, read_only=False)
131 mock_harlequin.assert_called_once_with(
132 adapter=mock_adapter.return_value,
133 profile_name=None,
134 connection_hash=mock_adapter.return_value.connection_id,
135 viewer_max_rows=DEFAULT_VIEWER_MAX_ROWS,
136 query_limit=None,
137 keymap_names=DEFAULT_KEYMAP_NAMES,
138 user_defined_keymaps=[],
139 theme=DEFAULT_THEME,
140 show_files=None,
141 show_s3=None,
142 export_path=None,
143 ssh_tunnel=None,
144 )
145
146
147 @pytest.mark.parametrize(
148 "harlequin_args", ["--init-path foo", ":memory: -i foo", "-init foo"]
149 )
150 def test_custom_init_script(
151 mock_harlequin: MagicMock,
152 mock_adapter: MagicMock,
153 harlequin_args: str,
154 mock_empty_config: None,
155 ) -> None:
156 runner = CliRunner()
157 res = invoke(runner, harlequin_args)
158 assert res.exit_code == 0
159 mock_adapter.assert_called_once()
160 assert mock_adapter.call_args
161 assert mock_adapter.call_args.kwargs["init_path"] == Path("foo").resolve()
162
163
164 @pytest.mark.parametrize("harlequin_args", ["--no-init", ":memory: --no-init"])
165 def test_no_init_script(
166 mock_harlequin: MagicMock,
167 mock_adapter: MagicMock,
168 harlequin_args: str,
169 mock_empty_config: None,
170 ) -> None:
171 runner = CliRunner()
172 res = invoke(runner, harlequin_args)
173 assert res.exit_code == 0
174 mock_adapter.assert_called_once()
175 assert mock_adapter.call_args
176 assert mock_adapter.call_args.kwargs["no_init"] is True
177
178
179 @pytest.mark.parametrize(
180 "harlequin_args", ["--theme one-dark", ":memory: -t one-dark", "foo.db -t one-dark"]
181 )
182 def test_theme(
183 mock_harlequin: MagicMock,
184 mock_adapter: MagicMock,
185 harlequin_args: str,
186 mock_empty_config: None,
187 ) -> None:
188 runner = CliRunner()
189 res = invoke(runner, harlequin_args)
190 assert res.exit_code == 0
191 mock_harlequin.assert_called_once()
192 assert mock_harlequin.call_args
193 assert mock_harlequin.call_args.kwargs["theme"] == "one-dark"
194
195
196 @pytest.mark.parametrize(
197 ("harlequin_args", "expected"),
198 [
199 ("--viewer-max-rows 10", 10),
200 (":memory: --viewer-max-rows 1000000", 1_000_000),
201 ("foo.db --viewer-max-rows 5000000000", 5_000_000_000),
202 # both spellings of "hold everything"
203 ("--viewer-max-rows 0", None),
204 ("--viewer-max-rows -1", None),
205 ],
206 )
207 def test_viewer_max_rows(
208 mock_harlequin: MagicMock,
209 mock_adapter: MagicMock,
210 harlequin_args: str,
211 expected: int | None,
212 mock_empty_config: None,
213 ) -> None:
214 runner = CliRunner()
215 res = invoke(runner, harlequin_args)
216 assert res.exit_code == 0
217 mock_harlequin.assert_called_once()
218 assert mock_harlequin.call_args
219 assert mock_harlequin.call_args.kwargs["viewer_max_rows"] == expected
220 # the display cap says nothing about what is fetched
221 assert mock_harlequin.call_args.kwargs["query_limit"] is None
222
223
224 @pytest.mark.parametrize(
225 ("harlequin_args", "expected"),
226 [
227 ("--limit 10", 10),
228 (":memory: --limit 10", 10),
229 ("foo.db --limit 10", 10),
230 # a header and no rows, which is how you ask what a query returns
231 ("--limit 0", 0),
232 # every row, which is what an unset limit does too
233 ("--limit -1", None),
234 ],
235 )
236 def test_limit_is_the_hard_fetch_limit(
237 mock_harlequin: MagicMock,
238 mock_adapter: MagicMock,
239 harlequin_args: str,
240 expected: int | None,
241 mock_empty_config: None,
242 ) -> None:
243 """The same limit hsql applies, and the Run Query Bar shows it."""
244 runner = CliRunner()
245 res = invoke(runner, harlequin_args)
246 assert res.exit_code == 0
247 assert mock_harlequin.call_args
248 assert mock_harlequin.call_args.kwargs["query_limit"] == expected
249 # the viewer's cap is untouched by it
250 assert mock_harlequin.call_args.kwargs["viewer_max_rows"] == DEFAULT_VIEWER_MAX_ROWS
251
252
253 def test_unset_limit_fetches_everything(
254 mock_harlequin: MagicMock,
255 mock_adapter: MagicMock,
256 mock_empty_config: None,
257 ) -> None:
258 """Naming nothing is the full fetch the IDE has always done."""
259 runner = CliRunner()
260 res = invoke(runner, "")
261 assert res.exit_code == 0
262 assert mock_harlequin.call_args
263 assert mock_harlequin.call_args.kwargs["query_limit"] is None
264
265
266 def test_the_two_limits_are_independent(
267 mock_harlequin: MagicMock,
268 mock_adapter: MagicMock,
269 mock_empty_config: None,
270 ) -> None:
271 runner = CliRunner()
272 res = invoke(runner, "--limit 10 --viewer-max-rows 20")
273 assert res.exit_code == 0
274 assert mock_harlequin.call_args
275 assert mock_harlequin.call_args.kwargs["query_limit"] == 10
276 assert mock_harlequin.call_args.kwargs["viewer_max_rows"] == 20
277
278
279 def test_a_profile_limit_is_a_fetch_limit(
280 mock_harlequin: MagicMock,
281 mock_adapter: MagicMock,
282 data_dir: Path,
283 ) -> None:
284 """The profile that motivated the split, read the new way.
285
286 `limit = 200_000` in a config file both commands share now means the same
287 thing in both: 200,000 rows leave the database, and nothing has an opinion
288 about what the Results Viewer does with them.
289 """
290 runner = CliRunner()
291 config_path = data_dir / "unit_tests" / "config" / "good_config.toml"
292 res = invoke(runner, f"--config-path {config_path.as_posix()}")
293 assert res.exit_code == 0
294 assert mock_harlequin.call_args
295 assert mock_harlequin.call_args.kwargs["query_limit"] == 200_000
296 assert mock_harlequin.call_args.kwargs["viewer_max_rows"] == DEFAULT_VIEWER_MAX_ROWS
297
298
299 @pytest.mark.parametrize("harlequin_args", ["--show-files .", "-f .", "foo.db -f ."])
300 def test_show_files(
301 mock_harlequin: MagicMock,
302 mock_adapter: MagicMock,
303 harlequin_args: str,
304 mock_empty_config: None,
305 ) -> None:
306 runner = CliRunner()
307 res = invoke(runner, harlequin_args)
308 assert res.exit_code == 0
309 mock_harlequin.assert_called_once()
310 assert mock_harlequin.call_args
311 assert mock_harlequin.call_args.kwargs["show_files"] == Path(".")
312
313
314 @pytest.mark.parametrize(
315 "harlequin_args,export_path",
316 [
317 ("--output .harlequin", Path(".harlequin")),
318 ("-o .harlequin/", Path(".harlequin")),
319 ("-o exports/out.csv", Path("exports/out.csv")),
320 ("", None),
321 ],
322 )
323 def test_output_sets_the_export_path(
324 mock_harlequin: MagicMock,
325 mock_adapter: MagicMock,
326 harlequin_args: str,
327 export_path: Path | None,
328 mock_empty_config: None,
329 ) -> None:
330 """`-o` is where the Data Exporter starts, whether it names a folder or a
331 file."""
332 runner = CliRunner()
333 res = invoke(runner, harlequin_args)
334 assert res.exit_code == 0
335 assert mock_harlequin.call_args
336 assert mock_harlequin.call_args.kwargs["export_path"] == export_path
337
338
339 @pytest.mark.parametrize(
340 "harlequin_args",
341 [
342 "--adapter duckdb",
343 "-a duckdb",
344 "-a DUCKDB",
345 ],
346 )
347 def test_adapter_opt(
348 mock_harlequin: MagicMock,
349 mock_adapter: MagicMock,
350 harlequin_args: str,
351 mock_empty_config: None,
352 ) -> None:
353 runner = CliRunner()
354 res = invoke(runner, harlequin_args)
355 assert res.exit_code == 0
356 mock_harlequin.assert_called_once()
357 assert mock_harlequin.call_args
358 assert mock_harlequin.call_args.kwargs["adapter"] == mock_adapter.return_value
359
360
361 @pytest.mark.parametrize(
362 "harlequin_args",
363 [
364 "--adapter foo",
365 "-a bar",
366 ],
367 )
368 def test_bad_adapter_opt(
369 mock_harlequin: MagicMock,
370 mock_adapter: MagicMock,
371 harlequin_args: str,
372 mock_empty_config: None,
373 ) -> None:
374 runner = CliRunner()
375 res = invoke(runner, harlequin_args)
376 assert res.exit_code == 2
377 key_words = ["Error", "Invalid", "-a", "-adapter", "duckdb"]
378 assert all([w in res.stderr for w in key_words])
379
380
381 @pytest.mark.parametrize(
382 "harlequin_args",
383 [
384 "--profile test-profile",
385 "-P test-profile",
386 ],
387 )
388 def test_profile_opt(
389 mock_harlequin: MagicMock,
390 mock_adapter: MagicMock,
391 harlequin_args: str,
392 mock_load_config: Config,
393 ) -> None:
394 runner = CliRunner()
395 res = invoke(runner, harlequin_args)
396 assert res.exit_code == 0
397 mock_harlequin.assert_called_once()
398 assert mock_harlequin.call_args
399 assert mock_harlequin.call_args.kwargs["theme"] == "fruity"
400
401
402 @pytest.mark.parametrize(
403 "harlequin_args",
404 [
405 "--profile test-profile -t zenburn",
406 "-P test-profile --theme zenburn",
407 ],
408 )
409 def test_profile_override(
410 mock_harlequin: MagicMock,
411 mock_adapter: MagicMock,
412 harlequin_args: str,
413 mock_load_config: Config,
414 ) -> None:
415 runner = CliRunner()
416 res = invoke(runner, harlequin_args)
417 assert res.exit_code == 0
418 mock_harlequin.assert_called_once()
419 assert mock_harlequin.call_args
420 assert mock_harlequin.call_args.kwargs["theme"] == "zenburn"
421
422
423 @pytest.mark.parametrize(
424 "harlequin_args",
425 [
426 "--profile foo",
427 "-P bar",
428 ],
429 )
430 def test_bad_profile_opt(
431 mock_harlequin: MagicMock,
432 mock_adapter: MagicMock,
433 harlequin_args: str,
434 mock_load_config: Config,
435 ) -> None:
436 runner = CliRunner()
437 res = invoke(runner, harlequin_args)
438 assert res.exit_code == 2
439 key_words = ["profile", "config"]
440 assert all([w in res.stderr for w in key_words])
441
442
443 @pytest.mark.parametrize("filename", ["good_config.toml", "pyproject.toml"])
444 def test_config_path(
445 mock_harlequin: MagicMock,
446 mock_adapter: MagicMock,
447 data_dir: Path,
448 filename: str,
449 ) -> None:
450 runner = CliRunner()
451 config_path = data_dir / "unit_tests" / "config" / filename
452 res = invoke(runner, f"--config-path {config_path.as_posix()}")
453 assert res.exit_code == 0
454 mock_harlequin.assert_called_once()
455 assert mock_harlequin.call_args
456 # should use default profile of my-duckdb-profile
457 assert mock_harlequin.call_args.kwargs["query_limit"] == 200_000
458 mock_adapter.assert_called_once()
459 assert mock_adapter.call_args.kwargs["conn_str"] == ["my-database.db"]
460 assert mock_adapter.call_args.kwargs["read_only"] is False
461 assert mock_adapter.call_args.kwargs["extension"] == ["httpfs", "spatial"]
462
463
464 @pytest.mark.parametrize("filename", ["good_config.toml", "pyproject.toml"])
465 def test_config_path_fron_env(
466 mock_harlequin: MagicMock,
467 mock_adapter: MagicMock,
468 data_dir: Path,
469 filename: str,
470 ) -> None:
471 runner = CliRunner()
472 config_path = data_dir / "unit_tests" / "config" / filename
473 res = invoke(runner, env={"HARLEQUIN_CONFIG_PATH": config_path.as_posix()})
474 assert res.exit_code == 0
475 mock_harlequin.assert_called_once()
476 assert mock_harlequin.call_args
477 # should use default profile of my-duckdb-profile
478 assert mock_harlequin.call_args.kwargs["query_limit"] == 200_000
479 mock_adapter.assert_called_once()
480 assert mock_adapter.call_args.kwargs["conn_str"] == ["my-database.db"]
481 assert mock_adapter.call_args.kwargs["read_only"] is False
482 assert mock_adapter.call_args.kwargs["extension"] == ["httpfs", "spatial"]
483
484
485 @pytest.mark.parametrize("filename", ["good_config.toml", "pyproject.toml"])
486 def test_conn_str_overrides_the_profile(
487 mock_harlequin: MagicMock,
488 mock_adapter: MagicMock,
489 data_dir: Path,
490 filename: str,
491 ) -> None:
492 """A conn_str on the command line beats the profile's.
493
494 test_config_path is the other half: with none passed, the profile's survives.
495 """
496 runner = CliRunner()
497 config_path = data_dir / "unit_tests" / "config" / filename
498 res = invoke(runner, f"--config-path {config_path.as_posix()} other.db")
499 assert res.exit_code == 0
500 mock_adapter.assert_called_once()
501 assert mock_adapter.call_args.kwargs["conn_str"] == ("other.db",)
502 # unrelated profile values are untouched
503 assert mock_adapter.call_args.kwargs["extension"] == ["httpfs", "spatial"]
504 assert mock_harlequin.call_args.kwargs["query_limit"] == 200_000
505
506
507 def test_bad_config_exits(
508 mock_harlequin: MagicMock,
509 mock_adapter: MagicMock,
510 data_dir: Path,
511 ) -> None:
512 runner = CliRunner()
513 config_path = data_dir / "unit_tests" / "config" / "default_no_exist.toml"
514 res = invoke(runner, f"--config-path {config_path.as_posix()}")
515 assert res.exit_code == 2
516 key_words = ["default_profile", "foo"]
517 assert all([w in res.stderr for w in key_words])
518
519
520 def test_a_profile_naming_an_uninstalled_adapter_says_so(
521 mock_harlequin: MagicMock,
522 mock_adapter: MagicMock,
523 tmp_path: Path,
524 ) -> None:
525 """The one name the command's `--adapter` Choice never sees.
526
527 click vets what is typed, so a name nothing installed provides can only
528 arrive from a config file -- and that is the profile the pass over the
529 arguments could not turn into an adapter.
530 """
531 config_path = tmp_path / ".harlequin.toml"
532 config_path.write_text('[profiles.gone]\nadapter = "notreal"\n')
533 runner = CliRunner()
534 res = invoke(runner, f"--config-path {config_path.as_posix()} -P gone")
535 assert res.exit_code == 2
536 said = _said(res.stderr)
537 assert "notreal" in said
538 # and what it could have been
539 assert "duckdb" in said
540
541
542 @pytest.mark.skipif(
543 not hasattr(sqlite3.Connection, "enable_load_extension"),
544 reason="Extension option not supported on many pythons.",
545 )
546 def test_sqlite_extensions(
547 mock_harlequin: MagicMock,
548 mock_sqlite_adapter: MagicMock,
549 mock_empty_config: None,
550 data_dir: Path,
551 ) -> None:
552 extension_path = data_dir / "unit_tests" / "sqlite_extension" / "hello0"
553 runner = CliRunner()
554 res = invoke(runner, f"-a sqlite --extension {extension_path.as_posix()}")
555 assert res.exit_code == 0
556
557
558 @pytest.mark.skipif(
559 hasattr(sqlite3.Connection, "enable_load_extension"),
560 reason="Extension option not supported on many pythons.",
561 )
562 def test_sqlite_extension_not_supported(
563 mock_harlequin: MagicMock,
564 mock_sqlite_adapter: MagicMock,
565 mock_empty_config: None,
566 data_dir: Path,
567 ) -> None:
568 extension_path = data_dir / "unit_tests" / "sqlite_extension" / "hello0"
569 runner = CliRunner()
570 res = invoke(runner, f"-a sqlite --extension {extension_path.as_posix()}")
571 assert res.exit_code == 2
572 assert "No such option" in res.stderr
573
574
575 # --- read-only ---------------------------------------------------------------
576
577
578 @pytest.mark.parametrize("harlequin_args", ["--read-only", "-r"])
579 def test_read_only_reaches_the_adapter_however_it_is_spelled(
580 mock_harlequin: MagicMock,
581 mock_adapter: MagicMock,
582 harlequin_args: str,
583 mock_empty_config: None,
584 ) -> None:
585 """It is an argument of the adapter's constructor rather than an option any
586 adapter declares, so every adapter takes it under the same name."""
587 runner = CliRunner()
588 res = invoke(runner, harlequin_args)
589 assert res.exit_code == 0
590 assert mock_adapter.call_args
591 assert mock_adapter.call_args.kwargs["read_only"] is True
592
593
594 def test_read_only_from_a_profile_reaches_the_adapter(
595 mock_harlequin: MagicMock, mock_adapter: MagicMock, data_dir: Path
596 ) -> None:
597 """`read_only` is the command's key in a profile, whatever adapter it names."""
598 config_path = data_dir / "unit_tests" / "config" / "good_config.toml"
599 runner = CliRunner()
600 res = invoke(runner, f"--config-path {config_path.as_posix()}")
601 assert res.exit_code == 0
602 assert mock_adapter.call_args
603 assert mock_adapter.call_args.kwargs["read_only"] is False
604
605
606 def test_read_only_refuses_an_adapter_that_does_not_declare_it(
607 mock_harlequin: MagicMock, mock_adapter: MagicMock, mock_empty_config: None
608 ) -> None:
609 """Before the app starts, and before the adapter is constructed: an adapter
610 that cannot enforce read-only is free to ignore the argument, and a session
611 that believed it was read-only would write."""
612 mock_adapter.IMPLEMENTS_READ_ONLY = False
613 runner = CliRunner()
614 res = invoke(runner, "--read-only")
615 assert res.exit_code == 2
616 mock_adapter.assert_not_called()
617 mock_harlequin.assert_not_called()
618
619
620 # --- pointing at the other command -------------------------------------------
621
622 _ANSI = re.compile(r"\x1b(?:\[[0-9;]*[a-zA-Z]|\][^\x1b\x07]*(?:\x1b\\|\x07))")
623
624
625 def _said(stderr: str) -> str:
626 """What a reader sees, out of what rich-click wrote.
627
628 rich-click renders errors into a panel at truecolor, so the message arrives
629 styled, wrapped at whatever width the terminal happens to be, and with the
630 option name colored -- which puts escape sequences in the middle of the
631 sentence, not only around it.
632 """
633 return " ".join(_ANSI.sub("", stderr).split())
634
635
636 @pytest.mark.parametrize("option", ["-c", "--command", "--csv", "-A", "--null-string"])
637 def test_an_hsql_option_names_hsql(
638 mock_adapter: MagicMock, mock_empty_config: None, option: str
639 ) -> None:
640 """The likeliest first mistake now that there are two commands."""
641 runner = CliRunner()
642 res = invoke(runner, [option, "select 1"])
643 assert res.exit_code == 2
644 # rich-click wraps the panel it renders errors in, so the message arrives
645 # broken across lines at whatever width the terminal happens to be
646 said = _said(res.stderr)
647 assert f"{option} is not a harlequin option" in said
648 assert f"Did you mean 'hsql {option}'?" in said
649 assert HEADLESS_DOCS_URL in said
650
651
652 def test_an_unknown_option_that_is_not_hsqls_is_unchanged(
653 mock_adapter: MagicMock, mock_empty_config: None
654 ) -> None:
655 """click's own message, and its own suggestion, for everything else."""
656 runner = CliRunner()
657 res = invoke(runner, ["--thmee", "nord"])
658 assert res.exit_code == 2
659 said = _said(res.stderr)
660 # click has reworded this message across releases (8.4 quotes the option
661 # and lists every near match), so assert on its parts rather than on one
662 # release's sentence
663 assert "No such option" in said
664 assert "--thmee" in said
665 # click's own suggestion, drawn from this command's options
666 assert "--theme" in said
667 assert "hsql" not in said
668
669
670 def test_what_this_command_reads_off_the_other_one(mock_empty_config: None) -> None:
671 """Both are read from hsql's real command rather than copied from it.
672
673 Which means asserting they are hsql's own surface and not an adapter's:
674 building it must not import one, or the sets would grow with whatever
675 happens to be installed.
676 """
677 assert "--csv" in hsql_spellings()
678 assert "--theme" not in hsql_spellings()
679 assert "--no-init" not in hsql_spellings(), "an adapter's option, not hsql's"
680
681 assert {"format", "stats", "on_error"} <= hsql_profile_keys()
682 assert "no_init" not in hsql_profile_keys()