File: test_filepath.py

package info (click to toggle)
python-inquirerpy 0.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: python: 9,463; makefile: 15
file content (293 lines) | stat: -rw-r--r-- 10,614 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import os
import shutil
import tempfile
import unittest
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import ANY, call, patch

from prompt_toolkit.buffer import Buffer
from prompt_toolkit.completion import CompleteEvent
from prompt_toolkit.document import Document
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.input.defaults import create_pipe_input
from prompt_toolkit.output import DummyOutput
from prompt_toolkit.shortcuts.prompt import CompleteStyle

from InquirerPy.exceptions import InvalidArgument
from InquirerPy.prompts.filepath import FilePathCompleter, FilePathPrompt
from InquirerPy.utils import InquirerPyStyle
from InquirerPy.validator import PathValidator


class TestFilePath(unittest.TestCase):
    def setUp(self):
        self.dirs_to_create = ["dir1", "dir2", "dir3", ".dir"]
        self.files_to_create = ["file1", "file2", "file3", ".file"]
        self.test_dir = Path(tempfile.mkdtemp())
        self.create_temp_files()

    def tearDown(self):
        shutil.rmtree(self.test_dir)

    @contextmanager
    def chdir(self, directory):
        orig_dir = os.getcwd()
        os.chdir(directory)
        try:
            yield
        finally:
            os.chdir(orig_dir)

    def create_temp_files(self):
        for directory in self.dirs_to_create:
            self.test_dir.joinpath(directory).mkdir(exist_ok=True)
        for file in self.files_to_create:
            with self.test_dir.joinpath(file).open("wb") as output_file:
                output_file.write("".encode("UTF-8"))

    def test_completer_explicit_currdir_all(self):
        with self.chdir(self.test_dir):
            completer = FilePathCompleter()
            doc_text = "./"
            doc = Document(doc_text, len(doc_text))
            event = CompleteEvent()
            completions = [
                completion.text
                for completion in list(completer.get_completions(doc, event))
            ]
            self.assertEqual(
                sorted(completions),
                sorted(self.dirs_to_create + self.files_to_create),
            )

    def test_completer_currdir_file(self):
        with self.chdir(self.test_dir):
            completer = FilePathCompleter()
            doc_text = "./file"
            doc = Document(doc_text, len(doc_text))
            event = CompleteEvent()
            completions = [
                completion.text
                for completion in list(completer.get_completions(doc, event))
            ]
            self.assertEqual(sorted(completions), ["file1", "file2", "file3"])

    def test_completer_hidden(self):
        with self.chdir(self.test_dir):
            completer = FilePathCompleter()
            doc_text = "."
            doc = Document(doc_text, len(doc_text))
            event = CompleteEvent()
            completions = [
                completion.text
                for completion in list(completer.get_completions(doc, event))
            ]
            self.assertEqual(sorted(completions), [".dir", ".file"])

    def test_completer_normal(self):
        with self.chdir(self.test_dir):
            completer = FilePathCompleter()
            doc_text = "dir"
            doc = Document(doc_text, len(doc_text))
            event = CompleteEvent()
            completions = [
                completion.text
                for completion in list(completer.get_completions(doc, event))
            ]
            self.assertEqual(sorted(completions), ["dir1", "dir2", "dir3"])

    def test_completer_expanduser(self):
        with self.chdir(self.test_dir):
            completer = FilePathCompleter()
            doc_text = "~/"
            doc = Document(doc_text, len(doc_text))
            event = CompleteEvent()
            completions = [
                completion.text
                for completion in list(completer.get_completions(doc, event))
            ]
            self.assertGreater(len(completions), 0)

    def test_completer_dir_only(self):
        with self.chdir(self.test_dir):
            completer = FilePathCompleter(only_directories=True)
            doc_text = "./"
            doc = Document(doc_text, len(doc_text))
            event = CompleteEvent()
            completions = [
                completion.text
                for completion in list(completer.get_completions(doc, event))
            ]
            self.assertEqual(sorted(completions), sorted(self.dirs_to_create))

    def test_completer_file_only(self):
        with self.chdir(self.test_dir):
            completer = FilePathCompleter(only_files=True)
            doc_text = "./"
            doc = Document(doc_text, len(doc_text))
            event = CompleteEvent()
            completions = [
                completion.text
                for completion in list(completer.get_completions(doc, event))
            ]
            self.assertEqual(sorted(completions), sorted(self.files_to_create))

    def test_input(self):
        with create_pipe_input() as inp:
            inp.send_text("./file1\n")
            filepath_prompt = FilePathPrompt(
                message="hello",
                style=InquirerPyStyle({"qmark": "bold"}),
                input=inp,
                output=DummyOutput(),
            )
            result = filepath_prompt.execute()
            self.assertEqual(result, "./file1")
            self.assertEqual(filepath_prompt.status["answered"], True)
            self.assertEqual(filepath_prompt.status["result"], "./file1")

    def test_default_answer(self):
        with create_pipe_input() as inp:
            inp.send_text("\n")
            filepath_prompt = FilePathPrompt(
                message="hello",
                style=InquirerPyStyle({"qmark": "bold"}),
                default=".vim",
                input=inp,
                output=DummyOutput(),
            )
            result = filepath_prompt.execute()
            self.assertEqual(result, ".vim")
            self.assertEqual(filepath_prompt.status["answered"], True)
            self.assertEqual(filepath_prompt.status["result"], ".vim")

    @patch.object(Buffer, "validate_and_handle")
    def test_validation(self, mocked_validate):
        def _hello():
            filepath_prompt._session.app.exit(result="hello")

        with create_pipe_input() as inp:
            mocked_validate.side_effect = _hello
            inp.send_text("hello\n")
            filepath_prompt = FilePathPrompt(
                message="fooboo",
                style=InquirerPyStyle({"qmark": ""}),
                default=".vim",
                validate=PathValidator(),
                input=inp,
                output=DummyOutput(),
            )
            result = filepath_prompt.execute()
            mocked_validate.assert_called_once()
            self.assertEqual(result, "hello")
            self.assertEqual(filepath_prompt.status["answered"], False)
            self.assertEqual(filepath_prompt.status["result"], None)

    def test_get_prompt_message(self):
        filepath_prompt = FilePathPrompt(
            message="brah", style=InquirerPyStyle({"foo": ""}), qmark="!", amark="x"
        )
        message = filepath_prompt._get_prompt_message()
        self.assertEqual(
            message,
            [
                ("class:questionmark", "!"),
                ("class:question", " brah"),
                ("class:instruction", " "),
            ],
        )

        filepath_prompt.status["answered"] = True
        filepath_prompt.status["result"] = "hello"
        message = filepath_prompt._get_prompt_message()
        self.assertEqual(
            message,
            [
                ("class:answermark", "x"),
                ("class:answered_question", " brah"),
                ("class:answer", " hello"),
            ],
        )

    @patch("InquirerPy.prompts.input.SimpleLexer")
    @patch("InquirerPy.prompts.filepath.FilePathPrompt._get_prompt_message")
    @patch("InquirerPy.base.simple.Style.from_dict")
    @patch("InquirerPy.base.simple.KeyBindings")
    @patch("InquirerPy.prompts.input.PromptSession")
    def test_callable_called(
        self,
        MockedSession,
        MockedKeyBindings,
        MockedStyle,
        mocked_message,
        MockedLexer,
    ):
        def _validation(_):
            return True

        FilePathPrompt(
            message="yes",
            style=InquirerPyStyle({"yes": ""}),
            default="",
            qmark="XD",
            multicolumn_complete=True,
            validate=_validation,
            vi_mode=True,
            only_directories=True,
        )
        kb = MockedKeyBindings()
        style = MockedStyle()
        lexer = MockedLexer()
        MockedSession.assert_called_once_with(
            message=mocked_message,
            key_bindings=kb,
            style=style,
            completer=ANY,
            validator=ANY,
            validate_while_typing=False,
            input=None,
            output=None,
            editing_mode=EditingMode.VI,
            lexer=lexer,
            is_password=False,
            multiline=False,
            complete_style=CompleteStyle.MULTI_COLUMN,
            wrap_lines=True,
            bottom_toolbar=None,
        )

        MockedStyle.assert_has_calls([call({"yes": ""})])

    def test_invalid_argument(self):
        self.assertRaises(InvalidArgument, FilePathPrompt, "hello", None, False, 12)
        FilePathPrompt(message="hello", default=lambda _: "12")

    @patch("os.name")
    def test_completer_explicit_currdir_all_win(self, mocked_platform):
        with self.chdir(self.test_dir):
            completer = FilePathCompleter()
            doc_text = ".\\"
            doc = Document(doc_text, len(doc_text))
            event = CompleteEvent()
            completions = [
                completion.text
                for completion in list(completer.get_completions(doc, event))
            ]
            self.assertEqual(
                sorted(completions),
                sorted(self.dirs_to_create + self.files_to_create),
            )

    @patch("os.name")
    def test_completer_currdir_file_win(self, mocked_platform):
        with self.chdir(self.test_dir):
            completer = FilePathCompleter()
            doc_text = ".\\file"
            doc = Document(doc_text, len(doc_text))
            event = CompleteEvent()
            completions = [
                completion.text
                for completion in list(completer.get_completions(doc, event))
            ]
            self.assertEqual(sorted(completions), ["file1", "file2", "file3"])