Coverage for tests/test_rich_markup_mode.py: 100%

115 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-19 20:29 +0000

1from typing import List 1abcdefghi

2 

3import pytest 1abcdefghi

4import typer 1abcdefghi

5import typer.completion 1abcdefghi

6from typer.testing import CliRunner 1abcdefghi

7 

8runner = CliRunner() 1abcdefghi

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

10 

11 

12def test_rich_markup_mode_none(): 1abcdefghi

13 app = typer.Typer(rich_markup_mode=None) 1abcdefghi

14 

15 @app.command() 1abcdefghi

16 def main(arg: str): 1abcdefghi

17 """Main function""" 

18 print(f"Hello {arg}") 1abcdefghi

19 

20 assert app.rich_markup_mode is None 1abcdefghi

21 

22 result = runner.invoke(app, ["World"]) 1abcdefghi

23 assert "Hello World" in result.stdout 1abcdefghi

24 

25 result = runner.invoke(app, ["--help"]) 1abcdefghi

26 assert all(c not in result.stdout for c in rounded) 1abcdefghi

27 

28 

29def test_rich_markup_mode_rich(): 1abcdefghi

30 app = typer.Typer(rich_markup_mode="rich") 1abcdefghi

31 

32 @app.command() 1abcdefghi

33 def main(arg: str): 1abcdefghi

34 """Main function""" 

35 print(f"Hello {arg}") 1abcdefghi

36 

37 assert app.rich_markup_mode == "rich" 1abcdefghi

38 

39 result = runner.invoke(app, ["World"]) 1abcdefghi

40 assert "Hello World" in result.stdout 1abcdefghi

41 

42 result = runner.invoke(app, ["--help"]) 1abcdefghi

43 assert any(c in result.stdout for c in rounded) 1abcdefghi

44 

45 

46@pytest.mark.parametrize( 1abcdefghi

47 "mode,lines", 

48 [ 

49 pytest.param( 

50 "markdown", 

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

52 ), 

53 pytest.param( 

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

55 ), 

56 pytest.param( 

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

58 ), 

59 ], 

60) 

61def test_markup_mode_newline_pr815(mode: str, lines: List[str]): 1abcdefghi

62 app = typer.Typer(rich_markup_mode=mode) 1abcdefghi

63 

64 @app.command() 1abcdefghi

65 def main(arg: str): 1abcdefghi

66 """First line 

67 

68 Line 1 

69 

70 Line 2 

71 

72 Line 3 

73 """ 

74 print(f"Hello {arg}") 1abcdefghi

75 

76 assert app.rich_markup_mode == mode 1abcdefghi

77 

78 result = runner.invoke(app, ["World"]) 1abcdefghi

79 assert "Hello World" in result.stdout 1abcdefghi

80 

81 result = runner.invoke(app, ["--help"]) 1abcdefghi

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

83 if mode: 1abcdefghi

84 assert any(c in result.stdout for c in rounded) 1abcdefghi

85 help_start = result_lines.index("First line") 1abcdefghi

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

87 assert help_start != -1 1abcdefghi

88 assert result_lines[help_start:arg_start] == lines 1abcdefghi

89 

90 

91@pytest.mark.parametrize( 1abcdefghi

92 "mode,lines", 

93 [ 

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

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

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

97 ], 

98) 

99def test_markup_mode_newline_issue447(mode: str, lines: List[str]): 1abcdefghi

100 app = typer.Typer(rich_markup_mode=mode) 1abcdefghi

101 

102 @app.command() 1abcdefghi

103 def main(arg: str): 1abcdefghi

104 """First line 

105 

106 Line 1 

107 Line 2 

108 Line 3 

109 """ 

110 print(f"Hello {arg}") 1abcdefghi

111 

112 assert app.rich_markup_mode == mode 1abcdefghi

113 

114 result = runner.invoke(app, ["World"]) 1abcdefghi

115 assert "Hello World" in result.stdout 1abcdefghi

116 

117 result = runner.invoke(app, ["--help"]) 1abcdefghi

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

119 if mode: 1abcdefghi

120 assert any(c in result.stdout for c in rounded) 1abcdefghi

121 help_start = result_lines.index("First line") 1abcdefghi

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

123 assert help_start != -1 1abcdefghi

124 assert result_lines[help_start:arg_start] == lines 1abcdefghi

125 

126 

127@pytest.mark.parametrize( 1abcdefghi

128 "mode,lines", 

129 [ 

130 pytest.param( 

131 "markdown", 

132 [ 

133 "This header is long", 

134 "", 

135 "Line 1", 

136 "", 

137 "Line 2 continues here", 

138 "", 

139 "Line 3", 

140 "", 

141 ], 

142 ), 

143 pytest.param( 

144 "rich", 

145 [ 

146 "This header is long", 

147 "", 

148 "Line 1", 

149 "", 

150 "Line 2", 

151 "continues here", 

152 "", 

153 "Line 3", 

154 "", 

155 ], 

156 ), 

157 pytest.param( 

158 None, 

159 [ 

160 "This header is long", 

161 "", 

162 "Line 1", 

163 "", 

164 "Line 2 continues here", 

165 "", 

166 "Line 3", 

167 "", 

168 ], 

169 ), 

170 ], 

171) 

172def test_markup_mode_newline_mixed(mode: str, lines: List[str]): 1abcdefghi

173 app = typer.Typer(rich_markup_mode=mode) 1abcdefghi

174 

175 @app.command() 1abcdefghi

176 def main(arg: str): 1abcdefghi

177 """This header 

178 is long 

179 

180 Line 1 

181 

182 Line 2 

183 continues here 

184 

185 Line 3 

186 """ 

187 print(f"Hello {arg}") 1abcdefghi

188 

189 assert app.rich_markup_mode == mode 1abcdefghi

190 

191 result = runner.invoke(app, ["World"]) 1abcdefghi

192 assert "Hello World" in result.stdout 1abcdefghi

193 

194 result = runner.invoke(app, ["--help"]) 1abcdefghi

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

196 if mode: 1abcdefghi

197 assert any(c in result.stdout for c in rounded) 1abcdefghi

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

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

200 assert help_start != -1 1abcdefghi

201 assert result_lines[help_start:arg_start] == lines 1abcdefghi

202 

203 

204@pytest.mark.parametrize( 1abcdefghi

205 "mode,lines", 

206 [ 

207 pytest.param( 

208 "markdown", 

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

210 marks=pytest.mark.xfail, 

211 ), 

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

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

214 ], 

215) 

216def test_markup_mode_bullets_single_newline(mode: str, lines: List[str]): 1abcdefghi

217 app = typer.Typer(rich_markup_mode=mode) 1abcdefghi

218 

219 @app.command() 1abcdefghi

220 def main(arg: str): 1abcdefghi

221 """First line 

222 

223 - 1 

224 - 2 

225 - 3 

226 """ 

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

228 

229 assert app.rich_markup_mode == mode 1abcdefghi

230 

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

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

233 

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

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

236 if mode: 1abcdefghi

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

238 help_start = result_lines.index("First line") 1abcdefghi

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

240 assert help_start != -1 1abcdefghi

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

242 

243 

244@pytest.mark.parametrize( 1abcdefghi

245 "mode,lines", 

246 [ 

247 pytest.param( 

248 "markdown", 

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

250 marks=pytest.mark.xfail, 

251 ), 

252 ( 

253 "rich", 

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

255 ), 

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

257 ], 

258) 

259def test_markup_mode_bullets_double_newline(mode: str, lines: List[str]): 1abcdefghi

260 app = typer.Typer(rich_markup_mode=mode) 1abcdefghi

261 

262 @app.command() 1abcdefghi

263 def main(arg: str): 1abcdefghi

264 """First line 

265 

266 - 1 

267 

268 - 2 

269 

270 - 3 

271 """ 

272 print(f"Hello {arg}") 1abcdefghi

273 

274 assert app.rich_markup_mode == mode 1abcdefghi

275 

276 result = runner.invoke(app, ["World"]) 1abcdefghi

277 assert "Hello World" in result.stdout 1abcdefghi

278 

279 result = runner.invoke(app, ["--help"]) 1abcdefghi

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

281 if mode: 1abcdefghi

282 assert any(c in result.stdout for c in rounded) 1abcdefghi

283 help_start = result_lines.index("First line") 1abcdefghi

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

285 assert help_start != -1 1abcdefghi

286 assert result_lines[help_start:arg_start] == lines 1abcdefghi

287 

288 

289def test_markup_mode_default(): 1abcdefghi

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

291 app = typer.Typer() 1abcdefghi

292 assert app.rich_markup_mode == "rich" 1abcdefghi