Coverage for tests / unit_tests / managers / test_scons_manager.py: 100%
38 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-08 02:47 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-08 02:47 +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-synth-extra-options": "-dsp -xyz",
30 "nextpnr-extra-options": "--freq 13",
31 "gtkwave-extra-options": "--rcvar=do_initial_zoom_fit 1",
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 {
45 type: "hx8k"
46 pack: "tq144:4k"
47 }
48}
49environment {
50 platform_id: "TBD"
51 is_windows: true # TBD
52 terminal_mode: FORCE_TERMINAL
53 theme_name: "light"
54 debug_level: 0
55 yosys_path: "TBD"
56 trellis_path: "TBD"
57 scons_shell_id: ""
58}
59apio_env_params {
60 env_name: "default"
61 board_id: "alhambra-ii"
62 top_module: "my_module"
63 yosys_synth_extra_options: "-dsp -xyz"
64 nextpnr_extra_options: "--freq 13"
65 gtkwave_extra_options: '--rcvar=do_initial_zoom_fit 1'
66}
67"""
69EXPECTED2 = """
70timestamp: "TBD"
71arch: ICE40
72fpga_info {
73 fpga_id: "ice40hx4k-tq144-8k"
74 part_num: "ICE40HX4K-TQ144"
75 size: "8k"
76 ice40 {
77 type: "hx8k"
78 pack: "tq144:4k"
79 }
80}
81verbosity {
82 all: true
83 synth: true
84 pnr: true
85}
86environment {
87 platform_id: "TBD"
88 is_windows: true # TBD
89 terminal_mode: FORCE_TERMINAL
90 theme_name: "light"
91 debug_level: 0
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_synth_extra_options: "-dsp -xyz"
101 nextpnr_extra_options: "--freq 13"
102 gtkwave_extra_options: '--rcvar=do_initial_zoom_fit 1'
103}
104target {
105 lint {
106 top_module: "my_module"
107 verilator_all: true
108 verilator_no_style: true
109 verilator_no_warns: "aa"
110 verilator_no_warns: "bb"
111 verilator_warns: "cc"
112 verilator_warns: "dd"
113 }
114}
115"""
118def test_default_params(apio_runner: ApioRunner):
119 """Tests the construct_scons_params() with default values."""
121 with apio_runner.in_sandbox() as sb:
123 # -- Setup a Scons object.
124 sb.write_apio_ini(TEST_APIO_INI_DICT)
125 apio_ctx = ApioContext(
126 project_policy=ProjectPolicy.PROJECT_REQUIRED,
127 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
128 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
129 )
130 scons = SConsManager(apio_ctx)
132 # -- Get the actual value.
133 scons_params = scons.construct_scons_params()
135 # -- Construct the expected value. We fill in non deterministic values.
136 expected = text_format.Parse(EXPECTED1, SconsParams())
137 expected.timestamp = scons_params.timestamp
138 expected.environment.platform_id = apio_ctx.platform_id
139 expected.environment.is_windows = apio_ctx.is_windows
140 expected.environment.yosys_path = str(
141 sb.packages_dir / "oss-cad-suite/share/yosys"
142 )
143 expected.environment.trellis_path = str(
144 sb.packages_dir / "oss-cad-suite/share/trellis"
145 )
146 expected.environment.scons_shell_id = apio_ctx.scons_shell_id
148 # -- Compare actual to expected values.
149 assert str(scons_params) == str(expected)
152def test_explicit_params(apio_runner: ApioRunner):
153 """Tests the construct_scons_params() method with values override.."""
155 with apio_runner.in_sandbox() as sb:
157 # -- Setup a Scons object.
158 sb.write_apio_ini(TEST_APIO_INI_DICT)
159 apio_ctx = ApioContext(
160 project_policy=ProjectPolicy.PROJECT_REQUIRED,
161 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
162 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
163 )
164 scons = SConsManager(apio_ctx)
166 # -- Get the actual value.
167 target_params = TargetParams(
168 lint=LintParams(
169 top_module="my_module",
170 verilator_all=True,
171 verilator_no_style=True,
172 verilator_no_warns=["aa", "bb"],
173 verilator_warns=["cc", "dd"],
174 )
175 )
176 verbosity = Verbosity(all=True, synth=True, pnr=True)
177 scons_params = scons.construct_scons_params(
178 verbosity=verbosity, target_params=target_params
179 )
181 # -- Construct the expected value. We fill in non deterministic values.
182 expected = text_format.Parse(EXPECTED2, SconsParams())
183 expected.timestamp = scons_params.timestamp
184 expected.environment.platform_id = apio_ctx.platform_id
185 expected.environment.is_windows = apio_ctx.is_windows
186 expected.environment.yosys_path = str(
187 sb.packages_dir / "oss-cad-suite/share/yosys"
188 )
189 expected.environment.trellis_path = str(
190 sb.packages_dir / "oss-cad-suite/share/trellis"
191 )
192 expected.environment.scons_shell_id = apio_ctx.scons_shell_id
194 # -- Compare actual to expected values.
195 assert str(scons_params) == str(expected)