-
Notifications
You must be signed in to change notification settings - Fork 134
/
test_cli.py
2251 lines (1705 loc) · 72.6 KB
/
test_cli.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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import logging
import os
import platform
import warnings
import subprocess
from configparser import RawConfigParser
from datetime import datetime
from functools import partial
from shlex import split as shlex_split
from textwrap import dedent
from unittest import mock
import pytest
from testfixtures import LogCapture
import bumpversion
from bumpversion import exceptions
from bumpversion.cli import DESCRIPTION, main, split_args_in_optional_and_positional
def _get_subprocess_env():
env = os.environ.copy()
env['HGENCODING'] = 'utf-8'
return env
SUBPROCESS_ENV = _get_subprocess_env()
call = partial(subprocess.call, env=SUBPROCESS_ENV, shell=True)
check_call = partial(subprocess.check_call, env=SUBPROCESS_ENV)
check_output = partial(subprocess.check_output, env=SUBPROCESS_ENV)
xfail_if_no_git = pytest.mark.xfail(
call("git version") != 0,
reason="git is not installed"
)
xfail_if_no_hg = pytest.mark.xfail(
call("hg version") != 0,
reason="hg is not installed"
)
VCS_GIT = pytest.param("git", marks=xfail_if_no_git())
VCS_MERCURIAL = pytest.param("hg", marks=xfail_if_no_hg())
COMMIT = "[bumpversion]\ncommit = True"
COMMIT_NOT_TAG = "[bumpversion]\ncommit = True\ntag = False"
@pytest.fixture(params=[VCS_GIT, VCS_MERCURIAL])
def vcs(request):
"""Return all supported VCS systems (git, hg)."""
return request.param
@pytest.fixture(params=[VCS_GIT])
def git(request):
"""Return git as VCS (not hg)."""
return request.param
@pytest.fixture(params=['.bumpversion.cfg', 'setup.cfg'])
def configfile(request):
"""Return both config-file styles ('.bumpversion.cfg', 'setup.cfg')."""
return request.param
@pytest.fixture(params=[
"file",
"file(suffix)",
"file (suffix with space)",
"file (suffix lacking closing paren",
])
def file_keyword(request):
"""Return multiple possible styles for the bumpversion:file keyword."""
return request.param
try:
RawConfigParser(empty_lines_in_values=False)
using_old_configparser = False
except TypeError:
using_old_configparser = True
xfail_if_old_configparser = pytest.mark.xfail(
using_old_configparser,
reason="configparser doesn't support empty_lines_in_values"
)
def _mock_calls_to_string(called_mock):
return ["{}|{}|{}".format(
name,
args[0] if len(args) > 0 else args,
repr(kwargs) if len(kwargs) > 0 else ""
) for name, args, kwargs in called_mock.mock_calls]
EXPECTED_OPTIONS = r"""
[-h]
[--config-file FILE]
[--verbose]
[--list]
[--allow-dirty]
[--parse REGEX]
[--serialize FORMAT]
[--search SEARCH]
[--replace REPLACE]
[--current-version VERSION]
[--dry-run]
--new-version VERSION
[--commit | --no-commit]
[--tag | --no-tag]
[--sign-tags | --no-sign-tags]
[--tag-name TAG_NAME]
[--tag-message TAG_MESSAGE]
[--message COMMIT_MSG]
part
[file [file ...]]
""".strip().splitlines()
EXPECTED_USAGE = (r"""
%s
positional arguments:
part Part of the version to be bumped.
file Files to change (default: [])
optional arguments:
-h, --help show this help message and exit
--config-file FILE Config file to read most of the variables from
(default: .bumpversion.cfg)
--verbose Print verbose logging to stderr (default: 0)
--list List machine readable information (default: False)
--allow-dirty Don't abort if working directory is dirty (default:
False)
--parse REGEX Regex parsing the version string (default:
(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+))
--serialize FORMAT How to format what is parsed back to a version
(default: ['{major}.{minor}.{patch}'])
--search SEARCH Template for complete string to search (default:
{current_version})
--replace REPLACE Template for complete string to replace (default:
{new_version})
--current-version VERSION
Version that needs to be updated (default: None)
--dry-run, -n Don't write any files, just pretend. (default: False)
--new-version VERSION
New version that should be in the files (default:
None)
--commit Commit to version control (default: False)
--no-commit Do not commit to version control
--tag Create a tag in version control (default: False)
--no-tag Do not create a tag in version control
--sign-tags Sign tags if created (default: False)
--no-sign-tags Do not sign tags if created
--tag-name TAG_NAME Tag name (only works with --tag) (default:
v{new_version})
--tag-message TAG_MESSAGE
Tag message (default: Bump version: {current_version}
→ {new_version})
--message COMMIT_MSG, -m COMMIT_MSG
Commit message (default: Bump version:
{current_version} → {new_version})
""" % DESCRIPTION).lstrip()
def test_usage_string(tmpdir, capsys):
tmpdir.chdir()
with pytest.raises(SystemExit):
main(['--help'])
out, err = capsys.readouterr()
assert err == ""
for option_line in EXPECTED_OPTIONS:
assert option_line in out, "Usage string is missing {}".format(option_line)
assert EXPECTED_USAGE in out
def test_usage_string_fork(tmpdir):
tmpdir.chdir()
if platform.system() == "Windows":
# There are encoding problems on Windows with the encoding of →
tmpdir.join(".bumpversion.cfg").write(dedent("""
[bumpversion]
message: Bump version: {current_version} to {new_version}
tag_message: 'Bump version: {current_version} to {new_version}
"""))
try:
out = check_output('bumpversion --help', shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
out = e.output
if b'usage: bumpversion [-h]' not in out:
print(out)
assert b'usage: bumpversion [-h]' in out
def test_regression_help_in_work_dir(tmpdir, capsys, vcs):
tmpdir.chdir()
tmpdir.join("some_source.txt").write("1.7.2013")
check_call([vcs, "init"])
check_call([vcs, "add", "some_source.txt"])
check_call([vcs, "commit", "-m", "initial commit"])
check_call([vcs, "tag", "v1.7.2013"])
with pytest.raises(SystemExit):
main(['--help'])
out, err = capsys.readouterr()
for option_line in EXPECTED_OPTIONS:
assert option_line in out, "Usage string is missing {}".format(option_line)
if vcs == "git":
assert "Version that needs to be updated (default: 1.7.2013)" in out
else:
assert EXPECTED_USAGE in out
def test_defaults_in_usage_with_config(tmpdir, capsys):
tmpdir.chdir()
tmpdir.join("my_defaults.cfg").write("""[bumpversion]
current_version: 18
new_version: 19
[bumpversion:file:file1]
[bumpversion:file:file2]
[bumpversion:file:file3]""")
with pytest.raises(SystemExit):
main(['--config-file', 'my_defaults.cfg', '--help'])
out, err = capsys.readouterr()
assert "Version that needs to be updated (default: 18)" in out
assert "New version that should be in the files (default: 19)" in out
assert "[--current-version VERSION]" in out
assert "[--new-version VERSION]" in out
assert "[file [file ...]]" in out
def test_missing_explicit_config_file(tmpdir):
tmpdir.chdir()
with pytest.raises(argparse.ArgumentTypeError):
main(['--config-file', 'missing.cfg'])
def test_simple_replacement(tmpdir):
tmpdir.join("VERSION").write("1.2.0")
tmpdir.chdir()
main(shlex_split("patch --current-version 1.2.0 --new-version 1.2.1 VERSION"))
assert "1.2.1" == tmpdir.join("VERSION").read()
def test_simple_replacement_in_utf8_file(tmpdir):
tmpdir.join("VERSION").write("Kröt1.3.0".encode(), 'wb')
tmpdir.chdir()
out = tmpdir.join("VERSION").read('rb')
main(shlex_split("patch --verbose --current-version 1.3.0 --new-version 1.3.1 VERSION"))
out = tmpdir.join("VERSION").read('rb')
assert "'Kr\\xc3\\xb6t1.3.1'" in repr(out)
def test_config_file(tmpdir):
tmpdir.join("file1").write("0.9.34")
tmpdir.join("my_bump_config.cfg").write("""[bumpversion]
current_version: 0.9.34
new_version: 0.9.35
[bumpversion:file:file1]""")
tmpdir.chdir()
main(shlex_split("patch --config-file my_bump_config.cfg"))
assert "0.9.35" == tmpdir.join("file1").read()
def test_default_config_files(tmpdir, configfile):
tmpdir.join("file2").write("0.10.2")
tmpdir.join(configfile).write("""[bumpversion]
current_version: 0.10.2
new_version: 0.10.3
[bumpversion:file:file2]""")
tmpdir.chdir()
main(['patch'])
assert "0.10.3" == tmpdir.join("file2").read()
def test_glob_keyword(tmpdir, configfile):
tmpdir.join("file1.txt").write("0.9.34")
tmpdir.join("file2.txt").write("0.9.34")
tmpdir.join(configfile).write("""[bumpversion]
current_version: 0.9.34
new_version: 0.9.35
[bumpversion:glob:*.txt]""")
tmpdir.chdir()
main(["patch"])
assert "0.9.35" == tmpdir.join("file1.txt").read()
assert "0.9.35" == tmpdir.join("file2.txt").read()
def test_glob_keyword_recursive(tmpdir, configfile):
tmpdir.mkdir("subdir").mkdir("subdir2")
file1 = tmpdir.join("subdir").join("file1.txt")
file1.write("0.9.34")
file2 = tmpdir.join("subdir").join("subdir2").join("file2.txt")
file2.write("0.9.34")
tmpdir.join(configfile).write("""[bumpversion]
current_version: 0.9.34
new_version: 0.9.35
[bumpversion:glob:**/*.txt]""")
tmpdir.chdir()
main(["patch"])
assert "0.9.35" == file1.read()
assert "0.9.35" == file2.read()
def test_file_keyword_with_suffix_is_accepted(tmpdir, configfile, file_keyword):
tmpdir.join("file2").write("0.10.2")
tmpdir.join(configfile).write(
"""[bumpversion]
current_version: 0.10.2
new_version: 0.10.3
[bumpversion:%s:file2]
""" % file_keyword
)
tmpdir.chdir()
main(['patch'])
assert "0.10.3" == tmpdir.join("file2").read()
def test_multiple_config_files(tmpdir):
tmpdir.join("file2").write("0.10.2")
tmpdir.join("setup.cfg").write("""[bumpversion]
current_version: 0.10.2
new_version: 0.10.3
[bumpversion:file:file2]""")
tmpdir.join(".bumpversion.cfg").write("""[bumpversion]
current_version: 0.10.2
new_version: 0.10.4
[bumpversion:file:file2]""")
tmpdir.chdir()
main(['patch'])
assert "0.10.4" == tmpdir.join("file2").read()
def test_single_file_processed_twice(tmpdir):
"""
Verify that a single file "file2" can be processed twice.
Use two file_ entries, both with a different suffix after
the underscore.
Employ different parse/serialize and search/replace configs
to verify correct interpretation.
"""
tmpdir.join("file2").write("dots: 0.10.2\ndashes: 0-10-2")
tmpdir.join("setup.cfg").write("""[bumpversion]
current_version: 0.10.2
new_version: 0.10.3
[bumpversion:file:file2]""")
tmpdir.join(".bumpversion.cfg").write(r"""[bumpversion]
current_version: 0.10.2
new_version: 0.10.4
[bumpversion:file (version with dots):file2]
search = dots: {current_version}
replace = dots: {new_version}
[bumpversion:file (version with dashes):file2]
search = dashes: {current_version}
replace = dashes: {new_version}
parse = (?P<major>\d+)-(?P<minor>\d+)-(?P<patch>\d+)
serialize = {major}-{minor}-{patch}
""")
tmpdir.chdir()
main(['patch'])
assert "dots: 0.10.4\ndashes: 0-10-4" == tmpdir.join("file2").read()
def test_config_file_is_updated(tmpdir):
tmpdir.join("file3").write("0.0.13")
tmpdir.join(".bumpversion.cfg").write("""[bumpversion]
current_version: 0.0.13
new_version: 0.0.14
[bumpversion:file:file3]""")
tmpdir.chdir()
main(['patch', '--verbose'])
assert """[bumpversion]
current_version = 0.0.14
[bumpversion:file:file3]
""" == tmpdir.join(".bumpversion.cfg").read()
def test_dry_run(tmpdir, vcs):
tmpdir.chdir()
config = """[bumpversion]
current_version = 0.12.0
tag = True
commit = True
message = DO NOT BUMP VERSIONS WITH THIS FILE
[bumpversion:file:file4]
"""
version = "0.12.0"
tmpdir.join("file4").write(version)
tmpdir.join(".bumpversion.cfg").write(config)
check_call([vcs, "init"])
check_call([vcs, "add", "file4"])
check_call([vcs, "add", ".bumpversion.cfg"])
check_call([vcs, "commit", "-m", "initial commit"])
main(['patch', '--dry-run'])
assert config == tmpdir.join(".bumpversion.cfg").read()
assert version == tmpdir.join("file4").read()
vcs_log = check_output([vcs, "log"]).decode('utf-8')
assert "initial commit" in vcs_log
assert "DO NOT" not in vcs_log
def test_dry_run_verbose_log(tmpdir, vcs):
tmpdir.chdir()
version = "0.12.0"
patch = "0.12.1"
v_parts = version.split('.')
p_parts = patch.split('.')
file = "file4"
message = "DO NOT BUMP VERSIONS WITH THIS FILE"
config = """[bumpversion]
current_version = {version}
tag = True
commit = True
message = {message}
[bumpversion:file:{file}]
""".format(version=version, file=file, message=message)
bumpcfg = ".bumpversion.cfg"
tmpdir.join(file).write(version)
tmpdir.join(bumpcfg).write(config)
check_call([vcs, "init"])
check_call([vcs, "add", file])
check_call([vcs, "add", bumpcfg])
check_call([vcs, "commit", "-m", "initial commit"])
with LogCapture(level=logging.INFO) as log_capture:
main(['patch', '--dry-run', '--verbose'])
vcs_name = "Mercurial" if vcs == "hg" else "Git"
log_capture.check_present(
# generic --verbose entries
('bumpversion.cli', 'INFO', 'Reading config file {}:'.format(bumpcfg)),
('bumpversion.cli', 'INFO', config),
('bumpversion.version_part', 'INFO',
"Parsing version '{}' using regexp '(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)'".format(version)),
('bumpversion.version_part', 'INFO',
'Parsed the following values: major={}, minor={}, patch={}'.format(v_parts[0], v_parts[1], v_parts[2])),
('bumpversion.cli', 'INFO', "Attempting to increment part 'patch'"),
('bumpversion.cli', 'INFO',
'Values are now: major={}, minor={}, patch={}'.format(p_parts[0], p_parts[1], p_parts[2])),
('bumpversion.cli', 'INFO', "Dry run active, won't touch any files."), # only in dry-run mode
('bumpversion.version_part', 'INFO',
"Parsing version '{}' using regexp '(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)'".format(patch)),
('bumpversion.version_part', 'INFO',
'Parsed the following values: major={}, minor={}, patch={}'.format(p_parts[0], p_parts[1], p_parts[2])),
('bumpversion.cli', 'INFO', "New version will be '{}'".format(patch)),
('bumpversion.cli', 'INFO', 'Asserting files {} contain the version string...'.format(file)),
('bumpversion.utils', 'INFO', "Found '{v}' in {f} at line 0: {v}".format(v=version, f=file)), # verbose
('bumpversion.utils', 'INFO', 'Would change file {}:'.format(file)), # dry-run change to 'would'
('bumpversion.utils', 'INFO',
'--- a/{f}\n+++ b/{f}\n@@ -1 +1 @@\n-{v}\n+{p}'.format(f=file, v=version, p=patch)),
('bumpversion.list', 'INFO', 'current_version={}'.format(version)),
('bumpversion.list', 'INFO', 'tag=True'),
('bumpversion.list', 'INFO', 'commit=True'),
('bumpversion.list', 'INFO', 'message={}'.format(message)),
('bumpversion.list', 'INFO', 'new_version={}'.format(patch)),
('bumpversion.cli', 'INFO', 'Would write to config file {}:'.format(bumpcfg)), # dry-run 'would'
('bumpversion.cli', 'INFO', config.replace(version, patch)),
# following entries are only present if both --verbose and --dry-run are specified
# all entries use 'would do x' variants instead of 'doing x'
('bumpversion.cli', 'INFO', 'Would prepare {vcs} commit'.format(vcs=vcs_name)),
('bumpversion.cli', 'INFO', "Would add changes in file '{file}' to {vcs}".format(file=file, vcs=vcs_name)),
('bumpversion.cli', 'INFO', "Would add changes in file '{file}' to {vcs}".format(file=bumpcfg, vcs=vcs_name)),
('bumpversion.cli', 'INFO', "Would commit to {vcs} with message '{msg}'".format(msg=message, vcs=vcs_name)),
('bumpversion.cli', 'INFO',
"Would tag 'v{p}' with message 'Bump version: {v} → {p}' in {vcs} and not signing"
.format(v=version, p=patch, vcs=vcs_name)),
order_matters=True
)
def test_bump_version(tmpdir):
tmpdir.join("file5").write("1.0.0")
tmpdir.chdir()
main(['patch', '--current-version', '1.0.0', 'file5'])
assert '1.0.1' == tmpdir.join("file5").read()
def test_bump_version_custom_main(tmpdir):
tmpdir.join("file6").write("XXX1;0;0")
tmpdir.chdir()
main([
'--current-version', 'XXX1;0;0',
'--parse', r'XXX(?P<spam>\d+);(?P<blob>\d+);(?P<slurp>\d+)',
'--serialize', 'XXX{spam};{blob};{slurp}',
'blob',
'file6'
])
assert 'XXX1;1;0' == tmpdir.join("file6").read()
def test_bump_version_custom_parse_serialize_configfile(tmpdir):
tmpdir.join("file12").write("ZZZ8;0;0")
tmpdir.chdir()
tmpdir.join(".bumpversion.cfg").write(r"""[bumpversion]
current_version = ZZZ8;0;0
serialize = ZZZ{spam};{blob};{slurp}
parse = ZZZ(?P<spam>\d+);(?P<blob>\d+);(?P<slurp>\d+)
[bumpversion:file:file12]
""")
main(['blob'])
assert 'ZZZ8;1;0' == tmpdir.join("file12").read()
def test_bumpversion_custom_parse_semver(tmpdir):
tmpdir.join("file15").write("XXX1.1.7-master+allan1")
tmpdir.chdir()
main([
'--current-version', '1.1.7-master+allan1',
'--parse', r'(?P<major>\d+).(?P<minor>\d+).(?P<patch>\d+)(-(?P<pre_release>[^\+]+))?(\+(?P<meta>.*))?',
'--serialize', '{major}.{minor}.{patch}-{pre_release}+{meta}',
'meta',
'file15'
])
assert 'XXX1.1.7-master+allan2' == tmpdir.join("file15").read()
def test_dirty_work_dir(tmpdir, vcs):
tmpdir.chdir()
check_call([vcs, "init"])
tmpdir.join("dirty").write("i'm dirty")
check_call([vcs, "add", "dirty"])
vcs_name = "Mercurial" if vcs == "hg" else "Git"
vcs_output = "A dirty" if vcs == "hg" else "A dirty"
with pytest.raises(exceptions.WorkingDirectoryIsDirtyException):
with LogCapture() as log_capture:
main(['patch', '--current-version', '1', '--new-version', '2', 'file7'])
log_capture.check_present(
(
'bumpversion.cli',
'WARNING',
"{} working directory is not clean:\n"
"{}\n"
"\n"
"Use --allow-dirty to override this if you know what you're doing.".format(
vcs_name,
vcs_output
)
)
)
def test_force_dirty_work_dir(tmpdir, vcs):
tmpdir.chdir()
check_call([vcs, "init"])
tmpdir.join("dirty2").write("i'm dirty! 1.1.1")
check_call([vcs, "add", "dirty2"])
main([
'patch',
'--allow-dirty',
'--current-version',
'1.1.1',
'dirty2'
])
assert "i'm dirty! 1.1.2" == tmpdir.join("dirty2").read()
def test_bump_major(tmpdir):
tmpdir.join("fileMAJORBUMP").write("4.2.8")
tmpdir.chdir()
main(['--current-version', '4.2.8', 'major', 'fileMAJORBUMP'])
assert '5.0.0' == tmpdir.join("fileMAJORBUMP").read()
def test_commit_and_tag(tmpdir, vcs):
tmpdir.chdir()
check_call([vcs, "init"])
tmpdir.join("VERSION").write("47.1.1")
check_call([vcs, "add", "VERSION"])
check_call([vcs, "commit", "-m", "initial commit"])
main(['patch', '--current-version', '47.1.1', '--commit', 'VERSION'])
assert '47.1.2' == tmpdir.join("VERSION").read()
log = check_output([vcs, "log", "-p"]).decode("utf-8")
assert '-47.1.1' in log
assert '+47.1.2' in log
assert 'Bump version: 47.1.1 → 47.1.2' in log
tag_out = check_output([vcs, {"git": "tag", "hg": "tags"}[vcs]])
assert b'v47.1.2' not in tag_out
main(['patch', '--current-version', '47.1.2', '--commit', '--tag', 'VERSION'])
assert '47.1.3' == tmpdir.join("VERSION").read()
check_output([vcs, "log", "-p"])
tag_out = check_output([vcs, {"git": "tag", "hg": "tags"}[vcs]])
assert b'v47.1.3' in tag_out
def test_commit_and_tag_with_configfile(tmpdir, vcs):
tmpdir.chdir()
tmpdir.join(".bumpversion.cfg").write("""[bumpversion]\ncommit = True\ntag = True""")
check_call([vcs, "init"])
tmpdir.join("VERSION").write("48.1.1")
check_call([vcs, "add", "VERSION"])
check_call([vcs, "commit", "-m", "initial commit"])
main(['patch', '--current-version', '48.1.1', '--no-tag', 'VERSION'])
assert '48.1.2' == tmpdir.join("VERSION").read()
log = check_output([vcs, "log", "-p"]).decode("utf-8")
assert '-48.1.1' in log
assert '+48.1.2' in log
assert 'Bump version: 48.1.1 → 48.1.2' in log
tag_out = check_output([vcs, {"git": "tag", "hg": "tags"}[vcs]])
assert b'v48.1.2' not in tag_out
main(['patch', '--current-version', '48.1.2', 'VERSION'])
assert '48.1.3' == tmpdir.join("VERSION").read()
check_output([vcs, "log", "-p"])
tag_out = check_output([vcs, {"git": "tag", "hg": "tags"}[vcs]])
assert b'v48.1.3' in tag_out
@pytest.mark.parametrize("config", [COMMIT, COMMIT_NOT_TAG])
def test_commit_and_not_tag_with_configfile(tmpdir, vcs, config):
tmpdir.chdir()
tmpdir.join(".bumpversion.cfg").write(config)
check_call([vcs, "init"])
tmpdir.join("VERSION").write("48.1.1")
check_call([vcs, "add", "VERSION"])
check_call([vcs, "commit", "-m", "initial commit"])
main(['patch', '--current-version', '48.1.1', 'VERSION'])
assert '48.1.2' == tmpdir.join("VERSION").read()
log = check_output([vcs, "log", "-p"]).decode("utf-8")
assert '-48.1.1' in log
assert '+48.1.2' in log
assert 'Bump version: 48.1.1 → 48.1.2' in log
tag_out = check_output([vcs, {"git": "tag", "hg": "tags"}[vcs]])
assert b'v48.1.2' not in tag_out
def test_commit_explicitly_false(tmpdir, vcs):
tmpdir.chdir()
tmpdir.join(".bumpversion.cfg").write("""[bumpversion]
current_version: 10.0.0
commit = False
tag = False""")
check_call([vcs, "init"])
tmpdir.join("tracked_file").write("10.0.0")
check_call([vcs, "add", "tracked_file"])
check_call([vcs, "commit", "-m", "initial commit"])
main(['patch', 'tracked_file'])
assert '10.0.1' == tmpdir.join("tracked_file").read()
log = check_output([vcs, "log", "-p"]).decode("utf-8")
assert "10.0.1" not in log
diff = check_output([vcs, "diff"]).decode("utf-8")
assert "10.0.1" in diff
def test_commit_configfile_true_cli_false_override(tmpdir, vcs):
tmpdir.chdir()
tmpdir.join(".bumpversion.cfg").write("""[bumpversion]
current_version: 27.0.0
commit = True""")
check_call([vcs, "init"])
tmpdir.join("dont_commit_file").write("27.0.0")
check_call([vcs, "add", "dont_commit_file"])
check_call([vcs, "commit", "-m", "initial commit"])
main(['patch', '--no-commit', 'dont_commit_file'])
assert '27.0.1' == tmpdir.join("dont_commit_file").read()
log = check_output([vcs, "log", "-p"]).decode("utf-8")
assert "27.0.1" not in log
diff = check_output([vcs, "diff"]).decode("utf-8")
assert "27.0.1" in diff
def test_bump_version_environment(tmpdir):
tmpdir.join("on_jenkins").write("2.3.4")
tmpdir.chdir()
os.environ['BUILD_NUMBER'] = "567"
main([
'--verbose',
'--current-version', '2.3.4',
'--parse', r'(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+).*',
'--serialize', '{major}.{minor}.{patch}.pre{$BUILD_NUMBER}',
'patch',
'on_jenkins',
])
del os.environ['BUILD_NUMBER']
assert '2.3.5.pre567' == tmpdir.join("on_jenkins").read()
def test_current_version_from_tag(tmpdir, git):
# prepare
tmpdir.join("update_from_tag").write("26.6.0")
tmpdir.chdir()
check_call([git, "init"])
check_call([git, "add", "update_from_tag"])
check_call([git, "commit", "-m", "initial"])
check_call([git, "tag", "v26.6.0"])
# don't give current-version, that should come from tag
main(['patch', 'update_from_tag'])
assert '26.6.1' == tmpdir.join("update_from_tag").read()
def test_current_version_from_tag_written_to_config_file(tmpdir, git):
# prepare
tmpdir.join("updated_also_in_config_file").write("14.6.0")
tmpdir.chdir()
tmpdir.join(".bumpversion.cfg").write("""[bumpversion]""")
check_call([git, "init"])
check_call([git, "add", "updated_also_in_config_file"])
check_call([git, "commit", "-m", "initial"])
check_call([git, "tag", "v14.6.0"])
# don't give current-version, that should come from tag
main([
'patch',
'updated_also_in_config_file',
'--commit',
'--tag',
])
assert '14.6.1' == tmpdir.join("updated_also_in_config_file").read()
assert '14.6.1' in tmpdir.join(".bumpversion.cfg").read()
def test_distance_to_latest_tag_as_part_of_new_version(tmpdir, git):
# prepare
tmpdir.join("my_source_file").write("19.6.0")
tmpdir.chdir()
check_call([git, "init"])
check_call([git, "add", "my_source_file"])
check_call([git, "commit", "-m", "initial"])
check_call([git, "tag", "v19.6.0"])
check_call([git, "commit", "--allow-empty", "-m", "Just a commit 1"])
check_call([git, "commit", "--allow-empty", "-m", "Just a commit 2"])
check_call([git, "commit", "--allow-empty", "-m", "Just a commit 3"])
# don't give current-version, that should come from tag
main([
'patch',
'--parse', r'(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+).*',
'--serialize', '{major}.{minor}.{patch}-pre{distance_to_latest_tag}',
'my_source_file',
])
assert '19.6.1-pre3' == tmpdir.join("my_source_file").read()
def test_override_vcs_current_version(tmpdir, git):
# prepare
tmpdir.join("contains_actual_version").write("6.7.8")
tmpdir.chdir()
check_call([git, "init"])
check_call([git, "add", "contains_actual_version"])
check_call([git, "commit", "-m", "initial"])
check_call([git, "tag", "v6.7.8"])
# update file
tmpdir.join("contains_actual_version").write("7.0.0")
check_call([git, "add", "contains_actual_version"])
# but forgot to tag or forgot to push --tags
check_call([git, "commit", "-m", "major release"])
# if we don't give current-version here we get
# "AssertionError: Did not find string 6.7.8 in file contains_actual_version"
main(['patch', '--current-version', '7.0.0', 'contains_actual_version'])
assert '7.0.1' == tmpdir.join("contains_actual_version").read()
def test_non_existing_file(tmpdir):
tmpdir.chdir()
with pytest.raises(IOError):
main(shlex_split("patch --current-version 1.2.0 --new-version 1.2.1 does_not_exist.txt"))
def test_non_existing_second_file(tmpdir):
tmpdir.chdir()
tmpdir.join("my_source_code.txt").write("1.2.3")
with pytest.raises(IOError):
main(shlex_split("patch --current-version 1.2.3 my_source_code.txt does_not_exist2.txt"))
# first file is unchanged because second didn't exist
assert '1.2.3' == tmpdir.join("my_source_code.txt").read()
def test_read_version_tags_only(tmpdir, git):
# prepare
tmpdir.join("update_from_tag").write("29.6.0")
tmpdir.chdir()
check_call([git, "init"])
check_call([git, "add", "update_from_tag"])
check_call([git, "commit", "-m", "initial"])
check_call([git, "tag", "v29.6.0"])
check_call([git, "commit", "--allow-empty", "-m", "a commit"])
check_call([git, "tag", "jenkins-deploy-my-project-2"])
# don't give current-version, that should come from tag
main(['patch', 'update_from_tag'])
assert '29.6.1' == tmpdir.join("update_from_tag").read()
def test_tag_name(tmpdir, vcs):
tmpdir.chdir()
check_call([vcs, "init"])
tmpdir.join("VERSION").write("31.1.1")
check_call([vcs, "add", "VERSION"])
check_call([vcs, "commit", "-m", "initial commit"])
main([
'patch', '--current-version', '31.1.1', '--commit', '--tag',
'VERSION', '--tag-name', 'ReleasedVersion-{new_version}'
])
tag_out = check_output([vcs, {"git": "tag", "hg": "tags"}[vcs]])
assert b'ReleasedVersion-31.1.2' in tag_out
def test_message_from_config_file(tmpdir, vcs):
tmpdir.chdir()
check_call([vcs, "init"])
tmpdir.join("VERSION").write("400.0.0")
check_call([vcs, "add", "VERSION"])
check_call([vcs, "commit", "-m", "initial commit"])
tmpdir.join(".bumpversion.cfg").write("""[bumpversion]
current_version: 400.0.0
new_version: 401.0.0
commit: True
tag: True
message: {current_version} was old, {new_version} is new
tag_name: from-{current_version}-to-{new_version}""")
main(['major', 'VERSION'])
log = check_output([vcs, "log", "-p"])
assert b'400.0.0 was old, 401.0.0 is new' in log
tag_out = check_output([vcs, {"git": "tag", "hg": "tags"}[vcs]])
assert b'from-400.0.0-to-401.0.0' in tag_out
def test_all_parts_in_message_and_serialize_and_tag_name_from_config_file(tmpdir, vcs):
"""
Ensure that major/minor/patch *and* custom parts can be used everywhere.
- As [part] in 'serialize'.
- As new_[part] and previous_[part] in 'message'.
- As new_[part] and previous_[part] in 'tag_name'.
In message and tag_name, also ensure that new_version and
current_version are correct.
"""
tmpdir.chdir()
check_call([vcs, "init"])
tmpdir.join("VERSION").write("400.1.2.101")
check_call([vcs, "add", "VERSION"])
check_call([vcs, "commit", "-m", "initial commit"])
tmpdir.join(".bumpversion.cfg").write(r"""[bumpversion]
current_version: 400.1.2.101
new_version: 401.2.3.102
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+).(?P<custom>\d+)
serialize = {major}.{minor}.{patch}.{custom}
commit: True
tag: True
message: {current_version}/{current_major}.{current_minor}.{current_patch} custom {current_custom} becomes {new_version}/{new_major}.{new_minor}.{new_patch} custom {new_custom}
tag_name: from-{current_version}-aka-{current_major}.{current_minor}.{current_patch}-custom-{current_custom}-to-{new_version}-aka-{new_major}.{new_minor}.{new_patch}-custom-{new_custom}
[bumpversion:part:custom]
""")
main(['major', 'VERSION'])
log = check_output([vcs, "log", "-p"])
assert b'400.1.2.101/400.1.2 custom 101 becomes 401.2.3.102/401.2.3 custom 102' in log
tag_out = check_output([vcs, {"git": "tag", "hg": "tags"}[vcs]])
assert b'from-400.1.2.101-aka-400.1.2-custom-101-to-401.2.3.102-aka-401.2.3-custom-102' in tag_out
def test_all_parts_in_replace_from_config_file(tmpdir, vcs):
"""
Ensure that major/minor/patch *and* custom parts can be used in 'replace'.
"""
tmpdir.chdir()
check_call([vcs, "init"])
tmpdir.join("VERSION").write("my version is 400.1.2.101\n")
check_call([vcs, "add", "VERSION"])
check_call([vcs, "commit", "-m", "initial commit"])
tmpdir.join(".bumpversion.cfg").write(r"""[bumpversion]
current_version: 400.1.2.101
new_version: 401.2.3.102
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+).(?P<custom>\d+)
serialize = {major}.{minor}.{patch}.{custom}
commit: True
tag: False
[bumpversion:part:custom]
[bumpversion:VERSION]
search = my version is {current_version}
replace = my version is {new_major}.{new_minor}.{new_patch}.{new_custom}""")