[FIX] Redirect the saved session under every test runner, not just pytest - #2
Merged
Conversation
conftest.py redirects XDG_CONFIG_HOME so a test that reaches `sp auth login` cannot overwrite the developer's real session. That guard only works under pytest: conftest is a pytest mechanism, and the suite is also run with unittest and nose2, where the fixture is never loaded and the protection silently does nothing. It is not theoretical. The docstring already records one live token lost this way, and it has now happened a second time, with `python -m unittest discover -s tests`. The plaintext token is returned only at creation, so an overwritten session cannot be recovered. Move the redirection into the tests package __init__, which pytest, nose2 and `python -m unittest discover` from the repository root all import before collecting anything. `discover -s tests` imports test modules top-level and never imports the package, so each test module also imports SESSION_SANDBOX explicitly, which pulls the package in whichever way the suite was started. conftest.py is left in place: under pytest it still narrows this to a per-test directory, and overriding an already-redirected variable is harmless. Verified by planting a sentinel in ~/.config/sp/config.json and running the suite three ways -- `discover -s tests`, `discover` from the root, and pytest. 210 tests pass and the sentinel is untouched in each; before this change the first of those overwrote it.
The sandbox import carried a "keep first" comment and a trailing explanation that pushed the line past the wrap limit, so isort reformatted it into a backslash continuation and the check job failed on the resulting diff. The comment was also wrong. `config.config_path()` reads XDG_CONFIG_HOME when it is called, not when it is imported, so nothing depends on this import preceding the others -- and `tests/__init__.py` runs before any submodule body regardless, because Python imports the parent package first. Shortening the comment lets isort sort the line wherever it belongs.
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.
conftest.pyexists to stop a test that reachessp auth loginfrom overwriting the developer's real session. It only works under pytest.conftest.pyis a pytest mechanism. This suite is also run withunittestandnose2, and under those the fixture is never loaded — the guard silently does nothing, and the first test that saves a token writes straight to~/.config/sp/config.json.That is not hypothetical. The conftest docstring already records one live token lost this way:
It has now happened a second time, running
python -m unittest discover -s tests. The config came back withbase_urlset tohttp://example.test/api/v1, and the real token was gone for good.The existing
ConfigIsolationTestsguard does fire — but only after the damage, since it is just another test in the same run.Fix
Move the redirection into the tests package
__init__, which pytest, nose2 andpython -m unittest discoverfrom the repository root all import before collecting anything.One invocation still escapes that:
python -m unittest discover -s testsimports test modules top-level and never imports the package. So each test module also importsSESSION_SANDBOXexplicitly:python -mputs the working directory onsys.path, so that import resolves and pulls the package in however the suite was started.HOMEis redirected as well asXDG_CONFIG_HOME, becauseconfig_path()falls back to~/.configwhen the latter is unset.conftest.pyis left alone: under pytest it still narrows this to a per-test directory, and overriding an already-redirected variable is harmless.Verification
Planted a sentinel in
~/.config/sp/config.jsonand ran the suite three ways:unittest discover -s testsunittest discover(repo root)pytest210 tests pass in each case.
Why not just tell people to use pytest
The suite runs green under
unittestandnose2today, so there is nothing signalling that those runners are unsafe — and relying on everyone remembering is what failed the first time, which is the reason the fixture was made autouse rather than opt-in. This extends the same reasoning one step further: the protection should not depend on which runner someone happens to type.