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