1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
"""Tests for [the `cli` module][pytkdocs.cli]."""
from __future__ import annotations
import io
import json
import pytest
from pytkdocs import cli, debug
def test_show_help(capsys: pytest.CaptureFixture) -> None:
"""Show help.
Parameters:
capsys: Pytest fixture to capture output.
"""
with pytest.raises(SystemExit):
cli.main(["-h"])
captured = capsys.readouterr()
assert "pytkdocs" in captured.out
def test_read_whole_stdin(monkeypatch: pytest.MonkeyPatch) -> None:
"""Read whole standard input."""
monkeypatch.setattr(
"sys.stdin",
io.StringIO(
"""
{
"objects": [
{
"path": "pytkdocs.cli.main"
},
{
"path": "pytkdocs.cli.get_parser"
}
]
}
""",
),
)
cli.main([])
def test_read_stdin_line_by_line(monkeypatch: pytest.MonkeyPatch) -> None:
"""Read standard input line by line."""
monkeypatch.setattr(
"sys.stdin",
io.StringIO(
'{"objects": [{"path": "pytkdocs.cli.main"}]}\n{"objects": [{"path": "pytkdocs.cli.get_parser"}]}\n',
),
)
cli.main(["--line-by-line"])
def test_load_complete_tree(monkeypatch: pytest.MonkeyPatch) -> None:
"""Load `pytkdocs` own documentation."""
monkeypatch.setattr("sys.stdin", io.StringIO('{"objects": [{"path": "pytkdocs"}]}'))
cli.main(["--line-by-line"])
def test_discard_stdout(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture) -> None:
"""Discard standard output at import time."""
monkeypatch.setattr("sys.stdin", io.StringIO('{"objects": [{"path": "tests.fixtures.corrupt_output"}]}'))
cli.main(["--line-by-line"])
captured = capsys.readouterr()
assert not captured.out.startswith("*corruption intensifies*")
# assert no JSON parsing error
json.loads(captured.out)
def test_exception_raised_while_discard_stdout(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture) -> None:
"""Check that an error is still printed when an exception is raised and stdout is discarded."""
monkeypatch.setattr("sys.stdin", io.StringIO('{"objects": [{"path": "pytkdocs.cli"}]}'))
# raise an exception during the process
monkeypatch.setattr("pytkdocs.cli.process_json", lambda _: 1 / 0)
# assert no exception
cli.main(["--line-by-line"])
# assert json error was written to stdout
captured = capsys.readouterr()
assert captured.out
# assert no JSON parsing error
json.loads(captured.out)
def test_load_complete_tests_tree(monkeypatch: pytest.MonkeyPatch) -> None:
"""Load `pytkdocs` own tests' documentation."""
monkeypatch.setattr("sys.stdin", io.StringIO('{"objects": [{"path": "tests"}]}'))
cli.main(["--line-by-line"])
def test_show_version(capsys: pytest.CaptureFixture) -> None:
"""Show version.
Parameters:
capsys: Pytest fixture to capture output.
"""
with pytest.raises(SystemExit):
cli.main(["-V"])
captured = capsys.readouterr()
assert debug.get_version() in captured.out
def test_show_debug_info(capsys: pytest.CaptureFixture) -> None:
"""Show debug information.
Parameters:
capsys: Pytest fixture to capture output.
"""
with pytest.raises(SystemExit):
cli.main(["--debug-info"])
captured = capsys.readouterr().out.lower()
assert "python" in captured
assert "system" in captured
assert "environment" in captured
assert "packages" in captured
|