Coverage for tests/unit_tests/test_conftest.py: 100%
109 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 03:53 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 03:53 +0000
1"""
2Tests of the functionality of the pytest helper conftest.py
3"""
5import sys
6from dataclasses import dataclass
7from tests.conftest import ApioRunner
8from apio.common.apio_console import cout, console, cstyle
9from apio.apio_context import (
10 ApioContext,
11 ProjectPolicy,
12 RemoteConfigPolicy,
13 PackagesPolicy,
14)
17@dataclass(frozen=True)
18class State:
19 """Capture the I/O state for verification."""
21 stdout_id: int
22 stderr_id: int
23 console_id: int
24 console_file_id: int
26 @classmethod
27 def snapshot(cls) -> "State":
28 """Snapshot the current state"""
29 c = console()
30 return State(
31 stdout_id=id(sys.stdout),
32 stderr_id=id(sys.stderr),
33 console_id=id(c),
34 console_file_id=id(c.file),
35 )
38def test_logger_minimal(apio_runner: ApioRunner):
39 """Tests ApioRunner logger functionality with minimal configuration
40 with no sandbox, no apio context, and no apio_console.py output."""
42 # -- Test
43 print("print-test-before")
45 with apio_runner.with_logger() as log:
46 print("print-test-inside")
48 print("print-test-after")
50 # -- Verify
51 assert "print-test-before" not in log.out
52 assert "print-test-inside" in log.out
53 assert "print-test-after" not in log.out
56def test_logger_with_console(apio_runner: ApioRunner):
57 """Tests ApioRunner logger functionality with sandbox only and
58 no apio context, and no apio_console.py output."""
60 with apio_runner.in_sandbox():
62 state1 = State.snapshot()
64 # -- Test
65 print("print-test-before")
66 cout("cout-test-before")
68 state2 = State.snapshot()
70 with apio_runner.with_logger() as log:
71 state3 = State.snapshot()
72 print("print-test-inside")
73 cout("cout-test-inside")
75 state4 = State.snapshot()
77 print("\n*** LOG:")
78 print(log.out)
79 print()
81 print("print-test-after")
82 cout("cout-test-after")
84 # -- Dump state snapshots
85 print(f"{state1=}")
86 print(f"{state2=}")
87 print(f"{state3=}")
88 print(f"{state4=}")
90 # -- Verify log
91 assert "print-test-before" not in log.out
92 assert "cout-test-before" not in log.out
94 assert "print-test-inside" in log.out
95 assert "cout-test-inside" in log.out
97 assert "print-test-after" not in log.out
98 assert "cout-test-after" not in log.out
101def test_logger_with_cstyle(apio_runner: ApioRunner):
102 """Tests ApioRunner logger functionality with sandbox and ,
103 apio_console.py cstyle and cout but no ApioContext."""
105 with apio_runner.in_sandbox():
107 state1 = State.snapshot()
109 # -- Invoke cstyle, in the past in interfered with the logger.
110 text = "red-text"
111 styled_text = cstyle(text, style="red")
112 print(styled_text)
113 print(repr(styled_text))
114 assert len(styled_text) > len(text)
115 assert text in styled_text
117 state2 = State.snapshot()
119 # -- Test
120 print("print-test-before")
121 cout("cout-test-before")
123 state3 = State.snapshot()
125 with apio_runner.with_logger() as log:
126 state4 = State.snapshot()
128 print("print-test-inside")
129 cout("cout-test-inside")
131 state5 = State.snapshot()
133 print("\n*** LOG:")
134 print(log.out)
135 print()
137 print("print-test-after")
138 cout("cout-test-after")
140 # -- Dump state snapshots
141 print(f"{state1=}")
142 print(f"{state2=}")
143 print(f"{state3=}")
144 print(f"{state4=}")
145 print(f"{state5=}")
147 # -- Verify
148 assert "print-test-before" not in log.out
149 assert "cout-test-before" not in log.out
151 assert "print-test-inside" in log.out
152 assert "cout-test-inside" in log.out
154 assert "print-test-after" not in log.out
155 assert "cout-test-after" not in log.out
158def test_logger_with_apio_ctx(apio_runner: ApioRunner):
159 """Tests ApioRunner logger functionality with sandbox and ,
160 apio context. This is the most typical test configuration."""
162 with apio_runner.in_sandbox() as sb:
164 state1 = State.snapshot()
166 # -- Create an apio context with a fake project.
167 sb.write_default_apio_ini()
168 _ = ApioContext(
169 project_policy=ProjectPolicy.PROJECT_REQUIRED,
170 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
171 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
172 )
174 state2 = State.snapshot()
176 # -- Test
177 print("print-test-before")
178 cout("cout-test-before")
180 state3 = State.snapshot()
182 with apio_runner.with_logger() as log:
183 state4 = State.snapshot()
185 print("print-test-inside")
186 cout("cout-test-inside")
188 state5 = State.snapshot()
190 print("\n*** LOG:")
191 print(log.out)
192 print()
194 print("print-test-after")
195 cout("cout-test-after")
197 # -- Dump state snapshots
198 print(f"{state1=}")
199 print(f"{state2=}")
200 print(f"{state3=}")
201 print(f"{state4=}")
202 print(f"{state5=}")
204 # -- Verify
205 assert "print-test-before" not in log.out
206 assert "cout-test-before" not in log.out
208 assert "print-test-inside" in log.out
209 assert "cout-test-inside" in log.out
211 assert "print-test-after" not in log.out
212 assert "cout-test-after" not in log.out