Coverage for tests/test_cli.py: 100.00%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Tests for [the `cli` module][pytkdocs.cli]."""
3import io
4import json
6import pytest
8from pytkdocs import cli
11def test_show_help(capsys):
12 """
13 Show help.
15 Arguments:
16 capsys: Pytest fixture to capture output.
17 """
18 with pytest.raises(SystemExit):
19 cli.main(["-h"])
20 captured = capsys.readouterr()
21 assert "pytkdocs" in captured.out
24def test_read_whole_stdin(monkeypatch):
25 """Read whole standard input."""
26 monkeypatch.setattr(
27 "sys.stdin",
28 io.StringIO(
29 """
30 {
31 "objects": [
32 {
33 "path": "pytkdocs.cli.main"
34 },
35 {
36 "path": "pytkdocs.cli.get_parser"
37 }
38 ]
39 }
40 """
41 ),
42 )
44 cli.main([])
47def test_read_stdin_line_by_line(monkeypatch):
48 """Read standard input line by line."""
49 monkeypatch.setattr(
50 "sys.stdin",
51 io.StringIO(
52 '{"objects": [{"path": "pytkdocs.cli.main"}]}\n{"objects": [{"path": "pytkdocs.cli.get_parser"}]}\n'
53 ),
54 )
55 cli.main(["--line-by-line"])
58def test_load_complete_tree(monkeypatch):
59 """Load `pytkdocs` own documentation."""
60 monkeypatch.setattr("sys.stdin", io.StringIO('{"objects": [{"path": "pytkdocs"}]}'))
61 cli.main(["--line-by-line"])
64def test_discard_stdout(monkeypatch, capsys):
65 """Discard standard output at import time."""
66 monkeypatch.setattr("sys.stdin", io.StringIO('{"objects": [{"path": "tests.fixtures.corrupt_output"}]}'))
67 cli.main(["--line-by-line"])
68 captured = capsys.readouterr()
69 assert not captured.out.startswith("*corruption intensifies*")
70 # assert no JSON parsing error
71 json.loads(captured.out)
74def test_exception_raised_while_discard_stdout(monkeypatch, capsys):
75 """Check that an error is still printed when an exception is raised and stdout is discarded."""
76 monkeypatch.setattr("sys.stdin", io.StringIO('{"objects": [{"path": "pytkdocs.cli"}]}'))
77 # raise an exception during the process
78 monkeypatch.setattr("pytkdocs.cli.process_json", lambda _: 1 / 0)
79 # assert no exception
80 cli.main(["--line-by-line"])
81 # assert json error was written to stdout
82 captured = capsys.readouterr()
83 assert captured.out
84 # assert no JSON parsing error
85 json.loads(captured.out)
88def test_load_complete_tests_tree(monkeypatch):
89 """Load `pytkdocs` own tests' documentation."""
90 monkeypatch.setattr("sys.stdin", io.StringIO('{"objects": [{"path": "tests"}]}'))
91 cli.main(["--line-by-line"])