1"""The workshop's test guard: what an extension mount writes goes back after every test.
3Registered under footman's ``footman.test_guards`` entry-point group, so
4footman's pytest plugin calls [guard][livery.workshop._test_guard.guard]
5once per test process, after it has pointed the runner's directories
6away, and enters what it returns around every test. Nothing here
7imports pytest: a session in a venv with the workshop loads this module
8whatever runs the tests.
10What an extension mount writes is put back after every test: the steps
11the listed extensions declare, the CI jobs they contribute and join,
12the task tree a run keeps, and the mount's record of what it found.
13Each of the three modules that hold them saves and restores its own
14state ([livery.workshop._steps.snapshot][],
15[livery.workshop._points.snapshot][] and
16[livery.workshop._extensions.snapshot][]). These registries are the
17workshop's own, so footman's registry guard does not hold them.
18Without the guard, a step one test mounted would judge the changes of
19every later test in the worker. Registries the test loaded itself go
20back as a process starts with them, and a session that loads none of
21them imports nothing for the guard.
22"""
32#: The workshop modules that hold what an extension mount writes.
34 "livery.workshop._extensions",
35 "livery.workshop._points",
36 "livery.workshop._steps",
37)
41 """Drop the check leg's label, and hand footman the mount guard.
43 The job runner names the leg, its shard included, to every child it
44 spawns, and a test that drives the gate in-process would otherwise
45 run as that shard and leave every step without the flag to the first
46 one. A test that wants a leg sets the variable itself.
48 Args:
49 **facts: What footman knows about the session: ``data_dir``,
50 ``cache_dir`` and ``config_dir``, the runner's directories
51 after it pointed them away. The guard reads none of them.
53 Returns:
54 [mount_state_stays][livery.workshop._test_guard.mount_state_stays],
55 which footman enters around every test.
56 """
57 from livery.workshop._ci_context import LEG_VARIABLE
59 del facts
60 os.environ.pop(LEG_VARIABLE, None)
61 return mount_state_stays
65 """Whether this process has loaded any module a mount writes into."""
66 return any(name in sys.modules for name in MOUNT_MODULES)
70 """Put the mount's registries back as a process starts with them."""
71 from livery.workshop import _extensions, _points, _steps
73 _steps.restore(_steps.snapshot(initial=True))
74 _points.restore(_points.snapshot(initial=True))
75 _extensions.restore(_extensions.snapshot(initial=True))
80 """Put back what an extension mount wrote during the test.
82 The registries go back as they were before the test. When the test
83 loaded them itself, they go back as a process starts with them. A
84 session that loads none of them imports nothing here.
85 """
86 if not _mount_loaded():
87 yield
88 if _mount_loaded():88 ↛ 89line 88 didn't jump to line 89 because the condition on line 88 was never true
89 _mount_reset()
90 return
91 from livery.workshop import _extensions, _points, _steps
93 steps, jobs, record = _steps.snapshot(), _points.snapshot(), _extensions.snapshot()
94 yield
95 _steps.restore(steps)
96 _points.restore(jobs)
97 _extensions.restore(record)