Coverage for tests / test_rich_markup_mode.py: 100%

146 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-11-18 11:12 +0000

1import os 1abcdefghij

2import subprocess 1abcdefghij

3import sys 1abcdefghij

4 

5import pytest 1abcdefghij

6import typer 1abcdefghij

7import typer.completion 1abcdefghij

8from typer.testing import CliRunner 1abcdefghij

9 

10runner = CliRunner() 1abcdefghij

11rounded = ["╭", "─", "┬", "╮", "│", "├", "┼", "┤", "╰", "┴", "╯"] 1abcdefghij

12 

13 

14def test_rich_markup_mode_none(): 1abcdefghij

15 app = typer.Typer(rich_markup_mode=None) 1abcdefghij

16 

17 @app.command() 1abcdefghij

18 def main(arg: str): 1abcdefghij

19 """Main function""" 

20 print(f"Hello {arg}") 1abcdefghij

21 

22 assert app.rich_markup_mode is None 1abcdefghij

23 

24 result = runner.invoke(app, ["World"]) 1abcdefghij

25 assert "Hello World" in result.stdout 1abcdefghij

26 

27 result = runner.invoke(app, ["--help"]) 1abcdefghij

28 assert "arg <str> [required]" in result.stdout 1abcdefghij

29 assert all(c not in result.stdout for c in rounded) 1abcdefghij

30 

31 

32def test_rich_markup_mode_rich(): 1abcdefghij

33 app = typer.Typer(rich_markup_mode="rich") 1abcdefghij

34 

35 @app.command() 1abcdefghij

36 def main(arg: str): 1abcdefghij

37 """Main function""" 

38 print(f"Hello {arg}") 1abcdefghij

39 

40 assert app.rich_markup_mode == "rich" 1abcdefghij

41 

42 result = runner.invoke(app, ["World"]) 1abcdefghij

43 assert "Hello World" in result.stdout 1abcdefghij

44 

45 result = runner.invoke(app, ["--help"]) 1abcdefghij

46 assert any(c in result.stdout for c in rounded) 1abcdefghij

47 

48 

49@pytest.mark.parametrize( 1abcdefghij

50 "env_var_value, expected_result", 

51 [ 

52 ("1", True), 

53 ("True", True), 

54 ("TRUE", True), 

55 ("true", True), 

56 ("0", False), 

57 ("False", False), 

58 ("FALSE", False), 

59 ("false", False), 

60 ("somerandomvalue", True), 

61 ], 

62) 

63def test_rich_markup_mode_envvar(env_var_value: str, expected_result: bool): 1abcdefghij

64 result = subprocess.run( 1abcdefghij

65 [ 

66 sys.executable, 

67 "-m", 

68 "coverage", 

69 "run", 

70 "-m", 

71 "typer", 

72 "tests/assets/cli/sample.py", 

73 "--help", 

74 ], 

75 capture_output=True, 

76 env={ 

77 **os.environ, 

78 "TYPER_USE_RICH": env_var_value, 

79 "PYTHONIOENCODING": "utf-8", 

80 }, 

81 encoding="utf-8", 

82 ) 

83 assert any(c in result.stdout for c in rounded) == expected_result 1abcdefghij

84 

85 

86@pytest.mark.parametrize( 1abcdefghij

87 "mode,lines", 

88 [ 

89 pytest.param( 

90 "markdown", 

91 ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""], 

92 ), 

93 pytest.param( 

94 "rich", ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""] 

95 ), 

96 pytest.param( 

97 None, ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""] 

98 ), 

99 ], 

100) 

101def test_markup_mode_newline_pr815(mode: str, lines: list[str]): 1abcdefghij

102 app = typer.Typer(rich_markup_mode=mode) 1abcdefghij

103 

104 @app.command() 1abcdefghij

105 def main(arg: str): 1abcdefghij

106 """First line 

107 

108 Line 1 

109 

110 Line 2 

111 

112 Line 3 

113 """ 

114 print(f"Hello {arg}") 1abcdefghij

115 

116 assert app.rich_markup_mode == mode 1abcdefghij

117 

118 result = runner.invoke(app, ["World"]) 1abcdefghij

119 assert "Hello World" in result.stdout 1abcdefghij

120 

121 result = runner.invoke(app, ["--help"]) 1abcdefghij

122 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefghij

123 if mode: 1abcdefghij

124 assert any(c in result.stdout for c in rounded) 1abcdefghij

125 help_start = result_lines.index("First line") 1abcdefghij

126 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefghij

127 assert help_start != -1 1abcdefghij

128 assert result_lines[help_start:arg_start] == lines 1abcdefghij

129 

130 

131@pytest.mark.parametrize( 1abcdefghij

132 "mode,lines", 

133 [ 

134 pytest.param("markdown", ["First line", "", "Line 1 Line 2 Line 3", ""]), 

135 pytest.param("rich", ["First line", "", "Line 1", "Line 2", "Line 3", ""]), 

136 pytest.param(None, ["First line", "", "Line 1 Line 2 Line 3", ""]), 

137 ], 

138) 

139def test_markup_mode_newline_issue447(mode: str, lines: list[str]): 1abcdefghij

140 app = typer.Typer(rich_markup_mode=mode) 1abcdefghij

141 

142 @app.command() 1abcdefghij

143 def main(arg: str): 1abcdefghij

144 """First line 

145 

146 Line 1 

147 Line 2 

148 Line 3 

149 """ 

150 print(f"Hello {arg}") 1abcdefghij

151 

152 assert app.rich_markup_mode == mode 1abcdefghij

153 

154 result = runner.invoke(app, ["World"]) 1abcdefghij

155 assert "Hello World" in result.stdout 1abcdefghij

156 

157 result = runner.invoke(app, ["--help"]) 1abcdefghij

158 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefghij

159 if mode: 1abcdefghij

160 assert any(c in result.stdout for c in rounded) 1abcdefghij

161 help_start = result_lines.index("First line") 1abcdefghij

162 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefghij

163 assert help_start != -1 1abcdefghij

164 assert result_lines[help_start:arg_start] == lines 1abcdefghij

165 

166 

167@pytest.mark.parametrize( 1abcdefghij

168 "mode,lines", 

169 [ 

170 pytest.param( 

171 "markdown", 

172 [ 

173 "This header is long", 

174 "", 

175 "Line 1", 

176 "", 

177 "Line 2 continues here", 

178 "", 

179 "Line 3", 

180 "", 

181 ], 

182 ), 

183 pytest.param( 

184 "rich", 

185 [ 

186 "This header is long", 

187 "", 

188 "Line 1", 

189 "", 

190 "Line 2", 

191 "continues here", 

192 "", 

193 "Line 3", 

194 "", 

195 ], 

196 ), 

197 pytest.param( 

198 None, 

199 [ 

200 "This header is long", 

201 "", 

202 "Line 1", 

203 "", 

204 "Line 2 continues here", 

205 "", 

206 "Line 3", 

207 "", 

208 ], 

209 ), 

210 ], 

211) 

212def test_markup_mode_newline_mixed(mode: str, lines: list[str]): 1abcdefghij

213 app = typer.Typer(rich_markup_mode=mode) 1abcdefghij

214 

215 @app.command() 1abcdefghij

216 def main(arg: str): 1abcdefghij

217 """This header 

218 is long 

219 

220 Line 1 

221 

222 Line 2 

223 continues here 

224 

225 Line 3 

226 """ 

227 print(f"Hello {arg}") 1abcdefghij

228 

229 assert app.rich_markup_mode == mode 1abcdefghij

230 

231 result = runner.invoke(app, ["World"]) 1abcdefghij

232 assert "Hello World" in result.stdout 1abcdefghij

233 

234 result = runner.invoke(app, ["--help"]) 1abcdefghij

235 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefghij

236 if mode: 1abcdefghij

237 assert any(c in result.stdout for c in rounded) 1abcdefghij

238 help_start = [i for i, row in enumerate(result_lines) if "This header" in row][0] 1abcdefghij

239 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefghij

240 assert help_start != -1 1abcdefghij

241 assert result_lines[help_start:arg_start] == lines 1abcdefghij

242 

243 

244@pytest.mark.parametrize( 1abcdefghij

245 "mode,lines", 

246 [ 

247 pytest.param( 

248 "markdown", 

249 ["First line", "", "• 1", "• 2", "• 3", ""], 

250 marks=pytest.mark.xfail, 

251 ), 

252 pytest.param("rich", ["First line", "", "- 1", "- 2", "- 3", ""]), 

253 pytest.param(None, ["First line", "", "- 1 - 2 - 3", ""]), 

254 ], 

255) 

256def test_markup_mode_bullets_single_newline(mode: str, lines: list[str]): 1abcdefghij

257 app = typer.Typer(rich_markup_mode=mode) 1abcdefghij

258 

259 @app.command() 1abcdefghij

260 def main(arg: str): 1abcdefghij

261 """First line 

262 

263 - 1 

264 - 2 

265 - 3 

266 """ 

267 print(f"Hello {arg}") 1abcdefghij

268 

269 assert app.rich_markup_mode == mode 1abcdefghij

270 

271 result = runner.invoke(app, ["World"]) 1abcdefghij

272 assert "Hello World" in result.stdout 1abcdefghij

273 

274 result = runner.invoke(app, ["--help"]) 1abcdefghij

275 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefghij

276 if mode: 1abcdefghij

277 assert any(c in result.stdout for c in rounded) 1abcdefghij

278 help_start = result_lines.index("First line") 1abcdefghij

279 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefghij

280 assert help_start != -1 1abcdefghij

281 assert result_lines[help_start:arg_start] == lines 1abcdefghij

282 

283 

284@pytest.mark.parametrize( 1abcdefghij

285 "mode,lines", 

286 [ 

287 pytest.param( 

288 "markdown", 

289 ["First line", "", "• 1", "• 2", "• 3", ""], 

290 marks=pytest.mark.xfail, 

291 ), 

292 ( 

293 "rich", 

294 ["First line", "", "- 1", "", "- 2", "", "- 3", ""], 

295 ), 

296 (None, ["First line", "", "- 1", "", "- 2", "", "- 3", ""]), 

297 ], 

298) 

299def test_markup_mode_bullets_double_newline(mode: str, lines: list[str]): 1abcdefghij

300 app = typer.Typer(rich_markup_mode=mode) 1abcdefghij

301 

302 @app.command() 1abcdefghij

303 def main(arg: str): 1abcdefghij

304 """First line 

305 

306 - 1 

307 

308 - 2 

309 

310 - 3 

311 """ 

312 print(f"Hello {arg}") 1abcdefghij

313 

314 assert app.rich_markup_mode == mode 1abcdefghij

315 

316 result = runner.invoke(app, ["World"]) 1abcdefghij

317 assert "Hello World" in result.stdout 1abcdefghij

318 

319 result = runner.invoke(app, ["--help"]) 1abcdefghij

320 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefghij

321 if mode: 1abcdefghij

322 assert any(c in result.stdout for c in rounded) 1abcdefghij

323 help_start = result_lines.index("First line") 1abcdefghij

324 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] 1abcdefghij

325 assert help_start != -1 1abcdefghij

326 assert result_lines[help_start:arg_start] == lines 1abcdefghij

327 

328 

329def test_markup_mode_default(): 1abcdefghij

330 # We're assuming the test suite is run with rich installed 

331 app = typer.Typer() 1abcdefghij

332 assert app.rich_markup_mode == "rich" 1abcdefghij

333 

334 

335@pytest.mark.parametrize( 1abcdefghij

336 "mode", 

337 ["markdown", "rich", None], 

338) 

339def test_markup_mode_epilog(mode): 1abcdefghij

340 app = typer.Typer(rich_markup_mode=mode) 1abcdefghij

341 

342 help_string = "Here is a help string" 1abcdefghij

343 

344 epilog_string = """ 1abcdefghij

345 Just wrapping up: 

346 

347 This is the first conclusion 

348 Here is conclusion two 

349 

350 That's all folks! 

351 """ 

352 

353 @app.command(help=help_string, epilog=epilog_string) 1abcdefghij

354 def main(): 1abcdefghij

355 print("Hello World") 1abcdefghij

356 

357 assert app.rich_markup_mode == mode 1abcdefghij

358 

359 result = runner.invoke(app) 1abcdefghij

360 assert "Hello World" in result.stdout 1abcdefghij

361 

362 result = runner.invoke(app, ["--help"]) 1abcdefghij

363 result_lines = [line.strip() for line in result.stdout.split("\n")] 1abcdefghij

364 first_index = result_lines.index("Just wrapping up:") 1abcdefghij

365 assert first_index > 0 1abcdefghij

366 assert result_lines[first_index + 1] == "" 1abcdefghij

367 

368 if mode == "rich": 1abcdefghij

369 assert result_lines[first_index + 2] == "This is the first conclusion" 1abcdefghij

370 assert result_lines[first_index + 3] == "Here is conclusion two" 1abcdefghij

371 assert result_lines[first_index + 4] == "" 1abcdefghij

372 assert result_lines[first_index + 5] == "That's all folks!" 1abcdefghij

373 else: 

374 assert ( 1abcdefghij

375 result_lines[first_index + 2] 

376 == "This is the first conclusion Here is conclusion two" 

377 ) 

378 assert result_lines[first_index + 3] == "" 1abcdefghij

379 assert result_lines[first_index + 4] == "That's all folks!" 1abcdefghij