Coverage for tests / unit_tests / managers / test_scons_manager.py: 100%

38 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-25 02:31 +0000

1""" 

2Tests of the scons manager scons.py 

3""" 

4 

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 

20 

21 

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} 

35 

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} 

60apio_env_params { 

61 env_name: "default" 

62 board_id: "alhambra-ii" 

63 top_module: "my_module" 

64 yosys_extra_options: "-dsp -xyz" 

65 nextpnr_extra_options: "--freq 13" 

66 gtkwave_extra_options: '--rcvar=do_initial_zoom_fit 1' 

67 verilator_extra_options: "-Wno-fatal", 

68} 

69""" 

70 

71EXPECTED2 = """ 

72timestamp: "TBD" 

73arch: ICE40 

74fpga_info { 

75 fpga_id: "ice40hx4k-tq144-8k" 

76 part_num: "ICE40HX4K-TQ144" 

77 size: "8k" 

78 ice40_params { 

79 type: "hx8k" 

80 package: "tq144:4k" 

81 } 

82} 

83verbosity { 

84 all: true 

85 synth: true 

86 pnr: true 

87} 

88environment { 

89 platform_id: "TBD" 

90 is_windows: true # TBD 

91 terminal_mode: FORCE_TERMINAL 

92 theme_name: "light" 

93 debug_level: 0 

94 yosys_path: "TBD" 

95 trellis_path: "TBD" 

96 scons_shell_id: "" 

97} 

98apio_env_params { 

99 env_name: "default" 

100 board_id: "alhambra-ii" 

101 top_module: "my_module" 

102 yosys_extra_options: "-dsp -xyz" 

103 nextpnr_extra_options: "--freq 13" 

104 gtkwave_extra_options: '--rcvar=do_initial_zoom_fit 1' 

105 verilator_extra_options: "-Wno-fatal" 

106} 

107target { 

108 lint { 

109 top_module: "my_module" 

110 } 

111} 

112""" 

113 

114 

115def test_default_params(apio_runner: ApioRunner): 

116 """Tests the construct_scons_params() with default values.""" 

117 

118 with apio_runner.in_sandbox() as sb: 

119 

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) 

128 

129 # -- Get the actual value. 

130 scons_params = scons.construct_scons_params() 

131 

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 

144 

145 # -- Compare actual to expected values. 

146 assert str(scons_params) == str(expected) 

147 

148 

149def test_explicit_params(apio_runner: ApioRunner): 

150 """Tests the construct_scons_params() method with values override..""" 

151 

152 with apio_runner.in_sandbox() as sb: 

153 

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) 

162 

163 # -- Get the actual value. 

164 target_params = TargetParams( 

165 lint=LintParams( 

166 top_module="my_module", 

167 ) 

168 ) 

169 verbosity = Verbosity(all=True, synth=True, pnr=True) 

170 scons_params = scons.construct_scons_params( 

171 verbosity=verbosity, target_params=target_params 

172 ) 

173 

174 # -- Construct the expected value. We fill in non deterministic values. 

175 expected = text_format.Parse(EXPECTED2, SconsParams()) 

176 expected.timestamp = scons_params.timestamp 

177 expected.environment.platform_id = apio_ctx.platform_id 

178 expected.environment.is_windows = apio_ctx.is_windows 

179 expected.environment.yosys_path = str( 

180 sb.packages_dir / "oss-cad-suite/share/yosys" 

181 ) 

182 expected.environment.trellis_path = str( 

183 sb.packages_dir / "oss-cad-suite/share/trellis" 

184 ) 

185 expected.environment.scons_shell_id = apio_ctx.scons_shell_id 

186 

187 # -- Compare actual to expected values. 

188 assert str(scons_params) == str(expected)