Coverage for tests / unit_tests / managers / test_scons_manager.py: 100%
38 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-24 01:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-24 01: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_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 }
32}
34# -- The non determinisitc values marked with TBD are patched by the test
35# -- at runtime.
36EXPECTED1 = """
37timestamp: "TBD"
38arch: ICE40
39fpga_info {
40 fpga_id: "ice40hx4k-tq144-8k"
41 part_num: "ICE40HX4K-TQ144"
42 size: "8k"
43 ice40 {
44 type: "hx8k"
45 pack: "tq144:4k"
46 }
47}
48environment {
49 platform_id: "TBD"
50 is_windows: true # TBD
51 terminal_mode: FORCE_TERMINAL
52 theme_name: "light"
53 debug_level: 0
54 yosys_path: "TBD"
55 trellis_path: "TBD"
56 scons_shell_id: ""
57}
58apio_env_params {
59 env_name: "default"
60 board_id: "alhambra-ii"
61 top_module: "my_module"
62 yosys_synth_extra_options: "-dsp -xyz"
63 nextpnr_extra_options: "--freq 13"
64}
65"""
67EXPECTED2 = """
68timestamp: "TBD"
69arch: ICE40
70fpga_info {
71 fpga_id: "ice40hx4k-tq144-8k"
72 part_num: "ICE40HX4K-TQ144"
73 size: "8k"
74 ice40 {
75 type: "hx8k"
76 pack: "tq144:4k"
77 }
78}
79verbosity {
80 all: true
81 synth: true
82 pnr: true
83}
84environment {
85 platform_id: "TBD"
86 is_windows: true # TBD
87 terminal_mode: FORCE_TERMINAL
88 theme_name: "light"
89 debug_level: 0
90 yosys_path: "TBD"
91 trellis_path: "TBD"
92 scons_shell_id: ""
93}
94apio_env_params {
95 env_name: "default"
96 board_id: "alhambra-ii"
97 top_module: "my_module"
98 yosys_synth_extra_options: "-dsp -xyz"
99 nextpnr_extra_options: "--freq 13"
100}
101target {
102 lint {
103 top_module: "my_module"
104 verilator_all: true
105 verilator_no_style: true
106 verilator_no_warns: "aa"
107 verilator_no_warns: "bb"
108 verilator_warns: "cc"
109 verilator_warns: "dd"
110 }
111}
112"""
115def test_default_params(apio_runner: ApioRunner):
116 """Tests the construct_scons_params() with default values."""
118 with apio_runner.in_sandbox() as sb:
120 # -- Setup a Scons object.
121 sb.write_apio_ini(TEST_APIO_INI_DICT)
122 apio_ctx = ApioContext(
123 project_policy=ProjectPolicy.PROJECT_REQUIRED,
124 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
125 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
126 )
127 scons = SConsManager(apio_ctx)
129 # -- Get the actual value.
130 scons_params = scons.construct_scons_params()
132 # -- Construct the expected value. We fill in non deterministic values.
133 expected = text_format.Parse(EXPECTED1, SconsParams())
134 expected.timestamp = scons_params.timestamp
135 expected.environment.platform_id = apio_ctx.platform_id
136 expected.environment.is_windows = apio_ctx.is_windows
137 expected.environment.yosys_path = str(
138 sb.packages_dir / "oss-cad-suite/share/yosys"
139 )
140 expected.environment.trellis_path = str(
141 sb.packages_dir / "oss-cad-suite/share/trellis"
142 )
143 expected.environment.scons_shell_id = apio_ctx.scons_shell_id
145 # -- Compare actual to expected values.
146 assert str(scons_params) == str(expected)
149def test_explicit_params(apio_runner: ApioRunner):
150 """Tests the construct_scons_params() method with values override.."""
152 with apio_runner.in_sandbox() as sb:
154 # -- Setup a Scons object.
155 sb.write_apio_ini(TEST_APIO_INI_DICT)
156 apio_ctx = ApioContext(
157 project_policy=ProjectPolicy.PROJECT_REQUIRED,
158 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
159 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
160 )
161 scons = SConsManager(apio_ctx)
163 # -- Get the actual value.
164 target_params = TargetParams(
165 lint=LintParams(
166 top_module="my_module",
167 verilator_all=True,
168 verilator_no_style=True,
169 verilator_no_warns=["aa", "bb"],
170 verilator_warns=["cc", "dd"],
171 )
172 )
173 verbosity = Verbosity(all=True, synth=True, pnr=True)
174 scons_params = scons.construct_scons_params(
175 verbosity=verbosity, target_params=target_params
176 )
178 # -- Construct the expected value. We fill in non deterministic values.
179 expected = text_format.Parse(EXPECTED2, SconsParams())
180 expected.timestamp = scons_params.timestamp
181 expected.environment.platform_id = apio_ctx.platform_id
182 expected.environment.is_windows = apio_ctx.is_windows
183 expected.environment.yosys_path = str(
184 sb.packages_dir / "oss-cad-suite/share/yosys"
185 )
186 expected.environment.trellis_path = str(
187 sb.packages_dir / "oss-cad-suite/share/trellis"
188 )
189 expected.environment.scons_shell_id = apio_ctx.scons_shell_id
191 # -- Compare actual to expected values.
192 assert str(scons_params) == str(expected)