Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
89 lines (80 loc) · 4.86 KB
/
Copy pathconftest.py
File metadata and controls
89 lines (80 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Global pytest configuration for the studyloop test suite.
Sets environment variables BEFORE any test modules import studyloop.
The critical setup here: forcing Rich to emit plain text instead of
ANSI escape codes so CLI-output assertions (``"#42" in result.output``)
work under ``click.testing.CliRunner``, which captures stdout into a
StringIO that Rich still treats as terminal-capable.
``NO_COLOR=1`` tells Rich to drop colors. ``TERM=dumb`` is required on
top of that -- Rich keeps emitting bold/underline escape codes until
it sees a non-ANSI terminal type.
These env vars affect only the test process, never user runtime.
"""
from __future__ import annotations
import os
import tempfile
from pathlib import Path
# These MUST be set before any `from studyloop...` import, because
# ``studyloop.output`` (and CLI submodules) construct a module-level
# ``Console()`` whose behaviour is fixed at construction time.
#
# Hard-assign, not ``setdefault`` -- the shell typically exports
# ``TERM=xterm-256color`` which Rich treats as ANSI-capable and will
# keep emitting bold/underline escape codes even under ``NO_COLOR``.
os.environ["NO_COLOR"] = "1"
os.environ["TERM"] = "dumb"
# Writable-state isolation for the WHOLE repo (both workspace packages).
# Set here, at import time, and via the environment rather than a fixture:
# ``load_settings()`` and ``get_db_path()`` read these at call time, and a
# test that shells out to the CLI inherits only the environment — a
# monkeypatched function does not cross a process boundary. That gap is how
# the suite previously ran migrations against the learner's real sessions.db
# and wrote a poisoned picker cache into their real state dir. See
# docs/issues/0005-vendor-picker-lists-repo-directories.md.
#
# Hard-assign, not ``setdefault``: the setup guide tells learners to
# ``export STUDYLOOP_CONFIG=...``, and the same variables can name their real
# sessions.db and state dir, so a value exported by the caller must never
# survive into the suite. STUDYLOOP_CONFIG is set here too, not only in the
# studyloop package conftest, because that conftest leaves all three alone once
# this one has claimed them (see the marker below). A test that needs its own
# paths still ``monkeypatch.setenv``s them; that override lasts for the test and
# unwinds back to these values.
#
# Once per process, recorded by pid: under ``--import-mode=importlib`` a test
# module's ``from conftest import ...`` executes the studyloop package conftest a
# second time, and re-pointing the sandbox then would move it out from under
# code that already bound it (``studyloop.settings._CONFIG_PATH``). A child
# process inherits the marker with its parent's pid, so it redirects again.
_SANDBOX_OWNER_ENV = "_STUDYLOOP_SUITE_SANDBOX_PID"
if os.environ.get(_SANDBOX_OWNER_ENV) != str(os.getpid()):
_TEST_STATE_ROOT = Path(tempfile.mkdtemp(prefix="studyloop-test-state-"))
os.environ["STUDYLOOP_STATE_DIR"] = str(_TEST_STATE_ROOT / "state")
os.environ["STUDYLOOP_DB"] = str(_TEST_STATE_ROOT / "sessions.db")
os.environ["STUDYLOOP_CONFIG"] = str(_TEST_STATE_ROOT / "config.yaml")
os.environ[_SANDBOX_OWNER_ENV] = str(os.getpid())
# tmux socket isolation for the WHOLE repo (C11 / R-49e): the developer's real
# tmux server must be unreachable from the suite. tmux resolves its socket
# directory from ``TMUX_TMPDIR`` for the default ``-L`` addressing every call
# site in this repository uses, and treats a set ``TMUX`` as "already inside a
# session" (``studyloop.tmux.is_in_tmux()``), so a private directory plus an
# unset ``TMUX`` redirects every tmux process the suite starts, including
# subprocess children, which build their environment from ``os.environ``.
#
# Set HERE, at import time, and not in a session hook. This used to live in
# ``packages/studyloop/tests/conftest.py``'s ``pytest_sessionstart``, and pytest
# loads that conftest as an initial plugin only when a path argument is its
# directory or a file under it (``testpaths`` makes it one). Hand pytest a
# directory ABOVE the tests -- ``pytest packages`` -- and the conftest is
# imported lazily during collection, after ``pytest_sessionstart`` has fired:
# on 2026-10-24 two full gates ran that way against the developer's real tmux
# server for 25 minutes each, and only the guard test noticed. This file is
# initial for every invocation rooted at the workspace, and import time has no
# such dependency. The studyloop package conftest claims the same thing under
# the same marker for a run rooted at that package, where this file is not
# loaded at all, and reads the directory back from the environment, so the
# two never disagree about which directory the suite is using.
_TMUX_OWNER_ENV = "_STUDYLOOP_SUITE_TMUX_PID"
if os.environ.get(_TMUX_OWNER_ENV) != str(os.getpid()):
os.environ["TMUX_TMPDIR"] = tempfile.mkdtemp(prefix="studyloop-test-tmux-")
os.environ.pop("TMUX", None)
os.environ[_TMUX_OWNER_ENV] = str(os.getpid())