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

109 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-23 03:53 +0000

1""" 

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

3""" 

4 

5import sys 

6from dataclasses import dataclass 

7from tests.conftest import ApioRunner 

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

9from apio.apio_context import ( 

10 ApioContext, 

11 ProjectPolicy, 

12 RemoteConfigPolicy, 

13 PackagesPolicy, 

14) 

15 

16 

17@dataclass(frozen=True) 

18class State: 

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

20 

21 stdout_id: int 

22 stderr_id: int 

23 console_id: int 

24 console_file_id: int 

25 

26 @classmethod 

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

28 """Snapshot the current state""" 

29 c = console() 

30 return State( 

31 stdout_id=id(sys.stdout), 

32 stderr_id=id(sys.stderr), 

33 console_id=id(c), 

34 console_file_id=id(c.file), 

35 ) 

36 

37 

38def test_logger_minimal(apio_runner: ApioRunner): 

39 """Tests ApioRunner logger functionality with minimal configuration 

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

41 

42 # -- Test 

43 print("print-test-before") 

44 

45 with apio_runner.with_logger() as log: 

46 print("print-test-inside") 

47 

48 print("print-test-after") 

49 

50 # -- Verify 

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

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

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

54 

55 

56def test_logger_with_console(apio_runner: ApioRunner): 

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

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

59 

60 with apio_runner.in_sandbox(): 

61 

62 state1 = State.snapshot() 

63 

64 # -- Test 

65 print("print-test-before") 

66 cout("cout-test-before") 

67 

68 state2 = State.snapshot() 

69 

70 with apio_runner.with_logger() as log: 

71 state3 = State.snapshot() 

72 print("print-test-inside") 

73 cout("cout-test-inside") 

74 

75 state4 = State.snapshot() 

76 

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

78 print(log.out) 

79 print() 

80 

81 print("print-test-after") 

82 cout("cout-test-after") 

83 

84 # -- Dump state snapshots 

85 print(f"{state1=}") 

86 print(f"{state2=}") 

87 print(f"{state3=}") 

88 print(f"{state4=}") 

89 

90 # -- Verify log 

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

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

93 

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

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

96 

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

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

99 

100 

101def test_logger_with_cstyle(apio_runner: ApioRunner): 

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

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

104 

105 with apio_runner.in_sandbox(): 

106 

107 state1 = State.snapshot() 

108 

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

110 text = "red-text" 

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

112 print(styled_text) 

113 print(repr(styled_text)) 

114 assert len(styled_text) > len(text) 

115 assert text in styled_text 

116 

117 state2 = State.snapshot() 

118 

119 # -- Test 

120 print("print-test-before") 

121 cout("cout-test-before") 

122 

123 state3 = State.snapshot() 

124 

125 with apio_runner.with_logger() as log: 

126 state4 = State.snapshot() 

127 

128 print("print-test-inside") 

129 cout("cout-test-inside") 

130 

131 state5 = State.snapshot() 

132 

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

134 print(log.out) 

135 print() 

136 

137 print("print-test-after") 

138 cout("cout-test-after") 

139 

140 # -- Dump state snapshots 

141 print(f"{state1=}") 

142 print(f"{state2=}") 

143 print(f"{state3=}") 

144 print(f"{state4=}") 

145 print(f"{state5=}") 

146 

147 # -- Verify 

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

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

150 

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

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

153 

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

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

156 

157 

158def test_logger_with_apio_ctx(apio_runner: ApioRunner): 

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

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

161 

162 with apio_runner.in_sandbox() as sb: 

163 

164 state1 = State.snapshot() 

165 

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

167 sb.write_default_apio_ini() 

168 _ = ApioContext( 

169 project_policy=ProjectPolicy.PROJECT_REQUIRED, 

170 remote_config_policy=RemoteConfigPolicy.CACHED_OK, 

171 packages_policy=PackagesPolicy.ENSURE_PACKAGES, 

172 ) 

173 

174 state2 = State.snapshot() 

175 

176 # -- Test 

177 print("print-test-before") 

178 cout("cout-test-before") 

179 

180 state3 = State.snapshot() 

181 

182 with apio_runner.with_logger() as log: 

183 state4 = State.snapshot() 

184 

185 print("print-test-inside") 

186 cout("cout-test-inside") 

187 

188 state5 = State.snapshot() 

189 

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

191 print(log.out) 

192 print() 

193 

194 print("print-test-after") 

195 cout("cout-test-after") 

196 

197 # -- Dump state snapshots 

198 print(f"{state1=}") 

199 print(f"{state2=}") 

200 print(f"{state3=}") 

201 print(f"{state4=}") 

202 print(f"{state5=}") 

203 

204 # -- Verify 

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

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

207 

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

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

210 

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

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