| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from pathlib import Path |
| 4 |
from typing import Any, Callable, Sequence |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from harlequin.adapter import HarlequinAdapter, HarlequinConnection |
| 9 |
from harlequin.config import load_config, load_profile |
| 10 |
from harlequin.config_wizard import _wizard |
| 11 |
from harlequin.options import FlagOption |
| 12 |
|
| 13 |
|
| 14 |
class FakePrompt: |
| 15 |
"""One questionary prompt, answered without a terminal.""" |
| 16 |
|
| 17 |
def __init__(self, answer: Any) -> None: |
| 18 |
self.answer = answer |
| 19 |
|
| 20 |
def ask(self) -> Any: |
| 21 |
return self.answer |
| 22 |
|
| 23 |
def unsafe_ask(self) -> Any: |
| 24 |
return self.answer |
| 25 |
|
| 26 |
|
| 27 |
@pytest.fixture |
| 28 |
def run_wizard(monkeypatch: pytest.MonkeyPatch) -> Callable[..., None]: |
| 29 |
"""Run the real wizard, with every prompt answered from a dict. |
| 30 |
|
| 31 |
Answers are keyed by a substring of the prompt's message; a prompt no test |
| 32 |
answers takes what the wizard offered it -- the value already in the |
| 33 |
profile, or the first choice -- so a test names only the prompts it is about. |
| 34 |
An optional `record` list collects every prompt's (message, kwargs). |
| 35 |
""" |
| 36 |
import questionary |
| 37 |
|
| 38 |
def fake( |
| 39 |
answers: dict[str, Any], |
| 40 |
fallback: Callable[..., Any], |
| 41 |
record: list[tuple[str, dict[str, Any]]] | None, |
| 42 |
) -> Callable[..., Any]: |
| 43 |
def prompt(message: str = "", **kwargs: Any) -> FakePrompt: |
| 44 |
if record is not None: |
| 45 |
record.append((message, kwargs)) |
| 46 |
for substring, answer in answers.items(): |
| 47 |
if substring in message: |
| 48 |
return FakePrompt(answer) |
| 49 |
return FakePrompt(fallback(**kwargs)) |
| 50 |
|
| 51 |
return prompt |
| 52 |
|
| 53 |
def first_choice(choices: Sequence[Any], **_: Any) -> Any: |
| 54 |
return choices[0] |
| 55 |
|
| 56 |
def the_default(default: Any = "", **_: Any) -> Any: |
| 57 |
return default |
| 58 |
|
| 59 |
def nothing_checked(**_: Any) -> list[Any]: |
| 60 |
return [] |
| 61 |
|
| 62 |
def yes(default: bool = True, **_: Any) -> bool: |
| 63 |
# the default it offered, which is what an unanswered prompt takes: a |
| 64 |
# confirm that offers none means yes, as questionary's does |
| 65 |
return bool(default) |
| 66 |
|
| 67 |
def run( |
| 68 |
config_path: Path, |
| 69 |
answers: dict[str, Any], |
| 70 |
record: list[tuple[str, dict[str, Any]]] | None = None, |
| 71 |
) -> None: |
| 72 |
fallbacks: tuple[tuple[str, Callable[..., Any]], ...] = ( |
| 73 |
("select", first_choice), |
| 74 |
("text", the_default), |
| 75 |
("path", the_default), |
| 76 |
# the prompt a secret option gets: same question, no echo |
| 77 |
("password", the_default), |
| 78 |
("checkbox", nothing_checked), |
| 79 |
("confirm", yes), |
| 80 |
) |
| 81 |
for name, fallback in fallbacks: |
| 82 |
monkeypatch.setattr(questionary, name, fake(answers, fallback, record)) |
| 83 |
_wizard(config_path=config_path) |
| 84 |
|
| 85 |
return run |
| 86 |
|
| 87 |
|
| 88 |
class TestDefaultProfile: |
| 89 |
def test_choosing_no_default_removes_it_from_the_file( |
| 90 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 91 |
) -> None: |
| 92 |
"""Turning a default off has to reach the file, or it did not happen. |
| 93 |
|
| 94 |
`[No default]` is the first choice the prompt offers, which is what the |
| 95 |
unanswered prompts take -- so this names it by leaving it unanswered. |
| 96 |
""" |
| 97 |
path = tmp_path / ".harlequin.toml" |
| 98 |
path.write_text( |
| 99 |
'default_profile = "one"\n\n[profiles.one]\n# keep me\ntheme = "fruity"\n' |
| 100 |
) |
| 101 |
|
| 102 |
run_wizard(path, {"Which profile would you like to update?": "one"}) |
| 103 |
|
| 104 |
assert load_config(config_path=path).default_profile is None |
| 105 |
written = path.read_text() |
| 106 |
assert "default_profile" not in written |
| 107 |
# and the write is still a write that keeps the file someone typed |
| 108 |
assert "# keep me" in written |
| 109 |
|
| 110 |
def test_setting_a_default_writes_it( |
| 111 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 112 |
) -> None: |
| 113 |
path = tmp_path / ".harlequin.toml" |
| 114 |
path.write_text('[profiles.one]\n# keep me\ntheme = "fruity"\n') |
| 115 |
|
| 116 |
run_wizard( |
| 117 |
path, |
| 118 |
{ |
| 119 |
"Which profile would you like to update?": "one", |
| 120 |
"Would you like to set a default profile?": "one", |
| 121 |
}, |
| 122 |
) |
| 123 |
|
| 124 |
assert load_config(config_path=path).default_profile == "one" |
| 125 |
assert "# keep me" in path.read_text() |
| 126 |
|
| 127 |
|
| 128 |
class TestSecrets: |
| 129 |
"""What the wizard writes down, and what it puts on the screen. |
| 130 |
|
| 131 |
Two different things for a secret, and the difference is the point: the |
| 132 |
file needs the token to connect with, and the terminal -- shared, scrolled |
| 133 |
back, screenshotted -- needs never to have seen it. |
| 134 |
""" |
| 135 |
|
| 136 |
SECRET = "hunter2-and-then-some" |
| 137 |
|
| 138 |
def test_a_secret_is_written_to_the_file_and_not_to_the_screen( |
| 139 |
self, |
| 140 |
tmp_path: Path, |
| 141 |
run_wizard: Callable[..., None], |
| 142 |
capsys: pytest.CaptureFixture[str], |
| 143 |
) -> None: |
| 144 |
path = tmp_path / ".harlequin.toml" |
| 145 |
path.write_text('[profiles.one]\ntheme = "fruity"\n') |
| 146 |
|
| 147 |
run_wizard( |
| 148 |
path, |
| 149 |
{ |
| 150 |
"Which profile would you like to update?": "one", |
| 151 |
"Which adapter": "duckdb", |
| 152 |
"adapter options": ["md_token"], |
| 153 |
"md_token": self.SECRET, |
| 154 |
}, |
| 155 |
) |
| 156 |
|
| 157 |
# the file gets the value: a profile that connected with asterisks |
| 158 |
# would be a worse bug than the one this is about |
| 159 |
assert load_config(config_path=path).profiles["one"]["md_token"] == self.SECRET |
| 160 |
# the confirmation panel does not |
| 161 |
printed = capsys.readouterr().out |
| 162 |
assert self.SECRET not in printed |
| 163 |
assert "********" in printed |
| 164 |
|
| 165 |
def test_a_variable_the_file_reads_a_secret_from_is_written_back_unresolved( |
| 166 |
self, |
| 167 |
tmp_path: Path, |
| 168 |
run_wizard: Callable[..., None], |
| 169 |
monkeypatch: pytest.MonkeyPatch, |
| 170 |
) -> None: |
| 171 |
"""`${MYPASSWORD}` is what the author typed, and what they keep. |
| 172 |
|
| 173 |
The wizard reads a config file and writes it back, so a value resolved |
| 174 |
on the way in would be a plaintext password on the way out -- which is |
| 175 |
why interpolation happens on the read path and not in `ConfigFile`. |
| 176 |
""" |
| 177 |
monkeypatch.setenv("MYPASSWORD", self.SECRET) |
| 178 |
path = tmp_path / ".harlequin.toml" |
| 179 |
path.write_text( |
| 180 |
'[profiles.one]\nmd_token = "${MYPASSWORD}"\ntheme = "fruity"\n' |
| 181 |
) |
| 182 |
|
| 183 |
run_wizard( |
| 184 |
path, |
| 185 |
{ |
| 186 |
"Which profile would you like to update?": "one", |
| 187 |
"Which adapter": "duckdb", |
| 188 |
# checked, and its value left at what the file already says |
| 189 |
"adapter options": ["md_token"], |
| 190 |
}, |
| 191 |
) |
| 192 |
|
| 193 |
written = path.read_text() |
| 194 |
assert "${MYPASSWORD}" in written |
| 195 |
assert self.SECRET not in written |
| 196 |
# and the run path still resolves it |
| 197 |
assert ( |
| 198 |
load_profile(config_path=path, profile_name="one")["md_token"] |
| 199 |
== self.SECRET |
| 200 |
) |
| 201 |
|
| 202 |
|
| 203 |
class TestReadOnly: |
| 204 |
"""The prompt for the one core option that decides what a session may do.""" |
| 205 |
|
| 206 |
def test_saying_yes_writes_the_key( |
| 207 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 208 |
) -> None: |
| 209 |
path = tmp_path / ".harlequin.toml" |
| 210 |
path.write_text('[profiles.one]\ntheme = "fruity"\n') |
| 211 |
|
| 212 |
run_wizard( |
| 213 |
path, |
| 214 |
{ |
| 215 |
"Which profile would you like to update?": "one", |
| 216 |
"connect read-only": True, |
| 217 |
}, |
| 218 |
) |
| 219 |
|
| 220 |
assert load_config(config_path=path).profiles["one"]["read_only"] is True |
| 221 |
|
| 222 |
def test_a_profile_that_is_not_read_only_says_nothing( |
| 223 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 224 |
) -> None: |
| 225 |
"""Read-write is the default, so the key is a line that says nothing.""" |
| 226 |
path = tmp_path / ".harlequin.toml" |
| 227 |
path.write_text('[profiles.one]\ntheme = "fruity"\n') |
| 228 |
|
| 229 |
run_wizard( |
| 230 |
path, |
| 231 |
{ |
| 232 |
"Which profile would you like to update?": "one", |
| 233 |
"connect read-only": False, |
| 234 |
}, |
| 235 |
) |
| 236 |
|
| 237 |
assert "read_only" not in load_config(config_path=path).profiles["one"] |
| 238 |
|
| 239 |
def test_saying_no_turns_off_a_profile_that_was_read_only( |
| 240 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 241 |
) -> None: |
| 242 |
"""Turning it off has to reach the file, or it did not happen.""" |
| 243 |
path = tmp_path / ".harlequin.toml" |
| 244 |
path.write_text('[profiles.one]\nread_only = true\ntheme = "fruity"\n') |
| 245 |
|
| 246 |
run_wizard( |
| 247 |
path, |
| 248 |
{ |
| 249 |
"Which profile would you like to update?": "one", |
| 250 |
"connect read-only": False, |
| 251 |
}, |
| 252 |
) |
| 253 |
|
| 254 |
assert "read_only" not in load_config(config_path=path).profiles["one"] |
| 255 |
|
| 256 |
def test_the_question_is_not_put_to_an_adapter_that_cannot_answer_it( |
| 257 |
self, |
| 258 |
tmp_path: Path, |
| 259 |
run_wizard: Callable[..., None], |
| 260 |
monkeypatch: pytest.MonkeyPatch, |
| 261 |
) -> None: |
| 262 |
"""A profile that said yes is one Harlequin would refuse to start under. |
| 263 |
|
| 264 |
Answered yes here, so a question that was put would have written the |
| 265 |
key -- and the profile that had it loses it, which is the same answer |
| 266 |
`--config validate` gives about the file it was already in. |
| 267 |
""" |
| 268 |
|
| 269 |
class Undeclared(HarlequinAdapter): |
| 270 |
ADAPTER_OPTIONS = None |
| 271 |
|
| 272 |
def __init__(self, conn_str: Sequence[str], **options: Any) -> None: |
| 273 |
raise NotImplementedError |
| 274 |
|
| 275 |
def connect(self) -> HarlequinConnection: |
| 276 |
raise NotImplementedError |
| 277 |
|
| 278 |
monkeypatch.setattr( |
| 279 |
"harlequin.config_wizard.load_adapter_plugins", |
| 280 |
lambda: {"undeclared": Undeclared}, |
| 281 |
) |
| 282 |
path = tmp_path / ".harlequin.toml" |
| 283 |
path.write_text('[profiles.one]\nadapter = "undeclared"\nread_only = true\n') |
| 284 |
|
| 285 |
run_wizard( |
| 286 |
path, |
| 287 |
{ |
| 288 |
"Which profile would you like to update?": "one", |
| 289 |
"connect read-only": True, |
| 290 |
}, |
| 291 |
) |
| 292 |
|
| 293 |
assert "read_only" not in load_config(config_path=path).profiles["one"] |
| 294 |
|
| 295 |
def test_the_prompt_offers_what_the_profile_already_says( |
| 296 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 297 |
) -> None: |
| 298 |
"""Unanswered, it takes the default -- so a read-only profile rewritten |
| 299 |
without touching this question is still read-only.""" |
| 300 |
path = tmp_path / ".harlequin.toml" |
| 301 |
path.write_text('[profiles.one]\nread_only = true\ntheme = "fruity"\n') |
| 302 |
|
| 303 |
run_wizard(path, {"Which profile would you like to update?": "one"}) |
| 304 |
|
| 305 |
assert load_config(config_path=path).profiles["one"]["read_only"] is True |
| 306 |
|
| 307 |
|
| 308 |
class TestProfileWrite: |
| 309 |
def test_the_wizard_keeps_the_comments_in_a_profile_it_did_not_touch( |
| 310 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 311 |
) -> None: |
| 312 |
path = tmp_path / ".harlequin.toml" |
| 313 |
path.write_text( |
| 314 |
'default_profile = "one"\n' |
| 315 |
"\n" |
| 316 |
"[profiles.one]\n" |
| 317 |
"# I like this theme best\n" |
| 318 |
'theme = "fruity"\n' |
| 319 |
"# the database I use every day\n" |
| 320 |
'conn_str = ["analytics.db"]\n' |
| 321 |
) |
| 322 |
|
| 323 |
run_wizard( |
| 324 |
path, |
| 325 |
{ |
| 326 |
"Which profile would you like to update?": "[Create a New Profile]", |
| 327 |
"What would you like to name your profile?": "two", |
| 328 |
"Would you like to set a default profile?": "one", |
| 329 |
}, |
| 330 |
) |
| 331 |
|
| 332 |
written = path.read_text() |
| 333 |
assert "# I like this theme best" in written |
| 334 |
assert "# the database I use every day" in written |
| 335 |
config = load_config(config_path=path) |
| 336 |
assert config.profiles["one"] == { |
| 337 |
"theme": "fruity", |
| 338 |
"conn_str": ["analytics.db"], |
| 339 |
} |
| 340 |
assert "two" in config.profiles |
| 341 |
assert config.default_profile == "one" |
| 342 |
|
| 343 |
|
| 344 |
class TestDefaults: |
| 345 |
"""Prompts that offered a value the user did not ask for (#1105). |
| 346 |
|
| 347 |
The wizard's job is to write what a user typed, not to guess: a fresh |
| 348 |
profile gets no `limit`, and an adapter option that stays blank (or gets |
| 349 |
cleared) is not written at all. |
| 350 |
""" |
| 351 |
|
| 352 |
def test_a_fresh_profile_is_not_given_a_limit( |
| 353 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 354 |
) -> None: |
| 355 |
path = tmp_path / ".harlequin.toml" |
| 356 |
path.write_text('[profiles.one]\nadapter = "duckdb"\n') |
| 357 |
|
| 358 |
run_wizard(path, {"Which profile would you like to update?": "one"}) |
| 359 |
|
| 360 |
assert "limit" not in load_config(config_path=path).profiles["one"] |
| 361 |
|
| 362 |
def test_an_existing_limit_is_offered_and_kept( |
| 363 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 364 |
) -> None: |
| 365 |
path = tmp_path / ".harlequin.toml" |
| 366 |
path.write_text('[profiles.one]\nadapter = "duckdb"\nlimit = 500\n') |
| 367 |
|
| 368 |
run_wizard(path, {"Which profile would you like to update?": "one"}) |
| 369 |
|
| 370 |
assert load_config(config_path=path).profiles["one"]["limit"] == 500 |
| 371 |
|
| 372 |
def test_blank_removes_an_existing_limit( |
| 373 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 374 |
) -> None: |
| 375 |
path = tmp_path / ".harlequin.toml" |
| 376 |
path.write_text('[profiles.one]\nadapter = "duckdb"\nlimit = 500\n') |
| 377 |
|
| 378 |
run_wizard( |
| 379 |
path, |
| 380 |
{ |
| 381 |
"Which profile would you like to update?": "one", |
| 382 |
"How many rows should each query fetch": "", |
| 383 |
}, |
| 384 |
) |
| 385 |
|
| 386 |
assert "limit" not in load_config(config_path=path).profiles["one"] |
| 387 |
|
| 388 |
def test_a_typed_limit_is_written( |
| 389 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 390 |
) -> None: |
| 391 |
path = tmp_path / ".harlequin.toml" |
| 392 |
path.write_text('[profiles.one]\nadapter = "duckdb"\n') |
| 393 |
|
| 394 |
run_wizard( |
| 395 |
path, |
| 396 |
{ |
| 397 |
"Which profile would you like to update?": "one", |
| 398 |
"How many rows should each query fetch": "1000", |
| 399 |
}, |
| 400 |
) |
| 401 |
|
| 402 |
assert load_config(config_path=path).profiles["one"]["limit"] == 1000 |
| 403 |
|
| 404 |
def test_minus_one_is_accepted_and_omitted( |
| 405 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 406 |
) -> None: |
| 407 |
"""`-1` still means no limit, which is the app default: no key.""" |
| 408 |
path = tmp_path / ".harlequin.toml" |
| 409 |
path.write_text('[profiles.one]\nadapter = "duckdb"\n') |
| 410 |
|
| 411 |
run_wizard( |
| 412 |
path, |
| 413 |
{ |
| 414 |
"Which profile would you like to update?": "one", |
| 415 |
"How many rows should each query fetch": "-1", |
| 416 |
}, |
| 417 |
) |
| 418 |
|
| 419 |
assert "limit" not in load_config(config_path=path).profiles["one"] |
| 420 |
|
| 421 |
def test_the_limit_prompt_offers_blank_and_says_so( |
| 422 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 423 |
) -> None: |
| 424 |
path = tmp_path / ".harlequin.toml" |
| 425 |
path.write_text('[profiles.one]\nadapter = "duckdb"\n') |
| 426 |
record: list[tuple[str, dict[str, Any]]] = [] |
| 427 |
|
| 428 |
run_wizard( |
| 429 |
path, {"Which profile would you like to update?": "one"}, record=record |
| 430 |
) |
| 431 |
|
| 432 |
limit_prompt = next( |
| 433 |
kwargs |
| 434 |
for message, kwargs in record |
| 435 |
if "How many rows should each query fetch" in message |
| 436 |
) |
| 437 |
assert limit_prompt["default"] == "" |
| 438 |
assert ( |
| 439 |
limit_prompt["instruction"] |
| 440 |
== "Leave blank for app defaults; enter -1 for no limit." |
| 441 |
) |
| 442 |
validate = limit_prompt["validate"] |
| 443 |
assert validate("") is True |
| 444 |
assert validate("1000") is True |
| 445 |
assert validate("abc") is False |
| 446 |
|
| 447 |
def test_a_blank_secret_option_is_not_written( |
| 448 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 449 |
) -> None: |
| 450 |
"""The `****` a secret's `None` used to be shown as never reaches the file.""" |
| 451 |
path = tmp_path / ".harlequin.toml" |
| 452 |
path.write_text('[profiles.one]\nadapter = "duckdb"\n') |
| 453 |
|
| 454 |
run_wizard( |
| 455 |
path, |
| 456 |
{ |
| 457 |
"Which profile would you like to update?": "one", |
| 458 |
"adapter options": ["md_token"], |
| 459 |
"md_token": "", |
| 460 |
}, |
| 461 |
) |
| 462 |
|
| 463 |
assert "md_token" not in load_config(config_path=path).profiles["one"] |
| 464 |
|
| 465 |
def test_a_blank_text_option_is_not_written( |
| 466 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 467 |
) -> None: |
| 468 |
path = tmp_path / ".harlequin.toml" |
| 469 |
path.write_text('[profiles.one]\nadapter = "duckdb"\n') |
| 470 |
|
| 471 |
run_wizard( |
| 472 |
path, |
| 473 |
{ |
| 474 |
"Which profile would you like to update?": "one", |
| 475 |
"adapter options": ["custom-extension-repo"], |
| 476 |
"custom-extension-repo": "", |
| 477 |
}, |
| 478 |
) |
| 479 |
|
| 480 |
assert ( |
| 481 |
"custom_extension_repo" not in load_config(config_path=path).profiles["one"] |
| 482 |
) |
| 483 |
|
| 484 |
def test_a_blank_list_option_is_not_written( |
| 485 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 486 |
) -> None: |
| 487 |
"""Blank must not become a list that holds one empty item.""" |
| 488 |
path = tmp_path / ".harlequin.toml" |
| 489 |
path.write_text('[profiles.one]\nadapter = "duckdb"\n') |
| 490 |
|
| 491 |
run_wizard( |
| 492 |
path, |
| 493 |
{ |
| 494 |
"Which profile would you like to update?": "one", |
| 495 |
"adapter options": ["extension"], |
| 496 |
"extension": "", |
| 497 |
}, |
| 498 |
) |
| 499 |
|
| 500 |
assert "extension" not in load_config(config_path=path).profiles["one"] |
| 501 |
|
| 502 |
def test_blank_clears_an_existing_adapter_option( |
| 503 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 504 |
) -> None: |
| 505 |
path = tmp_path / ".harlequin.toml" |
| 506 |
path.write_text('[profiles.one]\nadapter = "duckdb"\nmd_token = "hunter2"\n') |
| 507 |
|
| 508 |
run_wizard( |
| 509 |
path, |
| 510 |
{ |
| 511 |
"Which profile would you like to update?": "one", |
| 512 |
"adapter options": ["md_token"], |
| 513 |
"md_token": "", |
| 514 |
}, |
| 515 |
) |
| 516 |
|
| 517 |
assert "md_token" not in load_config(config_path=path).profiles["one"] |
| 518 |
|
| 519 |
def test_an_explicit_false_on_a_flag_is_written( |
| 520 |
self, |
| 521 |
tmp_path: Path, |
| 522 |
run_wizard: Callable[..., None], |
| 523 |
monkeypatch: pytest.MonkeyPatch, |
| 524 |
) -> None: |
| 525 |
"""A flag whose declared default is true needs `false` stored to override it.""" |
| 526 |
|
| 527 |
class FakeAdapter(HarlequinAdapter): |
| 528 |
ADAPTER_OPTIONS = [ |
| 529 |
FlagOption(name="legacy-mode", description="x", default=True) |
| 530 |
] |
| 531 |
|
| 532 |
def __init__(self, conn_str: Sequence[str], **options: Any) -> None: |
| 533 |
raise NotImplementedError |
| 534 |
|
| 535 |
def connect(self) -> HarlequinConnection: |
| 536 |
raise NotImplementedError |
| 537 |
|
| 538 |
monkeypatch.setattr( |
| 539 |
"harlequin.config_wizard.load_adapter_plugins", |
| 540 |
lambda: {"fake": FakeAdapter}, |
| 541 |
) |
| 542 |
path = tmp_path / ".harlequin.toml" |
| 543 |
path.write_text('[profiles.one]\nadapter = "fake"\n') |
| 544 |
|
| 545 |
run_wizard( |
| 546 |
path, |
| 547 |
{ |
| 548 |
"Which profile would you like to update?": "one", |
| 549 |
"adapter options": ["legacy-mode"], |
| 550 |
"legacy-mode": False, |
| 551 |
}, |
| 552 |
) |
| 553 |
|
| 554 |
assert load_config(config_path=path).profiles["one"]["legacy_mode"] is False |
| 555 |
|
| 556 |
|
| 557 |
class TestSsh: |
| 558 |
"""The tunnel's keys, which only a profile that has one should carry.""" |
| 559 |
|
| 560 |
PROFILE = '[profiles.one]\nadapter = "duckdb"\n' |
| 561 |
|
| 562 |
def test_saying_no_writes_no_ssh_keys( |
| 563 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 564 |
) -> None: |
| 565 |
path = tmp_path / ".harlequin.toml" |
| 566 |
path.write_text(self.PROFILE) |
| 567 |
|
| 568 |
run_wizard( |
| 569 |
path, |
| 570 |
{ |
| 571 |
"Which profile would you like to update?": "one", |
| 572 |
"Do you connect via SSH?": False, |
| 573 |
}, |
| 574 |
) |
| 575 |
|
| 576 |
profile = load_config(config_path=path).profiles["one"] |
| 577 |
assert not [key for key in profile if key.startswith("ssh")] |
| 578 |
|
| 579 |
def test_the_question_defaults_to_no( |
| 580 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 581 |
) -> None: |
| 582 |
"""An unanswered confirm takes the default it offered.""" |
| 583 |
path = tmp_path / ".harlequin.toml" |
| 584 |
path.write_text(self.PROFILE) |
| 585 |
record: list[tuple[str, dict[str, Any]]] = [] |
| 586 |
|
| 587 |
run_wizard( |
| 588 |
path, {"Which profile would you like to update?": "one"}, record=record |
| 589 |
) |
| 590 |
|
| 591 |
asked = [kwargs for message, kwargs in record if "via SSH" in message] |
| 592 |
assert asked and asked[0]["default"] is False |
| 593 |
profile = load_config(config_path=path).profiles["one"] |
| 594 |
assert not [key for key in profile if key.startswith("ssh")] |
| 595 |
|
| 596 |
def test_saying_yes_writes_the_tunnel( |
| 597 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 598 |
) -> None: |
| 599 |
path = tmp_path / ".harlequin.toml" |
| 600 |
path.write_text(self.PROFILE) |
| 601 |
|
| 602 |
run_wizard( |
| 603 |
path, |
| 604 |
{ |
| 605 |
"Which profile would you like to update?": "one", |
| 606 |
"Do you connect via SSH?": True, |
| 607 |
"What SSH destination": "redshift_prod", |
| 608 |
"What should it forward?": "15439:data.example.com:5439", |
| 609 |
"Should ssh fail rather than prompt": True, |
| 610 |
"How many seconds should Harlequin wait": "30", |
| 611 |
}, |
| 612 |
) |
| 613 |
|
| 614 |
profile = load_config(config_path=path).profiles["one"] |
| 615 |
assert profile["ssh_host"] == "redshift_prod" |
| 616 |
assert profile["ssh_forward"] == ["15439:data.example.com:5439"] |
| 617 |
assert profile["ssh_batch_mode"] is True |
| 618 |
assert profile["ssh_timeout"] == 30 |
| 619 |
|
| 620 |
def test_a_forward_the_ssh_config_supplies_is_left_out( |
| 621 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 622 |
) -> None: |
| 623 |
"""The motivating case: the Host block has the LocalForward.""" |
| 624 |
path = tmp_path / ".harlequin.toml" |
| 625 |
path.write_text(self.PROFILE) |
| 626 |
|
| 627 |
run_wizard( |
| 628 |
path, |
| 629 |
{ |
| 630 |
"Which profile would you like to update?": "one", |
| 631 |
"Do you connect via SSH?": True, |
| 632 |
"What SSH destination": "redshift_prod", |
| 633 |
"What should it forward?": "", |
| 634 |
"Should ssh fail rather than prompt": False, |
| 635 |
"How many seconds should Harlequin wait": "", |
| 636 |
}, |
| 637 |
) |
| 638 |
|
| 639 |
profile = load_config(config_path=path).profiles["one"] |
| 640 |
assert profile["ssh_host"] == "redshift_prod" |
| 641 |
assert "ssh_forward" not in profile |
| 642 |
assert "ssh_batch_mode" not in profile |
| 643 |
assert "ssh_timeout" not in profile |
| 644 |
|
| 645 |
def test_several_forwards_are_written_as_a_list( |
| 646 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 647 |
) -> None: |
| 648 |
path = tmp_path / ".harlequin.toml" |
| 649 |
path.write_text(self.PROFILE) |
| 650 |
|
| 651 |
run_wizard( |
| 652 |
path, |
| 653 |
{ |
| 654 |
"Which profile would you like to update?": "one", |
| 655 |
"Do you connect via SSH?": True, |
| 656 |
"What SSH destination": "web-1", |
| 657 |
"What should it forward?": "15439:one:5439 15440:two:5440", |
| 658 |
}, |
| 659 |
) |
| 660 |
|
| 661 |
assert load_config(config_path=path).profiles["one"]["ssh_forward"] == [ |
| 662 |
"15439:one:5439", |
| 663 |
"15440:two:5440", |
| 664 |
] |
| 665 |
|
| 666 |
def test_an_existing_tunnel_is_offered_back( |
| 667 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 668 |
) -> None: |
| 669 |
path = tmp_path / ".harlequin.toml" |
| 670 |
path.write_text( |
| 671 |
"[profiles.one]\n" |
| 672 |
'adapter = "duckdb"\n' |
| 673 |
'ssh_host = "redshift_prod"\n' |
| 674 |
'ssh_forward = ["15439:data.example.com:5439"]\n' |
| 675 |
) |
| 676 |
record: list[tuple[str, dict[str, Any]]] = [] |
| 677 |
|
| 678 |
run_wizard( |
| 679 |
path, {"Which profile would you like to update?": "one"}, record=record |
| 680 |
) |
| 681 |
|
| 682 |
offered = { |
| 683 |
message: kwargs.get("default") |
| 684 |
for message, kwargs in record |
| 685 |
if "via SSH" in message or "SSH destination" in message |
| 686 |
} |
| 687 |
assert offered["Do you connect via SSH?"] is True |
| 688 |
assert "redshift_prod" in str(offered) |
| 689 |
profile = load_config(config_path=path).profiles["one"] |
| 690 |
assert profile["ssh_host"] == "redshift_prod" |
| 691 |
assert profile["ssh_forward"] == ["15439:data.example.com:5439"] |
| 692 |
|
| 693 |
def test_the_wizard_never_writes_the_key_a_config_file_may_not_answer( |
| 694 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 695 |
) -> None: |
| 696 |
"""`ssh_allow_reuse` is read from the command line only.""" |
| 697 |
path = tmp_path / ".harlequin.toml" |
| 698 |
path.write_text(self.PROFILE) |
| 699 |
record: list[tuple[str, dict[str, Any]]] = [] |
| 700 |
|
| 701 |
run_wizard( |
| 702 |
path, |
| 703 |
{ |
| 704 |
"Which profile would you like to update?": "one", |
| 705 |
"Do you connect via SSH?": True, |
| 706 |
"What SSH destination": "web-1", |
| 707 |
}, |
| 708 |
record=record, |
| 709 |
) |
| 710 |
|
| 711 |
assert "reuse" not in " ".join(message for message, _ in record).lower() |
| 712 |
assert "ssh_allow_reuse" not in load_config(config_path=path).profiles["one"] |
| 713 |
|
| 714 |
def test_the_tunnel_is_asked_about_before_the_adapter_options( |
| 715 |
self, tmp_path: Path, run_wizard: Callable[..., None] |
| 716 |
) -> None: |
| 717 |
path = tmp_path / ".harlequin.toml" |
| 718 |
path.write_text(self.PROFILE) |
| 719 |
record: list[tuple[str, dict[str, Any]]] = [] |
| 720 |
|
| 721 |
run_wizard( |
| 722 |
path, {"Which profile would you like to update?": "one"}, record=record |
| 723 |
) |
| 724 |
|
| 725 |
messages = [message for message, _ in record] |
| 726 |
asked = next(i for i, m in enumerate(messages) if "via SSH" in m) |
| 727 |
options = next(i for i, m in enumerate(messages) if "adapter options" in m) |
| 728 |
assert asked < options |