File: test_help.py

package info (click to toggle)
python-semantic-release 10.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,236 kB
  • sloc: python: 36,523; sh: 340; makefile: 156
file content (232 lines) | stat: -rw-r--r-- 7,153 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest
from pytest_lazy_fixtures.lazy_fixture import lf as lazy_fixture

from semantic_release.cli.commands.changelog import changelog
from semantic_release.cli.commands.generate_config import generate_config
from semantic_release.cli.commands.main import main
from semantic_release.cli.commands.publish import publish
from semantic_release.cli.commands.version import version

from tests.const import MAIN_PROG_NAME, SUCCESS_EXIT_CODE
from tests.fixtures.repos import repo_w_trunk_only_conventional_commits
from tests.util import assert_exit_code

if TYPE_CHECKING:
    from click import Command

    from tests.conftest import RunCliFn
    from tests.fixtures import UpdatePyprojectTomlFn
    from tests.fixtures.git_repo import BuiltRepoResult


# Define the expected exit code for the help command
HELP_EXIT_CODE = SUCCESS_EXIT_CODE


@pytest.mark.parametrize(
    "help_option", ("-h", "--help"), ids=lambda opt: opt.lstrip("-")
)
@pytest.mark.parametrize(
    "command",
    (main, changelog, generate_config, publish, version),
    ids=lambda cmd: cmd.name,
)
def test_help_no_repo(
    help_option: str,
    command: Command,
    run_cli: RunCliFn,
    change_to_ex_proj_dir: None,
):
    """
    Test that the help message is displayed even when the current directory is not a git repository
    and there is not a configuration file available.
    Documented issue #840
    """
    # Generate some expected output that should be specific per command
    cmd_usage = str.join(
        " ",
        list(
            filter(
                None,
                [
                    "Usage:",
                    MAIN_PROG_NAME,
                    command.name if command.name != "main" else "",
                    "[OPTIONS]",
                    "" if command.name != main.name else "COMMAND [ARGS]...",
                ],
            )
        ),
    )

    # Create the arguments list for subcommands unless its main
    args = list(
        filter(None, [command.name if command.name != main.name else "", help_option])
    )

    # Run the command with the help option
    result = run_cli(args, invoke_kwargs={"prog_name": MAIN_PROG_NAME})

    # Evaluate result
    assert_exit_code(HELP_EXIT_CODE, result, [MAIN_PROG_NAME, *args])
    assert cmd_usage in result.output


@pytest.mark.parametrize(
    "help_option", ("-h", "--help"), ids=lambda opt: opt.lstrip("-")
)
@pytest.mark.parametrize(
    "command",
    (main, changelog, generate_config, publish, version),
    ids=lambda cmd: cmd.name,
)
@pytest.mark.usefixtures(repo_w_trunk_only_conventional_commits.__name__)
def test_help_valid_config(
    help_option: str,
    command: Command,
    run_cli: RunCliFn,
):
    """
    Test that the help message is displayed when the current directory is a git repository
    and there is a valid configuration file available.
    Documented issue #840
    """
    cmd_usage = str.join(
        " ",
        list(
            filter(
                None,
                [
                    "Usage:",
                    MAIN_PROG_NAME,
                    command.name if command.name != main.name else "",
                    "[OPTIONS]",
                    "" if command.name != main.name else "COMMAND [ARGS]...",
                ],
            )
        ),
    )

    # Create the arguments list for subcommands unless its main
    args = list(
        filter(None, [command.name if command.name != main.name else "", help_option])
    )

    # Run the command with the help option
    result = run_cli(args, invoke_kwargs={"prog_name": MAIN_PROG_NAME})

    # Evaluate result
    assert_exit_code(HELP_EXIT_CODE, result, [MAIN_PROG_NAME, *args])
    assert cmd_usage in result.output


@pytest.mark.parametrize(
    "help_option", ("-h", "--help"), ids=lambda opt: opt.lstrip("-")
)
@pytest.mark.parametrize(
    "command",
    (main, changelog, generate_config, publish, version),
    ids=lambda cmd: cmd.name,
)
@pytest.mark.usefixtures(repo_w_trunk_only_conventional_commits.__name__)
def test_help_invalid_config(
    help_option: str,
    command: Command,
    run_cli: RunCliFn,
    update_pyproject_toml: UpdatePyprojectTomlFn,
):
    """
    Test that the help message is displayed when the current directory is a git repository
    and there is an invalid configuration file available.
    Documented issue #840
    """
    # Update the configuration file to have an invalid value
    update_pyproject_toml("tool.semantic_release.remote.type", "invalidhvcs")

    # Generate some expected output that should be specific per command
    cmd_usage = str.join(
        " ",
        list(
            filter(
                None,
                [
                    "Usage:",
                    MAIN_PROG_NAME,
                    command.name if command.name != "main" else "",
                    "[OPTIONS]",
                    "" if command.name != main.name else "COMMAND [ARGS]...",
                ],
            )
        ),
    )

    # Create the arguments list for subcommands unless its main
    args = list(
        filter(None, [command.name if command.name != main.name else "", help_option])
    )

    # Run the command with the help option
    result = run_cli(args, invoke_kwargs={"prog_name": MAIN_PROG_NAME})

    # Evaluate result
    assert_exit_code(HELP_EXIT_CODE, result, [MAIN_PROG_NAME, *args])
    assert cmd_usage in result.output


@pytest.mark.parametrize(
    "help_option", ("-h", "--help"), ids=lambda opt: opt.lstrip("-")
)
@pytest.mark.parametrize(
    "command",
    (main, changelog, generate_config, publish, version),
    ids=lambda cmd: cmd.name,
)
@pytest.mark.parametrize(
    "repo_result", [lazy_fixture(repo_w_trunk_only_conventional_commits.__name__)]
)
def test_help_non_release_branch(
    help_option: str,
    command: Command,
    run_cli: RunCliFn,
    repo_result: BuiltRepoResult,
):
    """
    Test that the help message is displayed even when the current branch is not a release branch.
    Documented issue #840
    """
    # Create & checkout a non-release branch
    non_release_branch = repo_result["repo"].create_head("feature-branch")
    non_release_branch.checkout()

    # Generate some expected output that should be specific per command
    cmd_usage = str.join(
        " ",
        list(
            filter(
                None,
                [
                    "Usage:",
                    MAIN_PROG_NAME,
                    command.name if command.name != "main" else "",
                    "[OPTIONS]",
                    "" if command.name != main.name else "COMMAND [ARGS]...",
                ],
            )
        ),
    )

    # Create the arguments list for subcommands unless its main
    args = list(
        filter(None, [command.name if command.name != main.name else "", help_option])
    )

    # Run the command with the help option
    result = run_cli(args, invoke_kwargs={"prog_name": MAIN_PROG_NAME})

    # Evaluate result
    assert_exit_code(HELP_EXIT_CODE, result, [MAIN_PROG_NAME, *args])
    assert cmd_usage in result.output