diff --git a/src/inquirer/questions.py b/src/inquirer/questions.py index 06085ef..e9f0692 100644 --- a/src/inquirer/questions.py +++ b/src/inquirer/questions.py @@ -226,7 +226,7 @@ def validate(self, current: str): except (ValueError, OSError) as e: raise errors.ValidationError(e) - if self._exists and not path.exists(): + if (self._exists is True and not path.exists()) or (self._exists is False and path.exists()): raise errors.ValidationError(current) # os.path.isdir and isfile check also existence of the path, diff --git a/tests/unit/test_question.py b/tests/unit/test_question.py index 085ded7..0c84c9d 100644 --- a/tests/unit/test_question.py +++ b/tests/unit/test_question.py @@ -330,10 +330,33 @@ def do_test(path_type, path, result=True): do_test(questions.Path.DIRECTORY, some_existing_dir) do_test(questions.Path.DIRECTORY, some_non_existing_dir, False) do_test(questions.Path.DIRECTORY, some_existing_file, False) - finally: shutil.rmtree(root) + def test_dir_and_file_same_name(self): + root = pathlib.Path(tempfile.mkdtemp()) + some_file = root / "foo" + some_dir = root / "foo/" + + def do_test(exists, type): + q = questions.Path("test", exists=exists, path_type=type) + with self.assertRaises(errors.ValidationError): + q.validate(str(some_dir)) + + some_file.touch() + try: + do_test(exists=True, type=questions.Path.DIRECTORY) + do_test(exists=False, type=questions.Path.DIRECTORY) + finally: + some_file.unlink() + + some_dir.mkdir() + try: + do_test(exists=True, type=questions.Path.FILE) + do_test(exists=False, type=questions.Path.FILE) + finally: + some_dir.rmdir() + def test_default_value_validation(self): root = pathlib.Path(tempfile.mkdtemp()) some_non_existing_dir = root / "some_non_existing_dir"