File: test_report.py

package info (click to toggle)
python-sphinx-chango 0.5.0-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,776 kB
  • sloc: python: 4,909; javascript: 74; makefile: 23
file content (137 lines) | stat: -rw-r--r-- 4,804 bytes parent folder | download
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#  SPDX-FileCopyrightText: 2024-present Hinrich Mahler <chango@mahlerhome.de>
#
#  SPDX-License-Identifier: MIT

from pathlib import Path

import pytest
from click import UsageError

from chango.constants import MarkupLanguage
from tests.cli.conftest import ReuseCliRunner


class TestReport:
    @pytest.mark.parametrize(
        "markup",
        [MarkupLanguage.MARKDOWN, MarkupLanguage.HTML, None],
        ids=["Markdown", "HTML", "DefaultMarkup"],
    )
    @pytest.mark.parametrize(
        "markup_option", ["--markup", "-m"], ids=["LongMarkup", "ShortMarkup"]
    )
    @pytest.mark.parametrize(
        ("output", "output_option"), [(None, None), (True, "--output"), (True, "-o")]
    )
    def test_report_version(
        self,
        cli: ReuseCliRunner,
        mock_chango_instance,
        markup,
        markup_option,
        output,
        output_option,
        tmp_path: Path,
    ):
        file_path = tmp_path / "output_file"
        version_note = mock_chango_instance.load_version_note.return_value
        version_note.render.return_value = "expected_render_output"

        args = ["report", "version", "--uid", "1.2.3"]
        if markup is not None:
            args.extend([markup_option, markup.value])
        if output is True:
            args.extend([output_option, file_path.as_posix()])

        result = cli.invoke(args=args)

        assert result.check_exit_code()

        if output is not None:
            assert result.stdout == f"Report written to {file_path}\n"
        else:
            assert result.stdout == "expected_render_output\n"

        mock_chango_instance.load_version_note.assert_called_once_with("1.2.3")
        version_note.render.assert_called_once_with(markup=markup or MarkupLanguage.MARKDOWN)
        if output is True:
            assert file_path.read_text() == "expected_render_output"

    @pytest.mark.parametrize(
        "markup",
        [MarkupLanguage.MARKDOWN, MarkupLanguage.HTML, None],
        ids=["Markdown", "HTML", "DefaultMarkup"],
    )
    @pytest.mark.parametrize(
        "markup_option", ["--markup", "-m"], ids=["LongMarkup", "ShortMarkup"]
    )
    @pytest.mark.parametrize(
        ("output", "output_option"), [(None, None), (True, "--output"), (True, "-o")]
    )
    def test_report_history(
        self,
        cli: ReuseCliRunner,
        mock_chango_instance,
        markup,
        markup_option,
        output,
        output_option,
        tmp_path: Path,
    ):
        file_path = tmp_path / "output_file"
        version_history = mock_chango_instance.load_version_history.return_value
        version_history.render.return_value = "expected_render_output"

        args = ["report", "history"]
        if markup is not None:
            args.extend([markup_option, markup.value])
        if output is True:
            args.extend([output_option, file_path.as_posix()])

        result = cli.invoke(args=args)

        assert result.check_exit_code()

        if output is not None:
            assert result.stdout == f"Report written to {file_path}\n"
        else:
            assert result.stdout == "expected_render_output\n"

        mock_chango_instance.load_version_history.assert_called_once_with()
        version_history.render.assert_called_once_with(markup=markup or MarkupLanguage.MARKDOWN)
        if output is True:
            assert file_path.read_text() == "expected_render_output"

    @pytest.mark.parametrize("invalidity_type", ["dir", "non_writable"])
    @pytest.mark.parametrize("subcommand", ["version", "history"], ids=["Version", "History"])
    def test_report_invalid_file(
        self,
        cli: ReuseCliRunner,
        mock_chango_instance,
        tmp_path: Path,
        invalidity_type,
        subcommand,
    ):
        file_path = tmp_path / "output_file"
        if invalidity_type == "dir":
            output_path = tmp_path
        elif invalidity_type == "non_writable":
            file_path.touch()
            file_path.chmod(0o444)
            output_path = file_path

        args = ["report", subcommand, "--uid", "1.2.3", "--output", output_path.as_posix()]
        result = cli.invoke(args=args)

        assert result.check_exit_code(UsageError.exit_code)
        mock_chango_instance.load_version_note.assert_not_called()

    @pytest.mark.parametrize("subcommand", ["version", "history"], ids=["Version", "History"])
    def test_report_invalid_markup(self, cli: ReuseCliRunner, mock_chango_instance, subcommand):
        args = ["report", subcommand, "--markup", "invalid_markup"]
        if subcommand == "version":
            args.extend(["--uid", "1.2.3"])
        result = cli.invoke(args=args)

        assert result.check_exit_code(UsageError.exit_code)
        mock_chango_instance.load_version_note.assert_not_called()