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

36 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-06 10:20 +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-synth-extra-options": "-dsp -xyz", 

30 "nextpnr-extra-options": "--freq 13", 

31 } 

32} 

33 

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} 

57apio_env_params { 

58 env_name: "default" 

59 board_id: "alhambra-ii" 

60 top_module: "my_module" 

61 yosys_synth_extra_options: "-dsp -xyz" 

62 nextpnr_extra_options: "--freq 13" 

63} 

64""" 

65 

66EXPECTED2 = """ 

67timestamp: "TBD" 

68arch: ICE40 

69fpga_info { 

70 fpga_id: "ice40hx4k-tq144-8k" 

71 part_num: "ICE40HX4K-TQ144" 

72 size: "8k" 

73 ice40 { 

74 type: "hx8k" 

75 pack: "tq144:4k" 

76 } 

77} 

78verbosity { 

79 all: true 

80 synth: true 

81 pnr: true 

82} 

83environment { 

84 platform_id: "TBD" 

85 is_windows: true # TBD 

86 terminal_mode: FORCE_TERMINAL 

87 theme_name: "light" 

88 debug_level: 0 

89 yosys_path: "TBD" 

90 trellis_path: "TBD" 

91} 

92apio_env_params { 

93 env_name: "default" 

94 board_id: "alhambra-ii" 

95 top_module: "my_module" 

96 yosys_synth_extra_options: "-dsp -xyz" 

97 nextpnr_extra_options: "--freq 13" 

98} 

99target { 

100 lint { 

101 top_module: "my_module" 

102 verilator_all: true 

103 verilator_no_style: true 

104 verilator_no_warns: "aa" 

105 verilator_no_warns: "bb" 

106 verilator_warns: "cc" 

107 verilator_warns: "dd" 

108 } 

109} 

110""" 

111 

112 

113def test_default_params(apio_runner: ApioRunner): 

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

115 

116 with apio_runner.in_sandbox() as sb: 

117 

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) 

126 

127 # -- Get the actual value. 

128 scons_params = scons.construct_scons_params() 

129 

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 

142 # -- Compare actual to expected values. 

143 assert str(scons_params) == str(expected) 

144 

145 

146def test_explicit_params(apio_runner: ApioRunner): 

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

148 

149 with apio_runner.in_sandbox() as sb: 

150 

151 # -- Setup a Scons object. 

152 sb.write_apio_ini(TEST_APIO_INI_DICT) 

153 apio_ctx = ApioContext( 

154 project_policy=ProjectPolicy.PROJECT_REQUIRED, 

155 remote_config_policy=RemoteConfigPolicy.CACHED_OK, 

156 packages_policy=PackagesPolicy.ENSURE_PACKAGES, 

157 ) 

158 scons = SConsManager(apio_ctx) 

159 

160 # -- Get the actual value. 

161 target_params = TargetParams( 

162 lint=LintParams( 

163 top_module="my_module", 

164 verilator_all=True, 

165 verilator_no_style=True, 

166 verilator_no_warns=["aa", "bb"], 

167 verilator_warns=["cc", "dd"], 

168 ) 

169 ) 

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

171 scons_params = scons.construct_scons_params( 

172 verbosity=verbosity, target_params=target_params 

173 ) 

174 

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

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

177 expected.timestamp = scons_params.timestamp 

178 expected.environment.platform_id = apio_ctx.platform_id 

179 expected.environment.is_windows = apio_ctx.is_windows 

180 expected.environment.yosys_path = str( 

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

182 ) 

183 expected.environment.trellis_path = str( 

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

185 ) 

186 

187 # -- Compare actual to expected values. 

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