diff --git a/src/inquirer/questions.py b/src/inquirer/questions.py index 8f930450..0637766a 100644 --- a/src/inquirer/questions.py +++ b/src/inquirer/questions.py @@ -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 diff --git a/tests/unit/test_question.py b/tests/unit/test_question.py index 068061d3..0b64bc79 100644 --- a/tests/unit/test_question.py +++ b/tests/unit/test_question.py @@ -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"