Coverage for tests/unit_tests/test_conftest.py: 100%
109 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
1# This file is part of the Apio project
2# (C) 2016-present FPGAwars
3# Project authors:
4# * Jesús Arroyo (Jesus89) (2016-2019)
5# * Juan Gonzalez (obijuan) (2019-2024)
6# * The Apio Team (2024-present)
7# License: GPLv2
9"""
10Tests of the functionality of the pytest helper conftest.py
11"""
13import sys
14from dataclasses import dataclass
15from tests.conftest import ApioRunner
16from apio.common.apio_console import cout, console, cstyle
17from apio.apio_context import (
18 ApioContext,
19 ProjectPolicy,
20 RemoteConfigPolicy,
21 PackagesPolicy,
22)
25@dataclass(frozen=True)
26class State:
27 """Capture the I/O state for verification."""
29 stdout_id: int
30 stderr_id: int
31 console_id: int
32 console_file_id: int
34 @classmethod
35 def snapshot(cls) -> "State":
36 """Snapshot the current state"""
37 c = console()
38 return State(
39 stdout_id=id(sys.stdout),
40 stderr_id=id(sys.stderr),
41 console_id=id(c),
42 console_file_id=id(c.file),
43 )
46def test_logger_minimal(apio_runner: ApioRunner):
47 """Tests ApioRunner logger functionality with minimal configuration
48 with no sandbox, no apio context, and no apio_console.py output."""
50 # -- Test
51 print("print-test-before")
53 with apio_runner.with_logger() as log:
54 print("print-test-inside")
56 print("print-test-after")
58 # -- Verify
59 assert "print-test-before" not in log.out
60 assert "print-test-inside" in log.out
61 assert "print-test-after" not in log.out
64def test_logger_with_console(apio_runner: ApioRunner):
65 """Tests ApioRunner logger functionality with sandbox only and
66 no apio context, and no apio_console.py output."""
68 with apio_runner.in_sandbox():
70 state1 = State.snapshot()
72 # -- Test
73 print("print-test-before")
74 cout("cout-test-before")
76 state2 = State.snapshot()
78 with apio_runner.with_logger() as log:
79 state3 = State.snapshot()
80 print("print-test-inside")
81 cout("cout-test-inside")
83 state4 = State.snapshot()
85 print("\n*** LOG:")
86 print(log.out)
87 print()
89 print("print-test-after")
90 cout("cout-test-after")
92 # -- Dump state snapshots
93 print(f"{state1=}")
94 print(f"{state2=}")
95 print(f"{state3=}")
96 print(f"{state4=}")
98 # -- Verify log
99 assert "print-test-before" not in log.out
100 assert "cout-test-before" not in log.out
102 assert "print-test-inside" in log.out
103 assert "cout-test-inside" in log.out
105 assert "print-test-after" not in log.out
106 assert "cout-test-after" not in log.out
109def test_logger_with_cstyle(apio_runner: ApioRunner):
110 """Tests ApioRunner logger functionality with sandbox and ,
111 apio_console.py cstyle and cout but no ApioContext."""
113 with apio_runner.in_sandbox():
115 state1 = State.snapshot()
117 # -- Invoke cstyle, in the past in interfered with the logger.
118 text = "red-text"
119 styled_text = cstyle(text, style="red")
120 print(styled_text)
121 print(repr(styled_text))
122 assert len(styled_text) > len(text)
123 assert text in styled_text
125 state2 = State.snapshot()
127 # -- Test
128 print("print-test-before")
129 cout("cout-test-before")
131 state3 = State.snapshot()
133 with apio_runner.with_logger() as log:
134 state4 = State.snapshot()
136 print("print-test-inside")
137 cout("cout-test-inside")
139 state5 = State.snapshot()
141 print("\n*** LOG:")
142 print(log.out)
143 print()
145 print("print-test-after")
146 cout("cout-test-after")
148 # -- Dump state snapshots
149 print(f"{state1=}")
150 print(f"{state2=}")
151 print(f"{state3=}")
152 print(f"{state4=}")
153 print(f"{state5=}")
155 # -- Verify
156 assert "print-test-before" not in log.out
157 assert "cout-test-before" not in log.out
159 assert "print-test-inside" in log.out
160 assert "cout-test-inside" in log.out
162 assert "print-test-after" not in log.out
163 assert "cout-test-after" not in log.out
166def test_logger_with_apio_ctx(apio_runner: ApioRunner):
167 """Tests ApioRunner logger functionality with sandbox and ,
168 apio context. This is the most typical test configuration."""
170 with apio_runner.in_sandbox() as sb:
172 state1 = State.snapshot()
174 # -- Create an apio context with a fake project.
175 sb.write_default_apio_ini()
176 _ = ApioContext(
177 project_policy=ProjectPolicy.PROJECT_REQUIRED,
178 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
179 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
180 )
182 state2 = State.snapshot()
184 # -- Test
185 print("print-test-before")
186 cout("cout-test-before")
188 state3 = State.snapshot()
190 with apio_runner.with_logger() as log:
191 state4 = State.snapshot()
193 print("print-test-inside")
194 cout("cout-test-inside")
196 state5 = State.snapshot()
198 print("\n*** LOG:")
199 print(log.out)
200 print()
202 print("print-test-after")
203 cout("cout-test-after")
205 # -- Dump state snapshots
206 print(f"{state1=}")
207 print(f"{state2=}")
208 print(f"{state3=}")
209 print(f"{state4=}")
210 print(f"{state5=}")
212 # -- Verify
213 assert "print-test-before" not in log.out
214 assert "cout-test-before" not in log.out
216 assert "print-test-inside" in log.out
217 assert "cout-test-inside" in log.out
219 assert "print-test-after" not in log.out
220 assert "cout-test-after" not in log.out