forked from picklepete/pyicloud
-
-
Notifications
You must be signed in to change notification settings - Fork 40
docs: describe the test filesystem guard as it actually behaves #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MrJarnould
wants to merge
2
commits into
timlaing:main
Choose a base branch
from
MrJarnould:docs/describe-the-test-filesystem-guard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| """Tests for the filesystem guard the other tests run under. | ||
|
|
||
| The guard in `tests/conftest.py` is described in CONTRIBUTING.md and AGENTS.md, | ||
| and those descriptions had drifted from it -- which produced repeated review | ||
| findings against tests that were using the guard exactly as intended. These | ||
| tests pin the behaviour the documentation now describes, so the two cannot | ||
| disagree again without something failing. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from pathlib import Path | ||
| import tempfile | ||
|
|
||
| import pytest | ||
|
|
||
| from tests.conftest import FileSystemAccessError | ||
|
|
||
| ALLOWED_MARKER = "python-test-results" | ||
|
|
||
|
|
||
| def _allowed_path(name: str) -> str: | ||
| """Return a path inside the sanctioned location.""" | ||
|
|
||
| return os.path.join(tempfile.gettempdir(), ALLOWED_MARKER, name) | ||
|
|
||
|
|
||
| def test_open_is_blocked_outside_the_sanctioned_path() -> None: | ||
| """`builtins.open` raises rather than touching the developer's disk.""" | ||
|
|
||
| with ( | ||
| pytest.raises(FileSystemAccessError), | ||
| open( # noqa: PTH123 | ||
| "/etc/hosts", encoding="utf-8" | ||
| ) as handle, | ||
| ): | ||
| handle.read() | ||
|
|
||
|
|
||
| def test_making_directories_is_blocked_outside_the_sanctioned_path() -> None: | ||
| """The same applies to directory creation.""" | ||
|
|
||
| with pytest.raises(FileSystemAccessError): | ||
| os.mkdir("/tmp/pyicloud-should-not-exist") # noqa: PTH102 | ||
|
|
||
| with pytest.raises(FileSystemAccessError): | ||
| os.makedirs("/tmp/pyicloud-should-not-exist/nested") # noqa: PTH103 | ||
|
|
||
|
|
||
| def test_os_open_is_blocked_outside_the_sanctioned_path() -> None: | ||
| """`os.open` is guarded separately from `builtins.open`.""" | ||
|
|
||
| with pytest.raises(FileSystemAccessError): | ||
| os.open("/etc/hosts", os.O_RDONLY) | ||
|
|
||
|
|
||
| def test_chmod_is_blocked_outside_the_sanctioned_path() -> None: | ||
| """The fifth guarded entry point, and the easiest to forget.""" | ||
|
|
||
| with pytest.raises(FileSystemAccessError): | ||
| os.chmod("/etc/hosts", 0o644) # noqa: PTH101 | ||
|
|
||
|
|
||
| def test_the_sanctioned_path_is_an_escape_hatch_not_a_loophole() -> None: | ||
| """`python-test-results` paths are permitted, by design. | ||
|
|
||
| `tests/test_cmdline.py` relies on this for its session directories. A | ||
| reviewer reading only "new tests must mock any file I/O" would call that a | ||
| violation; it is the guard working as intended. | ||
| """ | ||
|
|
||
| target = _allowed_path("guard-check") | ||
| os.makedirs(target, exist_ok=True) # noqa: PTH103 | ||
| assert ALLOWED_MARKER in target | ||
|
|
||
|
|
||
| def test_pathlib_reads_are_not_intercepted() -> None: | ||
| """`Path.read_text` goes through `io.open`, which the guard does not patch. | ||
|
|
||
| This is why loading a JSON fixture works, and it works inside a test body | ||
| as well as at import time -- so the ordering of the session-scoped fixtures | ||
| is not the whole explanation. Seven test modules depend on this. | ||
| """ | ||
|
|
||
| conftest = Path(__file__).resolve().parent / "conftest.py" | ||
|
|
||
| content = conftest.read_text(encoding="utf-8") | ||
|
|
||
| assert "FileSystemAccessError" in content | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.