diff --git a/newsfragments/+3f45d2d2.bugfix.rst b/newsfragments/+3f45d2d2.bugfix.rst new file mode 100644 index 00000000..9f6f74b2 --- /dev/null +++ b/newsfragments/+3f45d2d2.bugfix.rst @@ -0,0 +1,2 @@ +Fixed an issue, where defining load paths in ini/toml configuration would fail +due to pytest's LocalPath not having an endswith method. diff --git a/pytest_postgresql/config.py b/pytest_postgresql/config.py index 01f3e267..a772136e 100644 --- a/pytest_postgresql/config.py +++ b/pytest_postgresql/config.py @@ -3,6 +3,7 @@ from pathlib import Path from typing import Any, List, Optional, TypedDict, Union +from _pytest._py.path import LocalPath from pytest import FixtureRequest @@ -50,10 +51,12 @@ def get_postgresql_option(option: str) -> Any: ) -def detect_paths(load_paths: List[str]) -> List[Union[Path, str]]: +def detect_paths(load_paths: List[Union[LocalPath, str]]) -> List[Union[Path, str]]: """Convert path to sql files to Path instances.""" converted_load_paths: List[Union[Path, str]] = [] for path in load_paths: + if isinstance(path, LocalPath): + path = str(path) if path.endswith(".sql"): converted_load_paths.append(Path(path)) else: diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 00000000..aa39cb24 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,22 @@ +"""Config tests.""" + +from pathlib import Path +from typing import Union + +import pytest +from _pytest._py.path import LocalPath + +from pytest_postgresql.config import detect_paths + + +@pytest.mark.parametrize( + "path, want", + ( + ("test.sql", Path("test.sql")), + ("load.function", "load.function"), + (LocalPath("test.sql"), Path("test.sql").absolute()), # type: ignore[no-untyped-call] + ), +) +def test_detect_paths(path: Union[str, LocalPath], want: Union[Path, str]) -> None: + """Check the correctness of detect_paths function.""" + assert detect_paths([path]) == [want] diff --git a/tests/test_postgres_options_plugin.py b/tests/test_postgres_options_plugin.py index 1ba4bc27..c87fc3ea 100644 --- a/tests/test_postgres_options_plugin.py +++ b/tests/test_postgres_options_plugin.py @@ -48,6 +48,15 @@ def test_postgres_loader_in_cli(pointed_pytester: Pytester) -> None: ret.assert_outcomes(passed=1) +def test_postgres_loader_in_ini(pointed_pytester: Pytester) -> None: + """Check that pytest.ini arguments are honored for load.""" + pointed_pytester.copy_example("test_load.py") + test_sql_path = pointed_pytester.copy_example("test.sql") + pointed_pytester.makefile(".ini", pytest=f"[pytest]\npostgresql_load = {test_sql_path}\n") + ret = pointed_pytester.runpytest("test_load.py") + ret.assert_outcomes(passed=1) + + postgresql_proc_to_override = postgresql_proc()