Skip to content

✨ feat(testing): redirect every directory under a test root - #590

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:feat/test-isolation
Sep 26, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:feat/test-isolation

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Sep 26, 2026 •

Copy link
Copy Markdown
Member

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 PlatformDirs("app").site_log_dir on /Library/Logs/app under macOS and /var/log/app under Linux, and site_cache_dir, site_state_dir and site_bin_dir escape the same way. The how-to guide fell back to monkeypatching single functions.

platformdirs.testing.isolated_dirs(root) swaps the directory properties of the host PlatformDirs class, so each kind resolves to <root>/<kind> (followed by appname and version for the app-scoped kinds), close to the layout of Qt test mode. ✨ Patching the class reaches the *_path variants, iter_*, multipath results and the module-level functions from one place and applies to whichever class PlatformDirs resolves 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-in platformdirs_isolated fixture wraps the same context around tmp_path.

The plugin registers through pytest11. Every pytest session in an environment with platformdirs installed imports the platformdirs package at startup, which took 3 ms in the fastest of six python -X importtime runs on macOS, while platformdirs.testing loads only when a test requests the fixture and -p no:platformdirs turns the plugin off. ⚠️ The patch stays inside the current process, so subprocesses and direct instances of another platform class such as Unix on macOS keep the real directories.

@gaborbernat gaborbernat added the enhancement New feature or request label Sep 26, 2026
@gaborbernat
gaborbernat requested a review from ofek as a code owner September 26, 2026 02:27
@gaborbernat gaborbernat added the enhancement New feature or request label Sep 26, 2026
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
gaborbernat merged commit 9f2ee08 into tox-dev:main Sep 26, 2026
36 checks passed
@gaborbernat
gaborbernat deleted the feat/test-isolation branch October 3, 2026 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant