Coverage for tests / test_rich_markup_mode.py: 100%

115 statements  

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

1import pytest 1abcdefgh

2import typer 1abcdefgh

3import typer.completion 1abcdefgh

4from typer.testing import CliRunner 1abcdefgh

5 

6runner = CliRunner() 1abcdefgh

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

8 

9 

10def test_rich_markup_mode_none(): 1abcdefgh

11 app = typer.Typer(rich_markup_mode=None) 1abcdefgh

12 

13 @app.command() 1abcdefgh

14 def main(arg: str): 1abcdefgh

15 """Main function""" 

16 print(f"Hello {arg}") 1abcdefgh

17 

18 assert app.rich_markup_mode is None 1abcdefgh

19 

20 result = runner.invoke(app, ["World"]) 1abcdefgh

21 assert "Hello World" in result.stdout 1abcdefgh

22 

23 result = runner.invoke(app, ["--help"]) 1abcdefgh

24 assert "ARG [required]" in result.stdout 1abcdefgh

25 assert all(c not in result.stdout for c in rounded) 1abcdefgh

26 

27 

28def test_rich_markup_mode_rich(): 1abcdefgh

29 app = typer.Typer(rich_markup_mode="rich") 1abcdefgh

30 

31 @app.command() 1abcdefgh

32 def main(arg: str): 1abcdefgh

33 """Main function""" 

34 print(f"Hello {arg}") 1abcdefgh

35 

36 assert app.rich_markup_mode == "rich" 1abcdefgh

37 

38 result = runner.invoke(app, ["World"]) 1abcdefgh

39 assert "Hello World" in result.stdout 1abcdefgh

40 

41 result = runner.invoke(app, ["--help"]) 1abcdefgh

42 assert any(c in result.stdout for c in rounded) 1abcdefgh

43 

44 

45@pytest.mark.parametrize( 1abcdefgh

46 "mode,lines", 

47 [ 

48 pytest.param( 

49 "markdown", 

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

51 ), 

52 pytest.param( 

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

54 ), 

55 pytest.param( 

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

57 ), 

58 ], 

59) 

60def test_markup_mode_newline_pr815(mode: str, lines: list[str]): 1abcdefgh

61 app = typer.Typer(rich_markup_mode=mode) 1abcdefgh

62 

63 @app.command() 1abcdefgh

64 def main(arg: str): 1abcdefgh

65 """First line 

66 

67 Line 1 

68 

69 Line 2 

70 

71 Line 3 

72 """ 

73 print(f"Hello {arg}") 1abcdefgh

74 

75 assert app.rich_markup_mode == mode 1abcdefgh

76 

77 result = runner.invoke(app, ["World"]) 1abcdefgh

78 assert "Hello World" in result.stdout 1abcdefgh

79 

80 result = runner.invoke(app, ["--help"]) 1abcdefgh

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

82 if mode: 1abcdefgh

83 assert any(c in result.stdout for c in rounded) 1abcdefgh

84 help_start = result_lines.index("First line") 1abcdefgh

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

86 assert help_start != -1 1abcdefgh

87 assert result_lines[help_start:arg_start] == lines 1abcdefgh

88 

89 

90@pytest.mark.parametrize( 1abcdefgh

91 "mode,lines", 

92 [ 

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

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

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

96 ], 

97) 

98def test_markup_mode_newline_issue447(mode: str, lines: list[str]): 1abcdefgh

99 app = typer.Typer(rich_markup_mode=mode) 1abcdefgh

100 

101 @app.command() 1abcdefgh

102 def main(arg: str): 1abcdefgh

103 """First line 

104 

105 Line 1 

106 Line 2 

107 Line 3 

108 """ 

109 print(f"Hello {arg}") 1abcdefgh

110 

111 assert app.rich_markup_mode == mode 1abcdefgh

112 

113 result = runner.invoke(app, ["World"]) 1abcdefgh

114 assert "Hello World" in result.stdout 1abcdefgh

115 

116 result = runner.invoke(app, ["--help"]) 1abcdefgh

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

118 if mode: 1abcdefgh

119 assert any(c in result.stdout for c in rounded) 1abcdefgh

120 help_start = result_lines.index("First line") 1abcdefgh

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

122 assert help_start != -1 1abcdefgh

123 assert result_lines[help_start:arg_start] == lines 1abcdefgh

124 

125 

126@pytest.mark.parametrize( 1abcdefgh

127 "mode,lines", 

128 [ 

129 pytest.param( 

130 "markdown", 

131 [ 

132 "This header is long", 

133 "", 

134 "Line 1", 

135 "", 

136 "Line 2 continues here", 

137 "", 

138 "Line 3", 

139 "", 

140 ], 

141 ), 

142 pytest.param( 

143 "rich", 

144 [ 

145 "This header is long", 

146 "", 

147 "Line 1", 

148 "", 

149 "Line 2", 

150 "continues here", 

151 "", 

152 "Line 3", 

153 "", 

154 ], 

155 ), 

156 pytest.param( 

157 None, 

158 [ 

159 "This header is long", 

160 "", 

161 "Line 1", 

162 "", 

163 "Line 2 continues here", 

164 "", 

165 "Line 3", 

166 "", 

167 ], 

168 ), 

169 ], 

170) 

171def test_markup_mode_newline_mixed(mode: str, lines: list[str]): 1abcdefgh

172 app = typer.Typer(rich_markup_mode=mode) 1abcdefgh

173 

174 @app.command() 1abcdefgh

175 def main(arg: str): 1abcdefgh

176 """This header 

177 is long 

178 

179 Line 1 

180 

181 Line 2 

182 continues here 

183 

184 Line 3 

185 """ 

186 print(f"Hello {arg}") 1abcdefgh

187 

188 assert app.rich_markup_mode == mode 1abcdefgh

189 

190 result = runner.invoke(app, ["World"]) 1abcdefgh

191 assert "Hello World" in result.stdout 1abcdefgh

192 

193 result = runner.invoke(app, ["--help"]) 1abcdefgh

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

195 if mode: 1abcdefgh

196 assert any(c in result.stdout for c in rounded) 1abcdefgh

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

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

199 assert help_start != -1 1abcdefgh

200 assert result_lines[help_start:arg_start] == lines 1abcdefgh

201 

202 

203@pytest.mark.parametrize( 1abcdefgh

204 "mode,lines", 

205 [ 

206 pytest.param( 

207 "markdown", 

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

209 marks=pytest.mark.xfail, 

210 ), 

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

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

213 ], 

214) 

215def test_markup_mode_bullets_single_newline(mode: str, lines: list[str]): 1abcdefgh

216 app = typer.Typer(rich_markup_mode=mode) 1abcdefgh

217 

218 @app.command() 1abcdefgh

219 def main(arg: str): 1abcdefgh

220 """First line 

221 

222 - 1 

223 - 2 

224 - 3 

225 """ 

226 print(f"Hello {arg}") 1abcdefgh

227 

228 assert app.rich_markup_mode == mode 1abcdefgh

229 

230 result = runner.invoke(app, ["World"]) 1abcdefgh

231 assert "Hello World" in result.stdout 1abcdefgh

232 

233 result = runner.invoke(app, ["--help"]) 1abcdefgh

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

235 if mode: 1abcdefgh

236 assert any(c in result.stdout for c in rounded) 1abcdefgh

237 help_start = result_lines.index("First line") 1abcdefgh

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

239 assert help_start != -1 1abcdefgh

240 assert result_lines[help_start:arg_start] == lines 1abcdefgh

241 

242 

243@pytest.mark.parametrize( 1abcdefgh

244 "mode,lines", 

245 [ 

246 pytest.param( 

247 "markdown", 

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

249 marks=pytest.mark.xfail, 

250 ), 

251 ( 

252 "rich", 

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

254 ), 

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

256 ], 

257) 

258def test_markup_mode_bullets_double_newline(mode: str, lines: list[str]): 1abcdefgh

259 app = typer.Typer(rich_markup_mode=mode) 1abcdefgh

260 

261 @app.command() 1abcdefgh

262 def main(arg: str): 1abcdefgh

263 """First line 

264 

265 - 1 

266 

267 - 2 

268 

269 - 3 

270 """ 

271 print(f"Hello {arg}") 1abcdefgh

272 

273 assert app.rich_markup_mode == mode 1abcdefgh

274 

275 result = runner.invoke(app, ["World"]) 1abcdefgh

276 assert "Hello World" in result.stdout 1abcdefgh

277 

278 result = runner.invoke(app, ["--help"]) 1abcdefgh

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

280 if mode: 1abcdefgh

281 assert any(c in result.stdout for c in rounded) 1abcdefgh

282 help_start = result_lines.index("First line") 1abcdefgh

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

284 assert help_start != -1 1abcdefgh

285 assert result_lines[help_start:arg_start] == lines 1abcdefgh

286 

287 

288def test_markup_mode_default(): 1abcdefgh

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

290 app = typer.Typer() 1abcdefgh

291 assert app.rich_markup_mode == "rich" 1abcdefgh