Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions tests/test_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest

from sp_cli import classifier
from tests import SESSION_SANDBOX # noqa: F401


class ClassifierTests(unittest.TestCase):
Expand Down
1 change: 1 addition & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}],
Expand Down
1 change: 1 addition & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions tests/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions tests/test_triage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest

from sp_cli import triage
from tests import SESSION_SANDBOX # noqa: F401


class IsFailureTests(unittest.TestCase):
Expand Down
1 change: 1 addition & 0 deletions tests/test_ux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': [
Expand Down
Loading