Skip to content

Commit 66a240f

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

File tree

4 files changed

+109
-209
lines changed

4 files changed

+109
-209
lines changed

tests/commands/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
import pytest
4+
from pytest_mock import MockerFixture
45

56
from commitizen import defaults
67
from commitizen.config import BaseConfig
@@ -52,3 +53,8 @@ def changelog_path() -> str:
5253
@pytest.fixture()
5354
def config_path() -> str:
5455
return os.path.join(os.getcwd(), "pyproject.toml")
56+
57+
58+
@pytest.fixture()
59+
def success_mock(mocker: MockerFixture):
60+
return mocker.patch("commitizen.out.success")

tests/commands/test_check_command.py

Lines changed: 68 additions & 162 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(
@@ -172,15 +166,10 @@ def test_check_no_conventional_commit(commit_msg, config, mocker: MockFixture, t
172166
"bump: 0.0.1 -> 1.0.0",
173167
),
174168
)
175-
def test_check_conventional_commit(commit_msg, config, mocker: MockFixture, tmpdir):
176-
success_mock = mocker.patch("commitizen.out.success")
177-
169+
def test_check_conventional_commit(commit_msg, config, success_mock, tmpdir):
178170
tempfile = tmpdir.join("temp_commit_file")
179171
tempfile.write(commit_msg)
180-
181-
check_cmd = commands.Check(config=config, arguments={"commit_msg_file": tempfile})
182-
183-
check_cmd()
172+
commands.Check(config=config, arguments={"commit_msg_file": tempfile})()
184173
success_mock.assert_called_once()
185174

186175

@@ -189,33 +178,24 @@ def test_check_command_when_commit_file_not_found(config):
189178
commands.Check(config=config, arguments={"commit_msg_file": "no_such_file"})()
190179

191180

192-
def test_check_a_range_of_git_commits(config, mocker: MockFixture):
193-
success_mock = mocker.patch("commitizen.out.success")
181+
def test_check_a_range_of_git_commits(config, success_mock, mocker: MockFixture):
194182
mocker.patch(
195183
"commitizen.git.get_commits", return_value=_build_fake_git_commits(COMMIT_LOG)
196184
)
197185

198-
check_cmd = commands.Check(
199-
config=config, arguments={"rev_range": "HEAD~10..master"}
200-
)
201-
202-
check_cmd()
186+
commands.Check(config=config, arguments={"rev_range": "HEAD~10..master"})()
203187
success_mock.assert_called_once()
204188

205189

206190
def test_check_a_range_of_git_commits_and_failed(config, mocker: MockFixture):
207-
error_mock = mocker.patch("commitizen.out.error")
208191
mocker.patch(
209192
"commitizen.git.get_commits",
210193
return_value=_build_fake_git_commits(["This commit does not follow rule"]),
211194
)
212-
check_cmd = commands.Check(
213-
config=config, arguments={"rev_range": "HEAD~10..master"}
214-
)
215195

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

220200

221201
def test_check_command_with_invalid_argument(config):
@@ -234,123 +214,78 @@ def test_check_command_with_invalid_argument(config):
234214
def test_check_command_with_empty_range(config: BaseConfig, util: UtilFixture):
235215
# must initialize git with a commit
236216
util.create_file_and_commit("feat: initial")
237-
238-
check_cmd = commands.Check(config=config, arguments={"rev_range": "master..master"})
239217
with pytest.raises(NoCommitsFoundError) as excinfo:
240-
check_cmd()
241-
218+
commands.Check(config=config, arguments={"rev_range": "master..master"})()
242219
assert "No commit found with range: 'master..master'" in str(excinfo)
243220

244221

245222
def test_check_a_range_of_failed_git_commits(config, mocker: MockFixture):
246-
ill_formated_commits_msgs = [
223+
ill_formatted_commits_msgs = [
247224
"First commit does not follow rule",
248225
"Second commit does not follow rule",
249226
("Third commit does not follow rule\nIll-formatted commit with body"),
250227
]
251228
mocker.patch(
252229
"commitizen.git.get_commits",
253-
return_value=_build_fake_git_commits(ill_formated_commits_msgs),
254-
)
255-
check_cmd = commands.Check(
256-
config=config, arguments={"rev_range": "HEAD~10..master"}
230+
return_value=_build_fake_git_commits(ill_formatted_commits_msgs),
257231
)
258232

259233
with pytest.raises(InvalidCommitMessageError) as excinfo:
260-
check_cmd()
261-
assert all([msg in str(excinfo.value) for msg in ill_formated_commits_msgs])
234+
commands.Check(config=config, arguments={"rev_range": "HEAD~10..master"})()
235+
assert all([msg in str(excinfo.value) for msg in ill_formatted_commits_msgs])
262236

263237

264-
def test_check_command_with_valid_message(config, mocker: MockFixture):
265-
success_mock = mocker.patch("commitizen.out.success")
266-
check_cmd = commands.Check(
238+
def test_check_command_with_valid_message(config, success_mock, mocker: MockFixture):
239+
commands.Check(
267240
config=config, arguments={"message": "fix(scope): some commit message"}
268-
)
269-
270-
check_cmd()
241+
)()
271242
success_mock.assert_called_once()
272243

273244

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-
245+
@pytest.mark.parametrize("message", ["bad commit", ""])
246+
def test_check_command_with_invalid_message(config, message):
278247
with pytest.raises(InvalidCommitMessageError):
279-
check_cmd()
280-
error_mock.assert_called_once()
281-
248+
commands.Check(config=config, arguments={"message": message})()
282249

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-
287-
with pytest.raises(InvalidCommitMessageError):
288-
check_cmd()
289-
error_mock.assert_called_once()
290250

291-
292-
def test_check_command_with_allow_abort_arg(config, mocker: MockFixture):
293-
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()
251+
def test_check_command_with_allow_abort_arg(config, success_mock):
252+
commands.Check(config=config, arguments={"message": "", "allow_abort": True})()
299253
success_mock.assert_called_once()
300254

301255

302-
def test_check_command_with_allow_abort_config(config, mocker: MockFixture):
303-
success_mock = mocker.patch("commitizen.out.success")
256+
def test_check_command_with_allow_abort_config(config, success_mock):
304257
config.settings["allow_abort"] = True
305-
check_cmd = commands.Check(config=config, arguments={"message": ""})
306-
307-
check_cmd()
258+
commands.Check(config=config, arguments={"message": ""})()
308259
success_mock.assert_called_once()
309260

310261

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

322267

323-
def test_check_command_with_allowed_prefixes_arg(config, mocker: MockFixture):
324-
success_mock = mocker.patch("commitizen.out.success")
325-
check_cmd = commands.Check(
268+
def test_check_command_with_allowed_prefixes_arg(config, success_mock):
269+
commands.Check(
326270
config=config,
327271
arguments={"message": "custom! test", "allowed_prefixes": ["custom!"]},
328-
)
329-
330-
check_cmd()
272+
)()
331273
success_mock.assert_called_once()
332274

333275

334-
def test_check_command_with_allowed_prefixes_config(config, mocker: MockFixture):
335-
success_mock = mocker.patch("commitizen.out.success")
276+
def test_check_command_with_allowed_prefixes_config(config, success_mock):
336277
config.settings["allowed_prefixes"] = ["custom!"]
337-
check_cmd = commands.Check(config=config, arguments={"message": "custom! test"})
338-
339-
check_cmd()
278+
commands.Check(config=config, arguments={"message": "custom! test"})()
340279
success_mock.assert_called_once()
341280

342281

343-
def test_check_command_override_allowed_prefixes_config(config, mocker: MockFixture):
344-
error_mock = mocker.patch("commitizen.out.error")
282+
def test_check_command_override_allowed_prefixes_config(config):
345283
config.settings["allow_abort"] = ["fixup!"]
346-
check_cmd = commands.Check(
347-
config=config,
348-
arguments={"message": "fixup! test", "allowed_prefixes": ["custom!"]},
349-
)
350-
351284
with pytest.raises(InvalidCommitMessageError):
352-
check_cmd()
353-
error_mock.assert_called_once()
285+
commands.Check(
286+
config=config,
287+
arguments={"message": "fixup! test", "allowed_prefixes": ["custom!"]},
288+
)()
354289

355290

356291
def test_check_command_with_pipe_message(
@@ -424,93 +359,64 @@ def test_check_conventional_commit_succeed_with_git_diff(
424359
assert "Commit validation: successful!" in out
425360

426361

427-
def test_check_command_with_message_length_limit(config, mocker: MockFixture):
428-
success_mock = mocker.patch("commitizen.out.success")
362+
def test_check_command_with_message_length_limit(config, success_mock):
429363
message = "fix(scope): some commit message"
430-
check_cmd = commands.Check(
364+
commands.Check(
431365
config=config,
432366
arguments={"message": message, "message_length_limit": len(message) + 1},
433-
)
434-
435-
check_cmd()
367+
)()
436368
success_mock.assert_called_once()
437369

438370

439-
def test_check_command_with_message_length_limit_exceeded(config, mocker: MockFixture):
440-
error_mock = mocker.patch("commitizen.out.error")
371+
def test_check_command_with_message_length_limit_exceeded(config):
441372
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-
447373
with pytest.raises(CommitMessageLengthExceededError):
448-
check_cmd()
449-
error_mock.assert_called_once()
450-
374+
commands.Check(
375+
config=config,
376+
arguments={"message": message, "message_length_limit": len(message) - 1},
377+
)()
451378

452-
def test_check_command_with_amend_prefix_default(config, mocker: MockFixture):
453-
success_mock = mocker.patch("commitizen.out.success")
454-
check_cmd = commands.Check(config=config, arguments={"message": "amend! test"})
455379

456-
check_cmd()
380+
def test_check_command_with_amend_prefix_default(config, success_mock):
381+
commands.Check(config=config, arguments={"message": "amend! test"})()
457382
success_mock.assert_called_once()
458383

459384

460-
def test_check_command_with_config_message_length_limit(config, mocker: MockFixture):
461-
success_mock = mocker.patch("commitizen.out.success")
385+
def test_check_command_with_config_message_length_limit(config, success_mock):
462386
message = "fix(scope): some commit message"
463-
464387
config.settings["message_length_limit"] = len(message) + 1
465-
466-
check_cmd = commands.Check(
388+
commands.Check(
467389
config=config,
468390
arguments={"message": message},
469-
)
470-
471-
check_cmd()
391+
)()
472392
success_mock.assert_called_once()
473393

474394

475-
def test_check_command_with_config_message_length_limit_exceeded(
476-
config, mocker: MockFixture
477-
):
478-
error_mock = mocker.patch("commitizen.out.error")
395+
def test_check_command_with_config_message_length_limit_exceeded(config):
479396
message = "fix(scope): some commit message"
480-
481397
config.settings["message_length_limit"] = len(message) - 1
482-
483-
check_cmd = commands.Check(
484-
config=config,
485-
arguments={"message": message},
486-
)
487-
488398
with pytest.raises(CommitMessageLengthExceededError):
489-
check_cmd()
490-
error_mock.assert_called_once()
399+
commands.Check(
400+
config=config,
401+
arguments={"message": message},
402+
)()
491403

492404

493405
def test_check_command_cli_overrides_config_message_length_limit(
494-
config, mocker: MockFixture
406+
config, success_mock, mocker: MockFixture
495407
):
496-
success_mock = mocker.patch("commitizen.out.success")
497408
message = "fix(scope): some commit message"
498-
499409
config.settings["message_length_limit"] = len(message) - 1
500-
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-
)
410+
for message_length_limit in [len(message) + 1, None]:
411+
success_mock.reset_mock()
412+
commands.Check(
413+
config=config,
414+
arguments={
415+
"message": message,
416+
"message_length_limit": message_length_limit,
417+
},
418+
)()
419+
success_mock.assert_called_once()
514420

515421

516422
class ValidationCz(BaseCommitizen):

0 commit comments

Comments
 (0)