Skip to content

Commit

Permalink
path: validate that path_type is one of the valid types
Browse files Browse the repository at this point in the history
  • Loading branch information
Cube707 committed Jun 21, 2024
1 parent b8f9797 commit d03e29e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/inquirer/questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,11 @@ class Path(Text):
def __init__(self, name, default=None, path_type="any", exists=None, normalize_to_absolute_path=False, **kwargs):
super().__init__(name, default=default, **kwargs)

self._path_type = path_type
if path_type in (Path.ANY, Path.FILE, Path.DIRECTORY):
self._path_type = path_type
else:
raise ValueError("'path_type' must be one of [ANY, FILE, DIRECTORY]")

self._exists = exists
self._normalize_to_absolute_path = normalize_to_absolute_path

Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,20 @@ def test_default_value_validation(self):

questions.Path("path", default="~/.toggl_log")

def test_path_type_value_validation(self):
questions.Path("abs_path", path_type=questions.Path.ANY)
questions.Path("abs_path", path_type="any")
questions.Path("abs_path", path_type=questions.Path.FILE)
questions.Path("abs_path", path_type="file")
questions.Path("abs_path", path_type=questions.Path.DIRECTORY)
questions.Path("abs_path", path_type="directory")

with self.assertRaises(ValueError):
questions.Path("abs_path", path_type=questions.Path.kind)

with self.assertRaises(ValueError):
questions.Path("abs_path", path_type="false")


def test_tagged_value():
LABEL = "label"
Expand Down

0 comments on commit d03e29e

Please sign in to comment.