| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import subprocess |
| 4 |
from typing import Callable |
| 5 |
from unittest.mock import MagicMock |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from harlequin.cli import DEFAULT_KEYMAP_NAMES |
| 10 |
from harlequin.exception import HarlequinConfigError |
| 11 |
from harlequin.keymap import HarlequinKeyMap |
| 12 |
from harlequin.plugins import ( |
| 13 |
adapter_names, |
| 14 |
load_adapter, |
| 15 |
load_adapter_plugins, |
| 16 |
load_keymap_plugins, |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
def _adapter_packages(code: str) -> str: |
| 21 |
"""A snippet that runs `code`, then reports the adapters it imported.""" |
| 22 |
return ( |
| 23 |
f"{code}\n" |
| 24 |
"import sys\n" |
| 25 |
"print(','.join(sorted({m.split('.')[0] for m in list(sys.modules)" |
| 26 |
" if m.startswith('harlequin_')})))\n" |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
def test_adapter_names_agree_with_the_loaded_adapters() -> None: |
| 31 |
"""The cheap listing and the expensive one must name the same adapters. |
| 32 |
|
| 33 |
A subset, not equality: an installed adapter that won't import is named here |
| 34 |
and dropped by `load_adapter_plugins()`. |
| 35 |
""" |
| 36 |
names = adapter_names() |
| 37 |
assert set(load_adapter_plugins().keys()) <= set(names) |
| 38 |
assert "duckdb" in names |
| 39 |
assert "sqlite" in names |
| 40 |
assert names == sorted(names) |
| 41 |
|
| 42 |
|
| 43 |
def test_adapter_names_imports_no_adapter( |
| 44 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 45 |
) -> None: |
| 46 |
"""Naming the installed adapters must not import any of them.""" |
| 47 |
proc = run_python( |
| 48 |
_adapter_packages( |
| 49 |
"from harlequin.plugins import adapter_names\n" |
| 50 |
"assert 'duckdb' in adapter_names()" |
| 51 |
) |
| 52 |
) |
| 53 |
assert proc.stdout.strip() == "" |
| 54 |
|
| 55 |
|
| 56 |
def test_load_adapter_loads_exactly_one_adapter( |
| 57 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 58 |
) -> None: |
| 59 |
assert load_adapter("duckdb") is load_adapter_plugins()["duckdb"] |
| 60 |
proc = run_python( |
| 61 |
_adapter_packages( |
| 62 |
"from harlequin.plugins import load_adapter\nload_adapter('duckdb')" |
| 63 |
) |
| 64 |
) |
| 65 |
assert proc.stdout.strip() == "harlequin_duckdb" |
| 66 |
|
| 67 |
|
| 68 |
def test_load_adapter_raises_for_an_unknown_name() -> None: |
| 69 |
with pytest.raises(HarlequinConfigError) as exc_info: |
| 70 |
load_adapter("not-an-adapter") |
| 71 |
# names the adapter that was asked for, and the ones that could have been |
| 72 |
assert "not-an-adapter" in str(exc_info.value) |
| 73 |
assert "duckdb" in str(exc_info.value) |
| 74 |
|
| 75 |
|
| 76 |
def test_load_adapter_raises_when_the_adapter_will_not_import( |
| 77 |
monkeypatch: pytest.MonkeyPatch, |
| 78 |
) -> None: |
| 79 |
"""A named adapter that won't import is fatal, unlike one found by scanning.""" |
| 80 |
ep = MagicMock() |
| 81 |
ep.name = "broken" |
| 82 |
ep.load.side_effect = ImportError("no module named nope", name="nope") |
| 83 |
monkeypatch.setattr("harlequin.plugins.entry_points", lambda group: [ep]) |
| 84 |
with pytest.raises(HarlequinConfigError) as exc_info: |
| 85 |
load_adapter("broken") |
| 86 |
assert "broken" in str(exc_info.value) |
| 87 |
assert "no module named nope" in str(exc_info.value) |
| 88 |
|
| 89 |
|
| 90 |
def test_load_keymap_plugins() -> None: |
| 91 |
built_in_keymaps = load_keymap_plugins(user_defined_keymaps=[]) |
| 92 |
assert len(built_in_keymaps) == 1 |
| 93 |
assert DEFAULT_KEYMAP_NAMES[0] in built_in_keymaps |
| 94 |
assert isinstance(built_in_keymaps[DEFAULT_KEYMAP_NAMES[0]], HarlequinKeyMap) |
| 95 |
assert built_in_keymaps[DEFAULT_KEYMAP_NAMES[0]].bindings |
| 96 |
|
| 97 |
|
| 98 |
def test_do_not_load_keymaps_that_duplicate_plugin_names() -> None: |
| 99 |
my_map = HarlequinKeyMap(name=DEFAULT_KEYMAP_NAMES[0], bindings=[]) |
| 100 |
with pytest.raises(HarlequinConfigError) as exc_info: |
| 101 |
_ = load_keymap_plugins(user_defined_keymaps=[my_map]) |
| 102 |
assert "vscode" in str(exc_info) |
| 103 |
|
| 104 |
|
| 105 |
def test_plugin_load_failure_goes_to_stderr( |
| 106 |
run_python: Callable[[str], subprocess.CompletedProcess[str]], |
| 107 |
) -> None: |
| 108 |
"""A plug-in that won't import must not contaminate piped output.""" |
| 109 |
proc = run_python( |
| 110 |
"from unittest.mock import MagicMock, patch\n" |
| 111 |
"ep = MagicMock()\n" |
| 112 |
"ep.name = 'broken'\n" |
| 113 |
"ep.load.side_effect = ImportError('no module named nope', name='nope')\n" |
| 114 |
"with patch('harlequin.plugins.entry_points', return_value=[ep]):\n" |
| 115 |
" from harlequin.plugins import load_adapter_plugins\n" |
| 116 |
" load_adapter_plugins()\n" |
| 117 |
) |
| 118 |
assert proc.stdout == "" |
| 119 |
assert "could not load the installed plug-in" in proc.stderr |