Skip to content

Commit 1907280

Browse files
committed
test: remove unreachable code in pytest.raises block, fix some malformed tests
1 parent 52d84c1 commit 1907280

File tree

3 files changed

+57
-125
lines changed

3 files changed

+57
-125
lines changed

tests/commands/test_check_command.py

Lines changed: 48 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,12 @@ def test_check_conventional_commit_succeeds(
149149
),
150150
),
151151
)
152-
def test_check_no_conventional_commit(commit_msg, config, mocker: MockFixture, tmpdir):
153-
with pytest.raises(InvalidCommitMessageError):
154-
error_mock = mocker.patch("commitizen.out.error")
155-
156-
tempfile = tmpdir.join("temp_commit_file")
157-
tempfile.write(commit_msg)
152+
def test_check_no_conventional_commit(commit_msg, config, tmpdir):
153+
tempfile = tmpdir.join("temp_commit_file")
154+
tempfile.write(commit_msg)
158155

159-
check_cmd = commands.Check(
160-
config=config, arguments={"commit_msg_file": tempfile}
161-
)
162-
check_cmd()
163-
error_mock.assert_called_once()
156+
with pytest.raises(InvalidCommitMessageError):
157+
commands.Check(config=config, arguments={"commit_msg_file": tempfile})()
164158

165159

166160
@pytest.mark.parametrize(
@@ -204,18 +198,14 @@ def test_check_a_range_of_git_commits(config, mocker: MockFixture):
204198

205199

206200
def test_check_a_range_of_git_commits_and_failed(config, mocker: MockFixture):
207-
error_mock = mocker.patch("commitizen.out.error")
208201
mocker.patch(
209202
"commitizen.git.get_commits",
210203
return_value=_build_fake_git_commits(["This commit does not follow rule"]),
211204
)
212-
check_cmd = commands.Check(
213-
config=config, arguments={"rev_range": "HEAD~10..master"}
214-
)
215205

216-
with pytest.raises(InvalidCommitMessageError):
217-
check_cmd()
218-
error_mock.assert_called_once()
206+
with pytest.raises(InvalidCommitMessageError) as excinfo:
207+
commands.Check(config=config, arguments={"rev_range": "HEAD~10..master"})()
208+
assert "This commit does not follow rule" in str(excinfo.value)
219209

220210

221211
def test_check_command_with_invalid_argument(config):
@@ -271,86 +261,54 @@ def test_check_command_with_valid_message(config, mocker: MockFixture):
271261
success_mock.assert_called_once()
272262

273263

274-
def test_check_command_with_invalid_message(config, mocker: MockFixture):
275-
error_mock = mocker.patch("commitizen.out.error")
276-
check_cmd = commands.Check(config=config, arguments={"message": "bad commit"})
277-
278-
with pytest.raises(InvalidCommitMessageError):
279-
check_cmd()
280-
error_mock.assert_called_once()
281-
282-
283-
def test_check_command_with_empty_message(config, mocker: MockFixture):
284-
error_mock = mocker.patch("commitizen.out.error")
285-
check_cmd = commands.Check(config=config, arguments={"message": ""})
286-
264+
@pytest.mark.parametrize("message", ["bad commit", ""])
265+
def test_check_command_with_invalid_message(config, message):
287266
with pytest.raises(InvalidCommitMessageError):
288-
check_cmd()
289-
error_mock.assert_called_once()
267+
commands.Check(config=config, arguments={"message": message})()
290268

291269

292270
def test_check_command_with_allow_abort_arg(config, mocker: MockFixture):
293271
success_mock = mocker.patch("commitizen.out.success")
294-
check_cmd = commands.Check(
295-
config=config, arguments={"message": "", "allow_abort": True}
296-
)
297-
298-
check_cmd()
272+
commands.Check(config=config, arguments={"message": "", "allow_abort": True})()
299273
success_mock.assert_called_once()
300274

301275

302276
def test_check_command_with_allow_abort_config(config, mocker: MockFixture):
303277
success_mock = mocker.patch("commitizen.out.success")
304278
config.settings["allow_abort"] = True
305-
check_cmd = commands.Check(config=config, arguments={"message": ""})
306-
307-
check_cmd()
279+
commands.Check(config=config, arguments={"message": ""})()
308280
success_mock.assert_called_once()
309281

310282

311-
def test_check_command_override_allow_abort_config(config, mocker: MockFixture):
312-
error_mock = mocker.patch("commitizen.out.error")
283+
def test_check_command_override_allow_abort_config(config):
313284
config.settings["allow_abort"] = True
314-
check_cmd = commands.Check(
315-
config=config, arguments={"message": "", "allow_abort": False}
316-
)
317-
318285
with pytest.raises(InvalidCommitMessageError):
319-
check_cmd()
320-
error_mock.assert_called_once()
286+
commands.Check(config=config, arguments={"message": "", "allow_abort": False})()
321287

322288

323289
def test_check_command_with_allowed_prefixes_arg(config, mocker: MockFixture):
324290
success_mock = mocker.patch("commitizen.out.success")
325-
check_cmd = commands.Check(
291+
commands.Check(
326292
config=config,
327293
arguments={"message": "custom! test", "allowed_prefixes": ["custom!"]},
328-
)
329-
330-
check_cmd()
294+
)()
331295
success_mock.assert_called_once()
332296

333297

334298
def test_check_command_with_allowed_prefixes_config(config, mocker: MockFixture):
335299
success_mock = mocker.patch("commitizen.out.success")
336300
config.settings["allowed_prefixes"] = ["custom!"]
337-
check_cmd = commands.Check(config=config, arguments={"message": "custom! test"})
338-
339-
check_cmd()
301+
commands.Check(config=config, arguments={"message": "custom! test"})()
340302
success_mock.assert_called_once()
341303

342304

343-
def test_check_command_override_allowed_prefixes_config(config, mocker: MockFixture):
344-
error_mock = mocker.patch("commitizen.out.error")
305+
def test_check_command_override_allowed_prefixes_config(config):
345306
config.settings["allow_abort"] = ["fixup!"]
346-
check_cmd = commands.Check(
347-
config=config,
348-
arguments={"message": "fixup! test", "allowed_prefixes": ["custom!"]},
349-
)
350-
351307
with pytest.raises(InvalidCommitMessageError):
352-
check_cmd()
353-
error_mock.assert_called_once()
308+
commands.Check(
309+
config=config,
310+
arguments={"message": "fixup! test", "allowed_prefixes": ["custom!"]},
311+
)()
354312

355313

356314
def test_check_command_with_pipe_message(
@@ -427,33 +385,25 @@ def test_check_conventional_commit_succeed_with_git_diff(
427385
def test_check_command_with_message_length_limit(config, mocker: MockFixture):
428386
success_mock = mocker.patch("commitizen.out.success")
429387
message = "fix(scope): some commit message"
430-
check_cmd = commands.Check(
388+
commands.Check(
431389
config=config,
432390
arguments={"message": message, "message_length_limit": len(message) + 1},
433-
)
434-
435-
check_cmd()
391+
)()
436392
success_mock.assert_called_once()
437393

438394

439-
def test_check_command_with_message_length_limit_exceeded(config, mocker: MockFixture):
440-
error_mock = mocker.patch("commitizen.out.error")
395+
def test_check_command_with_message_length_limit_exceeded(config):
441396
message = "fix(scope): some commit message"
442-
check_cmd = commands.Check(
443-
config=config,
444-
arguments={"message": message, "message_length_limit": len(message) - 1},
445-
)
446-
447397
with pytest.raises(CommitMessageLengthExceededError):
448-
check_cmd()
449-
error_mock.assert_called_once()
398+
commands.Check(
399+
config=config,
400+
arguments={"message": message, "message_length_limit": len(message) - 1},
401+
)()
450402

451403

452404
def test_check_command_with_amend_prefix_default(config, mocker: MockFixture):
453405
success_mock = mocker.patch("commitizen.out.success")
454-
check_cmd = commands.Check(config=config, arguments={"message": "amend! test"})
455-
456-
check_cmd()
406+
commands.Check(config=config, arguments={"message": "amend! test"})()
457407
success_mock.assert_called_once()
458408

459409

@@ -463,54 +413,42 @@ def test_check_command_with_config_message_length_limit(config, mocker: MockFixt
463413

464414
config.settings["message_length_limit"] = len(message) + 1
465415

466-
check_cmd = commands.Check(
416+
commands.Check(
467417
config=config,
468418
arguments={"message": message},
469-
)
419+
)()
470420

471-
check_cmd()
472421
success_mock.assert_called_once()
473422

474423

475-
def test_check_command_with_config_message_length_limit_exceeded(
476-
config, mocker: MockFixture
477-
):
478-
error_mock = mocker.patch("commitizen.out.error")
424+
def test_check_command_with_config_message_length_limit_exceeded(config):
479425
message = "fix(scope): some commit message"
480426

481427
config.settings["message_length_limit"] = len(message) - 1
482428

483-
check_cmd = commands.Check(
484-
config=config,
485-
arguments={"message": message},
486-
)
487-
488429
with pytest.raises(CommitMessageLengthExceededError):
489-
check_cmd()
490-
error_mock.assert_called_once()
430+
commands.Check(
431+
config=config,
432+
arguments={"message": message},
433+
)()
491434

492435

493436
def test_check_command_cli_overrides_config_message_length_limit(
494437
config, mocker: MockFixture
495438
):
496-
success_mock = mocker.patch("commitizen.out.success")
497439
message = "fix(scope): some commit message"
498-
499440
config.settings["message_length_limit"] = len(message) - 1
500441

501-
check_cmd = commands.Check(
502-
config=config,
503-
arguments={"message": message, "message_length_limit": len(message) + 1},
504-
)
505-
506-
check_cmd()
507-
success_mock.assert_called_once()
508-
509-
success_mock.reset_mock()
510-
check_cmd = commands.Check(
511-
config=config,
512-
arguments={"message": message, "message_length_limit": None},
513-
)
442+
for message_length_limit in [len(message) + 1, None]:
443+
success_mock = mocker.patch("commitizen.out.success")
444+
commands.Check(
445+
config=config,
446+
arguments={
447+
"message": message,
448+
"message_length_limit": message_length_limit,
449+
},
450+
)()
451+
success_mock.assert_called_once()
514452

515453

516454
class ValidationCz(BaseCommitizen):

tests/commands/test_commit_command.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ def test_commit_backup_on_failure(config, mocker: MockFixture):
6969
commit_mock.return_value = cmd.Command("", "error", b"", b"", 9)
7070
error_mock = mocker.patch("commitizen.out.error")
7171

72+
commit_cmd = commands.Commit(config, {})
73+
temp_file = commit_cmd.backup_file_path
7274
with pytest.raises(CommitError):
73-
commit_cmd = commands.Commit(config, {})
74-
temp_file = commit_cmd.backup_file_path
7575
commit_cmd()
7676

7777
prompt_mock.assert_called_once()
@@ -191,8 +191,7 @@ def test_commit_command_with_dry_run_option(config, mocker: MockFixture):
191191
}
192192

193193
with pytest.raises(DryRunExit):
194-
commit_cmd = commands.Commit(config, {"dry_run": True})
195-
commit_cmd()
194+
commands.Commit(config, {"dry_run": True})()
196195

197196

198197
@pytest.mark.usefixtures("staging_is_clean")
@@ -236,8 +235,7 @@ def test_commit_command_with_invalid_write_message_to_file_option(
236235
}
237236

238237
with pytest.raises(NotAllowed):
239-
commit_cmd = commands.Commit(config, {"write_message_to_file": tmp_path})
240-
commit_cmd()
238+
commands.Commit(config, {"write_message_to_file": tmp_path})()
241239

242240

243241
@pytest.mark.usefixtures("staging_is_clean")
@@ -316,8 +314,7 @@ def test_commit_when_nothing_to_commit(config, mocker: MockFixture):
316314
is_staging_clean_mock.return_value = True
317315

318316
with pytest.raises(NothingToCommitError) as excinfo:
319-
commit_cmd = commands.Commit(config, {})
320-
commit_cmd()
317+
commands.Commit(config, {})()
321318

322319
assert "No files added to staging!" in str(excinfo.value)
323320

@@ -379,8 +376,7 @@ def test_commit_when_customized_expected_raised(config, mocker: MockFixture, cap
379376
prompt_mock.side_effect = _err
380377

381378
with pytest.raises(CustomError) as excinfo:
382-
commit_cmd = commands.Commit(config, {})
383-
commit_cmd()
379+
commands.Commit(config, {})()
384380

385381
# Assert only the content in the formatted text
386382
assert "This is the root custom err" in str(excinfo.value)
@@ -395,8 +391,7 @@ def test_commit_when_non_customized_expected_raised(
395391
prompt_mock.side_effect = _err
396392

397393
with pytest.raises(ValueError):
398-
commit_cmd = commands.Commit(config, {})
399-
commit_cmd()
394+
commands.Commit(config, {})()
400395

401396

402397
@pytest.mark.usefixtures("staging_is_clean")
@@ -405,8 +400,7 @@ def test_commit_when_no_user_answer(config, mocker: MockFixture, capsys):
405400
prompt_mock.return_value = None
406401

407402
with pytest.raises(NoAnswersError):
408-
commit_cmd = commands.Commit(config, {})
409-
commit_cmd()
403+
commands.Commit(config, {})()
410404

411405

412406
def test_commit_in_non_git_project(tmpdir, config):

tests/test_cz_customize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ def config_with_unicode(request):
376376

377377

378378
def test_initialize_cz_customize_failed():
379+
config = BaseConfig()
379380
with pytest.raises(MissingCzCustomizeConfigError) as excinfo:
380-
config = BaseConfig()
381381
_ = CustomizeCommitsCz(config)
382382

383383
assert MissingCzCustomizeConfigError.message in str(excinfo.value)

0 commit comments

Comments
 (0)