From 473f22c5bde76329de5144cf5c0b952dd7c6f67b Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Wed, 2 Feb 2022 15:04:20 +1100 Subject: [PATCH] fix(filepath): windows completion #32 --- InquirerPy/prompts/filepath.py | 11 ++++++----- examples/alternate/filepath.py | 5 ++++- examples/classic/filepath.py | 5 ++++- tests/prompts/test_filepath.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/InquirerPy/prompts/filepath.py b/InquirerPy/prompts/filepath.py index 990a878..e18b879 100644 --- a/InquirerPy/prompts/filepath.py +++ b/InquirerPy/prompts/filepath.py @@ -37,6 +37,7 @@ class FilePathCompleter(Completer): def __init__(self, only_directories: bool = False, only_files: bool = False): self._only_directories = only_directories self._only_files = only_files + self._delimiter = "/" if os.name == "posix" else "\\" def get_completions( self, document, complete_event @@ -51,11 +52,11 @@ def get_completions( dirname = Path.cwd() validation = lambda file, doc_text: True elif document.text.startswith("~"): - dirname = Path(os.path.dirname("%s%s" % (Path.home(), document.text[1:]))) + dirname = Path(os.path.dirname(f"{Path.home()}{document.text[1:]}")) validation = lambda file, doc_text: str(file).startswith( - "%s%s" % (Path.home(), doc_text[1:]) + f"{Path.home()}{doc_text[1:]}" ) - elif document.text.startswith("./"): + elif document.text.startswith(f".{self._delimiter}"): dirname = Path(os.path.dirname(document.text)) validation = lambda file, doc_text: str(file).startswith(doc_text[2:]) else: @@ -78,9 +79,9 @@ def _get_completion( file_name = file.name display_name = file_name if file.is_dir(): - display_name = "%s/" % file_name + display_name = f"{file_name}{self._delimiter}" yield Completion( - "%s" % file.name, + file.name, start_position=-1 * len(os.path.basename(document.text)), display=display_name, ) diff --git a/examples/alternate/filepath.py b/examples/alternate/filepath.py index 3c1fc8a..4787fba 100644 --- a/examples/alternate/filepath.py +++ b/examples/alternate/filepath.py @@ -1,11 +1,14 @@ +import os + from InquirerPy import inquirer from InquirerPy.validator import PathValidator def main(): + home_path = "~/" if os.name == "posix" else "C:\\" src_path = inquirer.filepath( message="Enter file to upload:", - default="~/", + default=home_path, validate=PathValidator(is_file=True, message="Input is not a file"), only_files=True, ).execute() diff --git a/examples/classic/filepath.py b/examples/classic/filepath.py index a59ba71..aa17809 100644 --- a/examples/classic/filepath.py +++ b/examples/classic/filepath.py @@ -1,14 +1,17 @@ +import os + from InquirerPy import prompt from InquirerPy.validator import PathValidator def main(): + home_path = "~/" if os.name == "posix" else "C:\\" questions = [ { "type": "filepath", "message": "Enter file to upload:", "name": "location", - "default": "~/", + "default": home_path, "validate": PathValidator(is_file=True, message="Input is not a file"), "only_files": True, }, diff --git a/tests/prompts/test_filepath.py b/tests/prompts/test_filepath.py index b6afde5..a0af4d9 100644 --- a/tests/prompts/test_filepath.py +++ b/tests/prompts/test_filepath.py @@ -261,3 +261,32 @@ def _validation(_): def test_invalid_argument(self): self.assertRaises(InvalidArgument, FilePathPrompt, "hello", None, False, 12) FilePathPrompt(message="hello", default=lambda _: "12") + + @patch("platform.system") + 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("platform.system") + 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"])