File: test_path.py

package info (click to toggle)
python-questionary 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 960 kB
  • sloc: python: 3,917; makefile: 66
file content (148 lines) | stat: -rw-r--r-- 4,384 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
import prompt_toolkit
import pytest
from prompt_toolkit.completion import Completer
from prompt_toolkit.completion import Completion

from tests.utils import KeyInputs
from tests.utils import feed_cli_with_input


@pytest.fixture
def path_completion_tree(tmp_path):
    needed_directories = [
        tmp_path / "foo",
        tmp_path / "foo" / "buz",  # alphabetically after baz.any
        tmp_path / "bar",
        tmp_path / "baz",
    ]

    needed_files = [tmp_path / "foo" / "baz.any", tmp_path / "foo" / "foobar.any"]

    for d in needed_directories:
        d.mkdir()

    for f in needed_files:
        f.open("a").close()
    return tmp_path


def test_path():
    message = "Pick your path "
    text = "myfile.py" + KeyInputs.ENTER
    result, cli = feed_cli_with_input("path", message, text)
    assert result == "myfile.py"


@pytest.mark.skipif(
    prompt_toolkit.__version__.startswith("2"), reason="requires prompt toolkit >= 3.0"
)
def test_complete_path(path_completion_tree):
    test_input = str(path_completion_tree / "ba")
    message = "Pick your path"
    texts = [
        test_input,
        KeyInputs.TAB + KeyInputs.TAB + KeyInputs.ENTER,
        KeyInputs.ENTER,
    ]

    result, cli = feed_cli_with_input("path", message, texts, 0.1)
    assert result == str(path_completion_tree / "baz")


@pytest.mark.skipif(
    prompt_toolkit.__version__.startswith("2"), reason="requires prompt toolkit >= 3.0"
)
def test_complete_requires_explicit_enter(path_completion_tree):
    # checks that an autocomplete needs to be confirmed with an enter and that the
    # enter doesn't directly submit the result
    test_input = str(path_completion_tree / "ba")
    message = "Pick your path"
    texts = [
        test_input,
        KeyInputs.TAB + KeyInputs.TAB + KeyInputs.ENTER,
        "foo" + KeyInputs.ENTER,
    ]

    result, cli = feed_cli_with_input("path", message, texts, 0.1)

    assert result == str(path_completion_tree / "baz" / "foo")


@pytest.mark.skipif(
    prompt_toolkit.__version__.startswith("2"), reason="requires prompt toolkit >= 3.0"
)
def test_complete_path_directories_only(path_completion_tree):
    test_input = str(path_completion_tree / "foo" / "b")
    message = "Pick your path"
    texts = [test_input, KeyInputs.TAB + KeyInputs.ENTER, KeyInputs.ENTER]

    result, cli = feed_cli_with_input(
        "path", message, texts, 0.1, only_directories=True
    )
    assert result == str(path_completion_tree / "foo" / "buz")


@pytest.mark.skipif(
    prompt_toolkit.__version__.startswith("2"), reason="requires prompt toolkit >= 3.0"
)
def test_get_paths(path_completion_tree):
    """Starting directories for path completion can be set."""
    test_input = "ba"
    message = "Pick your path"
    texts = [
        test_input,
        KeyInputs.TAB + KeyInputs.ENTER,
        KeyInputs.ENTER,
    ]

    result, cli = feed_cli_with_input(
        "path",
        message,
        texts,
        0.1,
        get_paths=lambda: [str(path_completion_tree / "foo")],
    )
    assert result == "baz.any"


@pytest.mark.skipif(
    prompt_toolkit.__version__.startswith("2"), reason="requires prompt toolkit >= 3.0"
)
def test_get_paths_validation(path_completion_tree):
    """`get_paths` must contain only existing directories."""
    test_input = str(path_completion_tree / "ba")
    message = "Pick your path"
    texts = [
        test_input,
        KeyInputs.TAB + KeyInputs.TAB + KeyInputs.ENTER,
        KeyInputs.ENTER,
    ]
    with pytest.raises(ValueError) as excinfo:
        feed_cli_with_input(
            "path",
            message,
            texts,
            0.1,
            get_paths=lambda: [str(path_completion_tree / "not_existing")],
        )
    assert "'get_paths' must return only existing directories" in str(excinfo)


@pytest.mark.skipif(
    prompt_toolkit.__version__.startswith("2"), reason="requires prompt toolkit >= 3.0"
)
def test_complete_custom_completer():
    test_path = "foobar"

    class CustomCompleter(Completer):
        def get_completions(self, _, __):
            yield Completion(test_path)

    message = "Pick your path"
    texts = ["baz", KeyInputs.TAB + KeyInputs.ENTER, KeyInputs.ENTER]

    result, cli = feed_cli_with_input(
        "path", message, texts, 0.1, completer=CustomCompleter()
    )
    assert result == "baz" + test_path