Skip to content

Commit

Permalink
path: ensure failure if file with existing dir and vice versa
Browse files Browse the repository at this point in the history
  • Loading branch information
Cube707 committed Jun 21, 2024
1 parent d1c3cac commit 464d9cd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/inquirer/questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 24 additions & 1 deletion tests/unit/test_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 464d9cd

Please sign in to comment.