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. I'm not sure whether more changes are needed.

Fixes: #344
  • Loading branch information
FantasqueX committed Dec 30, 2023
1 parent 04a90f8 commit c4c95da
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 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
53 changes: 38 additions & 15 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,41 @@ 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()
)

# 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)
prompt_toolkit_version = tuple([int(v) for v in prompt_toolkit.VERSION])

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:
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 c4c95da

Please sign in to comment.