Skip to content

Commit a9e7eb1

Browse files
committed
test: use match in pytest.raises
1 parent 9bd9e8b commit a9e7eb1

11 files changed

Lines changed: 114 additions & 121 deletions

tests/commands/test_bump_command.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,8 @@ def test_bump_tag_exists_raises_exception(util: UtilFixture):
314314
util.create_file_and_commit("feat: new file")
315315
git.tag("0.2.0")
316316

317-
with pytest.raises(BumpTagFailedError) as excinfo:
317+
with pytest.raises(BumpTagFailedError, match=re.escape("0.2.0")):
318318
util.run_cli("bump", "--yes")
319-
assert "0.2.0" in str(excinfo.value) # This should be a fatal error
320319

321320

322321
@pytest.mark.usefixtures("tmp_commitizen_project")
@@ -338,18 +337,17 @@ def test_bump_when_bumping_is_not_support(util: UtilFixture):
338337
"feat: new user interface\n\nBREAKING CHANGE: age is no longer supported"
339338
)
340339

341-
with pytest.raises(NoPatternMapError) as excinfo:
340+
with pytest.raises(NoPatternMapError, match="'cz_jira' rule does not support bump"):
342341
util.run_cli("-n", "cz_jira", "bump", "--yes")
343-
assert "'cz_jira' rule does not support bump" in str(excinfo.value)
344342

345343

346344
@pytest.mark.usefixtures("tmp_git_project")
347345
def test_bump_when_version_is_not_specify(util: UtilFixture):
348-
with pytest.raises(NoVersionSpecifiedError) as excinfo:
346+
with pytest.raises(
347+
NoVersionSpecifiedError, match=re.escape(NoVersionSpecifiedError.message)
348+
):
349349
util.run_cli("bump")
350350

351-
assert NoVersionSpecifiedError.message in str(excinfo.value)
352-
353351

354352
@pytest.mark.usefixtures("tmp_commitizen_project")
355353
def test_bump_when_no_new_commit(util: UtilFixture):
@@ -360,11 +358,11 @@ def test_bump_when_no_new_commit(util: UtilFixture):
360358
util.run_cli("bump", "--yes")
361359

362360
# bump without a new commit.
363-
with pytest.raises(NoCommitsFoundError) as excinfo:
361+
with pytest.raises(
362+
NoCommitsFoundError, match=r"\[NO_COMMITS_FOUND\]\nNo new commits found\."
363+
):
364364
util.run_cli("bump", "--yes")
365365

366-
assert "[NO_COMMITS_FOUND]\nNo new commits found." in str(excinfo.value)
367-
368366

369367
def test_bump_when_version_inconsistent_in_version_files(
370368
tmp_commitizen_project, util: UtilFixture
@@ -378,11 +376,12 @@ def test_bump_when_version_inconsistent_in_version_files(
378376

379377
util.create_file_and_commit("feat: new file")
380378

381-
with pytest.raises(CurrentVersionNotFoundError) as excinfo:
379+
with pytest.raises(
380+
CurrentVersionNotFoundError,
381+
match=re.escape("Current version 0.1.0 is not found in"),
382+
):
382383
util.run_cli("bump", "--yes", "--check-consistency")
383384

384-
assert "Current version 0.1.0 is not found in" in str(excinfo.value)
385-
386385

387386
def test_bump_major_version_zero_when_major_is_not_zero(
388387
tmp_commitizen_project, util: UtilFixture
@@ -403,13 +402,14 @@ def test_bump_major_version_zero_when_major_is_not_zero(
403402
util.create_tag("v1.0.0")
404403
util.create_file_and_commit("feat(user)!: new file")
405404

406-
with pytest.raises(NotAllowed) as excinfo:
405+
with pytest.raises(
406+
NotAllowed,
407+
match=re.escape(
408+
"--major-version-zero is meaningless for current version 1.0.0"
409+
),
410+
):
407411
util.run_cli("bump", "--yes", "--major-version-zero")
408412

409-
assert "--major-version-zero is meaningless for current version 1.0.0" in str(
410-
excinfo.value
411-
)
412-
413413

414414
def test_bump_files_only(tmp_commitizen_project, util: UtilFixture):
415415
tmp_version_file = tmp_commitizen_project / "__version__.py"
@@ -539,13 +539,14 @@ def test_prevent_prerelease_when_no_increment_detected(
539539

540540
util.create_file_and_commit("test: new file")
541541

542-
with pytest.raises(NoCommitsFoundError) as excinfo:
542+
with pytest.raises(
543+
NoCommitsFoundError,
544+
match=re.escape(
545+
"[NO_COMMITS_FOUND]\nNo commits found to generate a pre-release."
546+
),
547+
):
543548
util.run_cli("bump", "-pr", "beta")
544549

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

550551
@pytest.mark.usefixtures("tmp_commitizen_project")
551552
def test_bump_with_changelog_to_stdout_arg(
@@ -735,14 +736,14 @@ def test_bump_invalid_manual_version_raises_exception(
735736
):
736737
util.create_file_and_commit("feat: new file")
737738

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

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

747748
@pytest.mark.usefixtures("tmp_commitizen_project")
748749
@pytest.mark.parametrize(
@@ -770,13 +771,12 @@ def test_bump_manual_version(util: UtilFixture, manual_version):
770771
@pytest.mark.usefixtures("tmp_commitizen_project")
771772
def test_bump_manual_version_disallows_major_version_zero(util: UtilFixture):
772773
util.create_file_and_commit("feat: new file")
773-
with pytest.raises(NotAllowed) as excinfo:
774+
with pytest.raises(
775+
NotAllowed,
776+
match="--major-version-zero cannot be combined with MANUAL_VERSION",
777+
):
774778
util.run_cli("bump", "--yes", "--major-version-zero", "0.2.0")
775779

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

781781
@pytest.mark.parametrize(
782782
("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
@@ -861,11 +861,9 @@ def test_changelog_from_rev_range_not_found(
861861
util.create_file_and_commit("feat: new file")
862862
util.create_tag("1.0.0")
863863

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

867-
assert "Could not find a valid revision" in str(excinfo)
868-
869867

870868
@pytest.mark.usefixtures("tmp_commitizen_project")
871869
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
mock_path.return_value.read_text.return_value = (
7474
"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(
@@ -167,30 +166,31 @@ def test_check_a_range_of_git_commits_and_failed(config, mocker: MockFixture):
167166
return_value=_build_fake_git_commits(["This commit does not follow rule"]),
168167
)
169168

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

174174

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

186185

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

195195

196196
def test_check_a_range_of_failed_git_commits(config, mocker: MockFixture):
@@ -204,9 +204,11 @@ def test_check_a_range_of_failed_git_commits(config, mocker: MockFixture):
204204
return_value=_build_fake_git_commits(ill_formatted_commits_msgs),
205205
)
206206

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

211213

212214
def test_check_command_with_valid_message(config, success_mock: MockType):
@@ -277,9 +279,8 @@ def test_check_command_with_pipe_message_and_failed(
277279
):
278280
mocker.patch("sys.stdin", StringIO("bad commit message"))
279281

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

284285

285286
def test_check_command_with_comment_in_message_file(
@@ -440,11 +441,10 @@ def test_check_command_with_custom_validator_failed(
440441
mock_path.return_value.read_text.return_value = (
441442
"123-ABC issue id has wrong format and misses colon"
442443
)
443-
with pytest.raises(InvalidCommitMessageError) as excinfo:
444+
with pytest.raises(
445+
InvalidCommitMessageError,
446+
match=r"commit validation: failed![\s\S]*pattern: ",
447+
):
444448
util.run_cli(
445449
"--name", "cz_custom_validator", "check", "--commit-msg-file", "some_file"
446450
)
447-
assert "commit validation: failed!" in str(excinfo.value), (
448-
"Pattern validation unexpectedly passed"
449-
)
450-
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
import sys
23
from pathlib import Path
34
from unittest.mock import ANY
@@ -84,11 +85,11 @@ def test_commit_backup_on_failure(
8485

8586
@pytest.mark.usefixtures("staging_is_clean", "commit_mock")
8687
def test_commit_retry_fails_no_backup(config):
87-
with pytest.raises(NoCommitBackupError) as excinfo:
88+
with pytest.raises(
89+
NoCommitBackupError, match=re.escape(NoCommitBackupError.message)
90+
):
8891
commands.Commit(config, {"retry": True})()
8992

90-
assert NoCommitBackupError.message in str(excinfo.value)
91-
9293

9394
@pytest.mark.usefixtures("staging_is_clean", "backup_file")
9495
def test_commit_retry_works(
@@ -209,11 +210,9 @@ def test_commit_command_with_gpgsign_and_always_signoff_enabled(
209210
def test_commit_when_nothing_to_commit(config, mocker: MockFixture):
210211
mocker.patch("commitizen.git.is_staging_clean", return_value=True)
211212

212-
with pytest.raises(NothingToCommitError) as excinfo:
213+
with pytest.raises(NothingToCommitError, match="No files added to staging!"):
213214
commands.Commit(config, {})()
214215

215-
assert "No files added to staging!" in str(excinfo.value)
216-
217216

218217
@pytest.mark.usefixtures("staging_is_clean", "prompt_mock_feat")
219218
def test_commit_with_allow_empty(config, success_mock: MockType, commit_mock: MockType):
@@ -242,12 +241,9 @@ def test_commit_when_customized_expected_raised(config, mocker: MockFixture):
242241
_err = ValueError()
243242
_err.__context__ = CzException("This is the root custom err")
244243
mocker.patch("questionary.prompt", side_effect=_err)
245-
with pytest.raises(CustomError) as excinfo:
244+
with pytest.raises(CustomError, match="This is the root custom err"):
246245
commands.Commit(config, {})()
247246

248-
# Assert only the content in the formatted text
249-
assert "This is the root custom err" in str(excinfo.value)
250-
251247

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

tests/test_bump_update_version_in_files.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from collections.abc import Callable
23
from pathlib import Path
34
from shutil import copyfile
@@ -199,7 +200,14 @@ def test_file_version_inconsistent_error(
199200
]
200201
old_version = "1.2.3"
201202
new_version = "2.0.0"
202-
with pytest.raises(CurrentVersionNotFoundError) as excinfo:
203+
with pytest.raises(
204+
CurrentVersionNotFoundError,
205+
match=re.escape(
206+
f"Current version {old_version} 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+
):
203211
bump.update_version_in_files(
204212
old_version,
205213
new_version,
@@ -208,13 +216,6 @@ def test_file_version_inconsistent_error(
208216
encoding="utf-8",
209217
)
210218

211-
expected_msg = (
212-
f"Current version 1.2.3 is not found in {inconsistent_python_version_file}.\n"
213-
"The version defined in commitizen configuration and the ones in "
214-
"version_files are possibly inconsistent."
215-
)
216-
assert expected_msg in str(excinfo.value)
217-
218219

219220
def test_multiple_versions_to_bump(
220221
multiple_versions_to_update_poetry_lock, file_regression
@@ -285,7 +286,14 @@ def test_update_version_in_files_with_check_consistency_true_failure(
285286
version_files = [commitizen_config_file, inconsistent_python_version_file]
286287

287288
# This should fail because inconsistent_python_version_file doesn't contain the current version
288-
with pytest.raises(CurrentVersionNotFoundError) as excinfo:
289+
with pytest.raises(
290+
CurrentVersionNotFoundError,
291+
match=re.escape(
292+
f"Current version {old_version} is not found in {inconsistent_python_version_file}.\n"
293+
"The version defined in commitizen configuration and the ones in "
294+
"version_files are possibly inconsistent."
295+
),
296+
):
289297
bump.update_version_in_files(
290298
old_version,
291299
new_version,
@@ -294,13 +302,6 @@ def test_update_version_in_files_with_check_consistency_true_failure(
294302
encoding="utf-8",
295303
)
296304

297-
expected_msg = (
298-
f"Current version {old_version} is not found in {inconsistent_python_version_file}.\n"
299-
"The version defined in commitizen configuration and the ones in "
300-
"version_files are possibly inconsistent."
301-
)
302-
assert expected_msg in str(excinfo.value)
303-
304305

305306
@pytest.mark.parametrize(
306307
("encoding", "filename"),

tests/test_changelog.py

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

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

1234-
assert "Change types contain duplicated types" in str(excinfo)
1235-
12361236

12371237
def test_render_changelog(
12381238
gitcommits, tags, changelog_content, any_changelog_format: ChangelogFormat
@@ -1543,9 +1543,10 @@ def test_get_next_tag_name_after_version(tags):
15431543
assert last_tag_name is None
15441544

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

15501551

15511552
@dataclass

0 commit comments

Comments
 (0)