Skip to content

Commit 9fe8a0a

Browse files
authored
Fix validation when invalid abs path is given (#30)
1 parent bbf52d3 commit 9fe8a0a

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

src/pytest_data_loader/types.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,18 @@ def _validate_path(self) -> None:
222222

223223
path = Path(orig_value)
224224
if path in (Path("."), Path(".."), Path(ROOT_DIR)):
225-
raise ValueError(f"Invalid path value: {str(orig_value)!r}")
226-
if path.is_absolute() and not path.exists():
227-
raise ValueError(
228-
f"The provided {'file' if path.is_file() else 'directory'} does not exist: {str(orig_value)!r}"
229-
)
225+
raise ValueError(f"Invalid path value: '{orig_value}'")
226+
if path.is_absolute():
227+
if not path.exists():
228+
raise ValueError(f"The provided path does not exist: '{orig_value}'")
229+
if path.is_dir() and self.loader.is_file_loader:
230+
raise ValueError(
231+
f"Invalid path: @{self.loader.__name__} loader must take a file path, not '{orig_value}'"
232+
)
233+
if path.is_file() and not self.loader.is_file_loader:
234+
raise ValueError(
235+
f"Invalid path: @{self.loader.__name__} loader must take a directory path, not '{orig_value}'"
236+
)
230237

231238
self._modify_value("path", path)
232239

tests/tests_plugin/helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TestContext:
2828
strip_trailing_whitespace: bool = True
2929

3030
@property
31-
def num_expected_tests(self):
31+
def num_expected_tests(self) -> int:
3232
if self.loader.is_file_loader:
3333
if self.loader.requires_parametrization:
3434
if self.test_file_ext == ".json":
@@ -95,7 +95,7 @@ def create_test_data_in_loader_dir(
9595

9696
if return_abs_path:
9797
return abs_file_path
98-
return relative_file_path
98+
return Path(relative_file_path)
9999

100100

101101
def create_test_context(

tests/tests_plugin/test_data_path.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,24 @@ def test_loader_with_valid_data_path(
3838

3939

4040
@pytest.mark.parametrize("collect_only", [True, False])
41-
@pytest.mark.parametrize("invalid_path", [".", "..", ROOT_DIR])
41+
@pytest.mark.parametrize(
42+
"invalid_path", [".", "..", ROOT_DIR, Path(ROOT_DIR, "dir"), Path(ROOT_DIR, "dir", "test.txt")]
43+
)
4244
def test_loader_with_invalid_data_path(test_context: TestContext, invalid_path: str, collect_only: bool) -> None:
4345
"""Test that invalid relative paths are handled properly"""
4446
result = run_pytest_with_context(test_context, path=invalid_path, collect_only=collect_only)
4547
_check_result_with_invalid_path(result, test_context.loader, invalid_path)
4648

4749

4850
@pytest.mark.parametrize("collect_only", [True, False])
51+
@pytest.mark.parametrize("is_abs_path", [False, True])
4952
def test_loader_with_unmatched_data_path_type(
50-
test_context: TestContext, loader: DataLoader, collect_only: bool
53+
test_context: TestContext, loader: DataLoader, loader_dir_name: str, is_abs_path: bool, collect_only: bool
5154
) -> None:
5255
"""Test that relative path type that isn't allowed for each loader is handled properly"""
53-
file_path = create_test_data_in_loader_dir(test_context.pytester, "some_dir", Path("other_dir", "foo.txt"))
56+
file_path = create_test_data_in_loader_dir(
57+
test_context.pytester, loader_dir_name, Path("other_dir", "foo.txt"), return_abs_path=is_abs_path
58+
)
5459
if loader.is_file_loader:
5560
unmatched_path = file_path.parent
5661
else:
@@ -86,8 +91,17 @@ def _check_result_with_invalid_path(result: RunResult, loader: DataLoader, inval
8691
assert result.ret == ExitCode.INTERRUPTED
8792
stdout = str(result.stdout)
8893
result.assert_outcomes(errors=1)
89-
if str(invalid_path) in (".", "..", ROOT_DIR):
90-
assert f"Invalid path value: {str(invalid_path)!r}" in stdout
94+
path = Path(invalid_path)
95+
if str(path) in (".", "..", ROOT_DIR):
96+
assert f"Invalid path value: '{path}'" in stdout
97+
elif path.is_absolute():
98+
if path.exists():
99+
assert (
100+
f"Invalid path: @{loader.__name__} loader must take a "
101+
f"{'file' if loader.is_file_loader and path.is_dir() else 'directory'} path, not '{path}'"
102+
) in stdout
103+
else:
104+
assert f"The provided path does not exist: '{path}'" in stdout
91105
else:
92106
file_or_dir = "directory" if loader == parametrize_dir else "file"
93-
assert f"Unable to locate the specified {file_or_dir} '{invalid_path}'" in stdout
107+
assert f"Unable to locate the specified {file_or_dir} '{path}'" in stdout

0 commit comments

Comments
 (0)