-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathtest_bump_command.py
790 lines (629 loc) · 25.3 KB
/
test_bump_command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
import inspect
import os
import re
import sys
from pathlib import Path
from typing import Tuple
from unittest.mock import MagicMock
import pytest
from pytest_mock import MockFixture
import commitizen.commands.bump as bump
from commitizen import cli, cmd, git
from commitizen.exceptions import (
BumpTagFailedError,
CommitizenException,
CurrentVersionNotFoundError,
DryRunExit,
ExitCode,
ExpectedExit,
InvalidManualVersion,
NoCommitsFoundError,
NoneIncrementExit,
NoPatternMapError,
NotAGitProjectError,
NotAllowed,
NoVersionSpecifiedError,
)
from tests.utils import create_file_and_commit, create_tag
@pytest.mark.parametrize(
"commit_msg",
(
"fix: username exception",
"fix(user): username exception",
"refactor: remove ini configuration support",
"refactor(config): remove ini configuration support",
"perf: update to use multiproess",
"perf(worker): update to use multiproess",
),
)
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_patch_increment(commit_msg, mocker: MockFixture):
create_file_and_commit(commit_msg)
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.1.1")
assert tag_exists is True
@pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file"))
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_minor_increment(commit_msg, mocker: MockFixture):
create_file_and_commit(commit_msg)
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.2.0")
cmd_res = cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"')
assert tag_exists is True and "commit:refs/tags/0.2.0\n" in cmd_res.out
@pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file"))
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_minor_increment_annotated(commit_msg, mocker: MockFixture):
create_file_and_commit(commit_msg)
testargs = ["cz", "bump", "--yes", "--annotated-tag"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.2.0")
cmd_res = cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"')
assert tag_exists is True and "tag:refs/tags/0.2.0\n" in cmd_res.out
_is_signed = git.is_signed_tag("0.2.0")
assert _is_signed is False
@pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file"))
@pytest.mark.usefixtures("tmp_commitizen_project_with_gpg")
def test_bump_minor_increment_signed(commit_msg, mocker: MockFixture):
create_file_and_commit(commit_msg)
testargs = ["cz", "bump", "--yes", "--gpg-sign"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.2.0")
cmd_res = cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"')
assert tag_exists is True and "tag:refs/tags/0.2.0\n" in cmd_res.out
_is_signed = git.is_signed_tag("0.2.0")
assert _is_signed is True
@pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file"))
def test_bump_minor_increment_annotated_config_file(
commit_msg, mocker: MockFixture, tmp_commitizen_project
):
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
tmp_commitizen_cfg_file.write(
f"{tmp_commitizen_cfg_file.read()}\n" f"annotated_tag = 1"
)
create_file_and_commit(commit_msg)
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.2.0")
cmd_res = cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"')
assert tag_exists is True and "tag:refs/tags/0.2.0\n" in cmd_res.out
_is_signed = git.is_signed_tag("0.2.0")
assert _is_signed is False
@pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file"))
def test_bump_minor_increment_signed_config_file(
commit_msg, mocker: MockFixture, tmp_commitizen_project_with_gpg
):
tmp_commitizen_cfg_file = tmp_commitizen_project_with_gpg.join("pyproject.toml")
tmp_commitizen_cfg_file.write(f"{tmp_commitizen_cfg_file.read()}\n" f"gpg_sign = 1")
create_file_and_commit(commit_msg)
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.2.0")
cmd_res = cmd.run('git for-each-ref refs/tags --format "%(objecttype):%(refname)"')
assert tag_exists is True and "tag:refs/tags/0.2.0\n" in cmd_res.out
_is_signed = git.is_signed_tag("0.2.0")
assert _is_signed is True
@pytest.mark.usefixtures("tmp_commitizen_project")
@pytest.mark.parametrize(
"commit_msg",
(
"feat: new user interface\n\nBREAKING CHANGE: age is no longer supported",
"feat!: new user interface\n\nBREAKING CHANGE: age is no longer supported",
"feat!: new user interface",
"feat(user): new user interface\n\nBREAKING CHANGE: age is no longer supported",
"feat(user)!: new user interface\n\nBREAKING CHANGE: age is no longer supported",
"feat(user)!: new user interface",
"BREAKING CHANGE: age is no longer supported",
"BREAKING-CHANGE: age is no longer supported",
),
)
def test_bump_major_increment(commit_msg, mocker: MockFixture):
create_file_and_commit(commit_msg)
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("1.0.0")
assert tag_exists is True
@pytest.mark.usefixtures("tmp_commitizen_project")
@pytest.mark.parametrize(
"commit_msg",
(
"feat: new user interface\n\nBREAKING CHANGE: age is no longer supported",
"feat!: new user interface\n\nBREAKING CHANGE: age is no longer supported",
"feat!: new user interface",
"feat(user): new user interface\n\nBREAKING CHANGE: age is no longer supported",
"feat(user)!: new user interface\n\nBREAKING CHANGE: age is no longer supported",
"feat(user)!: new user interface",
"BREAKING CHANGE: age is no longer supported",
"BREAKING-CHANGE: age is no longer supported",
),
)
def test_bump_major_increment_major_version_zero(commit_msg, mocker):
create_file_and_commit(commit_msg)
testargs = ["cz", "bump", "--yes", "--major-version-zero"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.2.0")
assert tag_exists is True
@pytest.mark.usefixtures("tmp_commitizen_project")
@pytest.mark.parametrize(
"commit_msg,increment,expected_tag",
[
("feat: new file", "PATCH", "0.1.1"),
("fix: username exception", "major", "1.0.0"),
("refactor: remove ini configuration support", "patch", "0.1.1"),
("BREAKING CHANGE: age is no longer supported", "minor", "0.2.0"),
],
)
def test_bump_command_increment_option(
commit_msg, increment, expected_tag, mocker: MockFixture
):
create_file_and_commit(commit_msg)
testargs = ["cz", "bump", "--increment", increment, "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist(expected_tag)
assert tag_exists is True
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_command_prelease(mocker: MockFixture):
# PRERELEASE
create_file_and_commit("feat: location")
testargs = ["cz", "bump", "--prerelease", "alpha", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.2.0a0")
assert tag_exists is True
# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
testargs = ["cz", "bump"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.2.0")
assert tag_exists is True
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_on_git_with_hooks_no_verify_disabled(mocker: MockFixture):
"""Bump commit without --no-verify"""
cmd.run("mkdir .git/hooks")
with open(".git/hooks/pre-commit", "w") as f:
f.write("#!/usr/bin/env bash\n" 'echo "0.1.0"')
cmd.run("chmod +x .git/hooks/pre-commit")
# MINOR
create_file_and_commit("feat: new file")
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.2.0")
assert tag_exists is True
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_tag_exists_raises_exception(mocker: MockFixture):
cmd.run("mkdir .git/hooks")
with open(".git/hooks/post-commit", "w") as f:
f.write("#!/usr/bin/env bash\n" "exit 9")
cmd.run("chmod +x .git/hooks/post-commit")
# MINOR
create_file_and_commit("feat: new file")
git.tag("0.2.0")
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
with pytest.raises(BumpTagFailedError) as excinfo:
cli.main()
assert "0.2.0" in str(excinfo.value) # This should be a fatal error
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_on_git_with_hooks_no_verify_enabled(mocker: MockFixture):
cmd.run("mkdir .git/hooks")
with open(".git/hooks/pre-commit", "w") as f:
f.write("#!/usr/bin/env bash\n" 'echo "0.1.0"')
cmd.run("chmod +x .git/hooks/pre-commit")
# MINOR
create_file_and_commit("feat: new file")
testargs = ["cz", "bump", "--yes", "--no-verify"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.2.0")
assert tag_exists is True
def test_bump_when_bumpping_is_not_support(mocker: MockFixture, tmp_commitizen_project):
create_file_and_commit(
"feat: new user interface\n\nBREAKING CHANGE: age is no longer supported"
)
testargs = ["cz", "-n", "cz_jira", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
with pytest.raises(NoPatternMapError) as excinfo:
cli.main()
assert "'cz_jira' rule does not support bump" in str(excinfo.value)
@pytest.mark.usefixtures("tmp_git_project")
def test_bump_when_version_is_not_specify(mocker: MockFixture):
mocker.patch.object(sys, "argv", ["cz", "bump"])
with pytest.raises(NoVersionSpecifiedError) as excinfo:
cli.main()
assert NoVersionSpecifiedError.message in str(excinfo.value)
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_when_no_new_commit(mocker: MockFixture):
"""bump without any commits since the last bump."""
# We need this first commit otherwise the revision is invalid.
create_file_and_commit("feat: initial")
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
# First bump.
# The next bump should fail since
# there is not a commit between the two bumps.
cli.main()
# bump without a new commit.
with pytest.raises(NoCommitsFoundError) as excinfo:
cli.main()
expected_error_message = "[NO_COMMITS_FOUND]\n" "No new commits found."
assert expected_error_message in str(excinfo.value)
def test_bump_when_version_inconsistent_in_version_files(
tmp_commitizen_project, mocker: MockFixture
):
tmp_version_file = tmp_commitizen_project.join("__version__.py")
tmp_version_file.write("100.999.10000")
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
tmp_commitizen_cfg_file.write(
f"{tmp_commitizen_cfg_file.read()}\n"
f'version_files = ["{tmp_version_file_string}"]'
)
create_file_and_commit("feat: new file")
testargs = ["cz", "bump", "--yes", "--check-consistency"]
mocker.patch.object(sys, "argv", testargs)
with pytest.raises(CurrentVersionNotFoundError) as excinfo:
cli.main()
partial_expected_error_message = "Current version 0.1.0 is not found in"
assert partial_expected_error_message in str(excinfo.value)
def test_bump_major_version_zero_when_major_is_not_zero(mocker, tmp_commitizen_project):
tmp_version_file = tmp_commitizen_project.join("__version__.py")
tmp_version_file.write("1.0.0")
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
tmp_commitizen_cfg_file.write(
f"[tool.commitizen]\n"
'version="1.0.0"\n'
f'version_files = ["{str(tmp_version_file_string)}"]'
)
tmp_changelog_file = tmp_commitizen_project.join("CHANGELOG.md")
tmp_changelog_file.write("## v1.0.0")
create_file_and_commit("feat(user): new file")
create_tag("v1.0.0")
create_file_and_commit("feat(user)!: new file")
testargs = ["cz", "bump", "--yes", "--major-version-zero"]
mocker.patch.object(sys, "argv", testargs)
with pytest.raises(NotAllowed) as excinfo:
cli.main()
expected_error_message = (
"--major-version-zero is meaningless for current version 1.0.0"
)
assert expected_error_message in str(excinfo.value)
def test_bump_shallow_clone(
tmp_commitizen_project,
tmp_path: Path,
mocker: MockFixture,
capsys: pytest.CaptureFixture,
):
# Set up repository with one released and one unreleased commit.
create_file_and_commit("feat: Add file for 0.1.0")
create_tag("0.1.0")
create_file_and_commit("feat: Add file for, but do not release 0.2.0")
# Shallow clone this local repository, so the release tag is missing.
cloned_repo = tmp_path / "cloned_repo"
# Use file:// URL, so Git does a shallow clone.
cmd.run(f"git clone --depth=1 file://{tmp_commitizen_project} {cloned_repo}")
os.chdir(cloned_repo)
# Bumping should work, but with a warning that we are pulling the tag from remote.
testargs = ["cz", "bump"]
mocker.patch.object(sys, "argv", testargs)
capsys.readouterr() # Reset capsys fixture
cli.main()
stdout, _stderr = capsys.readouterr()
assert re.search("Could not find tag.*trying to fetch", stdout)
def test_bump_files_only(mocker: MockFixture, tmp_commitizen_project):
tmp_version_file = tmp_commitizen_project.join("__version__.py")
tmp_version_file.write("0.1.0")
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
tmp_commitizen_cfg_file.write(
f"{tmp_commitizen_cfg_file.read()}\n"
f'version_files = ["{tmp_version_file_string}"]'
)
create_file_and_commit("feat: new user interface")
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.2.0")
assert tag_exists is True
create_file_and_commit("feat: another new feature")
testargs = ["cz", "bump", "--yes", "--files-only"]
mocker.patch.object(sys, "argv", testargs)
with pytest.raises(ExpectedExit):
cli.main()
tag_exists = git.tag_exist("0.3.0")
assert tag_exists is False
with open(tmp_version_file, "r") as f:
assert "0.3.0" in f.read()
with open(tmp_commitizen_cfg_file, "r") as f:
assert "0.3.0" in f.read()
def test_bump_local_version(mocker: MockFixture, tmp_commitizen_project):
tmp_version_file = tmp_commitizen_project.join("__version__.py")
tmp_version_file.write("4.5.1+0.1.0")
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
tmp_commitizen_cfg_file.write(
f"[tool.commitizen]\n"
'version="4.5.1+0.1.0"\n'
f'version_files = ["{tmp_version_file_string}"]'
)
create_file_and_commit("feat: new user interface")
testargs = ["cz", "bump", "--yes", "--local-version"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("4.5.1+0.2.0")
assert tag_exists is True
with open(tmp_version_file, "r") as f:
assert "4.5.1+0.2.0" in f.read()
def test_bump_dry_run(mocker: MockFixture, capsys, tmp_commitizen_project):
create_file_and_commit("feat: new file")
testargs = ["cz", "bump", "--yes", "--dry-run"]
mocker.patch.object(sys, "argv", testargs)
with pytest.raises(DryRunExit):
cli.main()
out, _ = capsys.readouterr()
assert "0.2.0" in out
tag_exists = git.tag_exist("0.2.0")
assert tag_exists is False
def test_bump_in_non_git_project(tmpdir, config, mocker: MockFixture):
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
with tmpdir.as_cwd():
with pytest.raises(NotAGitProjectError):
with pytest.raises(ExpectedExit):
cli.main()
def test_none_increment_exit_should_be_a_class():
assert inspect.isclass(NoneIncrementExit)
def test_none_increment_exit_should_be_expected_exit_subclass():
assert issubclass(NoneIncrementExit, CommitizenException)
def test_none_increment_exit_should_exist_in_bump():
assert hasattr(bump, "NoneIncrementExit")
def test_none_increment_exit_is_exception():
assert bump.NoneIncrementExit == NoneIncrementExit
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_none_increment_should_not_call_git_tag_and_error_code_is_not_zero(
mocker: MockFixture, tmp_commitizen_project
):
create_file_and_commit("test(test_get_all_droplets): fix bad comparison test")
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
# stash git.tag for later restore
stashed_git_tag = git.tag
dummy_value = git.tag("0.0.2")
git.tag = MagicMock(return_value=dummy_value)
with pytest.raises(NoneIncrementExit):
try:
cli.main()
except NoneIncrementExit as e:
git.tag.assert_not_called()
assert e.exit_code == ExitCode.NO_INCREMENT
raise e
# restore pop stashed
git.tag = stashed_git_tag
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_with_changelog_arg(mocker: MockFixture, changelog_path):
create_file_and_commit("feat(user): new file")
testargs = ["cz", "bump", "--yes", "--changelog"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.2.0")
assert tag_exists is True
with open(changelog_path, "r") as f:
out = f.read()
assert out.startswith("#")
assert "0.2.0" in out
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_with_changelog_config(mocker: MockFixture, changelog_path, config_path):
create_file_and_commit("feat(user): new file")
with open(config_path, "a") as fp:
fp.write("update_changelog_on_bump = true\n")
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("0.2.0")
assert tag_exists is True
with open(changelog_path, "r") as f:
out = f.read()
assert out.startswith("#")
assert "0.2.0" in out
def test_prevent_prerelease_when_no_increment_detected(
mocker: MockFixture, capsys, tmp_commitizen_project
):
create_file_and_commit("feat: new file")
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
out, _ = capsys.readouterr()
assert "0.2.0" in out
create_file_and_commit("test: new file")
testargs = ["cz", "bump", "-pr", "beta"]
mocker.patch.object(sys, "argv", testargs)
with pytest.raises(NoCommitsFoundError) as excinfo:
cli.main()
expected_error_message = (
"[NO_COMMITS_FOUND]\n" "No commits found to generate a pre-release."
)
assert expected_error_message in str(excinfo.value)
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_with_changelog_to_stdout_arg(mocker: MockFixture, capsys, changelog_path):
create_file_and_commit("feat(user): this should appear in stdout")
testargs = ["cz", "bump", "--yes", "--changelog-to-stdout"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
out, _ = capsys.readouterr()
assert "this should appear in stdout" in out
tag_exists = git.tag_exist("0.2.0")
assert tag_exists is True
with open(changelog_path, "r") as f:
out = f.read()
assert out.startswith("#")
assert "0.2.0" in out
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_with_changelog_to_stdout_dry_run_arg(
mocker: MockFixture, capsys, changelog_path
):
create_file_and_commit(
"feat(user): this should appear in stdout with dry-run enabled"
)
testargs = ["cz", "bump", "--yes", "--changelog-to-stdout", "--dry-run"]
mocker.patch.object(sys, "argv", testargs)
with pytest.raises(DryRunExit):
cli.main()
out, _ = capsys.readouterr()
tag_exists = git.tag_exist("0.2.0")
assert tag_exists is False
assert out.startswith("#")
assert "this should appear in stdout with dry-run enabled" in out
assert "0.2.0" in out
@pytest.mark.parametrize(
"version_filepath, version_regex, version_file_content",
[
pytest.param(
"pyproject.toml",
"pyproject.toml:^version",
"""
[tool.poetry]
name = "my_package"
version = "0.1.0"
""",
id="version in pyproject.toml with regex",
),
pytest.param(
"pyproject.toml",
"pyproject.toml",
"""
[tool.poetry]
name = "my_package"
version = "0.1.0"
""",
id="version in pyproject.toml without regex",
),
pytest.param(
"__init__.py",
"__init__.py:^__version__",
"""
'''This is a test file.'''
__version__ = "0.1.0"
""",
id="version in __init__.py with regex",
),
],
)
@pytest.mark.parametrize(
"cli_bump_changelog_args",
[
("cz", "bump", "--changelog", "--yes"),
(
"cz",
"bump",
"--changelog",
"--changelog-to-stdout",
"--annotated",
"--check-consistency",
"--yes",
),
],
ids=lambda cmd_tuple: " ".join(cmd_tuple),
)
def test_bump_changelog_command_commits_untracked_changelog_and_version_files(
tmp_commitizen_project,
mocker,
cli_bump_changelog_args: Tuple[str, ...],
version_filepath: str,
version_regex: str,
version_file_content: str,
):
"""Ensure that changelog always gets committed, no matter what version file or cli options get passed.
Steps:
- Append the version file's name and regex commitizen configuration lines to `pyproject.toml`.
- Append to or create the version file.
- Add a commit of type fix to be eligible for a version bump.
- Call commitizen main cli and assert that the `CHANGELOG.md` and the version file were committed.
"""
with tmp_commitizen_project.join("pyproject.toml").open(
mode="a"
) as commitizen_config:
commitizen_config.write(f"version_files = [\n" f"'{version_regex}'\n]")
with tmp_commitizen_project.join(version_filepath).open(mode="a+") as version_file:
version_file.write(version_file_content)
create_file_and_commit("fix: some test commit")
mocker.patch.object(sys, "argv", cli_bump_changelog_args)
cli.main()
commit_file_names = git.get_filenames_in_commit()
assert "CHANGELOG.md" in commit_file_names
assert version_filepath in commit_file_names
@pytest.mark.parametrize(
"testargs",
[
["cz", "bump", "--local-version", "1.2.3"],
["cz", "bump", "--prerelease", "rc", "1.2.3"],
["cz", "bump", "--devrelease", "0", "1.2.3"],
["cz", "bump", "--devrelease", "1", "1.2.3"],
["cz", "bump", "--increment", "PATCH", "1.2.3"],
],
)
def test_bump_invalid_manual_args_raises_exception(mocker, testargs):
mocker.patch.object(sys, "argv", testargs)
with pytest.raises(NotAllowed):
cli.main()
@pytest.mark.usefixtures("tmp_commitizen_project")
@pytest.mark.parametrize(
"manual_version",
[
"noversion",
"1.2..3",
],
)
def test_bump_invalid_manual_version_raises_exception(mocker, manual_version):
create_file_and_commit("feat: new file")
testargs = ["cz", "bump", "--yes", manual_version]
mocker.patch.object(sys, "argv", testargs)
with pytest.raises(InvalidManualVersion) as excinfo:
cli.main()
expected_error_message = (
"[INVALID_MANUAL_VERSION]\n" f"Invalid manual version: '{manual_version}'"
)
assert expected_error_message in str(excinfo.value)
@pytest.mark.usefixtures("tmp_commitizen_project")
@pytest.mark.parametrize(
"manual_version",
[
"0.0.1",
"0.1.0rc2",
"0.1.0.dev2",
"0.1.0+1.0.0",
"0.1.0rc2.dev2+1.0.0",
"0.1.1",
"0.2.0",
"1.0.0",
],
)
def test_bump_manual_version(mocker, manual_version):
create_file_and_commit("feat: new file")
testargs = ["cz", "bump", "--yes", manual_version]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist(manual_version)
assert tag_exists is True
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_manual_version_disallows_major_version_zero(mocker):
create_file_and_commit("feat: new file")
manual_version = "0.2.0"
testargs = ["cz", "bump", "--yes", "--major-version-zero", manual_version]
mocker.patch.object(sys, "argv", testargs)
with pytest.raises(NotAllowed) as excinfo:
cli.main()
expected_error_message = (
"--major-version-zero cannot be combined with MANUAL_VERSION"
)
assert expected_error_message in str(excinfo.value)