Repository navigation
✨ feat(testing): redirect every directory under a test root - #590
Merged
Merged
Conversation
Tests for code that uses platformdirs had no supported way to keep it out of the real home and system folders. Pointing HOME and every XDG_* variable at a temporary directory still leaves site_log_dir on /var/log/app under Linux and /Library/Logs/app under macOS, and site_bin_dir on /usr/local/bin. The how-to guide fell back to monkeypatch. isolated_dirs swaps the directory properties of the host PlatformDirs class for ones that resolve to <root>/<kind>, a layout close to Qt test mode. Exit restores the original descriptors. Patching the class reaches the *_path variants, the iter_* methods and the module-level functions from one place, and applies to Android as well; environment variables cover a different subset on each platform. The pytest11 entry point exposes the opt-in platformdirs_isolated fixture, and the plugin imports platformdirs.testing only when a test requests it.
gaborbernat
force-pushed
the
feat/test-isolation
branch
from
September 26, 2026 02:27
b8f6c6b to
795d8f0
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Tests for code that uses platformdirs had no supported way to keep it out of the real home and system folders. 🧪 Pointing
HOMEand everyXDG_*variable at a temporary directory still leavesPlatformDirs("app").site_log_diron/Library/Logs/appunder macOS and/var/log/appunder Linux, andsite_cache_dir,site_state_dirandsite_bin_direscape the same way. The how-to guide fell back to monkeypatching single functions.platformdirs.testing.isolated_dirs(root)swaps the directory properties of the hostPlatformDirsclass, so each kind resolves to<root>/<kind>(followed byappnameandversionfor the app-scoped kinds), close to the layout of Qt test mode. ✨ Patching the class reaches the*_pathvariants,iter_*,multipathresults and the module-level functions from one place and applies to whichever classPlatformDirsresolves to, Android included; environment variables cover a different subset on each platform. The original descriptors go back on exit, including after an exception. The opt-inplatformdirs_isolatedfixture wraps the same context aroundtmp_path.The plugin registers through⚠️ The patch stays inside the current process, so subprocesses and direct instances of another platform class such as
pytest11. Every pytest session in an environment with platformdirs installed imports theplatformdirspackage at startup, which took 3 ms in the fastest of sixpython -X importtimeruns on macOS, whileplatformdirs.testingloads only when a test requests the fixture and-p no:platformdirsturns the plugin off.Unixon macOS keep the real directories.