Coverage for tests/unit_tests/test_conftest.py: 100%

109 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-07 05:08 +0000

1# This file is part of the Apio project 

2# (C) 2016-present FPGAwars 

3# Project authors: 

4# * Jesús Arroyo (Jesus89) (2016-2019) 

5# * Juan Gonzalez (obijuan) (2019-2024) 

6# * The Apio Team (2024-present) 

7# License: GPLv2 

8 

9""" 

10Tests of the functionality of the pytest helper conftest.py 

11""" 

12 

13import sys 

14from dataclasses import dataclass 

15from tests.conftest import ApioRunner 

16from apio.common.apio_console import cout, console, cstyle 

17from apio.apio_context import ( 

18 ApioContext, 

19 ProjectPolicy, 

20 RemoteConfigPolicy, 

21 PackagesPolicy, 

22) 

23 

24 

25@dataclass(frozen=True) 

26class State: 

27 """Capture the I/O state for verification.""" 

28 

29 stdout_id: int 

30 stderr_id: int 

31 console_id: int 

32 console_file_id: int 

33 

34 @classmethod 

35 def snapshot(cls) -> "State": 

36 """Snapshot the current state""" 

37 c = console() 

38 return State( 

39 stdout_id=id(sys.stdout), 

40 stderr_id=id(sys.stderr), 

41 console_id=id(c), 

42 console_file_id=id(c.file), 

43 ) 

44 

45 

46def test_logger_minimal(apio_runner: ApioRunner): 

47 """Tests ApioRunner logger functionality with minimal configuration 

48 with no sandbox, no apio context, and no apio_console.py output.""" 

49 

50 # -- Test 

51 print("print-test-before") 

52 

53 with apio_runner.with_logger() as log: 

54 print("print-test-inside") 

55 

56 print("print-test-after") 

57 

58 # -- Verify 

59 assert "print-test-before" not in log.out 

60 assert "print-test-inside" in log.out 

61 assert "print-test-after" not in log.out 

62 

63 

64def test_logger_with_console(apio_runner: ApioRunner): 

65 """Tests ApioRunner logger functionality with sandbox only and 

66 no apio context, and no apio_console.py output.""" 

67 

68 with apio_runner.in_sandbox(): 

69 

70 state1 = State.snapshot() 

71 

72 # -- Test 

73 print("print-test-before") 

74 cout("cout-test-before") 

75 

76 state2 = State.snapshot() 

77 

78 with apio_runner.with_logger() as log: 

79 state3 = State.snapshot() 

80 print("print-test-inside") 

81 cout("cout-test-inside") 

82 

83 state4 = State.snapshot() 

84 

85 print("\n*** LOG:") 

86 print(log.out) 

87 print() 

88 

89 print("print-test-after") 

90 cout("cout-test-after") 

91 

92 # -- Dump state snapshots 

93 print(f"{state1=}") 

94 print(f"{state2=}") 

95 print(f"{state3=}") 

96 print(f"{state4=}") 

97 

98 # -- Verify log 

99 assert "print-test-before" not in log.out 

100 assert "cout-test-before" not in log.out 

101 

102 assert "print-test-inside" in log.out 

103 assert "cout-test-inside" in log.out 

104 

105 assert "print-test-after" not in log.out 

106 assert "cout-test-after" not in log.out 

107 

108 

109def test_logger_with_cstyle(apio_runner: ApioRunner): 

110 """Tests ApioRunner logger functionality with sandbox and , 

111 apio_console.py cstyle and cout but no ApioContext.""" 

112 

113 with apio_runner.in_sandbox(): 

114 

115 state1 = State.snapshot() 

116 

117 # -- Invoke cstyle, in the past in interfered with the logger. 

118 text = "red-text" 

119 styled_text = cstyle(text, style="red") 

120 print(styled_text) 

121 print(repr(styled_text)) 

122 assert len(styled_text) > len(text) 

123 assert text in styled_text 

124 

125 state2 = State.snapshot() 

126 

127 # -- Test 

128 print("print-test-before") 

129 cout("cout-test-before") 

130 

131 state3 = State.snapshot() 

132 

133 with apio_runner.with_logger() as log: 

134 state4 = State.snapshot() 

135 

136 print("print-test-inside") 

137 cout("cout-test-inside") 

138 

139 state5 = State.snapshot() 

140 

141 print("\n*** LOG:") 

142 print(log.out) 

143 print() 

144 

145 print("print-test-after") 

146 cout("cout-test-after") 

147 

148 # -- Dump state snapshots 

149 print(f"{state1=}") 

150 print(f"{state2=}") 

151 print(f"{state3=}") 

152 print(f"{state4=}") 

153 print(f"{state5=}") 

154 

155 # -- Verify 

156 assert "print-test-before" not in log.out 

157 assert "cout-test-before" not in log.out 

158 

159 assert "print-test-inside" in log.out 

160 assert "cout-test-inside" in log.out 

161 

162 assert "print-test-after" not in log.out 

163 assert "cout-test-after" not in log.out 

164 

165 

166def test_logger_with_apio_ctx(apio_runner: ApioRunner): 

167 """Tests ApioRunner logger functionality with sandbox and , 

168 apio context. This is the most typical test configuration.""" 

169 

170 with apio_runner.in_sandbox() as sb: 

171 

172 state1 = State.snapshot() 

173 

174 # -- Create an apio context with a fake project. 

175 sb.write_default_apio_ini() 

176 _ = ApioContext( 

177 project_policy=ProjectPolicy.PROJECT_REQUIRED, 

178 remote_config_policy=RemoteConfigPolicy.CACHED_OK, 

179 packages_policy=PackagesPolicy.ENSURE_PACKAGES, 

180 ) 

181 

182 state2 = State.snapshot() 

183 

184 # -- Test 

185 print("print-test-before") 

186 cout("cout-test-before") 

187 

188 state3 = State.snapshot() 

189 

190 with apio_runner.with_logger() as log: 

191 state4 = State.snapshot() 

192 

193 print("print-test-inside") 

194 cout("cout-test-inside") 

195 

196 state5 = State.snapshot() 

197 

198 print("\n*** LOG:") 

199 print(log.out) 

200 print() 

201 

202 print("print-test-after") 

203 cout("cout-test-after") 

204 

205 # -- Dump state snapshots 

206 print(f"{state1=}") 

207 print(f"{state2=}") 

208 print(f"{state3=}") 

209 print(f"{state4=}") 

210 print(f"{state5=}") 

211 

212 # -- Verify 

213 assert "print-test-before" not in log.out 

214 assert "cout-test-before" not in log.out 

215 

216 assert "print-test-inside" in log.out 

217 assert "cout-test-inside" in log.out 

218 

219 assert "print-test-after" not in log.out 

220 assert "cout-test-after" not in log.out