Coverage for tests/unit_tests/managers/test_scons_manager.py: 100%
46 statements
« prev ^ index » next coverage.py v7.14.3, created at 2026-06-24 03:51 +0000
« prev ^ index » next coverage.py v7.14.3, created at 2026-06-24 03:51 +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 prjxray_db_path: "TBD"
60 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
146 expected.environment.apio_home_path = str(apio_ctx.apio_home_dir)
147 expected.environment.env_build_path = str(apio_ctx.env_build_path)
149 # -- TODO: For the missing platforms it should be the null string ""
150 expected.environment.prjxray_db_path = str(
151 sb.packages_dir / "openxc7/share/nextpnr/external/prjxray-db"
152 )
154 # -- TODO: For the missing platforms it should be the null string ""
155 expected.environment.chipdb_path = str(
156 sb.packages_dir / "openxc7/chipdb"
157 )
159 # -- Compare actual to expected values.
160 assert str(scons_params) == str(expected)
163def test_explicit_params(apio_runner: ApioRunner):
164 """Tests the construct_scons_params() method with values override.."""
166 with apio_runner.in_sandbox() as sb:
168 # -- Setup a Scons object.
169 sb.write_apio_ini(TEST_APIO_INI_DICT)
170 apio_ctx = ApioContext(
171 project_policy=ProjectPolicy.PROJECT_REQUIRED,
172 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
173 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
174 )
175 scons = SConsManager(apio_ctx)
177 # -- Get the actual value.
178 target_params = TargetParams(
179 lint=LintParams(
180 top_module="my_module",
181 )
182 )
183 verbosity = Verbosity(all=True, synth=True, pnr=True)
184 scons_params = scons.construct_scons_params(
185 verbosity=verbosity, target_params=target_params
186 )
188 # -- Construct the expected value. We fill in non deterministic values.
189 expected = text_format.Parse(EXPECTED2, SconsParams())
190 expected.timestamp = scons_params.timestamp
191 expected.environment.platform_id = apio_ctx.platform_id
192 expected.environment.is_windows = apio_ctx.is_windows
193 expected.environment.yosys_path = str(
194 sb.packages_dir / "oss-cad-suite/share/yosys"
195 )
196 expected.environment.trellis_path = str(
197 sb.packages_dir / "oss-cad-suite/share/trellis"
198 )
199 expected.environment.scons_shell_id = apio_ctx.scons_shell_id
200 expected.environment.apio_home_path = str(apio_ctx.apio_home_dir)
201 expected.environment.env_build_path = str(apio_ctx.env_build_path)
203 # -- TODO: For the missing platforms it should be the null string ""
204 expected.environment.prjxray_db_path = str(
205 sb.packages_dir / "openxc7/share/nextpnr/external/prjxray-db"
206 )
208 # -- TODO: For the missing platforms it should be the null string ""
209 expected.environment.chipdb_path = str(
210 sb.packages_dir / "openxc7/chipdb"
211 )
213 # -- Compare actual to expected values.
214 assert str(scons_params) == str(expected)