|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +if sys.platform == "win32": |
| 11 | + pytest.skip("man command not available on Windows", allow_module_level=True) |
| 12 | + |
| 13 | +ROOT = Path(__file__).parents[2] |
| 14 | +RST_PATH = ROOT / "docs" / "man" / "tox.1.rst" |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def manpage_troff() -> bytes: |
| 19 | + from docutils.core import publish_string # noqa: PLC0415 |
| 20 | + |
| 21 | + content = "\n".join( |
| 22 | + line for line in RST_PATH.read_text(encoding="utf-8").splitlines() if line.strip() != ":orphan:" |
| 23 | + ) |
| 24 | + return publish_string(content, writer="manpage", settings_overrides={"report_level": 5}) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def manpage_rendered(manpage_troff: bytes, tmp_path: Path) -> str: |
| 29 | + man_file = tmp_path / "tox.1" |
| 30 | + man_file.write_bytes(manpage_troff) |
| 31 | + result = subprocess.run( |
| 32 | + ["man", str(man_file)], # noqa: S607 |
| 33 | + capture_output=True, |
| 34 | + text=True, |
| 35 | + env={"COLUMNS": "200", "LANG": "en_US.UTF-8", "PATH": "/usr/bin:/bin", "MANPAGER": "cat", "PAGER": "cat"}, |
| 36 | + check=False, |
| 37 | + ) |
| 38 | + return re.sub(r".\x08", "", result.stdout) |
| 39 | + |
| 40 | + |
| 41 | +def test_manpage_has_title_header(manpage_troff: bytes) -> None: |
| 42 | + output = manpage_troff.decode() |
| 43 | + match = re.search(r'^\.TH "([^"]*)" "([^"]*)" "([^"]*)" "([^"]*)" "([^"]*)"', output, re.MULTILINE) |
| 44 | + assert match is not None, f".TH header not found in:\n{output[:500]}" |
| 45 | + assert match.group(1) == "tox" |
| 46 | + assert match.group(2) == "1" |
| 47 | + assert match.group(5) == "User Commands" |
| 48 | + |
| 49 | + |
| 50 | +def test_manpage_has_name_section(manpage_troff: bytes) -> None: |
| 51 | + output = manpage_troff.decode() |
| 52 | + match = re.search(r"\.SH Name\n(.+)", output) |
| 53 | + assert match is not None, "Name section not found" |
| 54 | + assert "tox" in match.group(1) |
| 55 | + assert "virtualenv-based automation of test activities" in match.group(1) |
| 56 | + |
| 57 | + |
| 58 | +def test_manpage_has_all_sections(manpage_troff: bytes) -> None: |
| 59 | + output = manpage_troff.decode() |
| 60 | + sections = re.findall(r"^\.SH (.+)$", output, re.MULTILINE) |
| 61 | + expected = [ |
| 62 | + "Name", |
| 63 | + "SYNOPSIS", |
| 64 | + "DESCRIPTION", |
| 65 | + "COMMANDS", |
| 66 | + "OPTIONS", |
| 67 | + "FILES", |
| 68 | + "ENVIRONMENT VARIABLES", |
| 69 | + "SEE ALSO", |
| 70 | + "AUTHOR", |
| 71 | + ] |
| 72 | + assert sections == expected |
| 73 | + |
| 74 | + |
| 75 | +def test_manpage_renders_sections(manpage_rendered: str) -> None: |
| 76 | + assert "tox" in manpage_rendered |
| 77 | + for section in ("SYNOPSIS", "DESCRIPTION", "COMMANDS", "OPTIONS", "FILES", "SEE ALSO", "AUTHOR"): |
| 78 | + assert section in manpage_rendered, f"section {section!r} missing from rendered man output" |
| 79 | + |
| 80 | + |
| 81 | +def test_manpage_name_not_empty(manpage_rendered: str) -> None: |
| 82 | + lines = manpage_rendered.splitlines() |
| 83 | + name_idx = next((i for i, line in enumerate(lines) if "Name" in line or "NAME" in line), None) |
| 84 | + assert name_idx is not None, "NAME section not found in rendered output" |
| 85 | + name_line = lines[name_idx + 1].strip() |
| 86 | + assert "tox" in name_line |
| 87 | + assert "virtualenv-based automation of test activities" in name_line |
| 88 | + |
| 89 | + |
| 90 | +def test_manpage_header_shows_tox(manpage_rendered: str) -> None: |
| 91 | + first_line = manpage_rendered.splitlines()[0] |
| 92 | + assert "tox" in first_line.lower() |
| 93 | + |
| 94 | + |
| 95 | +def test_manpage_documents_all_commands() -> None: |
| 96 | + from argparse import _SubParsersAction # noqa: PLC0415, PLC2701 |
| 97 | + |
| 98 | + from tox.config.cli.parse import _get_parser_doc # noqa: PLC0415, PLC2701 |
| 99 | + |
| 100 | + parser = _get_parser_doc() |
| 101 | + rst = RST_PATH.read_text(encoding="utf-8") |
| 102 | + assert parser._subparsers is not None # noqa: SLF001 |
| 103 | + for action in parser._subparsers._actions: # noqa: SLF001 |
| 104 | + if isinstance(action, _SubParsersAction): |
| 105 | + for choice_action in action._choices_actions: # noqa: SLF001 |
| 106 | + assert choice_action.dest in rst, ( |
| 107 | + f"command {choice_action.dest!r} missing from manpage, regenerate with: " |
| 108 | + f"python tools/generate_manpage.py" |
| 109 | + ) |
| 110 | + |
| 111 | + |
| 112 | +def test_manpage_documents_all_options() -> None: |
| 113 | + from argparse import SUPPRESS, _SubParsersAction # noqa: PLC0415, PLC2701 |
| 114 | + |
| 115 | + from tox.config.cli.parse import _get_parser_doc # noqa: PLC0415, PLC2701 |
| 116 | + |
| 117 | + parser = _get_parser_doc() |
| 118 | + rst = RST_PATH.read_text(encoding="utf-8") |
| 119 | + seen: set[int] = set() |
| 120 | + for action in parser._actions: # noqa: SLF001 |
| 121 | + if id(action) in seen or action.help == SUPPRESS or isinstance(action, _SubParsersAction): |
| 122 | + continue |
| 123 | + seen.add(id(action)) |
| 124 | + if not action.option_strings: |
| 125 | + continue |
| 126 | + long_opt = next((o for o in action.option_strings if o.startswith("--")), action.option_strings[0]) |
| 127 | + assert long_opt in rst, ( |
| 128 | + f"option {long_opt!r} missing from manpage, regenerate with: python tools/generate_manpage.py" |
| 129 | + ) |
0 commit comments