Skip to content

Commit

Permalink
Try to support prompt_toolkit >3.0.37
Browse files Browse the repository at this point in the history
Currently one test will fail with latest prompt_toolkit(3.0.43).
test_blank_line_fix will throw RuntimeError: no running event loop.
By bisecting commit, the culprit is
prompt-toolkit/python-prompt-toolkit@a775996.
This commit replaces custom `get_event_loop` with `asyncio.get_event_loop`. The former will creator a new loop if `asyncio.get_running_loop` fails while the latter won't.
I mimic the changes in the examples to use `asyncio.run` and the test
passes.

Fixes: tmbo#344
  • Loading branch information
FantasqueX committed Dec 30, 2023
1 parent 04a90f8 commit e3eea63
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
10 changes: 5 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ license = "MIT"

[tool.poetry.dependencies]
python = ">=3.8"
prompt_toolkit = ">=2.0,<=3.0.36" # once https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1726 is fixed, this can be changed to ">=2.0,<4.0"
prompt_toolkit = ">=2.0,<4.0" # once https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1726 is fixed, this can be changed to ">=2.0,<4.0"

[tool.poetry.group.docs]
optional = true
Expand Down
52 changes: 39 additions & 13 deletions tests/prompts/test_common.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import asyncio
from unittest.mock import Mock
from unittest.mock import call

import pytest
import prompt_toolkit
from prompt_toolkit.document import Document
from prompt_toolkit.output import DummyOutput
from prompt_toolkit.styles import Attrs
from prompt_toolkit.validation import ValidationError
from prompt_toolkit.input.defaults import create_pipe_input
from prompt_toolkit.validation import Validator

from questionary import Choice
Expand Down Expand Up @@ -72,21 +75,44 @@ def get_prompt_tokens():

ic = InquirerControl(["a", "b", "c"])

def run(inp):
inp.send_text("")
layout = common.create_inquirer_layout(
ic, get_prompt_tokens, input=inp, output=DummyOutput()
)
prompt_toolkit_version = tuple([int(v) for v in prompt_toolkit.VERSION])

# usually this would be 2000000000000000000000000000000
# but `common._fix_unecessary_blank_lines` makes sure
# the main window is not as greedy (avoiding blank lines)
assert (
layout.container.preferred_height(100, 200).max
== 1000000000000000000000000000001
)
if prompt_toolkit_version >= (3, 0, 37):

async def run(inp):
inp.send_text("")
layout = common.create_inquirer_layout(
ic, get_prompt_tokens, input=inp, output=DummyOutput()
)

# usually this would be 2000000000000000000000000000000
# but `common._fix_unecessary_blank_lines` makes sure
# the main window is not as greedy (avoiding blank lines)
assert (
layout.container.preferred_height(100, 200).max
== 1000000000000000000000000000001
)

with create_pipe_input() as inp:
asyncio.run(run(inp))

else:

execute_with_input_pipe(run)
def run(inp):
inp.send_text("")
layout = common.create_inquirer_layout(
ic, get_prompt_tokens, input=inp, output=DummyOutput()
)

# usually this would be 2000000000000000000000000000000
# but `common._fix_unecessary_blank_lines` makes sure
# the main window is not as greedy (avoiding blank lines)
assert (
layout.container.preferred_height(100, 200).max
== 1000000000000000000000000000001
)

execute_with_input_pipe(run)


def test_prompt_highlight_coexist():
Expand Down

0 comments on commit e3eea63

Please sign in to comment.