diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index 8dbe8f447e..d8cac56f75 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -28,7 +28,7 @@ NotAllowed, NoVersionSpecifiedError, ) -from tests.utils import create_file_and_commit, create_tag, skip_below_py_3_13 +from tests.utils import create_file_and_commit, create_tag if TYPE_CHECKING: import py @@ -1438,19 +1438,6 @@ def test_bump_changelog_contains_increment_only(mocker, tmp_commitizen_project, assert "2.0.0" not in out -@skip_below_py_3_13 -def test_bump_command_shows_description_when_use_help_option( - mocker: MockFixture, capsys, file_regression -): - testargs = ["cz", "bump", "--help"] - mocker.patch.object(sys, "argv", testargs) - with pytest.raises(SystemExit): - cli.main() - - out, _ = capsys.readouterr() - file_regression.check(out, extension=".txt") - - @pytest.mark.usefixtures("tmp_commitizen_project") def test_bump_get_next(mocker: MockFixture, capsys): create_file_and_commit("feat: new file") diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index f2e2ecbd38..17594eaa53 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -30,7 +30,6 @@ create_tag, get_current_branch, merge_branch, - skip_below_py_3_13, switch_branch, wait_for_tag, ) @@ -1927,16 +1926,3 @@ class FakeTemplate: assert not target.exists() assert "Template filename is not set" in str(exc_info.value) - - -@skip_below_py_3_13 -def test_changelog_command_shows_description_when_use_help_option( - mocker: MockFixture, capsys, file_regression -): - testargs = ["cz", "changelog", "--help"] - mocker.patch.object(sys, "argv", testargs) - with pytest.raises(SystemExit): - cli.main() - - out, _ = capsys.readouterr() - file_regression.check(out, extension=".txt") diff --git a/tests/commands/test_check_command.py b/tests/commands/test_check_command.py index d545dbb18e..47dd3fe12c 100644 --- a/tests/commands/test_check_command.py +++ b/tests/commands/test_check_command.py @@ -15,7 +15,7 @@ InvalidCommitMessageError, NoCommitsFoundError, ) -from tests.utils import create_file_and_commit, skip_below_py_3_13 +from tests.utils import create_file_and_commit if TYPE_CHECKING: import re @@ -427,19 +427,6 @@ def test_check_conventional_commit_succeed_with_git_diff(mocker, capsys): assert "Commit validation: successful!" in out -@skip_below_py_3_13 -def test_check_command_shows_description_when_use_help_option( - mocker: MockFixture, capsys, file_regression -): - testargs = ["cz", "check", "--help"] - mocker.patch.object(sys, "argv", testargs) - with pytest.raises(SystemExit): - cli.main() - - out, _ = capsys.readouterr() - file_regression.check(out, extension=".txt") - - def test_check_command_with_message_length_limit(config, mocker: MockFixture): success_mock = mocker.patch("commitizen.out.success") message = "fix(scope): some commit message" diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index 3e408576fe..c987f4b3f3 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -1,11 +1,10 @@ import os -import sys from unittest.mock import ANY import pytest from pytest_mock import MockFixture -from commitizen import cli, cmd, commands +from commitizen import cmd, commands from commitizen.cz.exceptions import CzException from commitizen.cz.utils import get_backup_file_path from commitizen.exceptions import ( @@ -19,7 +18,6 @@ NotAllowed, NothingToCommitError, ) -from tests.utils import skip_below_py_3_13 @pytest.fixture @@ -512,19 +510,6 @@ def test_manual_edit(editor, config, mocker: MockFixture, tmp_path): assert edited_message == test_message.strip() -@skip_below_py_3_13 -def test_commit_command_shows_description_when_use_help_option( - mocker: MockFixture, capsys, file_regression -): - testargs = ["cz", "commit", "--help"] - mocker.patch.object(sys, "argv", testargs) - with pytest.raises(SystemExit): - cli.main() - - out, _ = capsys.readouterr() - file_regression.check(out, extension=".txt") - - @pytest.mark.usefixtures("staging_is_clean") @pytest.mark.parametrize( "out", ["no changes added to commit", "nothing added to commit"] diff --git a/tests/commands/test_common_command.py b/tests/commands/test_common_command.py new file mode 100644 index 0000000000..71ade5eda4 --- /dev/null +++ b/tests/commands/test_common_command.py @@ -0,0 +1,69 @@ +import sys + +import pytest +from pytest_mock import MockFixture + +from commitizen import cli +from commitizen.commands import Example, Info, ListCz, Schema + + +@pytest.mark.skipif( + sys.version_info < (3, 13), + reason="The output message of argparse is different between Python 3.13 and lower than Python 3.13", +) +@pytest.mark.parametrize( + "command", + [ + "bump", + "changelog", + "check", + "commit", + "example", + "info", + "init", + "ls", + "schema", + "version", + ], +) +def test_command_shows_description_when_use_help_option( + mocker: MockFixture, + capsys, + file_regression, + monkeypatch: pytest.MonkeyPatch, + command: str, +): + """Test that the command shows the description when the help option is used. + + Note: If the command description changes, please run `pytest tests/commands/test_common_command.py --regen-all` to regenerate the test files. + """ + # Force consistent terminal output + monkeypatch.setenv("COLUMNS", "80") + monkeypatch.setenv("TERM", "dumb") + monkeypatch.setenv("LC_ALL", "C") + monkeypatch.setenv("LANG", "C") + monkeypatch.setenv("NO_COLOR", "1") + monkeypatch.setenv("PAGER", "cat") + + testargs = ["cz", command, "--help"] + mocker.patch.object(sys, "argv", testargs) + with pytest.raises(SystemExit): + cli.main() + + out, _ = capsys.readouterr() + file_regression.check(out, extension=".txt") + + +@pytest.mark.parametrize( + "command", + [ + Example, + Info, + ListCz, + Schema, + ], +) +def test_simple_command_call_once(config, mocker: MockFixture, command): + write_mock = mocker.patch("commitizen.out.write") + command(config)() + write_mock.assert_called_once() diff --git a/tests/commands/test_bump_command/test_bump_command_shows_description_when_use_help_option.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_bump_.txt similarity index 100% rename from tests/commands/test_bump_command/test_bump_command_shows_description_when_use_help_option.txt rename to tests/commands/test_common_command/test_command_shows_description_when_use_help_option_bump_.txt diff --git a/tests/commands/test_changelog_command/test_changelog_command_shows_description_when_use_help_option.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_changelog_.txt similarity index 100% rename from tests/commands/test_changelog_command/test_changelog_command_shows_description_when_use_help_option.txt rename to tests/commands/test_common_command/test_command_shows_description_when_use_help_option_changelog_.txt diff --git a/tests/commands/test_check_command/test_check_command_shows_description_when_use_help_option.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_check_.txt similarity index 100% rename from tests/commands/test_check_command/test_check_command_shows_description_when_use_help_option.txt rename to tests/commands/test_common_command/test_command_shows_description_when_use_help_option_check_.txt diff --git a/tests/commands/test_commit_command/test_commit_command_shows_description_when_use_help_option.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_commit_.txt similarity index 100% rename from tests/commands/test_commit_command/test_commit_command_shows_description_when_use_help_option.txt rename to tests/commands/test_common_command/test_command_shows_description_when_use_help_option_commit_.txt diff --git a/tests/commands/test_example_command/test_example_command_shows_description_when_use_help_option.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_example_.txt similarity index 100% rename from tests/commands/test_example_command/test_example_command_shows_description_when_use_help_option.txt rename to tests/commands/test_common_command/test_command_shows_description_when_use_help_option_example_.txt diff --git a/tests/commands/test_info_command/test_info_command_shows_description_when_use_help_option.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_info_.txt similarity index 100% rename from tests/commands/test_info_command/test_info_command_shows_description_when_use_help_option.txt rename to tests/commands/test_common_command/test_command_shows_description_when_use_help_option_info_.txt diff --git a/tests/commands/test_init_command/test_init_command_shows_description_when_use_help_option.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_init_.txt similarity index 100% rename from tests/commands/test_init_command/test_init_command_shows_description_when_use_help_option.txt rename to tests/commands/test_common_command/test_command_shows_description_when_use_help_option_init_.txt diff --git a/tests/commands/test_ls_command/test_ls_command_shows_description_when_use_help_option.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_ls_.txt similarity index 100% rename from tests/commands/test_ls_command/test_ls_command_shows_description_when_use_help_option.txt rename to tests/commands/test_common_command/test_command_shows_description_when_use_help_option_ls_.txt diff --git a/tests/commands/test_schema_command/test_schema_command_shows_description_when_use_help_option.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_schema_.txt similarity index 100% rename from tests/commands/test_schema_command/test_schema_command_shows_description_when_use_help_option.txt rename to tests/commands/test_common_command/test_command_shows_description_when_use_help_option_schema_.txt diff --git a/tests/commands/test_version_command/test_version_command_shows_description_when_use_help_option.txt b/tests/commands/test_common_command/test_command_shows_description_when_use_help_option_version_.txt similarity index 100% rename from tests/commands/test_version_command/test_version_command_shows_description_when_use_help_option.txt rename to tests/commands/test_common_command/test_command_shows_description_when_use_help_option_version_.txt diff --git a/tests/commands/test_example_command.py b/tests/commands/test_example_command.py deleted file mode 100644 index 0521679f1c..0000000000 --- a/tests/commands/test_example_command.py +++ /dev/null @@ -1,26 +0,0 @@ -import sys - -import pytest -from pytest_mock import MockerFixture - -from commitizen import cli, commands -from tests.utils import skip_below_py_3_10 - - -def test_example(config, mocker: MockerFixture): - write_mock = mocker.patch("commitizen.out.write") - commands.Example(config)() - write_mock.assert_called_once() - - -@skip_below_py_3_10 -def test_example_command_shows_description_when_use_help_option( - mocker: MockerFixture, capsys, file_regression -): - testargs = ["cz", "example", "--help"] - mocker.patch.object(sys, "argv", testargs) - with pytest.raises(SystemExit): - cli.main() - - out, _ = capsys.readouterr() - file_regression.check(out, extension=".txt") diff --git a/tests/commands/test_info_command.py b/tests/commands/test_info_command.py deleted file mode 100644 index 2bd1553679..0000000000 --- a/tests/commands/test_info_command.py +++ /dev/null @@ -1,26 +0,0 @@ -import sys - -import pytest -from pytest_mock import MockerFixture - -from commitizen import cli, commands -from tests.utils import skip_below_py_3_10 - - -def test_info(config, mocker: MockerFixture): - write_mock = mocker.patch("commitizen.out.write") - commands.Info(config)() - write_mock.assert_called_once() - - -@skip_below_py_3_10 -def test_info_command_shows_description_when_use_help_option( - mocker: MockerFixture, capsys, file_regression -): - testargs = ["cz", "info", "--help"] - mocker.patch.object(sys, "argv", testargs) - with pytest.raises(SystemExit): - cli.main() - - out, _ = capsys.readouterr() - file_regression.check(out, extension=".txt") diff --git a/tests/commands/test_init_command.py b/tests/commands/test_init_command.py index 54fa271fd9..e766524139 100644 --- a/tests/commands/test_init_command.py +++ b/tests/commands/test_init_command.py @@ -2,17 +2,15 @@ import json import os -import sys from pathlib import Path from typing import TYPE_CHECKING, Any import pytest import yaml -from commitizen import cli, cmd, commands +from commitizen import cmd, commands from commitizen.__version__ import __version__ from commitizen.exceptions import InitFailedError, NoAnswersError -from tests.utils import skip_below_py_3_10 if TYPE_CHECKING: from pytest_mock import MockFixture @@ -275,19 +273,6 @@ def test_empty_input_returns_default(self, mocker: MockFixture, config: BaseConf assert result == "$version" # This is the default format from DEFAULT_SETTINGS -@skip_below_py_3_10 -def test_init_command_shows_description_when_use_help_option( - mocker: MockFixture, capsys, file_regression -): - testargs = ["cz", "init", "--help"] - mocker.patch.object(sys, "argv", testargs) - with pytest.raises(SystemExit): - cli.main() - - out, _ = capsys.readouterr() - file_regression.check(out, extension=".txt") - - def test_init_with_confirmed_tag_format( config: BaseConfig, mocker: MockFixture, tmpdir ): diff --git a/tests/commands/test_ls_command.py b/tests/commands/test_ls_command.py deleted file mode 100644 index 7225d2a85c..0000000000 --- a/tests/commands/test_ls_command.py +++ /dev/null @@ -1,26 +0,0 @@ -import sys - -import pytest -from pytest_mock import MockerFixture - -from commitizen import cli, commands -from tests.utils import skip_below_py_3_10 - - -def test_list_cz(config, mocker: MockerFixture): - write_mock = mocker.patch("commitizen.out.write") - commands.ListCz(config)() - write_mock.assert_called_once() - - -@skip_below_py_3_10 -def test_ls_command_shows_description_when_use_help_option( - mocker: MockerFixture, capsys, file_regression -): - testargs = ["cz", "ls", "--help"] - mocker.patch.object(sys, "argv", testargs) - with pytest.raises(SystemExit): - cli.main() - - out, _ = capsys.readouterr() - file_regression.check(out, extension=".txt") diff --git a/tests/commands/test_schema_command.py b/tests/commands/test_schema_command.py deleted file mode 100644 index 5e571721c5..0000000000 --- a/tests/commands/test_schema_command.py +++ /dev/null @@ -1,26 +0,0 @@ -import sys - -import pytest -from pytest_mock import MockerFixture - -from commitizen import cli, commands -from tests.utils import skip_below_py_3_10 - - -def test_schema(config, mocker: MockerFixture): - write_mock = mocker.patch("commitizen.out.write") - commands.Schema(config)() - write_mock.assert_called_once() - - -@skip_below_py_3_10 -def test_schema_command_shows_description_when_use_help_option( - mocker: MockerFixture, capsys, file_regression -): - testargs = ["cz", "schema", "--help"] - mocker.patch.object(sys, "argv", testargs) - with pytest.raises(SystemExit): - cli.main() - - out, _ = capsys.readouterr() - file_regression.check(out, extension=".txt") diff --git a/tests/commands/test_version_command.py b/tests/commands/test_version_command.py index a5faf4e16d..17f7238403 100644 --- a/tests/commands/test_version_command.py +++ b/tests/commands/test_version_command.py @@ -1,14 +1,12 @@ -import os import platform import sys import pytest from pytest_mock import MockerFixture -from commitizen import cli, commands +from commitizen import commands from commitizen.__version__ import __version__ from commitizen.config.base_config import BaseConfig -from tests.utils import skip_below_py_3_10 def test_version_for_showing_project_version_error(config, capsys): @@ -105,31 +103,6 @@ def test_version_use_version_provider( mock.set_version.assert_not_called() -@skip_below_py_3_10 -def test_version_command_shows_description_when_use_help_option( - mocker: MockerFixture, capsys, file_regression -): - # Force consistent terminal width for tests to avoid wrapping differences - # between single and multi-worker pytest modes - original_columns = os.environ.get("COLUMNS") - os.environ["COLUMNS"] = "80" - - try: - testargs = ["cz", "version", "--help"] - mocker.patch.object(sys, "argv", testargs) - with pytest.raises(SystemExit): - cli.main() - - out, _ = capsys.readouterr() - file_regression.check(out, extension=".txt") - finally: - # Restore original COLUMNS - if original_columns is not None: - os.environ["COLUMNS"] = original_columns - else: - os.environ.pop("COLUMNS", None) - - @pytest.mark.parametrize( "version, expected_version", [ diff --git a/tests/utils.py b/tests/utils.py index bea7f20a1d..f5557f76d6 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,12 +1,10 @@ from __future__ import annotations -import sys import time import uuid from pathlib import Path from typing import TYPE_CHECKING, NamedTuple -import pytest from deprecated import deprecated from commitizen import cmd, exceptions, git @@ -14,16 +12,6 @@ if TYPE_CHECKING: from commitizen.version_schemes import Increment, Prerelease -skip_below_py_3_10 = pytest.mark.skipif( - sys.version_info < (3, 10), - reason="The output message of argparse is different between Python 3.10 and lower than Python 3.10", -) - -skip_below_py_3_13 = pytest.mark.skipif( - sys.version_info < (3, 13), - reason="The output message of argparse is different between Python 3.13 and lower than Python 3.13", -) - class VersionSchemeTestArgs(NamedTuple): current_version: str