diff --git a/changelog.d/20241220_221301_Pawamoy_find_pyproject_upper_dirs.md b/changelog.d/20241220_221301_Pawamoy_find_pyproject_upper_dirs.md new file mode 100644 index 0000000..a158ecf --- /dev/null +++ b/changelog.d/20241220_221301_Pawamoy_find_pyproject_upper_dirs.md @@ -0,0 +1,40 @@ + + + + + + +### Fixed + +- Find `pyproject.toml` file in parent directories, not just next to the Pytest configuration file. + + diff --git a/src/inline_snapshot/pytest_plugin.py b/src/inline_snapshot/pytest_plugin.py index 496e7d7..0c540b4 100644 --- a/src/inline_snapshot/pytest_plugin.py +++ b/src/inline_snapshot/pytest_plugin.py @@ -76,7 +76,12 @@ def is_implementation_supported(): def pytest_configure(config): global flags - _config.config = _config.read_config(config.rootpath / "pyproject.toml") + directory = config.rootpath + while not (pyproject := directory / "pyproject.toml").exists(): + if directory == directory.parent: + break + directory = directory.parent + _config.config = _config.read_config(pyproject) if config.option.inline_snapshot is None: flags = set(_config.config.default_flags) diff --git a/src/inline_snapshot/testing/_example.py b/src/inline_snapshot/testing/_example.py index e61fda8..60d3b55 100644 --- a/src/inline_snapshot/testing/_example.py +++ b/src/inline_snapshot/testing/_example.py @@ -99,10 +99,16 @@ def dump_files(self): def _write_files(self, dir: Path): for name, content in self.files.items(): - (dir / name).write_text(content) + filename = dir / name + filename.parent.mkdir(exist_ok=True, parents=True) + filename.write_text(content) def _read_files(self, dir: Path): - return {p.name: p.read_text() for p in dir.iterdir() if p.is_file()} + return { + str(p.relative_to(dir)): p.read_text() + for p in [*dir.iterdir(), *dir.rglob("*.py")] + if p.is_file() + } def run_inline( self, diff --git a/tests/test_pytest_plugin.py b/tests/test_pytest_plugin.py index 5012797..49bc504 100644 --- a/tests/test_pytest_plugin.py +++ b/tests/test_pytest_plugin.py @@ -769,3 +769,34 @@ def test_outsource(): assert project.storage(storage_dir) == snapshot( ["2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824.html"] ) + + +def test_find_pyproject_in_parent_directories(tmp_path): + + Example( + { + "pyproject.toml": """\ +[tool.inline-snapshot] +hash-length=2 +""", + "project/pytest.ini": "", + "project/test_something.py": """\ +from inline_snapshot import outsource,snapshot,external + +def test_something(): + assert outsource("test") == snapshot() +""", + } + ).run_pytest( + ["--rootdir", "./project", "--inline-snapshot=create"], + changed_files=snapshot( + { + "project/test_something.py": """\ +from inline_snapshot import outsource,snapshot,external + +def test_something(): + assert outsource("test") == snapshot(external("9f*.txt")) +""" + } + ), + )