| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
from pathlib import Path |
| 5 |
from typing import Awaitable, Callable, List, Sequence |
| 6 |
|
| 7 |
import pytest |
| 8 |
from textual import events |
| 9 |
from textual.app import App |
| 10 |
from textual.pilot import Pilot |
| 11 |
from textual.widgets import TextArea |
| 12 |
from textual.widgets.text_area import Selection |
| 13 |
from textual.worker import WorkerFailed |
| 14 |
|
| 15 |
from harlequin import Harlequin |
| 16 |
from harlequin.autocomplete import BufferSymbols |
| 17 |
from harlequin.autocomplete import find_symbols as real_find_symbols |
| 18 |
from harlequin.components.code_editor import CodeEditor |
| 19 |
from harlequin.statements import find_separators, split |
| 20 |
from tests.functional_tests.helpers import ( |
| 21 |
expand_catalog_node, |
| 22 |
wait_for_catalog_tree, |
| 23 |
wait_for_editor, |
| 24 |
wait_for_error_modal, |
| 25 |
) |
| 26 |
from tests.waiting import POLL_INTERVAL, wait_for, wait_for_value |
| 27 |
|
| 28 |
|
| 29 |
@pytest.mark.asyncio |
| 30 |
async def test_query_formatting( |
| 31 |
app: Harlequin, |
| 32 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 33 |
) -> None: |
| 34 |
async with app.run_test() as pilot: |
| 35 |
await wait_for_workers(app) |
| 36 |
editor = await wait_for_editor(pilot, app) |
| 37 |
editor.text = "select\n\n1 FROM\n\n foo" |
| 38 |
|
| 39 |
await pilot.press("f4") |
| 40 |
assert editor.text == "select 1 from foo\n" |
| 41 |
assert list(app._notifications)[-1].message == "Formatted query." |
| 42 |
|
| 43 |
# formatting an already-formatted query notifies that nothing changed |
| 44 |
await pilot.press("f4") |
| 45 |
assert editor.text == "select 1 from foo\n" |
| 46 |
assert "no changes" in list(app._notifications)[-1].message |
| 47 |
|
| 48 |
|
| 49 |
async def loaded_buffer(pilot: Pilot, app: Harlequin, tab_id: str, text: str) -> None: |
| 50 |
"""Wait until `tab_id` is the active buffer and its text is in the editor. |
| 51 |
|
| 52 |
Activating a tab and swapping its state into the one editor are two steps |
| 53 |
on the message pump, so the text lands after the tab id does. |
| 54 |
""" |
| 55 |
collection = app.editor_collection |
| 56 |
await wait_for( |
| 57 |
pilot, |
| 58 |
lambda: collection.active == tab_id and collection.current_editor.text == text, |
| 59 |
description=f"{tab_id} to be the loaded buffer, holding {text!r}", |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
@pytest.mark.asyncio |
| 64 |
async def test_multiple_buffers( |
| 65 |
app: Harlequin, |
| 66 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 67 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 68 |
) -> None: |
| 69 |
snap_results: List[bool] = [] |
| 70 |
async with app.run_test(size=(120, 36)) as pilot: |
| 71 |
await wait_for_workers(app) |
| 72 |
editor = await wait_for_editor(pilot, app) |
| 73 |
assert app.editor_collection |
| 74 |
assert app.editor_collection.tab_count == 1 |
| 75 |
assert app.editor_collection.active == "tab-1" |
| 76 |
editor.text = "tab 1" |
| 77 |
await pilot.press("home") |
| 78 |
await pilot.pause() |
| 79 |
snap_results.append(await app_snapshot(app, "Tab 1 of 1 (No tabs)")) |
| 80 |
|
| 81 |
await pilot.press("ctrl+n") |
| 82 |
await loaded_buffer(pilot, app, "tab-2", "") |
| 83 |
await pilot.wait_for_scheduled_animations() |
| 84 |
assert app.editor_collection.tab_count == 2 |
| 85 |
editor.text = "tab 2" |
| 86 |
await pilot.press("home") |
| 87 |
await pilot.pause() |
| 88 |
snap_results.append(await app_snapshot(app, "Tab 2 of 2")) |
| 89 |
|
| 90 |
await pilot.press("ctrl+n") |
| 91 |
await loaded_buffer(pilot, app, "tab-3", "") |
| 92 |
await pilot.wait_for_scheduled_animations() |
| 93 |
assert app.editor_collection.tab_count == 3 |
| 94 |
editor.text = "tab 3" |
| 95 |
await pilot.press("home") |
| 96 |
await pilot.pause() |
| 97 |
snap_results.append(await app_snapshot(app, "Tab 3 of 3")) |
| 98 |
|
| 99 |
await pilot.press("ctrl+k") |
| 100 |
await loaded_buffer(pilot, app, "tab-1", "tab 1") |
| 101 |
await pilot.wait_for_scheduled_animations() |
| 102 |
assert app.editor_collection.tab_count == 3 |
| 103 |
snap_results.append(await app_snapshot(app, "Tab 1 of 3")) |
| 104 |
|
| 105 |
await pilot.press("ctrl+k") |
| 106 |
await loaded_buffer(pilot, app, "tab-2", "tab 2") |
| 107 |
await pilot.wait_for_scheduled_animations() |
| 108 |
assert app.editor_collection.tab_count == 3 |
| 109 |
snap_results.append(await app_snapshot(app, "Tab 2 of 3")) |
| 110 |
|
| 111 |
await pilot.press("ctrl+w") |
| 112 |
await loaded_buffer(pilot, app, "tab-3", "tab 3") |
| 113 |
await pilot.wait_for_scheduled_animations() |
| 114 |
assert app.editor_collection.tab_count == 2 |
| 115 |
# TODO: bring back this flaky test. |
| 116 |
# snap_results.append(await app_snapshot(app, "Tab 3 after deleting 2")) |
| 117 |
|
| 118 |
await pilot.press("ctrl+k") |
| 119 |
await loaded_buffer(pilot, app, "tab-1", "tab 1") |
| 120 |
await pilot.wait_for_scheduled_animations() |
| 121 |
snap_results.append(await app_snapshot(app, "Tab 1 of [1,3]")) |
| 122 |
|
| 123 |
await pilot.press("ctrl+k") |
| 124 |
await loaded_buffer(pilot, app, "tab-3", "tab 3") |
| 125 |
await pilot.wait_for_scheduled_animations() |
| 126 |
snap_results.append(await app_snapshot(app, "Tab 3 of [1,3]")) |
| 127 |
|
| 128 |
assert all(snap_results) |
| 129 |
|
| 130 |
|
| 131 |
@pytest.mark.asyncio |
| 132 |
async def test_buffers_keep_their_state( |
| 133 |
app: Harlequin, |
| 134 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 135 |
) -> None: |
| 136 |
"""Switching buffers has to carry everything the editor holds, not just the text.""" |
| 137 |
async with app.run_test(size=(120, 36)) as pilot: |
| 138 |
await wait_for_workers(app) |
| 139 |
editor = await wait_for_editor(pilot, app) |
| 140 |
|
| 141 |
editor.focus() |
| 142 |
await pilot.press(*"select 1") |
| 143 |
await pilot.press("ctrl+n") |
| 144 |
await pilot.pause() |
| 145 |
await pilot.press(*"select 2") |
| 146 |
await pilot.press("ctrl+k") |
| 147 |
await pilot.pause() |
| 148 |
|
| 149 |
assert app.editor_collection.active == "tab-1" |
| 150 |
assert editor.text == "select 1" |
| 151 |
assert editor.selection == Selection((0, 8), (0, 8)) |
| 152 |
|
| 153 |
# the undo history moves with the buffer: this undoes the typing |
| 154 |
# in buffer one, not the switch that loaded it. |
| 155 |
await pilot.press("ctrl+z") |
| 156 |
await pilot.pause() |
| 157 |
assert editor.text == "" |
| 158 |
await pilot.press("ctrl+z") |
| 159 |
await pilot.pause() |
| 160 |
assert editor.text == "" |
| 161 |
|
| 162 |
await pilot.press("ctrl+k") |
| 163 |
await pilot.pause() |
| 164 |
assert app.editor_collection.active == "tab-2" |
| 165 |
assert editor.text == "select 2" |
| 166 |
|
| 167 |
# a buffer scrolled away from its cursor comes back where it was, |
| 168 |
# instead of snapping to the cursor. |
| 169 |
assert editor.text_input is not None |
| 170 |
editor.text = "\n".join(f"select {i}" for i in range(100)) |
| 171 |
await pilot.press("ctrl+down", "ctrl+down", "ctrl+down") |
| 172 |
await pilot.pause() |
| 173 |
scrolled_to = editor.text_input.scroll_offset |
| 174 |
assert scrolled_to.y > 0 |
| 175 |
assert editor.selection == Selection((0, 0), (0, 0)) |
| 176 |
|
| 177 |
await pilot.press("ctrl+k") |
| 178 |
await pilot.pause() |
| 179 |
await pilot.press("ctrl+k") |
| 180 |
await pilot.pause() |
| 181 |
assert app.editor_collection.active == "tab-2" |
| 182 |
assert editor.text_input.scroll_offset == scrolled_to |
| 183 |
|
| 184 |
|
| 185 |
@pytest.mark.asyncio |
| 186 |
async def test_a_buffer_opened_on_the_way_out_does_not_crash( |
| 187 |
app: Harlequin, |
| 188 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 189 |
) -> None: |
| 190 |
"""The Query Editor mounts lazily, so a quit can land before its first buffer.""" |
| 191 |
async with app.run_test() as pilot: |
| 192 |
await wait_for_workers(app) |
| 193 |
editor = await wait_for_editor(pilot, app) |
| 194 |
|
| 195 |
collection = app.editor_collection |
| 196 |
# not awaited: removal marks the subtree, and the buffer opens into it |
| 197 |
collection.remove() |
| 198 |
editor = await collection.action_new_buffer() |
| 199 |
|
| 200 |
assert editor is collection.current_editor |
| 201 |
|
| 202 |
|
| 203 |
async def completions_for(pilot: Pilot, editor: CodeEditor, text: str) -> None: |
| 204 |
"""Wait until the completion list is open over a buffer holding `text`. |
| 205 |
|
| 206 |
A keypress reaches the list through the editor's own worker, so the list a |
| 207 |
snapshot catches is a keypress behind unless the test waits for it. |
| 208 |
""" |
| 209 |
await wait_for( |
| 210 |
pilot, |
| 211 |
lambda: ( |
| 212 |
editor.text == text |
| 213 |
and editor.completion_list.is_open |
| 214 |
and editor.completion_list.option_count > 0 |
| 215 |
), |
| 216 |
description=f"the completion list to open over {text!r}", |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
@pytest.mark.asyncio |
| 221 |
async def test_word_autocomplete( |
| 222 |
app_all_adapters: Harlequin, |
| 223 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 224 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 225 |
transaction_button_visible: Callable[[Harlequin], bool], |
| 226 |
) -> None: |
| 227 |
app = app_all_adapters |
| 228 |
snap_results: List[bool] = [] |
| 229 |
async with app.run_test() as pilot: |
| 230 |
await wait_for_workers(app) |
| 231 |
editor = await wait_for_editor(pilot, app) |
| 232 |
await wait_for_value( |
| 233 |
pilot, |
| 234 |
lambda: app.editor_collection.word_completer, |
| 235 |
description="the word completer to be built", |
| 236 |
) |
| 237 |
|
| 238 |
# we need to let the data catalog load the root's children |
| 239 |
await wait_for_catalog_tree(pilot, app) |
| 240 |
|
| 241 |
editor.focus() |
| 242 |
|
| 243 |
await pilot.press("s") |
| 244 |
await completions_for(pilot, editor, "s") |
| 245 |
await pilot.wait_for_scheduled_animations() |
| 246 |
snap_results.append(await app_snapshot(app, "s")) |
| 247 |
|
| 248 |
await pilot.press("e") |
| 249 |
await completions_for(pilot, editor, "se") |
| 250 |
await pilot.wait_for_scheduled_animations() |
| 251 |
snap_results.append(await app_snapshot(app, "se")) |
| 252 |
|
| 253 |
await pilot.press("l") |
| 254 |
await completions_for(pilot, editor, "sel") |
| 255 |
await pilot.wait_for_scheduled_animations() |
| 256 |
snap_results.append(await app_snapshot(app, "sel")) |
| 257 |
|
| 258 |
await pilot.press("backspace") |
| 259 |
await completions_for(pilot, editor, "se") |
| 260 |
await pilot.wait_for_scheduled_animations() |
| 261 |
snap_results.append(await app_snapshot(app, "se again")) |
| 262 |
|
| 263 |
await pilot.press("l") |
| 264 |
await pilot.pause() |
| 265 |
await wait_for_workers(app) |
| 266 |
await pilot.pause() |
| 267 |
await pilot.wait_for_scheduled_animations() |
| 268 |
await pilot.press("enter") |
| 269 |
await pilot.pause() |
| 270 |
await wait_for_workers(app) |
| 271 |
await pilot.pause() |
| 272 |
await pilot.wait_for_scheduled_animations() |
| 273 |
snap_results.append(await app_snapshot(app, "submitted")) |
| 274 |
|
| 275 |
if not (transaction_button_visible(app)): |
| 276 |
assert all(snap_results) |
| 277 |
|
| 278 |
|
| 279 |
@pytest.mark.skipif( |
| 280 |
sys.platform == "win32", reason="Initial snapshot very flaky on windows." |
| 281 |
) |
| 282 |
@pytest.mark.asyncio |
| 283 |
async def test_member_autocomplete( |
| 284 |
app_small_duck: Harlequin, |
| 285 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 286 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 287 |
) -> None: |
| 288 |
app = app_small_duck |
| 289 |
snap_results: List[bool] = [] |
| 290 |
async with app.run_test() as pilot: |
| 291 |
await wait_for_workers(app) |
| 292 |
|
| 293 |
# we need to expand the data catalog to load items into the completer |
| 294 |
tree = await wait_for_catalog_tree(pilot, app) |
| 295 |
for db_node in tree.root.children: |
| 296 |
await expand_catalog_node(pilot, db_node) |
| 297 |
await wait_for_workers(app) |
| 298 |
for schema_node in db_node.children: |
| 299 |
await expand_catalog_node(pilot, schema_node) |
| 300 |
await wait_for_workers(app) |
| 301 |
|
| 302 |
# the relations are on screen now, so the catalog prefetches their |
| 303 |
# columns; the completer holding one is what says they landed |
| 304 |
member_completer = await wait_for_value( |
| 305 |
pilot, |
| 306 |
lambda: app.editor_collection.member_completer, |
| 307 |
description="the member completer to be built", |
| 308 |
) |
| 309 |
await wait_for( |
| 310 |
pilot, |
| 311 |
lambda: bool(member_completer('"drivers".')), |
| 312 |
description="the prefetched columns to reach the member completer", |
| 313 |
interval=POLL_INTERVAL, |
| 314 |
) |
| 315 |
|
| 316 |
editor = await wait_for_editor(pilot, app) |
| 317 |
editor.text = '"drivers"' |
| 318 |
editor.selection = Selection((0, 9), (0, 9)) |
| 319 |
editor.focus() |
| 320 |
|
| 321 |
await pilot.press("full_stop") |
| 322 |
await completions_for(pilot, editor, '"drivers".') |
| 323 |
await pilot.wait_for_scheduled_animations() |
| 324 |
snap_results.append(await app_snapshot(app, "driver members")) |
| 325 |
|
| 326 |
await pilot.press("quotation_mark") |
| 327 |
# the editor auto-closes the quote |
| 328 |
await completions_for(pilot, editor, '"drivers".""') |
| 329 |
await pilot.wait_for_scheduled_animations() |
| 330 |
snap_results.append(await app_snapshot(app, "with quote")) |
| 331 |
|
| 332 |
await pilot.press("enter") |
| 333 |
await pilot.pause() |
| 334 |
await wait_for_workers(app) |
| 335 |
await pilot.pause() |
| 336 |
await pilot.wait_for_scheduled_animations() |
| 337 |
snap_results.append(await app_snapshot(app, "submitted")) |
| 338 |
|
| 339 |
assert all(snap_results) |
| 340 |
|
| 341 |
|
| 342 |
@pytest.mark.asyncio |
| 343 |
async def test_footer_inputs( |
| 344 |
app: Harlequin, |
| 345 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 346 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 347 |
) -> None: |
| 348 |
snap_results: List[bool] = [] |
| 349 |
async with app.run_test() as pilot: |
| 350 |
await wait_for_workers(app) |
| 351 |
|
| 352 |
editor = await wait_for_editor(pilot, app) |
| 353 |
assert editor.text_input is not None |
| 354 |
editor.text = "select 1" |
| 355 |
|
| 356 |
await pilot.press("ctrl+o") |
| 357 |
await pilot.pause() |
| 358 |
snap_results.append(await app_snapshot(app, "Open Input visible")) |
| 359 |
|
| 360 |
await pilot.press("ctrl+s") |
| 361 |
await pilot.pause() |
| 362 |
snap_results.append(await app_snapshot(app, "Save Input visible")) |
| 363 |
|
| 364 |
await pilot.press("escape") |
| 365 |
await wait_for( |
| 366 |
pilot, |
| 367 |
lambda: not editor.query("#textarea__save_input"), |
| 368 |
description="the Save Input to be dismissed", |
| 369 |
) |
| 370 |
snap_results.append(await app_snapshot(app, "No Input visible")) |
| 371 |
|
| 372 |
await pilot.press("ctrl+f") |
| 373 |
await pilot.pause() |
| 374 |
snap_results.append(await app_snapshot(app, "Find Input visible")) |
| 375 |
|
| 376 |
await pilot.press("escape") |
| 377 |
await pilot.pause() |
| 378 |
|
| 379 |
await pilot.press("ctrl+g") |
| 380 |
await pilot.pause() |
| 381 |
snap_results.append(await app_snapshot(app, "Goto Input visible")) |
| 382 |
|
| 383 |
assert all(snap_results) |
| 384 |
|
| 385 |
|
| 386 |
@pytest.mark.asyncio |
| 387 |
async def test_selected_queries( |
| 388 |
app: Harlequin, |
| 389 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 390 |
) -> None: |
| 391 |
"""Regression test for #929: the queries at the cursor or selection are |
| 392 |
returned once each, in the order they appear in the buffer.""" |
| 393 |
create = "create table foo (a int);" |
| 394 |
insert = "insert into foo values (1);" |
| 395 |
select = "select * from foo;" |
| 396 |
drop = "drop table foo;" |
| 397 |
async with app.run_test() as pilot: |
| 398 |
await wait_for_workers(app) |
| 399 |
|
| 400 |
editor = await wait_for_editor(pilot, app) |
| 401 |
|
| 402 |
assert editor.text_input is not None |
| 403 |
assert editor.text_input.is_syntax_aware |
| 404 |
editor.text = f"{create}\n{insert}\n{select}\n{drop}\n" |
| 405 |
await pilot.pause() |
| 406 |
|
| 407 |
# tree-sitter does not capture the semicolons in buffer order |
| 408 |
assert find_separators(editor.text) == [ |
| 409 |
(0, 25), |
| 410 |
(1, 27), |
| 411 |
(2, 18), |
| 412 |
(3, 15), |
| 413 |
] |
| 414 |
|
| 415 |
# the whole buffer |
| 416 |
editor.selection = Selection((0, 0), (4, 0)) |
| 417 |
assert editor.selected_queries() == [create, insert, select, drop] |
| 418 |
|
| 419 |
# a reverse selection is the same as a forward one |
| 420 |
editor.selection = Selection((4, 0), (0, 0)) |
| 421 |
assert editor.selected_queries() == [create, insert, select, drop] |
| 422 |
|
| 423 |
# a selection that partially covers two queries runs both of them |
| 424 |
editor.selection = Selection((1, 5), (2, 5)) |
| 425 |
assert editor.selected_queries() == [insert, select] |
| 426 |
|
| 427 |
# a cursor inside a query runs only that query |
| 428 |
for row, query in enumerate([create, insert, select, drop]): |
| 429 |
editor.selection = Selection((row, 3), (row, 3)) |
| 430 |
assert editor.selected_queries() == [query] |
| 431 |
|
| 432 |
# a cursor just after a semicolon runs the query it terminates, |
| 433 |
# not the one that follows it |
| 434 |
editor.selection = Selection((1, 27), (1, 27)) |
| 435 |
assert editor.selected_queries() == [insert] |
| 436 |
|
| 437 |
# a cursor at the start of a query runs that query |
| 438 |
editor.selection = Selection((2, 0), (2, 0)) |
| 439 |
assert editor.selected_queries() == [select] |
| 440 |
|
| 441 |
# a cursor in the trailing whitespace runs the last query |
| 442 |
editor.selection = Selection((4, 0), (4, 0)) |
| 443 |
assert editor.selected_queries() == [drop] |
| 444 |
|
| 445 |
# a semicolon in a string literal does not separate queries |
| 446 |
editor.text = "select 'a;b'" |
| 447 |
await pilot.pause() |
| 448 |
editor.selection = Selection((0, 0), (0, 0)) |
| 449 |
assert editor.selected_queries() == ["select 'a;b'"] |
| 450 |
|
| 451 |
|
| 452 |
@pytest.mark.asyncio |
| 453 |
async def test_symbol_scan_failure_warns_once_per_failure_streak( |
| 454 |
app: Harlequin, |
| 455 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 456 |
monkeypatch: pytest.MonkeyPatch, |
| 457 |
) -> None: |
| 458 |
"""A failed symbol scan warns instead of crashing the app. |
| 459 |
|
| 460 |
Regression test for #1117: read_symbols used Textual's default |
| 461 |
exit_on_error=True, so a buffer the scanner couldn't parse (or any bug in |
| 462 |
the scanner) took the whole app down with it. The failure warns once per |
| 463 |
streak -- repeated failures on the same buffer stay quiet until a scan |
| 464 |
succeeds again. |
| 465 |
""" |
| 466 |
WARNING = "Harlequin could not read this buffer's identifiers." |
| 467 |
outcomes: list[str] = ["fail", "fail", "ok", "fail"] |
| 468 |
# the editor schedules a scan of the buffer shortly after mounting; it |
| 469 |
# would cancel (exclusive group) whichever direct scan below it landed on, |
| 470 |
# so drop that handler from the registry before the app mounts. |
| 471 |
monkeypatch.setitem(CodeEditor._decorated_handlers, TextArea.Changed, []) |
| 472 |
|
| 473 |
def flaky_scan(text: str) -> BufferSymbols: |
| 474 |
outcome = outcomes.pop(0) |
| 475 |
if outcome == "fail": |
| 476 |
raise RuntimeError("symbol scan boom") |
| 477 |
return real_find_symbols(text) |
| 478 |
|
| 479 |
def warning_count() -> int: |
| 480 |
return len( |
| 481 |
[ |
| 482 |
notification |
| 483 |
for notification in list(app._notifications) |
| 484 |
if notification.severity == "warning" |
| 485 |
and notification.message == WARNING |
| 486 |
] |
| 487 |
) |
| 488 |
|
| 489 |
async with app.run_test() as pilot: |
| 490 |
await wait_for_workers(app) |
| 491 |
editor = await wait_for_editor(pilot, app) |
| 492 |
monkeypatch.setattr("harlequin.components.code_editor.find_symbols", flaky_scan) |
| 493 |
|
| 494 |
# the first failure of a streak warns ... |
| 495 |
with pytest.raises(WorkerFailed): |
| 496 |
await editor.read_symbols("select 1").wait() |
| 497 |
await pilot.pause() |
| 498 |
assert warning_count() == 1 |
| 499 |
|
| 500 |
# ... a repeated failure on the same buffer does not |
| 501 |
with pytest.raises(WorkerFailed): |
| 502 |
await editor.read_symbols("select 1").wait() |
| 503 |
await pilot.pause() |
| 504 |
assert warning_count() == 1 |
| 505 |
|
| 506 |
# a successful scan resets the streak |
| 507 |
await editor.read_symbols("select 1").wait() |
| 508 |
await wait_for( |
| 509 |
pilot, |
| 510 |
lambda: not editor._symbol_scan_failed, |
| 511 |
description="the successful scan to reset the failure streak", |
| 512 |
) |
| 513 |
assert warning_count() == 1 |
| 514 |
|
| 515 |
# so the next failure warns again |
| 516 |
with pytest.raises(WorkerFailed): |
| 517 |
await editor.read_symbols("select 1").wait() |
| 518 |
await pilot.pause() |
| 519 |
assert warning_count() == 2 |
| 520 |
|
| 521 |
# and the app never saw an unhandled exception |
| 522 |
assert app._exception is None |
| 523 |
assert app.is_running |
| 524 |
|
| 525 |
|
| 526 |
@pytest.mark.asyncio |
| 527 |
async def test_selected_queries_split_on_character_columns( |
| 528 |
app: Harlequin, |
| 529 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 530 |
) -> None: |
| 531 |
"""Regression test for #1015: the editor and `harlequin.statements` split |
| 532 |
at the same place. |
| 533 |
|
| 534 |
tree-sitter reports columns in bytes, and the editor used to feed one |
| 535 |
straight to `get_text_range()`, which wants characters. Any non-ASCII |
| 536 |
before a semicolon shifted the cut, and both halves came out as syntax |
| 537 |
errors -- this split into "select '日本語';select" and "2". |
| 538 |
""" |
| 539 |
async with app.run_test() as pilot: |
| 540 |
await wait_for_workers(app) |
| 541 |
|
| 542 |
editor = await wait_for_editor(pilot, app) |
| 543 |
|
| 544 |
editor.text = "select '日本語';select 2" |
| 545 |
await pilot.pause() |
| 546 |
editor.selection = Selection((0, 0), (0, 22)) |
| 547 |
assert editor.selected_queries() == ["select '日本語';", "select 2"] |
| 548 |
assert editor.selected_queries() == [s.sql for s in split(editor.text)] |
| 549 |
|
| 550 |
|
| 551 |
@pytest.mark.asyncio |
| 552 |
async def test_selected_queries_do_not_split_dollar_quoted_bodies( |
| 553 |
app: Harlequin, |
| 554 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 555 |
) -> None: |
| 556 |
"""Regression test for #1019: the editor and `harlequin.statements` agree, |
| 557 |
and neither treats a semicolon inside `$$ ... $$` as a separator.""" |
| 558 |
async with app.run_test() as pilot: |
| 559 |
await wait_for_workers(app) |
| 560 |
|
| 561 |
editor = await wait_for_editor(pilot, app) |
| 562 |
|
| 563 |
script = "create function f() as $$ select 1; $$; select 2" |
| 564 |
editor.text = script |
| 565 |
await pilot.pause() |
| 566 |
editor.selection = Selection((0, 0), (0, len(script))) |
| 567 |
assert editor.selected_queries() == [ |
| 568 |
"create function f() as $$ select 1; $$;", |
| 569 |
"select 2", |
| 570 |
] |
| 571 |
assert editor.selected_queries() == [s.sql for s in split(editor.text)] |
| 572 |
|
| 573 |
|
| 574 |
@pytest.mark.asyncio |
| 575 |
async def test_buffer_symbols_reach_the_completers( |
| 576 |
app: Harlequin, |
| 577 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 578 |
) -> None: |
| 579 |
async with app.run_test() as pilot: |
| 580 |
await wait_for_workers(app) |
| 581 |
editor = await wait_for_editor(pilot, app) |
| 582 |
word_completer = await wait_for_value( |
| 583 |
pilot, |
| 584 |
lambda: app.editor_collection.word_completer, |
| 585 |
description="the word completer to be built", |
| 586 |
) |
| 587 |
member_completer = app.editor_collection.member_completer |
| 588 |
assert member_completer is not None |
| 589 |
|
| 590 |
assert not word_completer("my_c") |
| 591 |
assert not member_completer("t.my_c") |
| 592 |
|
| 593 |
editor.text = ( |
| 594 |
"with my_cte as (select 1 as my_col) select t.my_col from my_cte t" |
| 595 |
) |
| 596 |
|
| 597 |
# the editor re-reads the buffer on a timer |
| 598 |
await wait_for( |
| 599 |
pilot, |
| 600 |
lambda: bool(word_completer("my_c")), |
| 601 |
description="the buffer's own symbols to reach the word completer", |
| 602 |
interval=POLL_INTERVAL, |
| 603 |
) |
| 604 |
assert word_completer("my_c")[0] == (("my_cte", "buf"), "my_cte") |
| 605 |
assert member_completer("t.my_c") == [(("t.my_col", "buf"), "t.my_col")] |
| 606 |
|
| 607 |
# symbols belong to the buffer they were read from, so switching tabs |
| 608 |
# away from that query drops them, and switching back brings them back. |
| 609 |
first_buffer_id = app.editor_collection.active |
| 610 |
assert first_buffer_id is not None |
| 611 |
await app.editor_collection.action_new_buffer() |
| 612 |
await wait_for( |
| 613 |
pilot, |
| 614 |
lambda: not word_completer("my_c"), |
| 615 |
description="the new buffer's symbols to replace the old one's", |
| 616 |
interval=POLL_INTERVAL, |
| 617 |
) |
| 618 |
|
| 619 |
app.editor_collection.tabs.active = first_buffer_id |
| 620 |
await wait_for( |
| 621 |
pilot, |
| 622 |
lambda: bool(word_completer("my_c")), |
| 623 |
description="the first buffer's symbols to come back", |
| 624 |
interval=POLL_INTERVAL, |
| 625 |
) |
| 626 |
assert word_completer("my_c")[0] == (("my_cte", "buf"), "my_cte") |
| 627 |
|
| 628 |
|
| 629 |
@pytest.mark.asyncio |
| 630 |
async def test_numbers_do_not_open_the_completion_list( |
| 631 |
app: Harlequin, |
| 632 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 633 |
) -> None: |
| 634 |
"""A number leaves the list closed, so enter inserts a newline.""" |
| 635 |
async with app.run_test() as pilot: |
| 636 |
await wait_for_workers(app) |
| 637 |
editor = await wait_for_editor(pilot, app) |
| 638 |
word_completer = await wait_for_value( |
| 639 |
pilot, |
| 640 |
lambda: app.editor_collection.word_completer, |
| 641 |
description="the word completer to be built", |
| 642 |
) |
| 643 |
word_completer.update_buffer_symbols(BufferSymbols(names=("foo_1",))) |
| 644 |
editor.focus() |
| 645 |
|
| 646 |
await pilot.press("1") |
| 647 |
await pilot.pause() |
| 648 |
await wait_for_workers(app) |
| 649 |
await pilot.pause() |
| 650 |
assert not editor.completion_list.is_open |
| 651 |
|
| 652 |
await pilot.press("enter") |
| 653 |
await pilot.pause() |
| 654 |
assert editor.text == "1\n" |
| 655 |
|
| 656 |
|
| 657 |
def press_alt_e(app: Harlequin) -> None: |
| 658 |
"""Sends alt+e the way a terminal does. |
| 659 |
|
| 660 |
ESC + e parses to the key `alt+e` carrying the character "e", which |
| 661 |
`pilot.press()` does not send and a focused TextArea would insert. |
| 662 |
""" |
| 663 |
key_event = events.Key("alt+e", "e") |
| 664 |
key_event.set_sender(app) |
| 665 |
assert app._driver is not None |
| 666 |
app._driver.send_message(key_event) |
| 667 |
|
| 668 |
|
| 669 |
@pytest.mark.asyncio |
| 670 |
async def test_external_editor_round_trip( |
| 671 |
app: Harlequin, |
| 672 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 673 |
monkeypatch: pytest.MonkeyPatch, |
| 674 |
) -> None: |
| 675 |
"""The buffer goes out to $EDITOR as a file and comes back as one edit. |
| 676 |
|
| 677 |
The suspend is patched out: the headless driver cannot suspend, so |
| 678 |
run_in_terminal is the seam that stands in for a human's editor. |
| 679 |
""" |
| 680 |
monkeypatch.setenv("EDITOR", "ed") |
| 681 |
seen_text: list[str] = [] |
| 682 |
|
| 683 |
def fake_run_in_terminal(app: App, argv: Sequence[str]) -> int: |
| 684 |
path = Path(argv[-1]) |
| 685 |
seen_text.append(path.read_text(encoding="utf-8")) |
| 686 |
path.write_text("select 2", encoding="utf-8") |
| 687 |
return 0 |
| 688 |
|
| 689 |
monkeypatch.setattr("harlequin.external.run_in_terminal", fake_run_in_terminal) |
| 690 |
|
| 691 |
async with app.run_test() as pilot: |
| 692 |
await wait_for_workers(app) |
| 693 |
editor = await wait_for_editor(pilot, app) |
| 694 |
editor.text = "select 1" |
| 695 |
editor.focus() |
| 696 |
await pilot.press("ctrl+end") |
| 697 |
|
| 698 |
press_alt_e(app) |
| 699 |
await pilot.pause() |
| 700 |
|
| 701 |
assert seen_text == ["select 1"] |
| 702 |
assert editor.text == "select 2" |
| 703 |
# the cursor comes back where it was, not at the start of the buffer |
| 704 |
assert editor.selection == Selection((0, 8), (0, 8)) |
| 705 |
|
| 706 |
# the round trip is one undo away |
| 707 |
await pilot.press("ctrl+z") |
| 708 |
await pilot.pause() |
| 709 |
assert editor.text == "select 1" |
| 710 |
|
| 711 |
|
| 712 |
@pytest.mark.asyncio |
| 713 |
async def test_external_editor_nonzero_exit_discards_the_edit( |
| 714 |
app: Harlequin, |
| 715 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 716 |
monkeypatch: pytest.MonkeyPatch, |
| 717 |
) -> None: |
| 718 |
monkeypatch.setenv("EDITOR", "ed") |
| 719 |
|
| 720 |
def fake_run_in_terminal(app: App, argv: Sequence[str]) -> int: |
| 721 |
Path(argv[-1]).write_text("select 2", encoding="utf-8") |
| 722 |
return 1 |
| 723 |
|
| 724 |
monkeypatch.setattr("harlequin.external.run_in_terminal", fake_run_in_terminal) |
| 725 |
|
| 726 |
async with app.run_test() as pilot: |
| 727 |
await wait_for_workers(app) |
| 728 |
editor = await wait_for_editor(pilot, app) |
| 729 |
editor.text = "select 1" |
| 730 |
editor.focus() |
| 731 |
|
| 732 |
press_alt_e(app) |
| 733 |
await pilot.pause() |
| 734 |
|
| 735 |
assert editor.text == "select 1" |
| 736 |
notification = list(app._notifications)[-1] |
| 737 |
assert "status 1" in notification.message |
| 738 |
assert notification.severity == "warning" |
| 739 |
|
| 740 |
|
| 741 |
@pytest.mark.asyncio |
| 742 |
async def test_external_editor_without_an_editor_named( |
| 743 |
app: Harlequin, |
| 744 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 745 |
monkeypatch: pytest.MonkeyPatch, |
| 746 |
) -> None: |
| 747 |
monkeypatch.delenv("VISUAL", raising=False) |
| 748 |
monkeypatch.delenv("EDITOR", raising=False) |
| 749 |
|
| 750 |
async with app.run_test() as pilot: |
| 751 |
await wait_for_workers(app) |
| 752 |
editor = await wait_for_editor(pilot, app) |
| 753 |
editor.text = "select 1" |
| 754 |
editor.focus() |
| 755 |
|
| 756 |
press_alt_e(app) |
| 757 |
modal = await wait_for_error_modal(pilot, app) |
| 758 |
assert "$EDITOR" in modal.text |
| 759 |
assert editor.text == "select 1" |
| 760 |
|
| 761 |
|
| 762 |
@pytest.mark.asyncio |
| 763 |
async def test_external_editor_in_a_terminal_that_cannot_suspend( |
| 764 |
app: Harlequin, |
| 765 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 766 |
monkeypatch: pytest.MonkeyPatch, |
| 767 |
) -> None: |
| 768 |
"""The real suspend, which the headless driver refuses, is an error modal.""" |
| 769 |
monkeypatch.setenv("EDITOR", sys.executable) |
| 770 |
|
| 771 |
async with app.run_test() as pilot: |
| 772 |
await wait_for_workers(app) |
| 773 |
editor = await wait_for_editor(pilot, app) |
| 774 |
editor.text = "select 1" |
| 775 |
editor.focus() |
| 776 |
|
| 777 |
press_alt_e(app) |
| 778 |
modal = await wait_for_error_modal(pilot, app) |
| 779 |
assert "suspend" in modal.text |
| 780 |
assert editor.text == "select 1" |
| 781 |
assert app.is_running |
| 782 |
|
| 783 |
|
| 784 |
@pytest.mark.asyncio |
| 785 |
async def test_external_editor_clamps_the_cursor_to_a_shorter_buffer( |
| 786 |
app: Harlequin, |
| 787 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 788 |
monkeypatch: pytest.MonkeyPatch, |
| 789 |
) -> None: |
| 790 |
"""A position the edited buffer no longer holds lands at the nearest one.""" |
| 791 |
monkeypatch.setenv("EDITOR", "ed") |
| 792 |
|
| 793 |
def fake_run_in_terminal(app: App, argv: Sequence[str]) -> int: |
| 794 |
Path(argv[-1]).write_text("select 1\nfrom f", encoding="utf-8") |
| 795 |
return 0 |
| 796 |
|
| 797 |
monkeypatch.setattr("harlequin.external.run_in_terminal", fake_run_in_terminal) |
| 798 |
|
| 799 |
async with app.run_test() as pilot: |
| 800 |
await wait_for_workers(app) |
| 801 |
editor = await wait_for_editor(pilot, app) |
| 802 |
editor.text = "select 1\nfrom foobar" |
| 803 |
editor.focus() |
| 804 |
await pilot.press("ctrl+end") |
| 805 |
assert editor.selection == Selection((1, 11), (1, 11)) |
| 806 |
|
| 807 |
press_alt_e(app) |
| 808 |
await pilot.pause() |
| 809 |
|
| 810 |
assert editor.text == "select 1\nfrom f" |
| 811 |
assert editor.selection == Selection((1, 6), (1, 6)) |