Skip to content

Commit

Permalink
fix: Find pyproject.toml in upper directories (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy authored Dec 21, 2024
1 parent b4e3b81 commit df78b7f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
40 changes: 40 additions & 0 deletions changelog.d/20241220_221301_Pawamoy_find_pyproject_upper_dirs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Removed
- A bullet item for the Removed category.
-->
<!--
### Added
- A bullet item for the Added category.
-->
<!--
### Changed
- A bullet item for the Changed category.
-->
<!--
### Deprecated
- A bullet item for the Deprecated category.
-->
### Fixed

- Find `pyproject.toml` file in parent directories, not just next to the Pytest configuration file.

<!--
### Security
- A bullet item for the Security category.
-->
7 changes: 6 additions & 1 deletion src/inline_snapshot/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions src/inline_snapshot/testing/_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions tests/test_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
"""
}
),
)

0 comments on commit df78b7f

Please sign in to comment.