Coverage for tests / test_cli / test_doc.py: 100%

49 statements  

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

1import os 1abcdefgh

2import subprocess 1abcdefgh

3import sys 1abcdefgh

4from pathlib import Path 1abcdefgh

5 

6 

7def test_doc(): 1abcdefgh

8 result = subprocess.run( 1abcdefgh

9 [ 

10 sys.executable, 

11 "-m", 

12 "coverage", 

13 "run", 

14 "-m", 

15 "typer", 

16 "tests.assets.cli.multi_app", 

17 "utils", 

18 "docs", 

19 "--name", 

20 "multiapp", 

21 ], 

22 capture_output=True, 

23 encoding="utf-8", 

24 ) 

25 docs_path: Path = Path(__file__).parent.parent / "assets/cli/multiapp-docs.md" 1abcdefgh

26 docs = docs_path.read_text() 1abcdefgh

27 assert docs in result.stdout 1abcdefgh

28 assert "**Arguments**" in result.stdout 1abcdefgh

29 

30 

31def test_doc_output(tmp_path: Path): 1abcdefgh

32 out_file: Path = tmp_path / "out.md" 1abcdefgh

33 result = subprocess.run( 1abcdefgh

34 [ 

35 sys.executable, 

36 "-m", 

37 "coverage", 

38 "run", 

39 "-m", 

40 "typer", 

41 "tests.assets.cli.multi_app", 

42 "utils", 

43 "docs", 

44 "--name", 

45 "multiapp", 

46 "--output", 

47 str(out_file), 

48 ], 

49 capture_output=True, 

50 encoding="utf-8", 

51 ) 

52 docs_path: Path = Path(__file__).parent.parent / "assets/cli/multiapp-docs.md" 1abcdefgh

53 docs = docs_path.read_text() 1abcdefgh

54 written_docs = out_file.read_text() 1abcdefgh

55 assert docs in written_docs 1abcdefgh

56 assert "Docs saved to:" in result.stdout 1abcdefgh

57 

58 

59def test_doc_title_output(tmp_path: Path): 1abcdefgh

60 out_file: Path = tmp_path / "out.md" 1abcdefgh

61 result = subprocess.run( 1abcdefgh

62 [ 

63 sys.executable, 

64 "-m", 

65 "coverage", 

66 "run", 

67 "-m", 

68 "typer", 

69 "tests.assets.cli.multi_app", 

70 "utils", 

71 "docs", 

72 "--name", 

73 "multiapp", 

74 "--title", 

75 "Awesome CLI", 

76 "--output", 

77 str(out_file), 

78 ], 

79 capture_output=True, 

80 encoding="utf-8", 

81 ) 

82 docs_path: Path = Path(__file__).parent.parent / "assets/cli/multiapp-docs-title.md" 1abcdefgh

83 docs = docs_path.read_text() 1abcdefgh

84 written_docs = out_file.read_text() 1abcdefgh

85 assert docs in written_docs 1abcdefgh

86 assert "Docs saved to:" in result.stdout 1abcdefgh

87 

88 

89def test_doc_no_rich(): 1abcdefgh

90 result = subprocess.run( 1abcdefgh

91 [ 

92 sys.executable, 

93 "-m", 

94 "coverage", 

95 "run", 

96 "-m", 

97 "typer", 

98 "tests.assets.cli.multi_app_norich", 

99 "utils", 

100 "docs", 

101 "--name", 

102 "multiapp", 

103 ], 

104 capture_output=True, 

105 encoding="utf-8", 

106 ) 

107 docs_path: Path = Path(__file__).parent.parent / "assets/cli/multiapp-docs.md" 1abcdefgh

108 docs = docs_path.read_text() 1abcdefgh

109 assert docs in result.stdout 1abcdefgh

110 assert "**Arguments**" in result.stdout 1abcdefgh

111 

112 

113def test_doc_not_existing(): 1abcdefgh

114 result = subprocess.run( 1abcdefgh

115 [ 

116 sys.executable, 

117 "-m", 

118 "coverage", 

119 "run", 

120 "-m", 

121 "typer", 

122 "no_typer", 

123 "utils", 

124 "docs", 

125 ], 

126 capture_output=True, 

127 encoding="utf-8", 

128 ) 

129 assert "Could not import as Python module:" in result.stderr 1abcdefgh

130 

131 

132def test_doc_no_typer(): 1abcdefgh

133 result = subprocess.run( 1abcdefgh

134 [ 

135 sys.executable, 

136 "-m", 

137 "coverage", 

138 "run", 

139 "-m", 

140 "typer", 

141 "tests/assets/cli/empty_script.py", 

142 "utils", 

143 "docs", 

144 ], 

145 capture_output=True, 

146 encoding="utf-8", 

147 ) 

148 assert "No Typer app found" in result.stderr 1abcdefgh

149 

150 

151def test_doc_file_not_existing(): 1abcdefgh

152 result = subprocess.run( 1abcdefgh

153 [ 

154 sys.executable, 

155 "-m", 

156 "coverage", 

157 "run", 

158 "-m", 

159 "typer", 

160 "assets/cli/not_existing.py", 

161 "utils", 

162 "docs", 

163 ], 

164 capture_output=True, 

165 encoding="utf-8", 

166 ) 

167 assert "Not a valid file or Python module:" in result.stderr 1abcdefgh

168 

169 

170def test_doc_html_output(tmp_path: Path): 1abcdefgh

171 out_file: Path = tmp_path / "out.md" 1abcdefgh

172 result = subprocess.run( 1abcdefgh

173 [ 

174 sys.executable, 

175 "-m", 

176 "coverage", 

177 "run", 

178 "-m", 

179 "typer", 

180 "tests.assets.cli.rich_formatted_app", 

181 "utils", 

182 "docs", 

183 "--title", 

184 "Awesome CLI", 

185 "--output", 

186 str(out_file), 

187 ], 

188 capture_output=True, 

189 encoding="utf-8", 

190 env={**os.environ, "PYTHONIOENCODING": "utf-8"}, 

191 ) 

192 docs_path: Path = ( 1abcdefgh

193 Path(__file__).parent.parent / "assets" / "cli" / "richformattedapp-docs.md" 

194 ) 

195 docs = docs_path.read_text(encoding="utf-8") 1abcdefgh

196 written_docs = out_file.read_text(encoding="utf-8") 1abcdefgh

197 assert docs in written_docs 1abcdefgh

198 assert "Docs saved to:" in result.stdout 1abcdefgh