Coverage for tests/test_rich_markup_mode.py: 100%

112 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-14 00:18 +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 marks=pytest.mark.xfail, 

53 ), 

54 pytest.param( 

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

56 ), 

57 pytest.param( 

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

59 ), 

60 ], 

61) 

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

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

64 

65 @app.command() 1abcdefghi

66 def main(arg: str): 1abcdefghi

67 """First line 

68 

69 Line 1 

70 

71 Line 2 

72 

73 Line 3 

74 """ 

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

76 

77 assert app.rich_markup_mode == mode 1abcdefghi

78 

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

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

81 

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

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

84 if mode: 1abcdefghi

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

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

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

88 assert help_start != -1 1abcdefghi

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

90 

91 

92@pytest.mark.parametrize( 1abcdefghi

93 "mode,lines", 

94 [ 

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

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

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

98 ], 

99) 

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

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

102 

103 @app.command() 1abcdefghi

104 def main(arg: str): 1abcdefghi

105 """First line 

106 

107 Line 1 

108 Line 2 

109 Line 3 

110 """ 

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

112 

113 assert app.rich_markup_mode == mode 1abcdefghi

114 

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

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

117 

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

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

120 if mode: 1abcdefghi

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

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

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

124 assert help_start != -1 1abcdefghi

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

126 

127 

128@pytest.mark.parametrize( 1abcdefghi

129 "mode,lines", 

130 [ 

131 pytest.param( 

132 "markdown", 

133 [ 

134 "This header is long", 

135 "", 

136 "Line 1", 

137 "", 

138 "Line 2 continues here", 

139 "", 

140 "Line 3", 

141 "", 

142 ], 

143 marks=pytest.mark.xfail, 

144 ), 

145 pytest.param( 

146 "rich", 

147 [ 

148 "This header is long", 

149 "", 

150 "Line 1", 

151 "", 

152 "Line 2", 

153 "continues here", 

154 "", 

155 "Line 3", 

156 "", 

157 ], 

158 ), 

159 pytest.param( 

160 None, 

161 [ 

162 "This header is long", 

163 "", 

164 "Line 1", 

165 "", 

166 "Line 2 continues here", 

167 "", 

168 "Line 3", 

169 "", 

170 ], 

171 ), 

172 ], 

173) 

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

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

176 

177 @app.command() 1abcdefghi

178 def main(arg: str): 1abcdefghi

179 """This header 

180 is long 

181 

182 Line 1 

183 

184 Line 2 

185 continues here 

186 

187 Line 3 

188 """ 

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

190 

191 assert app.rich_markup_mode == mode 1abcdefghi

192 

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

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

195 

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

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

198 if mode: 1abcdefghi

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

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

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

202 assert help_start != -1 1abcdefghi

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

204 

205 

206@pytest.mark.parametrize( 1abcdefghi

207 "mode,lines", 

208 [ 

209 pytest.param( 

210 "markdown", 

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

212 marks=pytest.mark.xfail, 

213 ), 

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

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

216 ], 

217) 

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

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

220 

221 @app.command() 1abcdefghi

222 def main(arg: str): 1abcdefghi

223 """First line 

224 

225 - 1 

226 - 2 

227 - 3 

228 """ 

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

230 

231 assert app.rich_markup_mode == mode 1abcdefghi

232 

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

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

235 

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

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

238 if mode: 1abcdefghi

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

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

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

242 assert help_start != -1 1abcdefghi

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

244 

245 

246@pytest.mark.parametrize( 1abcdefghi

247 "mode,lines", 

248 [ 

249 pytest.param( 

250 "markdown", 

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

252 marks=pytest.mark.xfail, 

253 ), 

254 ( 

255 "rich", 

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

257 ), 

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

259 ], 

260) 

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

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

263 

264 @app.command() 1abcdefghi

265 def main(arg: str): 1abcdefghi

266 """First line 

267 

268 - 1 

269 

270 - 2 

271 

272 - 3 

273 """ 

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

275 

276 assert app.rich_markup_mode == mode 1abcdefghi

277 

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

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

280 

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

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

283 if mode: 1abcdefghi

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

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

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

287 assert help_start != -1 1abcdefghi

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