Debian Codegraph @

tests/commands/test_version_command.py

raw
1 import platform
2 import sys
3
4 import pytest
5 from pytest_mock import MockerFixture
6
7 from commitizen import commands, git
8 from commitizen.__version__ import __version__
9 from commitizen.config.base_config import BaseConfig
10 from commitizen.cz.base import BaseCommitizen
11 from commitizen.cz.conventional_commits import ConventionalCommitsCz
12 from commitizen.exceptions import (
13 NoCommitsFoundError,
14 NoPatternMapError,
15 NotAGitProjectError,
16 )
17 from tests.utils import UtilFixture
18
19
20 def test_version_for_showing_project_version_error(config, capsys):
21 # No version specified in config
22 commands.Version(
23 config,
24 {"project": True},
25 )()
26 captured = capsys.readouterr()
27 assert "No project information in this project." in captured.err
28
29
30 def test_version_for_showing_project_version(config, capsys):
31 config.settings["version"] = "0.0.1"
32 commands.Version(
33 config,
34 {"project": True},
35 )()
36 captured = capsys.readouterr()
37 assert "0.0.1" in captured.out
38
39
40 @pytest.mark.parametrize("project", [True, False])
41 def test_version_for_showing_commitizen_version(config, capsys, project: bool):
42 commands.Version(
43 config,
44 {"project": project, "commitizen": True},
45 )()
46 captured = capsys.readouterr()
47 assert f"{__version__}" in captured.out
48
49
50 def test_version_for_showing_both_versions_no_project(config, capsys):
51 commands.Version(
52 config,
53 {"verbose": True},
54 )()
55 captured = capsys.readouterr()
56 assert f"Installed Commitizen Version: {__version__}" in captured.out
57 assert "No project information in this project." in captured.err
58
59
60 def test_version_for_showing_both_versions(config, capsys):
61 config.settings["version"] = "0.0.1"
62 commands.Version(
63 config,
64 {"verbose": True},
65 )()
66 captured = capsys.readouterr()
67 expected_out = (
68 f"Installed Commitizen Version: {__version__}\nProject Version: 0.0.1"
69 )
70 assert expected_out in captured.out
71
72
73 def test_version_for_showing_commitizen_system_info(config, capsys):
74 commands.Version(
75 config,
76 {"report": True},
77 )()
78 captured = capsys.readouterr()
79 assert f"Commitizen Version: {__version__}" in captured.out
80 assert f"Python Version: {sys.version}" in captured.out
81 assert f"Operating System: {platform.system()}" in captured.out
82
83
84 @pytest.mark.parametrize("project", [True, False])
85 @pytest.mark.usefixtures("tmp_git_project")
86 def test_version_use_version_provider(
87 mocker: MockerFixture,
88 config: BaseConfig,
89 capsys: pytest.CaptureFixture,
90 project: bool,
91 ):
92 version = "0.0.0"
93 mock = mocker.MagicMock(name="provider")
94 mock.get_version.return_value = version
95 get_provider = mocker.patch(
96 "commitizen.commands.version.get_provider", return_value=mock
97 )
98
99 commands.Version(
100 config,
101 {
102 "project": project,
103 "verbose": not project,
104 },
105 )()
106 captured = capsys.readouterr()
107
108 assert version in captured.out
109 get_provider.assert_called_once()
110 mock.get_version.assert_called_once()
111 mock.set_version.assert_not_called()
112
113
114 @pytest.mark.parametrize(
115 ("version", "expected_version"),
116 [
117 ("1.0.0", "1\n"),
118 ("2.1.3", "2\n"),
119 ("0.0.1", "0\n"),
120 ("0.1.0", "0\n"),
121 ],
122 )
123 def test_version_just_major(config, capsys, version: str, expected_version: str):
124 config.settings["version"] = version
125 commands.Version(
126 config,
127 {
128 "project": True,
129 "major": True,
130 },
131 )()
132 captured = capsys.readouterr()
133 assert expected_version == captured.out
134
135
136 @pytest.mark.parametrize(
137 ("version", "expected_version"),
138 [
139 ("1.0.0", "0\n"),
140 ("2.1.3", "1\n"),
141 ("0.0.1", "0\n"),
142 ("0.1.0", "1\n"),
143 ],
144 )
145 def test_version_just_minor(config, capsys, version: str, expected_version: str):
146 config.settings["version"] = version
147 commands.Version(
148 config,
149 {
150 "project": True,
151 "minor": True,
152 },
153 )()
154 captured = capsys.readouterr()
155 assert expected_version == captured.out
156
157
158 @pytest.mark.parametrize(
159 ("args", "expected_error"),
160 [
161 (
162 {"major": True},
163 "can only be used with MANUAL_VERSION, --project or --verbose.",
164 ),
165 (
166 {"minor": True},
167 "can only be used with MANUAL_VERSION, --project or --verbose.",
168 ),
169 (
170 {"patch": True},
171 "can only be used with MANUAL_VERSION, --project or --verbose.",
172 ),
173 ({"tag": True}, "Tag can only be used with --project or --verbose."),
174 ],
175 )
176 def test_version_invalid_combinations(config, capsys, args: dict, expected_error: str):
177 """Test that certain flag combinations produce errors."""
178 commands.Version(config, args)() # type: ignore[arg-type]
179 captured = capsys.readouterr()
180 assert not captured.out
181 assert expected_error in captured.err
182
183
184 @pytest.mark.parametrize(
185 ("version", "tag_format", "expected_output"),
186 [
187 ("1.2.3", "v$version", "v1.2.3\n"),
188 ("1.2.3", "$version", "1.2.3\n"),
189 ("2.0.0", "release-$version", "release-2.0.0\n"),
190 ("0.1.0", "ver$version", "ver0.1.0\n"),
191 ],
192 )
193 def test_version_with_tag_format(
194 config, capsys, version: str, tag_format: str, expected_output: str
195 ):
196 """Test --tag option applies tag_format to version"""
197 config.settings["version"] = version
198 config.settings["tag_format"] = tag_format
199 commands.Version(
200 config,
201 {
202 "project": True,
203 "tag": True,
204 },
205 )()
206 captured = capsys.readouterr()
207 assert captured.out == expected_output
208
209
210 @pytest.mark.parametrize(
211 ("next_increment", "current_version", "expected_version"),
212 [
213 ("MAJOR", "1.1.0", "2.0.0"),
214 ("MAJOR", "1.0.0", "2.0.0"),
215 ("MAJOR", "0.0.1", "1.0.0"),
216 ("MINOR", "1.1.0", "1.2.0"),
217 ("MINOR", "1.0.0", "1.1.0"),
218 ("MINOR", "0.0.1", "0.1.0"),
219 ("PATCH", "1.1.0", "1.1.1"),
220 ("PATCH", "1.0.0", "1.0.1"),
221 ("PATCH", "0.0.1", "0.0.2"),
222 ("NONE", "1.0.0", "1.0.0"),
223 ],
224 )
225 def test_next_version(
226 config, capsys, next_increment: str, current_version: str, expected_version: str
227 ):
228 config.settings["version"] = current_version
229 for project in (True, False):
230 commands.Version(
231 config,
232 {
233 "next": next_increment,
234 "project": project,
235 },
236 )()
237 captured = capsys.readouterr()
238 assert expected_version in captured.out
239
240 # Use the same settings to test the manual version
241 commands.Version(
242 config,
243 {
244 "manual_version": current_version,
245 "next": next_increment,
246 },
247 )()
248 captured = capsys.readouterr()
249 assert expected_version in captured.out
250
251
252 def test_next_version_invalid_version(config, capsys):
253 commands.Version(
254 config,
255 {
256 "manual_version": "INVALID",
257 },
258 )()
259 captured = capsys.readouterr()
260 assert "Invalid version: 'INVALID'" in captured.err
261
262
263 @pytest.mark.parametrize(
264 ("version", "expected_version"),
265 [
266 ("1.0.0", "0\n"),
267 ("2.1.3", "3\n"),
268 ("0.0.1", "1\n"),
269 ("0.1.0", "0\n"),
270 ],
271 )
272 def test_version_just_patch(config, capsys, version: str, expected_version: str):
273 config.settings["version"] = version
274 commands.Version(
275 config,
276 {
277 "project": True,
278 "patch": True,
279 },
280 )()
281 captured = capsys.readouterr()
282 assert expected_version == captured.out
283
284
285 def test_version_unknown_scheme(config, capsys):
286 config.settings["version"] = "1.0.0"
287 config.settings["version_scheme"] = "not_a_registered_scheme_name_xyz"
288 commands.Version(config, {"project": True})()
289 captured = capsys.readouterr()
290 assert "Unknown version scheme." in captured.err
291
292
293 @pytest.mark.parametrize(
294 ("commit_message", "expected_version"),
295 [
296 ("feat: new feature", "1.1.0"),
297 ("fix: a bug", "1.0.1"),
298 ("feat!: breaking change", "2.0.0"),
299 ("docs: update readme", "1.0.0"),
300 ],
301 )
302 @pytest.mark.usefixtures("tmp_git_project")
303 def test_version_next_use_git_commits(
304 config: BaseConfig,
305 capsys: pytest.CaptureFixture,
306 util: UtilFixture,
307 commit_message: str,
308 expected_version: str,
309 ):
310 """USE_GIT_COMMITS derives the next version from commits since the last tag."""
311 config.settings["version"] = "1.0.0"
312 util.create_file_and_commit("feat: initial commit")
313 util.create_tag("1.0.0")
314 util.create_file_and_commit(commit_message)
315
316 commands.Version(
317 config,
318 {"project": True, "next": "USE_GIT_COMMITS"},
319 )()
320
321 captured = capsys.readouterr()
322 assert captured.out == f"{expected_version}\n"
323
324
325 @pytest.mark.usefixtures("tmp_git_project")
326 def test_version_next_use_git_commits_filters_before_finding_increment(
327 config: BaseConfig,
328 capsys: pytest.CaptureFixture,
329 util: UtilFixture,
330 mocker: MockerFixture,
331 ):
332 """Commit-derived versions use the same filtering hook as bump."""
333
334 def filter_app_a_commits(
335 self: ConventionalCommitsCz, commits: list[git.GitCommit]
336 ) -> list[git.GitCommit]:
337 """Keep commits whose full message declares AppA."""
338 return [
339 commit
340 for commit in commits
341 if "'AppA'" in commit.message.partition("Applications:")[2]
342 ]
343
344 mocker.patch.object(
345 ConventionalCommitsCz,
346 "filter_commits_before_bump",
347 filter_app_a_commits,
348 )
349 config.settings["version"] = "1.0.0"
350 util.create_file_and_commit("feat: initial commit")
351 util.create_tag("1.0.0")
352 util.create_file_and_commit(
353 "feat: add AppB feature\n\nApplications: ['AppB']",
354 filename="app-b",
355 )
356 util.create_file_and_commit(
357 "fix: correct shared behavior\n\nApplications: ['AppA', 'AppB']",
358 filename="app-a",
359 )
360
361 commands.Version(
362 config,
363 {"project": True, "next": "USE_GIT_COMMITS"},
364 )()
365
366 captured = capsys.readouterr()
367 assert captured.out == "1.0.1\n"
368
369
370 @pytest.mark.usefixtures("tmp_git_project")
371 def test_version_next_use_git_commits_manual_version(
372 config: BaseConfig, capsys: pytest.CaptureFixture, util: UtilFixture
373 ):
374 """USE_GIT_COMMITS also works with an explicit MANUAL_VERSION."""
375 util.create_file_and_commit("feat: initial commit")
376 util.create_tag("1.0.0")
377 util.create_file_and_commit("feat: new feature")
378
379 commands.Version(
380 config,
381 {"manual_version": "1.0.0", "next": "USE_GIT_COMMITS"},
382 )()
383
384 captured = capsys.readouterr()
385 assert captured.out == "1.1.0\n"
386
387
388 @pytest.mark.usefixtures("tmp_git_project")
389 def test_version_next_use_git_commits_without_matching_tag(
390 config: BaseConfig, capsys: pytest.CaptureFixture, util: UtilFixture
391 ):
392 """When no tag matches the current version, all commits are considered."""
393 config.settings["version"] = "2.0.0"
394 util.create_file_and_commit("feat: initial commit")
395 util.create_file_and_commit("feat: new feature")
396
397 commands.Version(
398 config,
399 {"project": True, "next": "USE_GIT_COMMITS"},
400 )()
401
402 captured = capsys.readouterr()
403 assert captured.out == "2.1.0\n"
404
405
406 @pytest.mark.usefixtures("tmp_git_project")
407 def test_version_next_use_git_commits_major_version_zero(
408 config: BaseConfig, capsys: pytest.CaptureFixture, util: UtilFixture
409 ):
410 """major_version_zero uses the zero bump map so no major bump is emitted."""
411 config.settings["version"] = "0.1.0"
412 config.settings["major_version_zero"] = True
413 util.create_file_and_commit("feat: initial commit")
414 util.create_tag("0.1.0")
415 util.create_file_and_commit("feat!: breaking change")
416
417 commands.Version(
418 config,
419 {"project": True, "next": "USE_GIT_COMMITS"},
420 )()
421
422 captured = capsys.readouterr()
423 assert captured.out == "0.2.0\n"
424
425
426 @pytest.mark.usefixtures("tmp_git_project")
427 def test_version_next_use_git_commits_prerelease_without_commits(
428 config: BaseConfig, capsys: pytest.CaptureFixture, util: UtilFixture
429 ):
430 """A prerelease with no new commits finalizes into its release version."""
431 config.settings["version"] = "1.0.0rc1"
432 util.create_file_and_commit("feat: initial commit")
433 util.create_tag("1.0.0rc1")
434
435 commands.Version(
436 config,
437 {"project": True, "next": "USE_GIT_COMMITS"},
438 )()
439
440 captured = capsys.readouterr()
441 assert captured.out == "1.0.0\n"
442
443
444 @pytest.mark.usefixtures("tmp_git_project")
445 def test_version_next_use_git_commits_no_commits_raises(
446 config: BaseConfig, util: UtilFixture
447 ):
448 """No new commits since the last (non-prerelease) tag raises an error."""
449 config.settings["version"] = "1.0.0"
450 util.create_file_and_commit("feat: initial commit")
451 util.create_tag("1.0.0")
452
453 with pytest.raises(NoCommitsFoundError):
454 commands.Version(
455 config,
456 {"project": True, "next": "USE_GIT_COMMITS"},
457 )()
458
459
460 @pytest.mark.usefixtures("chdir")
461 def test_version_next_use_git_commits_outside_git_project_raises(
462 config: BaseConfig,
463 ):
464 """USE_GIT_COMMITS outside a git repository raises NotAGitProjectError."""
465 config.settings["version"] = "1.0.0"
466
467 with pytest.raises(NotAGitProjectError):
468 commands.Version(
469 config,
470 {"project": True, "next": "USE_GIT_COMMITS"},
471 )()
472
473
474 class _NoBumpRulesCz(BaseCommitizen):
475 """A commitizen rule without bump pattern or map to trigger NoPatternMapError."""
476
477 bump_pattern = None
478 bump_map = None
479
480 def questions(self):
481 return []
482
483 def message(self, answers):
484 return ""
485
486 def example(self) -> str:
487 return ""
488
489 def schema(self) -> str:
490 return ""
491
492 def schema_pattern(self) -> str:
493 return ""
494
495 def info(self) -> str:
496 return ""
497
498
499 @pytest.mark.usefixtures("tmp_git_project")
500 def test_version_next_use_git_commits_no_pattern_map_raises(
501 config: BaseConfig, util: UtilFixture, mocker: MockerFixture
502 ):
503 """A rule that does not support bumping raises NoPatternMapError."""
504 config.settings["version"] = "1.0.0"
505 mocker.patch(
506 "commitizen.factory.committer_factory",
507 return_value=_NoBumpRulesCz(config),
508 )
509 util.create_file_and_commit("feat: initial commit")
510 util.create_tag("1.0.0")
511 util.create_file_and_commit("feat: new feature")
512
513 with pytest.raises(NoPatternMapError):
514 commands.Version(
515 config,
516 {"project": True, "next": "USE_GIT_COMMITS"},
517 )()
518
519
520 def test_version_no_arguments_shows_commitizen_version(config, capsys):
521 commands.Version(config, {})()
522 captured = capsys.readouterr()
523 assert captured.out.strip() == __version__