Skip to content

Commit 9deab5b

Browse files
committed
test: use match in pytest.raises
1 parent 0032547 commit 9deab5b

11 files changed

+97
-120
lines changed

tests/commands/test_bump_command.py

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,8 @@ def test_bump_tag_exists_raises_exception(util: UtilFixture):
303303
util.create_file_and_commit("feat: new file")
304304
git.tag("0.2.0")
305305

306-
with pytest.raises(BumpTagFailedError) as excinfo:
306+
with pytest.raises(BumpTagFailedError, match="0.2.0"):
307307
util.run_cli("bump", "--yes")
308-
assert "0.2.0" in str(excinfo.value) # This should be a fatal error
309308

310309

311310
@pytest.mark.usefixtures("tmp_commitizen_project")
@@ -327,18 +326,17 @@ def test_bump_when_bumping_is_not_support(util: UtilFixture):
327326
"feat: new user interface\n\nBREAKING CHANGE: age is no longer supported"
328327
)
329328

330-
with pytest.raises(NoPatternMapError) as excinfo:
329+
with pytest.raises(NoPatternMapError, match="'cz_jira' rule does not support bump"):
331330
util.run_cli("-n", "cz_jira", "bump", "--yes")
332-
assert "'cz_jira' rule does not support bump" in str(excinfo.value)
333331

334332

335333
@pytest.mark.usefixtures("tmp_git_project")
336334
def test_bump_when_version_is_not_specify(util: UtilFixture):
337-
with pytest.raises(NoVersionSpecifiedError) as excinfo:
335+
with pytest.raises(
336+
NoVersionSpecifiedError, match=re.escape(NoVersionSpecifiedError.message)
337+
):
338338
util.run_cli("bump")
339339

340-
assert NoVersionSpecifiedError.message in str(excinfo.value)
341-
342340

343341
@pytest.mark.usefixtures("tmp_commitizen_project")
344342
def test_bump_when_no_new_commit(util: UtilFixture):
@@ -349,11 +347,11 @@ def test_bump_when_no_new_commit(util: UtilFixture):
349347
util.run_cli("bump", "--yes")
350348

351349
# bump without a new commit.
352-
with pytest.raises(NoCommitsFoundError) as excinfo:
350+
with pytest.raises(
351+
NoCommitsFoundError, match=r"\[NO_COMMITS_FOUND\]\nNo new commits found\."
352+
):
353353
util.run_cli("bump", "--yes")
354354

355-
assert "[NO_COMMITS_FOUND]\nNo new commits found." in str(excinfo.value)
356-
357355

358356
def test_bump_when_version_inconsistent_in_version_files(
359357
tmp_commitizen_project, util: UtilFixture
@@ -369,11 +367,11 @@ def test_bump_when_version_inconsistent_in_version_files(
369367

370368
util.create_file_and_commit("feat: new file")
371369

372-
with pytest.raises(CurrentVersionNotFoundError) as excinfo:
370+
with pytest.raises(
371+
CurrentVersionNotFoundError, match="Current version 0.1.0 is not found in"
372+
):
373373
util.run_cli("bump", "--yes", "--check-consistency")
374374

375-
assert "Current version 0.1.0 is not found in" in str(excinfo.value)
376-
377375

378376
def test_bump_major_version_zero_when_major_is_not_zero(
379377
tmp_commitizen_project, util: UtilFixture
@@ -394,13 +392,12 @@ def test_bump_major_version_zero_when_major_is_not_zero(
394392
util.create_tag("v1.0.0")
395393
util.create_file_and_commit("feat(user)!: new file")
396394

397-
with pytest.raises(NotAllowed) as excinfo:
395+
with pytest.raises(
396+
NotAllowed,
397+
match="--major-version-zero is meaningless for current version 1.0.0",
398+
):
398399
util.run_cli("bump", "--yes", "--major-version-zero")
399400

400-
assert "--major-version-zero is meaningless for current version 1.0.0" in str(
401-
excinfo.value
402-
)
403-
404401

405402
def test_bump_files_only(tmp_commitizen_project, util: UtilFixture):
406403
tmp_version_file = tmp_commitizen_project.join("__version__.py")
@@ -538,13 +535,14 @@ def test_prevent_prerelease_when_no_increment_detected(
538535

539536
util.create_file_and_commit("test: new file")
540537

541-
with pytest.raises(NoCommitsFoundError) as excinfo:
538+
with pytest.raises(
539+
NoCommitsFoundError,
540+
match=re.escape(
541+
"[NO_COMMITS_FOUND]\nNo commits found to generate a pre-release."
542+
),
543+
):
542544
util.run_cli("bump", "-pr", "beta")
543545

544-
assert "[NO_COMMITS_FOUND]\nNo commits found to generate a pre-release." in str(
545-
excinfo.value
546-
)
547-
548546

549547
@pytest.mark.usefixtures("tmp_commitizen_project")
550548
def test_bump_with_changelog_to_stdout_arg(
@@ -735,14 +733,14 @@ def test_bump_invalid_manual_version_raises_exception(
735733
):
736734
util.create_file_and_commit("feat: new file")
737735

738-
with pytest.raises(InvalidManualVersion) as excinfo:
736+
with pytest.raises(
737+
InvalidManualVersion,
738+
match=re.escape(
739+
f"[INVALID_MANUAL_VERSION]\nInvalid manual version: '{manual_version}'"
740+
),
741+
):
739742
util.run_cli("bump", "--yes", manual_version)
740743

741-
assert (
742-
f"[INVALID_MANUAL_VERSION]\nInvalid manual version: '{manual_version}'"
743-
in str(excinfo.value)
744-
)
745-
746744

747745
@pytest.mark.usefixtures("tmp_commitizen_project")
748746
@pytest.mark.parametrize(
@@ -770,13 +768,12 @@ def test_bump_manual_version(util: UtilFixture, manual_version):
770768
@pytest.mark.usefixtures("tmp_commitizen_project")
771769
def test_bump_manual_version_disallows_major_version_zero(util: UtilFixture):
772770
util.create_file_and_commit("feat: new file")
773-
with pytest.raises(NotAllowed) as excinfo:
771+
with pytest.raises(
772+
NotAllowed,
773+
match="--major-version-zero cannot be combined with MANUAL_VERSION",
774+
):
774775
util.run_cli("bump", "--yes", "--major-version-zero", "0.2.0")
775776

776-
assert "--major-version-zero cannot be combined with MANUAL_VERSION" in str(
777-
excinfo.value
778-
)
779-
780777

781778
@pytest.mark.parametrize(
782779
"initial_version, expected_version_after_bump",

tests/commands/test_changelog_command.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,11 +877,9 @@ def test_changelog_from_rev_range_not_found(
877877
util.create_file_and_commit("feat: new file")
878878
util.create_tag("1.0.0")
879879

880-
with pytest.raises(NoCommitsFoundError) as excinfo:
880+
with pytest.raises(NoCommitsFoundError, match="Could not find a valid revision"):
881881
util.run_cli("changelog", rev_range) # it shouldn't exist
882882

883-
assert "Could not find a valid revision" in str(excinfo)
884-
885883

886884
@pytest.mark.usefixtures("tmp_commitizen_project")
887885
def test_changelog_multiple_matching_tags(

tests/commands/test_check_command.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ def test_check_jira_fails(mocker: MockFixture, util: UtilFixture):
7373
"commitizen.commands.check.open",
7474
mocker.mock_open(read_data="random message for J-2 #fake_command blah"),
7575
)
76-
with pytest.raises(InvalidCommitMessageError) as excinfo:
76+
with pytest.raises(InvalidCommitMessageError, match="commit validation: failed!"):
7777
util.run_cli("-n", "cz_jira", "check", "--commit-msg-file", "some_file")
78-
assert "commit validation: failed!" in str(excinfo.value)
7978

8079

8180
@pytest.mark.parametrize(
@@ -169,30 +168,31 @@ def test_check_a_range_of_git_commits_and_failed(config, mocker: MockFixture):
169168
return_value=_build_fake_git_commits(["This commit does not follow rule"]),
170169
)
171170

172-
with pytest.raises(InvalidCommitMessageError) as excinfo:
171+
with pytest.raises(
172+
InvalidCommitMessageError, match="This commit does not follow rule"
173+
):
173174
commands.Check(config=config, arguments={"rev_range": "HEAD~10..master"})()
174-
assert "This commit does not follow rule" in str(excinfo.value)
175175

176176

177177
def test_check_command_with_invalid_argument(config):
178-
with pytest.raises(InvalidCommandArgumentError) as excinfo:
178+
with pytest.raises(
179+
InvalidCommandArgumentError,
180+
match="Only one of --rev-range, --message, and --commit-msg-file is permitted by check command!",
181+
):
179182
commands.Check(
180183
config=config,
181184
arguments={"commit_msg_file": "some_file", "rev_range": "HEAD~10..master"},
182185
)
183-
assert (
184-
"Only one of --rev-range, --message, and --commit-msg-file is permitted by check command!"
185-
in str(excinfo.value)
186-
)
187186

188187

189188
@pytest.mark.usefixtures("tmp_commitizen_project")
190189
def test_check_command_with_empty_range(config: BaseConfig, util: UtilFixture):
191190
# must initialize git with a commit
192191
util.create_file_and_commit("feat: initial")
193-
with pytest.raises(NoCommitsFoundError) as excinfo:
192+
with pytest.raises(
193+
NoCommitsFoundError, match="No commit found with range: 'master..master'"
194+
):
194195
commands.Check(config=config, arguments={"rev_range": "master..master"})()
195-
assert "No commit found with range: 'master..master'" in str(excinfo)
196196

197197

198198
def test_check_a_range_of_failed_git_commits(config, mocker: MockFixture):
@@ -206,9 +206,11 @@ def test_check_a_range_of_failed_git_commits(config, mocker: MockFixture):
206206
return_value=_build_fake_git_commits(ill_formatted_commits_msgs),
207207
)
208208

209-
with pytest.raises(InvalidCommitMessageError) as excinfo:
209+
with pytest.raises(
210+
InvalidCommitMessageError,
211+
match=r"[\s\S]*".join(ill_formatted_commits_msgs),
212+
):
210213
commands.Check(config=config, arguments={"rev_range": "HEAD~10..master"})()
211-
assert all([msg in str(excinfo.value) for msg in ill_formatted_commits_msgs])
212214

213215

214216
def test_check_command_with_valid_message(config, success_mock: MockType):
@@ -279,9 +281,8 @@ def test_check_command_with_pipe_message_and_failed(
279281
):
280282
mocker.patch("sys.stdin", StringIO("bad commit message"))
281283

282-
with pytest.raises(InvalidCommitMessageError) as excinfo:
284+
with pytest.raises(InvalidCommitMessageError, match="commit validation: failed!"):
283285
util.run_cli("check")
284-
assert "commit validation: failed!" in str(excinfo.value)
285286

286287

287288
def test_check_command_with_comment_in_message_file(
@@ -450,11 +451,10 @@ def test_check_command_with_custom_validator_failed(
450451
read_data="123-ABC issue id has wrong format and misses colon"
451452
),
452453
)
453-
with pytest.raises(InvalidCommitMessageError) as excinfo:
454+
with pytest.raises(
455+
InvalidCommitMessageError,
456+
match=r"commit validation: failed![\s\S]*pattern: ",
457+
):
454458
util.run_cli(
455459
"--name", "cz_custom_validator", "check", "--commit-msg-file", "some_file"
456460
)
457-
assert "commit validation: failed!" in str(excinfo.value), (
458-
"Pattern validation unexpectedly passed"
459-
)
460-
assert "pattern: " in str(excinfo.value), "Pattern not found in error message"

tests/commands/test_commit_command.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from pathlib import Path
23
from unittest.mock import ANY
34

@@ -81,11 +82,11 @@ def test_commit_backup_on_failure(
8182

8283
@pytest.mark.usefixtures("staging_is_clean", "commit_mock")
8384
def test_commit_retry_fails_no_backup(config):
84-
with pytest.raises(NoCommitBackupError) as excinfo:
85+
with pytest.raises(
86+
NoCommitBackupError, match=re.escape(NoCommitBackupError.message)
87+
):
8588
commands.Commit(config, {"retry": True})()
8689

87-
assert NoCommitBackupError.message in str(excinfo.value)
88-
8990

9091
@pytest.mark.usefixtures("staging_is_clean", "backup_file")
9192
def test_commit_retry_works(
@@ -206,11 +207,9 @@ def test_commit_command_with_gpgsign_and_always_signoff_enabled(
206207
def test_commit_when_nothing_to_commit(config, mocker: MockFixture):
207208
mocker.patch("commitizen.git.is_staging_clean", return_value=True)
208209

209-
with pytest.raises(NothingToCommitError) as excinfo:
210+
with pytest.raises(NothingToCommitError, match="No files added to staging!"):
210211
commands.Commit(config, {})()
211212

212-
assert "No files added to staging!" in str(excinfo.value)
213-
214213

215214
@pytest.mark.usefixtures("staging_is_clean", "prompt_mock_feat")
216215
def test_commit_with_allow_empty(config, success_mock: MockType, commit_mock: MockType):
@@ -239,12 +238,9 @@ def test_commit_when_customized_expected_raised(config, mocker: MockFixture):
239238
_err = ValueError()
240239
_err.__context__ = CzException("This is the root custom err")
241240
mocker.patch("questionary.prompt", side_effect=_err)
242-
with pytest.raises(CustomError) as excinfo:
241+
with pytest.raises(CustomError, match="This is the root custom err"):
243242
commands.Commit(config, {})()
244243

245-
# Assert only the content in the formatted text
246-
assert "This is the root custom err" in str(excinfo.value)
247-
248244

249245
@pytest.mark.usefixtures("staging_is_clean")
250246
def test_commit_when_non_customized_expected_raised(config, mocker: MockFixture):

tests/test_bump_update_version_in_files.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ def test_file_version_inconsistent_error(
193193
]
194194
old_version = "1.2.3"
195195
new_version = "2.0.0"
196-
with pytest.raises(CurrentVersionNotFoundError) as excinfo:
196+
with pytest.raises(
197+
CurrentVersionNotFoundError,
198+
match=f"Current version {old_version} is not found in {inconsistent_python_version_file}.\nThe version defined in commitizen configuration and the ones in version_files are possibly inconsistent.",
199+
):
197200
bump.update_version_in_files(
198201
old_version,
199202
new_version,
@@ -202,13 +205,6 @@ def test_file_version_inconsistent_error(
202205
encoding="utf-8",
203206
)
204207

205-
expected_msg = (
206-
f"Current version 1.2.3 is not found in {inconsistent_python_version_file}.\n"
207-
"The version defined in commitizen configuration and the ones in "
208-
"version_files are possibly inconsistent."
209-
)
210-
assert expected_msg in str(excinfo.value)
211-
212208

213209
def test_multiple_versions_to_bump(
214210
multiple_versions_to_update_poetry_lock, file_regression
@@ -277,7 +273,10 @@ def test_update_version_in_files_with_check_consistency_true_failure(
277273
version_files = [commitizen_config_file, inconsistent_python_version_file]
278274

279275
# This should fail because inconsistent_python_version_file doesn't contain the current version
280-
with pytest.raises(CurrentVersionNotFoundError) as excinfo:
276+
with pytest.raises(
277+
CurrentVersionNotFoundError,
278+
match=f"Current version {old_version} is not found in {inconsistent_python_version_file}.\nThe version defined in commitizen configuration and the ones in version_files are possibly inconsistent.",
279+
):
281280
bump.update_version_in_files(
282281
old_version,
283282
new_version,
@@ -286,13 +285,6 @@ def test_update_version_in_files_with_check_consistency_true_failure(
286285
encoding="utf-8",
287286
)
288287

289-
expected_msg = (
290-
f"Current version {old_version} is not found in {inconsistent_python_version_file}.\n"
291-
"The version defined in commitizen configuration and the ones in "
292-
"version_files are possibly inconsistent."
293-
)
294-
assert expected_msg in str(excinfo.value)
295-
296288

297289
@pytest.mark.parametrize(
298290
"encoding,filename",

tests/test_changelog.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,11 +1229,11 @@ def test_generate_ordered_changelog_tree(change_type_order, expected_reordering)
12291229

12301230
def test_generate_ordered_changelog_tree_raises():
12311231
change_type_order = ["BREAKING CHANGE", "feat", "refactor", "feat"]
1232-
with pytest.raises(InvalidConfigurationError) as excinfo:
1232+
with pytest.raises(
1233+
InvalidConfigurationError, match="Change types contain duplicated types"
1234+
):
12331235
list(changelog.generate_ordered_changelog_tree(COMMITS_TREE, change_type_order))
12341236

1235-
assert "Change types contain duplicated types" in str(excinfo)
1236-
12371237

12381238
def test_render_changelog(
12391239
gitcommits, tags, changelog_content, any_changelog_format: ChangelogFormat
@@ -1544,9 +1544,10 @@ def test_get_next_tag_name_after_version(tags):
15441544
assert last_tag_name is None
15451545

15461546
# Test error when version not found
1547-
with pytest.raises(changelog.NoCommitsFoundError) as exc_info:
1547+
with pytest.raises(
1548+
changelog.NoCommitsFoundError, match="Could not find a valid revision range"
1549+
):
15481550
changelog.get_next_tag_name_after_version(tags, "nonexistent")
1549-
assert "Could not find a valid revision range" in str(exc_info.value)
15501551

15511552

15521553
@dataclass

0 commit comments

Comments
 (0)