Skip to content

Commit 89e5732

Browse files
committed
test: remove unreachable code in pytest.raises block, fix some malformed tests and extract fixtures
1 parent 52d84c1 commit 89e5732

File tree

4 files changed

+147
-272
lines changed

4 files changed

+147
-272
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: 73 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import re
2020
from collections.abc import Mapping
2121

22-
from pytest_mock import MockFixture
22+
from pytest_mock import MockFixture, MockType
2323

2424
from commitizen.config.base_config import BaseConfig
2525
from commitizen.question import CzQuestion
@@ -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: MockType, 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,26 @@ 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(
182+
config, success_mock: MockType, mocker: MockFixture
183+
):
194184
mocker.patch(
195185
"commitizen.git.get_commits", return_value=_build_fake_git_commits(COMMIT_LOG)
196186
)
197187

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

205191

206192
def test_check_a_range_of_git_commits_and_failed(config, mocker: MockFixture):
207-
error_mock = mocker.patch("commitizen.out.error")
208193
mocker.patch(
209194
"commitizen.git.get_commits",
210195
return_value=_build_fake_git_commits(["This commit does not follow rule"]),
211196
)
212-
check_cmd = commands.Check(
213-
config=config, arguments={"rev_range": "HEAD~10..master"}
214-
)
215197

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

220202

221203
def test_check_command_with_invalid_argument(config):
@@ -234,123 +216,80 @@ def test_check_command_with_invalid_argument(config):
234216
def test_check_command_with_empty_range(config: BaseConfig, util: UtilFixture):
235217
# must initialize git with a commit
236218
util.create_file_and_commit("feat: initial")
237-
238-
check_cmd = commands.Check(config=config, arguments={"rev_range": "master..master"})
239219
with pytest.raises(NoCommitsFoundError) as excinfo:
240-
check_cmd()
241-
220+
commands.Check(config=config, arguments={"rev_range": "master..master"})()
242221
assert "No commit found with range: 'master..master'" in str(excinfo)
243222

244223

245224
def test_check_a_range_of_failed_git_commits(config, mocker: MockFixture):
246-
ill_formated_commits_msgs = [
225+
ill_formatted_commits_msgs = [
247226
"First commit does not follow rule",
248227
"Second commit does not follow rule",
249228
("Third commit does not follow rule\nIll-formatted commit with body"),
250229
]
251230
mocker.patch(
252231
"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"}
232+
return_value=_build_fake_git_commits(ill_formatted_commits_msgs),
257233
)
258234

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

263239

264-
def test_check_command_with_valid_message(config, mocker: MockFixture):
265-
success_mock = mocker.patch("commitizen.out.success")
266-
check_cmd = commands.Check(
240+
def test_check_command_with_valid_message(
241+
config, success_mock: MockType, mocker: MockFixture
242+
):
243+
commands.Check(
267244
config=config, arguments={"message": "fix(scope): some commit message"}
268-
)
269-
270-
check_cmd()
245+
)()
271246
success_mock.assert_called_once()
272247

273248

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-
249+
@pytest.mark.parametrize("message", ["bad commit", ""])
250+
def test_check_command_with_invalid_message(config, message):
278251
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-
287-
with pytest.raises(InvalidCommitMessageError):
288-
check_cmd()
289-
error_mock.assert_called_once()
290-
252+
commands.Check(config=config, arguments={"message": message})()
291253

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-
)
297254

298-
check_cmd()
255+
def test_check_command_with_allow_abort_arg(config, success_mock):
256+
commands.Check(config=config, arguments={"message": "", "allow_abort": True})()
299257
success_mock.assert_called_once()
300258

301259

302-
def test_check_command_with_allow_abort_config(config, mocker: MockFixture):
303-
success_mock = mocker.patch("commitizen.out.success")
260+
def test_check_command_with_allow_abort_config(config, success_mock):
304261
config.settings["allow_abort"] = True
305-
check_cmd = commands.Check(config=config, arguments={"message": ""})
306-
307-
check_cmd()
262+
commands.Check(config=config, arguments={"message": ""})()
308263
success_mock.assert_called_once()
309264

310265

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

322271

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(
272+
def test_check_command_with_allowed_prefixes_arg(config, success_mock):
273+
commands.Check(
326274
config=config,
327275
arguments={"message": "custom! test", "allowed_prefixes": ["custom!"]},
328-
)
329-
330-
check_cmd()
276+
)()
331277
success_mock.assert_called_once()
332278

333279

334-
def test_check_command_with_allowed_prefixes_config(config, mocker: MockFixture):
335-
success_mock = mocker.patch("commitizen.out.success")
280+
def test_check_command_with_allowed_prefixes_config(config, success_mock):
336281
config.settings["allowed_prefixes"] = ["custom!"]
337-
check_cmd = commands.Check(config=config, arguments={"message": "custom! test"})
338-
339-
check_cmd()
282+
commands.Check(config=config, arguments={"message": "custom! test"})()
340283
success_mock.assert_called_once()
341284

342285

343-
def test_check_command_override_allowed_prefixes_config(config, mocker: MockFixture):
344-
error_mock = mocker.patch("commitizen.out.error")
286+
def test_check_command_override_allowed_prefixes_config(config):
345287
config.settings["allow_abort"] = ["fixup!"]
346-
check_cmd = commands.Check(
347-
config=config,
348-
arguments={"message": "fixup! test", "allowed_prefixes": ["custom!"]},
349-
)
350-
351288
with pytest.raises(InvalidCommitMessageError):
352-
check_cmd()
353-
error_mock.assert_called_once()
289+
commands.Check(
290+
config=config,
291+
arguments={"message": "fixup! test", "allowed_prefixes": ["custom!"]},
292+
)()
354293

355294

356295
def test_check_command_with_pipe_message(
@@ -424,93 +363,64 @@ def test_check_conventional_commit_succeed_with_git_diff(
424363
assert "Commit validation: successful!" in out
425364

426365

427-
def test_check_command_with_message_length_limit(config, mocker: MockFixture):
428-
success_mock = mocker.patch("commitizen.out.success")
366+
def test_check_command_with_message_length_limit(config, success_mock):
429367
message = "fix(scope): some commit message"
430-
check_cmd = commands.Check(
368+
commands.Check(
431369
config=config,
432370
arguments={"message": message, "message_length_limit": len(message) + 1},
433-
)
434-
435-
check_cmd()
371+
)()
436372
success_mock.assert_called_once()
437373

438374

439-
def test_check_command_with_message_length_limit_exceeded(config, mocker: MockFixture):
440-
error_mock = mocker.patch("commitizen.out.error")
375+
def test_check_command_with_message_length_limit_exceeded(config):
441376
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-
447377
with pytest.raises(CommitMessageLengthExceededError):
448-
check_cmd()
449-
error_mock.assert_called_once()
450-
378+
commands.Check(
379+
config=config,
380+
arguments={"message": message, "message_length_limit": len(message) - 1},
381+
)()
451382

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"})
455383

456-
check_cmd()
384+
def test_check_command_with_amend_prefix_default(config, success_mock):
385+
commands.Check(config=config, arguments={"message": "amend! test"})()
457386
success_mock.assert_called_once()
458387

459388

460-
def test_check_command_with_config_message_length_limit(config, mocker: MockFixture):
461-
success_mock = mocker.patch("commitizen.out.success")
389+
def test_check_command_with_config_message_length_limit(config, success_mock):
462390
message = "fix(scope): some commit message"
463-
464391
config.settings["message_length_limit"] = len(message) + 1
465-
466-
check_cmd = commands.Check(
392+
commands.Check(
467393
config=config,
468394
arguments={"message": message},
469-
)
470-
471-
check_cmd()
395+
)()
472396
success_mock.assert_called_once()
473397

474398

475-
def test_check_command_with_config_message_length_limit_exceeded(
476-
config, mocker: MockFixture
477-
):
478-
error_mock = mocker.patch("commitizen.out.error")
399+
def test_check_command_with_config_message_length_limit_exceeded(config):
479400
message = "fix(scope): some commit message"
480-
481401
config.settings["message_length_limit"] = len(message) - 1
482-
483-
check_cmd = commands.Check(
484-
config=config,
485-
arguments={"message": message},
486-
)
487-
488402
with pytest.raises(CommitMessageLengthExceededError):
489-
check_cmd()
490-
error_mock.assert_called_once()
403+
commands.Check(
404+
config=config,
405+
arguments={"message": message},
406+
)()
491407

492408

493409
def test_check_command_cli_overrides_config_message_length_limit(
494-
config, mocker: MockFixture
410+
config, success_mock: MockType, mocker: MockFixture
495411
):
496-
success_mock = mocker.patch("commitizen.out.success")
497412
message = "fix(scope): some commit message"
498-
499413
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-
)
414+
for message_length_limit in [len(message) + 1, None]:
415+
success_mock.reset_mock()
416+
commands.Check(
417+
config=config,
418+
arguments={
419+
"message": message,
420+
"message_length_limit": message_length_limit,
421+
},
422+
)()
423+
success_mock.assert_called_once()
514424

515425

516426
class ValidationCz(BaseCommitizen):

0 commit comments

Comments
 (0)