Coverage for tests/test_cli.py: 100.00%

48 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-09 17:28 +0100

1"""Tests for [the `cli` module][pytkdocs.cli].""" 

2 

3from __future__ import annotations 

4 

5import io 

6import json 

7 

8import pytest 

9 

10from pytkdocs import cli, debug 

11 

12 

13def test_show_help(capsys: pytest.CaptureFixture) -> None: 

14 """Show help. 

15 

16 Parameters: 

17 capsys: Pytest fixture to capture output. 

18 """ 

19 with pytest.raises(SystemExit): 

20 cli.main(["-h"]) 

21 captured = capsys.readouterr() 

22 assert "pytkdocs" in captured.out 

23 

24 

25def test_read_whole_stdin(monkeypatch: pytest.MonkeyPatch) -> None: 

26 """Read whole standard input.""" 

27 monkeypatch.setattr( 

28 "sys.stdin", 

29 io.StringIO( 

30 """ 

31 { 

32 "objects": [ 

33 { 

34 "path": "pytkdocs.cli.main" 

35 }, 

36 { 

37 "path": "pytkdocs.cli.get_parser" 

38 } 

39 ] 

40 } 

41 """, 

42 ), 

43 ) 

44 

45 cli.main([]) 

46 

47 

48def test_read_stdin_line_by_line(monkeypatch: pytest.MonkeyPatch) -> None: 

49 """Read standard input line by line.""" 

50 monkeypatch.setattr( 

51 "sys.stdin", 

52 io.StringIO( 

53 '{"objects": [{"path": "pytkdocs.cli.main"}]}\n{"objects": [{"path": "pytkdocs.cli.get_parser"}]}\n', 

54 ), 

55 ) 

56 cli.main(["--line-by-line"]) 

57 

58 

59def test_load_complete_tree(monkeypatch: pytest.MonkeyPatch) -> None: 

60 """Load `pytkdocs` own documentation.""" 

61 monkeypatch.setattr("sys.stdin", io.StringIO('{"objects": [{"path": "pytkdocs"}]}')) 

62 cli.main(["--line-by-line"]) 

63 

64 

65def test_discard_stdout(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture) -> None: 

66 """Discard standard output at import time.""" 

67 monkeypatch.setattr("sys.stdin", io.StringIO('{"objects": [{"path": "tests.fixtures.corrupt_output"}]}')) 

68 cli.main(["--line-by-line"]) 

69 captured = capsys.readouterr() 

70 assert not captured.out.startswith("*corruption intensifies*") 

71 # assert no JSON parsing error 

72 json.loads(captured.out) 

73 

74 

75def test_exception_raised_while_discard_stdout(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture) -> None: 

76 """Check that an error is still printed when an exception is raised and stdout is discarded.""" 

77 monkeypatch.setattr("sys.stdin", io.StringIO('{"objects": [{"path": "pytkdocs.cli"}]}')) 

78 # raise an exception during the process 

79 monkeypatch.setattr("pytkdocs.cli.process_json", lambda _: 1 / 0) 

80 # assert no exception 

81 cli.main(["--line-by-line"]) 

82 # assert json error was written to stdout 

83 captured = capsys.readouterr() 

84 assert captured.out 

85 # assert no JSON parsing error 

86 json.loads(captured.out) 

87 

88 

89def test_load_complete_tests_tree(monkeypatch: pytest.MonkeyPatch) -> None: 

90 """Load `pytkdocs` own tests' documentation.""" 

91 monkeypatch.setattr("sys.stdin", io.StringIO('{"objects": [{"path": "tests"}]}')) 

92 cli.main(["--line-by-line"]) 

93 

94 

95def test_show_version(capsys: pytest.CaptureFixture) -> None: 

96 """Show version. 

97 

98 Parameters: 

99 capsys: Pytest fixture to capture output. 

100 """ 

101 with pytest.raises(SystemExit): 

102 cli.main(["-V"]) 

103 captured = capsys.readouterr() 

104 assert debug.get_version() in captured.out 

105 

106 

107def test_show_debug_info(capsys: pytest.CaptureFixture) -> None: 

108 """Show debug information. 

109 

110 Parameters: 

111 capsys: Pytest fixture to capture output. 

112 """ 

113 with pytest.raises(SystemExit): 

114 cli.main(["--debug-info"]) 

115 captured = capsys.readouterr().out.lower() 

116 assert "python" in captured 

117 assert "system" in captured 

118 assert "environment" in captured 

119 assert "packages" in captured