|
| 1 | +import io |
1 | 2 | import re |
2 | 3 | import subprocess |
| 4 | +import tarfile |
3 | 5 | from pathlib import Path |
4 | 6 | from typing import Dict, Union, cast |
5 | 7 |
|
6 | 8 | import pytest |
7 | 9 |
|
8 | | -from ..diagnostics import CannotOpenFile |
| 10 | +from ..diagnostics import CannotOpenFile, UnsupportedFormat |
9 | 11 | from ..types import FileId |
10 | 12 | from ..util_test import make_test |
11 | 13 |
|
12 | | - |
13 | | -def normalize(text: str) -> str: |
14 | | - """Remove any non-word characters to make groff output comparable |
15 | | - across platforms.""" |
16 | | - # Strip the strange escape characters that Groff inserts for TTYs |
17 | | - text = re.sub(r".\x08", "", text) |
18 | | - |
19 | | - # Strip the header: this varies platform to platform. |
20 | | - text = text.split("\n", 1)[1] |
21 | | - |
22 | | - # Remove non-word characters |
23 | | - return re.sub(r"[^\w]+", "", text) |
24 | | - |
25 | | - |
26 | | -def test_manpage() -> None: |
27 | | - with make_test( |
28 | | - { |
29 | | - Path( |
30 | | - "snooty.toml" |
31 | | - ): """ |
32 | | -name = "test_manpage" |
33 | | -
|
34 | | -[manpages.mongo] |
35 | | -file = "index.txt" |
36 | | -section = 1 |
37 | | -title = "The MongoDB Shell" |
38 | | -
|
39 | | -[manpages.missing] |
40 | | -file = "missing.txt" |
41 | | -section = 1 |
42 | | -title = "This Manpage Doesn't Exist" |
43 | | -""", |
44 | | - Path( |
45 | | - "source/index.txt" |
46 | | - ): """ |
| 14 | +PAGE_TEXT = """ |
47 | 15 | .. _mongo: |
48 | 16 |
|
49 | 17 | ========= |
@@ -147,35 +115,9 @@ def test_manpage() -> None: |
147 | 115 | } |
148 | 116 |
|
149 | 117 | Trailing paragraph. |
150 | | -""", |
151 | | - } |
152 | | - ) as result: |
153 | | - # Ensure that we have an error about the missing manpage |
154 | | - assert [type(diag) for diag in result.diagnostics[FileId("snooty.toml")]] == [ |
155 | | - CannotOpenFile |
156 | | - ] |
157 | | - |
158 | | - static_files = cast( |
159 | | - Dict[str, Union[str, bytes]], result.metadata["static_files"] |
160 | | - ) |
161 | | - |
162 | | - troff = static_files["mongo.1"] |
163 | | - |
164 | | - assert isinstance(troff, str) |
165 | | - |
166 | | - # Empty lines are discouraged in troff source |
167 | | - assert "\n\n" not in troff |
| 118 | +""" |
168 | 119 |
|
169 | | - try: |
170 | | - # Use GNU Roff to turn our generated troff source into text we can compare. |
171 | | - text = subprocess.check_output( |
172 | | - ["groff", "-T", "utf8", "-t", "-man"], encoding="utf-8", input=troff, |
173 | | - ) |
174 | | - except FileNotFoundError: |
175 | | - pytest.xfail("groff is not installed") |
176 | | - |
177 | | - assert normalize(text).strip() == normalize( |
178 | | - """mongo(1) General Commands Manual mongo(1) |
| 120 | +MANPAGE_TEXT = """mongo(1) General Commands Manual mongo(1) |
179 | 121 |
|
180 | 122 |
|
181 | 123 |
|
@@ -246,4 +188,98 @@ def test_manpage() -> None: |
246 | 188 |
|
247 | 189 |
|
248 | 190 | mongo(1)""" |
| 191 | + |
| 192 | + |
| 193 | +def normalize(text: str) -> str: |
| 194 | + """Remove any non-word characters to make groff output comparable |
| 195 | + across platforms.""" |
| 196 | + # Strip the strange escape characters that Groff inserts for TTYs |
| 197 | + text = re.sub(r".\x08", "", text) |
| 198 | + |
| 199 | + # Strip the header: this varies platform to platform. |
| 200 | + text = text.split("\n", 1)[1] |
| 201 | + |
| 202 | + # Remove non-word characters |
| 203 | + return re.sub(r"[^\w]+", "", text) |
| 204 | + |
| 205 | + |
| 206 | +def test_manpage() -> None: |
| 207 | + with make_test( |
| 208 | + { |
| 209 | + Path( |
| 210 | + "snooty.toml" |
| 211 | + ): """ |
| 212 | +name = "test_manpage" |
| 213 | +
|
| 214 | +[bundle] |
| 215 | +manpages = "manpages.tar.gz" |
| 216 | +
|
| 217 | +[manpages.mongo] |
| 218 | +file = "index.txt" |
| 219 | +section = 1 |
| 220 | +title = "The MongoDB Shell" |
| 221 | +
|
| 222 | +[manpages.missing] |
| 223 | +file = "missing.txt" |
| 224 | +section = 1 |
| 225 | +title = "This Manpage Doesn't Exist" |
| 226 | +""", |
| 227 | + Path("source/index.txt"): PAGE_TEXT, |
| 228 | + } |
| 229 | + ) as result: |
| 230 | + # Ensure that we have an error about the missing manpage |
| 231 | + assert [type(diag) for diag in result.diagnostics[FileId("snooty.toml")]] == [ |
| 232 | + CannotOpenFile |
| 233 | + ] |
| 234 | + |
| 235 | + static_files = cast( |
| 236 | + Dict[str, Union[str, bytes]], result.metadata["static_files"] |
249 | 237 | ) |
| 238 | + |
| 239 | + troff = static_files["mongo.1"] |
| 240 | + |
| 241 | + assert isinstance(troff, str) |
| 242 | + |
| 243 | + # Empty lines are discouraged in troff source |
| 244 | + assert "\n\n" not in troff |
| 245 | + |
| 246 | + try: |
| 247 | + # Use GNU Roff to turn our generated troff source into text we can compare. |
| 248 | + text = subprocess.check_output( |
| 249 | + ["groff", "-T", "utf8", "-t", "-man"], encoding="utf-8", input=troff, |
| 250 | + ) |
| 251 | + except FileNotFoundError: |
| 252 | + pytest.xfail("groff is not installed") |
| 253 | + |
| 254 | + assert normalize(text).strip() == normalize(MANPAGE_TEXT) |
| 255 | + |
| 256 | + tarball_data = static_files["manpages.tar.gz"] |
| 257 | + assert isinstance(tarball_data, bytes) |
| 258 | + tarball = io.BytesIO(tarball_data) |
| 259 | + with tarfile.open(None, "r:*", tarball) as tf: |
| 260 | + names = tf.getnames() |
| 261 | + assert sorted(names) == sorted(["mongo.1"]) |
| 262 | + assert tf.getmember("mongo.1").size == len(troff) |
| 263 | + |
| 264 | + |
| 265 | +def test_bundling_error() -> None: |
| 266 | + with make_test( |
| 267 | + { |
| 268 | + Path( |
| 269 | + "snooty.toml" |
| 270 | + ): """ |
| 271 | +name = "test_manpage" |
| 272 | +
|
| 273 | +[bundle] |
| 274 | +manpages = "manpages.goofy" |
| 275 | +
|
| 276 | +[manpages.mongo] |
| 277 | +file = "index.txt" |
| 278 | +section = 1 |
| 279 | +title = "The MongoDB Shell" |
| 280 | +""", |
| 281 | + Path("source/index.txt"): PAGE_TEXT, |
| 282 | + } |
| 283 | + ) as result: |
| 284 | + diagnostics = result.diagnostics[FileId("snooty.toml")] |
| 285 | + assert UnsupportedFormat in [type(diag) for diag in diagnostics] |
0 commit comments