Coverage for tests/unit_tests/managers/test_scons_manager.py: 100%
40 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 scons manager scons.py
3"""
5from google.protobuf import text_format
6from tests.conftest import ApioRunner
7from apio.common.proto.apio_scons_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
21TEST_APIO_INI_DICT = {
22 "[env:default]": {
23 # -- Required.
24 "board": "alhambra-ii",
25 # -- Optional.
26 "top-module": "my_module",
27 "format-verible-options": "\n --aaa bbb\n --ccc ddd",
28 "yosys-extra-options": "-dsp -xyz",
29 "nextpnr-extra-options": "--freq 13",
30 "gtkwave-extra-options": "--rcvar=do_initial_zoom_fit 1",
31 "verilator-extra-options": "-Wno-fatal",
32 }
33}
35# -- The non determinisitc values marked with TBD are patched by the test
36# -- at runtime.
37EXPECTED1 = """
38timestamp: "TBD"
39arch: ice40
40fpga_info {
41 fpga_id: "ice40hx4k-tq144-8k"
42 part_num: "ICE40HX4K-TQ144"
43 size: "8k"
44 ice40_params {
45 type: "hx8k"
46 package: "tq144:4k"
47 }
48}
49environment {
50 platform_id: "TBD"
51 is_windows: true # TBD
52 terminal_mode: FORCE_TERMINAL
53 theme_name: "light"
54 yosys_path: "TBD"
55 trellis_path: "TBD"
56 scons_shell_id: ""
57 xilinx_prjxray_db_path: "TBD"
58}
59apio_env_params {
60 env_name: "default"
61 board_id: "alhambra-ii"
62 top_module: "my_module"
63 yosys_extra_options: "-dsp -xyz"
64 nextpnr_extra_options: "--freq 13"
65 gtkwave_extra_options: '--rcvar=do_initial_zoom_fit 1'
66 verilator_extra_options: "-Wno-fatal",
67}
68"""
70EXPECTED2 = """
71timestamp: "TBD"
72arch: ice40
73fpga_info {
74 fpga_id: "ice40hx4k-tq144-8k"
75 part_num: "ICE40HX4K-TQ144"
76 size: "8k"
77 ice40_params {
78 type: "hx8k"
79 package: "tq144:4k"
80 }
81}
82verbosity {
83 all: true
84 synth: true
85 pnr: true
86}
87environment {
88 platform_id: "TBD"
89 is_windows: true # TBD
90 terminal_mode: FORCE_TERMINAL
91 theme_name: "light"
92 yosys_path: "TBD"
93 trellis_path: "TBD"
94 scons_shell_id: ""
95}
96apio_env_params {
97 env_name: "default"
98 board_id: "alhambra-ii"
99 top_module: "my_module"
100 yosys_extra_options: "-dsp -xyz"
101 nextpnr_extra_options: "--freq 13"
102 gtkwave_extra_options: '--rcvar=do_initial_zoom_fit 1'
103 verilator_extra_options: "-Wno-fatal"
104}
105target {
106 lint {
107 top_module: "my_module"
108 }
109}
110"""
113def test_default_params(apio_runner: ApioRunner):
114 """Tests the construct_scons_params() with default values."""
116 with apio_runner.in_sandbox() as sb:
118 # -- Setup a Scons object.
119 sb.write_apio_ini(TEST_APIO_INI_DICT)
120 apio_ctx = ApioContext(
121 project_policy=ProjectPolicy.PROJECT_REQUIRED,
122 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
123 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
124 )
125 scons = SConsManager(apio_ctx)
127 # -- Get the actual value.
128 scons_params = scons.construct_scons_params()
130 # -- Construct the expected value. We fill in non deterministic values.
131 expected = text_format.Parse(EXPECTED1, SconsParams())
132 expected.timestamp = scons_params.timestamp
133 expected.environment.platform_id = apio_ctx.platform_id
134 expected.environment.is_windows = apio_ctx.is_windows
135 expected.environment.yosys_path = str(
136 sb.packages_dir / "oss-cad-suite/share/yosys"
137 )
138 expected.environment.trellis_path = str(
139 sb.packages_dir / "oss-cad-suite/share/trellis"
140 )
141 expected.environment.scons_shell_id = apio_ctx.scons_shell_id
143 expected.environment.xilinx_prjxray_db_path = str(
144 sb.packages_dir / "openxc7/share/nextpnr/external/prjxray-db"
145 )
147 # -- Compare actual to expected values.
148 assert str(scons_params) == str(expected)
151def test_explicit_params(apio_runner: ApioRunner):
152 """Tests the construct_scons_params() method with values override.."""
154 with apio_runner.in_sandbox() as sb:
156 # -- Setup a Scons object.
157 sb.write_apio_ini(TEST_APIO_INI_DICT)
158 apio_ctx = ApioContext(
159 project_policy=ProjectPolicy.PROJECT_REQUIRED,
160 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
161 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
162 )
163 scons = SConsManager(apio_ctx)
165 # -- Get the actual value.
166 target_params = TargetParams(
167 lint=LintParams(
168 top_module="my_module",
169 )
170 )
171 verbosity = Verbosity(all=True, synth=True, pnr=True)
172 scons_params = scons.construct_scons_params(
173 verbosity=verbosity, target_params=target_params
174 )
176 # -- Construct the expected value. We fill in non deterministic values.
177 expected = text_format.Parse(EXPECTED2, SconsParams())
178 expected.timestamp = scons_params.timestamp
179 expected.environment.platform_id = apio_ctx.platform_id
180 expected.environment.is_windows = apio_ctx.is_windows
181 expected.environment.yosys_path = str(
182 sb.packages_dir / "oss-cad-suite/share/yosys"
183 )
184 expected.environment.trellis_path = str(
185 sb.packages_dir / "oss-cad-suite/share/trellis"
186 )
187 expected.environment.scons_shell_id = apio_ctx.scons_shell_id
189 expected.environment.xilinx_prjxray_db_path = str(
190 sb.packages_dir / "openxc7/share/nextpnr/external/prjxray-db"
191 )
193 # -- Compare actual to expected values.
194 assert str(scons_params) == str(expected)