Coverage for tests/unit_tests/managers/test_scons_manager.py: 100%
42 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-09 01:55 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-09 01:55 +0000
1"""
2Tests of the scons manager scons.py
3"""
5from google.protobuf import text_format
6from tests.conftest import ApioRunner
7from apio.common.proto.apio_pb2 import (
8 SconsParams,
9 Verbosity,
10 TargetParams,
11 LintParams,
12)
13from apio.apio_context import (
14 ApioContext,
15 PackagesPolicy,
16 ProjectPolicy,
17 RemoteConfigPolicy,
18)
19from apio.managers.scons_manager import SConsManager
22TEST_APIO_INI_DICT = {
23 "[env:default]": {
24 # -- Required.
25 "board": "alhambra-ii",
26 # -- Optional.
27 "top-module": "my_module",
28 "format-verible-options": "\n --aaa bbb\n --ccc ddd",
29 "yosys-extra-options": "-dsp -xyz",
30 "nextpnr-extra-options": "--freq 13",
31 "gtkwave-extra-options": "--rcvar=do_initial_zoom_fit 1",
32 "verilator-extra-options": "-Wno-fatal",
33 }
34}
36# -- The non determinisitc values marked with TBD are patched by the test
37# -- at runtime.
38EXPECTED1 = """
39timestamp: "TBD"
40arch: ICE40
41fpga_info {
42 fpga_id: "ice40hx4k-tq144-8k"
43 part_num: "ICE40HX4K-TQ144"
44 size: "8k"
45 ice40_params {
46 type: "hx8k"
47 package: "tq144:4k"
48 }
49}
50environment {
51 platform_id: "TBD"
52 is_windows: true # TBD
53 terminal_mode: FORCE_TERMINAL
54 theme_name: "light"
55 debug_level: 0
56 yosys_path: "TBD"
57 trellis_path: "TBD"
58 scons_shell_id: ""
59 xilinx_prjxray_db_path: "TBD"
60 xilinx_chipdb_path: "TBD"
61}
62apio_env_params {
63 env_name: "default"
64 board_id: "alhambra-ii"
65 top_module: "my_module"
66 yosys_extra_options: "-dsp -xyz"
67 nextpnr_extra_options: "--freq 13"
68 gtkwave_extra_options: '--rcvar=do_initial_zoom_fit 1'
69 verilator_extra_options: "-Wno-fatal",
70}
71"""
73EXPECTED2 = """
74timestamp: "TBD"
75arch: ICE40
76fpga_info {
77 fpga_id: "ice40hx4k-tq144-8k"
78 part_num: "ICE40HX4K-TQ144"
79 size: "8k"
80 ice40_params {
81 type: "hx8k"
82 package: "tq144:4k"
83 }
84}
85verbosity {
86 all: true
87 synth: true
88 pnr: true
89}
90environment {
91 platform_id: "TBD"
92 is_windows: true # TBD
93 terminal_mode: FORCE_TERMINAL
94 theme_name: "light"
95 debug_level: 0
96 yosys_path: "TBD"
97 trellis_path: "TBD"
98 scons_shell_id: ""
99}
100apio_env_params {
101 env_name: "default"
102 board_id: "alhambra-ii"
103 top_module: "my_module"
104 yosys_extra_options: "-dsp -xyz"
105 nextpnr_extra_options: "--freq 13"
106 gtkwave_extra_options: '--rcvar=do_initial_zoom_fit 1'
107 verilator_extra_options: "-Wno-fatal"
108}
109target {
110 lint {
111 top_module: "my_module"
112 }
113}
114"""
117def test_default_params(apio_runner: ApioRunner):
118 """Tests the construct_scons_params() with default values."""
120 with apio_runner.in_sandbox() as sb:
122 # -- Setup a Scons object.
123 sb.write_apio_ini(TEST_APIO_INI_DICT)
124 apio_ctx = ApioContext(
125 project_policy=ProjectPolicy.PROJECT_REQUIRED,
126 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
127 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
128 )
129 scons = SConsManager(apio_ctx)
131 # -- Get the actual value.
132 scons_params = scons.construct_scons_params()
134 # -- Construct the expected value. We fill in non deterministic values.
135 expected = text_format.Parse(EXPECTED1, SconsParams())
136 expected.timestamp = scons_params.timestamp
137 expected.environment.platform_id = apio_ctx.platform_id
138 expected.environment.is_windows = apio_ctx.is_windows
139 expected.environment.yosys_path = str(
140 sb.packages_dir / "oss-cad-suite/share/yosys"
141 )
142 expected.environment.trellis_path = str(
143 sb.packages_dir / "oss-cad-suite/share/trellis"
144 )
145 expected.environment.scons_shell_id = apio_ctx.scons_shell_id
147 # -- TODO: For the missing platforms it should be the null string ""
148 expected.environment.xilinx_prjxray_db_path = str(
149 sb.packages_dir / "openxc7/share/nextpnr/external/prjxray-db"
150 )
152 # -- TODO: For the missing platforms it should be the null string ""
153 expected.environment.xilinx_chipdb_path = str(
154 sb.packages_dir / "openxc7/chipdb"
155 )
157 # -- Compare actual to expected values.
158 assert str(scons_params) == str(expected)
161def test_explicit_params(apio_runner: ApioRunner):
162 """Tests the construct_scons_params() method with values override.."""
164 with apio_runner.in_sandbox() as sb:
166 # -- Setup a Scons object.
167 sb.write_apio_ini(TEST_APIO_INI_DICT)
168 apio_ctx = ApioContext(
169 project_policy=ProjectPolicy.PROJECT_REQUIRED,
170 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
171 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
172 )
173 scons = SConsManager(apio_ctx)
175 # -- Get the actual value.
176 target_params = TargetParams(
177 lint=LintParams(
178 top_module="my_module",
179 )
180 )
181 verbosity = Verbosity(all=True, synth=True, pnr=True)
182 scons_params = scons.construct_scons_params(
183 verbosity=verbosity, target_params=target_params
184 )
186 # -- Construct the expected value. We fill in non deterministic values.
187 expected = text_format.Parse(EXPECTED2, SconsParams())
188 expected.timestamp = scons_params.timestamp
189 expected.environment.platform_id = apio_ctx.platform_id
190 expected.environment.is_windows = apio_ctx.is_windows
191 expected.environment.yosys_path = str(
192 sb.packages_dir / "oss-cad-suite/share/yosys"
193 )
194 expected.environment.trellis_path = str(
195 sb.packages_dir / "oss-cad-suite/share/trellis"
196 )
197 expected.environment.scons_shell_id = apio_ctx.scons_shell_id
199 # -- TODO: For the missing platforms it should be the null string ""
200 expected.environment.xilinx_prjxray_db_path = str(
201 sb.packages_dir / "openxc7/share/nextpnr/external/prjxray-db"
202 )
204 # -- TODO: For the missing platforms it should be the null string ""
205 expected.environment.xilinx_chipdb_path = str(
206 sb.packages_dir / "openxc7/chipdb"
207 )
209 # -- Compare actual to expected values.
210 assert str(scons_params) == str(expected)