diff --git a/tests/__init__.py b/tests/__init__.py index ccccefe..a393c18 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1 +1,39 @@ -"""Tests for the sp CLI (sp_cli).""" +"""Tests for the sp CLI (sp_cli). + +Redirect the saved-session location before any test touches it. + +``sp auth login`` writes a bearer token to ``$XDG_CONFIG_HOME/sp/config.json``, +so a test reaching that code path writes to the developer's *real* config. The +plaintext token is returned only at creation, so an overwritten session is +unrecoverable -- and this has now cost a live token twice. + +``tests/conftest.py`` already guards this, but only 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 guard silently does nothing. + +Putting the redirect in the package ``__init__`` covers pytest, ``nose2``, and +``python -m unittest discover`` from the repository root. It does *not* cover +``python -m unittest discover -s tests``, which imports test modules top-level +and never imports this package -- which is why every test module imports +``SESSION_SANDBOX`` below explicitly. ``conftest.py`` still narrows this to a +per-test directory under pytest; overriding an already-redirected variable is +harmless. +""" + +import atexit +import os +import shutil +import tempfile + +#: Throwaway root standing in for the developer's home during tests. Test +#: modules import this name so that importing them runs the redirect below, +#: whichever runner collected them. +SESSION_SANDBOX = tempfile.mkdtemp(prefix='sp-cli-tests-') + +# HOME as well as XDG_CONFIG_HOME: config_path() falls back to ~/.config when +# XDG_CONFIG_HOME is unset, so redirecting only the latter leaves a gap. +os.environ['XDG_CONFIG_HOME'] = os.path.join(SESSION_SANDBOX, 'config') +os.environ['HOME'] = os.path.join(SESSION_SANDBOX, 'home') +os.makedirs(os.environ['HOME'], exist_ok=True) + +atexit.register(shutil.rmtree, SESSION_SANDBOX, True) diff --git a/tests/test_classifier.py b/tests/test_classifier.py index 34df46c..d9d3f05 100644 --- a/tests/test_classifier.py +++ b/tests/test_classifier.py @@ -3,6 +3,7 @@ import unittest from sp_cli import classifier +from tests import SESSION_SANDBOX # noqa: F401 class ClassifierTests(unittest.TestCase): diff --git a/tests/test_cli.py b/tests/test_cli.py index e299358..55b6915 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -8,6 +8,7 @@ from sp_cli.client import ApiError from sp_cli.main import cli +from tests import SESSION_SANDBOX # noqa: F401 RUNS_PAGE = { 'data': [{'run_id': 9299, 'status': 'fail', 'platform': 'windows', 'commit_sha': 'e6cd34e'}], diff --git a/tests/test_client.py b/tests/test_client.py index 90acb7f..67ffce4 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -6,6 +6,7 @@ import requests # type: ignore[import-untyped] from sp_cli.client import ApiClient, ApiError +from tests import SESSION_SANDBOX # noqa: F401 class FakeResponse: diff --git a/tests/test_compare.py b/tests/test_compare.py index ce8ff1c..c14ae8d 100644 --- a/tests/test_compare.py +++ b/tests/test_compare.py @@ -3,6 +3,7 @@ import unittest from sp_cli import compare +from tests import SESSION_SANDBOX # noqa: F401 def sample(test_id, status, exit_code=0, expected_rc=0, outputs=None): diff --git a/tests/test_history.py b/tests/test_history.py index 5f63204..347cc48 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -5,6 +5,7 @@ from sp_cli.history import (FLAKY, NEVER_PASSED, NEW_REGRESSION, NO_HISTORY, STILL_FAILING, UNKNOWN, classify_history, group_by_verdict, split_history, unknown_history) +from tests import SESSION_SANDBOX # noqa: F401 def entry(run_id, status, regression_test_id=137, signature=None): diff --git a/tests/test_triage.py b/tests/test_triage.py index 5a570c9..0f28da9 100644 --- a/tests/test_triage.py +++ b/tests/test_triage.py @@ -3,6 +3,7 @@ import unittest from sp_cli import triage +from tests import SESSION_SANDBOX # noqa: F401 class IsFailureTests(unittest.TestCase): diff --git a/tests/test_ux.py b/tests/test_ux.py index 1f493cd..c3c1f35 100644 --- a/tests/test_ux.py +++ b/tests/test_ux.py @@ -16,6 +16,7 @@ from sp_cli import config from sp_cli.main import cli from sp_cli.output import render +from tests import SESSION_SANDBOX # noqa: F401 CLASSIFIED_ROWS = { 'data': [