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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
|
from __future__ import annotations
import sys
from io import StringIO
import pytest
from pytest_mock import MockFixture
from commitizen import cli, commands, git
from commitizen.exceptions import (
InvalidCommandArgumentError,
InvalidCommitMessageError,
NoCommitsFoundError,
)
from tests.utils import create_file_and_commit, skip_below_py_3_13
COMMIT_LOG = [
"refactor: A code change that neither fixes a bug nor adds a feature",
r"refactor(cz/connventional_commit): use \S to check scope",
"refactor(git): remove unnecessary dot between git range",
"bump: version 1.16.3 → 1.16.4",
(
"Merge pull request #139 from Lee-W/fix-init-clean-config-file\n\n"
"Fix init clean config file"
),
"ci(pyproject.toml): add configuration for coverage",
"fix(commands/init): fix clean up file when initialize commitizen config\n\n#138",
"refactor(defaults): split config files into long term support and deprecated ones",
"bump: version 1.16.2 → 1.16.3",
(
"Merge pull request #136 from Lee-W/remove-redundant-readme\n\n"
"Remove redundant readme"
),
"fix: replace README.rst with docs/README.md in config files",
(
"refactor(docs): remove README.rst and use docs/README.md\n\n"
"By removing README.rst, we no longer need to maintain "
"two document with almost the same content\n"
"Github can read docs/README.md as README for the project."
),
"docs(check): pin pre-commit to v1.16.2",
"docs(check): fix pre-commit setup",
"bump: version 1.16.1 → 1.16.2",
"Merge pull request #135 from Lee-W/fix-pre-commit-hook\n\nFix pre commit hook",
"docs(check): enforce cz check only when committing",
(
'Revert "fix(pre-commit): set pre-commit check stage to commit-msg"\n\n'
"This reverts commit afc70133e4a81344928561fbf3bb20738dfc8a0b."
),
"feat!: add user stuff",
"fixup! test(commands): ignore fixup! prefix",
"fixup! test(commands): ignore squash! prefix",
]
def _build_fake_git_commits(commit_msgs: list[str]) -> list[git.GitCommit]:
return [git.GitCommit("test_rev", commit_msg) for commit_msg in commit_msgs]
def test_check_jira_fails(mocker: MockFixture):
testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data="random message for J-2 #fake_command blah"),
)
with pytest.raises(InvalidCommitMessageError) as excinfo:
cli.main()
assert "commit validation: failed!" in str(excinfo.value)
def test_check_jira_command_after_issue_one_space(mocker: MockFixture, capsys):
testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data="JR-23 #command some arguments etc"),
)
cli.main()
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out
def test_check_jira_command_after_issue_two_spaces(mocker: MockFixture, capsys):
testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data="JR-2 #command some arguments etc"),
)
cli.main()
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out
def test_check_jira_text_between_issue_and_command(mocker: MockFixture, capsys):
testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data="JR-234 some text #command some arguments etc"),
)
cli.main()
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out
def test_check_jira_multiple_commands(mocker: MockFixture, capsys):
testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data="JRA-23 some text #command1 args #command2 args"),
)
cli.main()
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out
def test_check_conventional_commit_succeeds(mocker: MockFixture, capsys):
testargs = ["cz", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data="fix(scope): some commit message"),
)
cli.main()
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out
@pytest.mark.parametrize(
"commit_msg",
(
"feat!(lang): removed polish language",
"no conventional commit",
(
"ci: check commit message on merge\n"
"testing with more complex commit mes\n\n"
"age with error"
),
),
)
def test_check_no_conventional_commit(commit_msg, config, mocker: MockFixture, tmpdir):
with pytest.raises(InvalidCommitMessageError):
error_mock = mocker.patch("commitizen.out.error")
tempfile = tmpdir.join("temp_commit_file")
tempfile.write(commit_msg)
check_cmd = commands.Check(
config=config, arguments={"commit_msg_file": tempfile}
)
check_cmd()
error_mock.assert_called_once()
@pytest.mark.parametrize(
"commit_msg",
(
"feat(lang)!: removed polish language",
"feat(lang): added polish language",
"feat: add polish language",
"bump: 0.0.1 -> 1.0.0",
),
)
def test_check_conventional_commit(commit_msg, config, mocker: MockFixture, tmpdir):
success_mock = mocker.patch("commitizen.out.success")
tempfile = tmpdir.join("temp_commit_file")
tempfile.write(commit_msg)
check_cmd = commands.Check(config=config, arguments={"commit_msg_file": tempfile})
check_cmd()
success_mock.assert_called_once()
def test_check_command_when_commit_file_not_found(config):
with pytest.raises(FileNotFoundError):
commands.Check(config=config, arguments={"commit_msg_file": "no_such_file"})()
def test_check_a_range_of_git_commits(config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
mocker.patch(
"commitizen.git.get_commits", return_value=_build_fake_git_commits(COMMIT_LOG)
)
check_cmd = commands.Check(
config=config, arguments={"rev_range": "HEAD~10..master"}
)
check_cmd()
success_mock.assert_called_once()
def test_check_a_range_of_git_commits_and_failed(config, mocker: MockFixture):
error_mock = mocker.patch("commitizen.out.error")
mocker.patch(
"commitizen.git.get_commits",
return_value=_build_fake_git_commits(["This commit does not follow rule"]),
)
check_cmd = commands.Check(
config=config, arguments={"rev_range": "HEAD~10..master"}
)
with pytest.raises(InvalidCommitMessageError):
check_cmd()
error_mock.assert_called_once()
def test_check_command_with_invalid_argument(config):
with pytest.raises(InvalidCommandArgumentError) as excinfo:
commands.Check(
config=config,
arguments={"commit_msg_file": "some_file", "rev_range": "HEAD~10..master"},
)
assert (
"Only one of --rev-range, --message, and --commit-msg-file is permitted by check command!"
in str(excinfo.value)
)
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_check_command_with_empty_range(config, mocker: MockFixture):
# must initialize git with a commit
create_file_and_commit("feat: initial")
check_cmd = commands.Check(config=config, arguments={"rev_range": "master..master"})
with pytest.raises(NoCommitsFoundError) as excinfo:
check_cmd()
assert "No commit found with range: 'master..master'" in str(excinfo)
def test_check_a_range_of_failed_git_commits(config, mocker: MockFixture):
ill_formated_commits_msgs = [
"First commit does not follow rule",
"Second commit does not follow rule",
("Third commit does not follow rule\nIll-formatted commit with body"),
]
mocker.patch(
"commitizen.git.get_commits",
return_value=_build_fake_git_commits(ill_formated_commits_msgs),
)
check_cmd = commands.Check(
config=config, arguments={"rev_range": "HEAD~10..master"}
)
with pytest.raises(InvalidCommitMessageError) as excinfo:
check_cmd()
assert all([msg in str(excinfo.value) for msg in ill_formated_commits_msgs])
def test_check_command_with_valid_message(config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
check_cmd = commands.Check(
config=config, arguments={"message": "fix(scope): some commit message"}
)
check_cmd()
success_mock.assert_called_once()
def test_check_command_with_invalid_message(config, mocker: MockFixture):
error_mock = mocker.patch("commitizen.out.error")
check_cmd = commands.Check(config=config, arguments={"message": "bad commit"})
with pytest.raises(InvalidCommitMessageError):
check_cmd()
error_mock.assert_called_once()
def test_check_command_with_empty_message(config, mocker: MockFixture):
error_mock = mocker.patch("commitizen.out.error")
check_cmd = commands.Check(config=config, arguments={"message": ""})
with pytest.raises(InvalidCommitMessageError):
check_cmd()
error_mock.assert_called_once()
def test_check_command_with_allow_abort_arg(config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
check_cmd = commands.Check(
config=config, arguments={"message": "", "allow_abort": True}
)
check_cmd()
success_mock.assert_called_once()
def test_check_command_with_allow_abort_config(config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
config.settings["allow_abort"] = True
check_cmd = commands.Check(config=config, arguments={"message": ""})
check_cmd()
success_mock.assert_called_once()
def test_check_command_override_allow_abort_config(config, mocker: MockFixture):
error_mock = mocker.patch("commitizen.out.error")
config.settings["allow_abort"] = True
check_cmd = commands.Check(
config=config, arguments={"message": "", "allow_abort": False}
)
with pytest.raises(InvalidCommitMessageError):
check_cmd()
error_mock.assert_called_once()
def test_check_command_with_allowed_prefixes_arg(config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
check_cmd = commands.Check(
config=config,
arguments={"message": "custom! test", "allowed_prefixes": ["custom!"]},
)
check_cmd()
success_mock.assert_called_once()
def test_check_command_with_allowed_prefixes_config(config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
config.settings["allowed_prefixes"] = ["custom!"]
check_cmd = commands.Check(config=config, arguments={"message": "custom! test"})
check_cmd()
success_mock.assert_called_once()
def test_check_command_override_allowed_prefixes_config(config, mocker: MockFixture):
error_mock = mocker.patch("commitizen.out.error")
config.settings["allow_abort"] = ["fixup!"]
check_cmd = commands.Check(
config=config,
arguments={"message": "fixup! test", "allowed_prefixes": ["custom!"]},
)
with pytest.raises(InvalidCommitMessageError):
check_cmd()
error_mock.assert_called_once()
def test_check_command_with_pipe_message(mocker: MockFixture, capsys):
testargs = ["cz", "check"]
mocker.patch.object(sys, "argv", testargs)
mocker.patch("sys.stdin", StringIO("fix(scope): some commit message"))
cli.main()
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out
def test_check_command_with_pipe_message_and_failed(mocker: MockFixture):
testargs = ["cz", "check"]
mocker.patch.object(sys, "argv", testargs)
mocker.patch("sys.stdin", StringIO("bad commit message"))
with pytest.raises(InvalidCommitMessageError) as excinfo:
cli.main()
assert "commit validation: failed!" in str(excinfo.value)
def test_check_command_with_comment_in_message_file(mocker: MockFixture, capsys):
testargs = ["cz", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(
read_data="# <type>: (If applied, this commit will...) <subject>\n"
"# |<---- Try to Limit to a Max of 50 char ---->|\n"
"ci: add commitizen pre-commit hook\n"
"\n"
"# Explain why this change is being made\n"
"# |<---- Try To Limit Each Line to a Max Of 72 Char ---->|\n"
"This pre-commit hook will check our commits automatically."
),
)
cli.main()
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out
def test_check_conventional_commit_succeed_with_git_diff(mocker, capsys):
commit_msg = (
"feat: This is a test commit\n"
"# Please enter the commit message for your changes. Lines starting\n"
"# with '#' will be ignored, and an empty message aborts the commit.\n"
"#\n"
"# On branch ...\n"
"# Changes to be committed:\n"
"# modified: ...\n"
"#\n"
"# ------------------------ >8 ------------------------\n"
"# Do not modify or remove the line above.\n"
"# Everything below it will be ignored.\n"
"diff --git a/... b/...\n"
"index f1234c..1c5678 1234\n"
"--- a/...\n"
"+++ b/...\n"
"@@ -92,3 +92,4 @@ class Command(BaseCommand):\n"
'+ "this is a test"\n'
)
testargs = ["cz", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data=commit_msg),
)
cli.main()
out, _ = capsys.readouterr()
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"
check_cmd = commands.Check(
config=config,
arguments={"message": message, "message_length_limit": len(message) + 1},
)
check_cmd()
success_mock.assert_called_once()
def test_check_command_with_message_length_limit_exceeded(config, mocker: MockFixture):
error_mock = mocker.patch("commitizen.out.error")
message = "fix(scope): some commit message"
check_cmd = commands.Check(
config=config,
arguments={"message": message, "message_length_limit": len(message) - 1},
)
with pytest.raises(InvalidCommitMessageError):
check_cmd()
error_mock.assert_called_once()
|