1- import re
1+ from shutil import copyfile
22
33import pytest
44
55from commitizen import bump
66from commitizen .exceptions import CurrentVersionNotFoundError
77
8- PYPROJECT = """
9- [tool.poetry]
10- name = "commitizen"
11- version = "1.2.3"
12- """
13-
14- VERSION_PY = """
15- __title__ = 'requests'
16- __description__ = 'Python HTTP for Humans.'
17- __url__ = 'http://python-requests.org'
18- __version__ = '1.2.3'
19- """
20-
21- INCONSISTENT_VERSION_PY = """
22- __title__ = 'requests'
23- __description__ = 'Python HTTP for Humans.'
24- __url__ = 'http://python-requests.org'
25- __version__ = '2.10.3'
26- """
27-
28- REPEATED_VERSION_NUMBER = """
29- {
30- "name": "magictool",
31- "version": "1.2.3",
32- "dependencies": {
33- "lodash": "1.2.3"
34- }
35- }
36- """
37-
38- # The order cannot be guaranteed here
39- CARGO_LOCK = """
40- [[package]]
41- name = "textwrap"
42- version = "1.2.3"
43-
44- [[package]]
45- name = "there-i-fixed-it"
46- version = "1.2.3"
47-
48- [[package]]
49- name = "other-project"
50- version = "1.2.3"
51- """
52-
53- DOCKER_COMPOSE = """
54- version: "3.3"
55-
56- services:
57- app:
58- image: my-repo/my-container:v1.2.3
59- command: my-command
60- """
61-
62- MULTIPLE_VERSIONS_TO_UPDATE_POETRY_LOCK = """
63- [[package]]
64- name = "to-update-1"
65- version = "1.2.9"
66-
67- [[package]]
68- name = "not-to-update"
69- version = "1.3.3"
70-
71- [[package]]
72- name = "not-to-update"
73- version = "1.3.3"
74-
75- [[package]]
76- name = "not-to-update"
77- version = "1.3.3"
78-
79- [[package]]
80- name = "not-to-update"
81- version = "1.3.3"
82-
83- [[package]]
84- name = "not-to-update"
85- version = "1.3.3"
86-
87- [[package]]
88- name = "to-update-2"
89- version = "1.2.9"
90- """
91-
928MULTIPLE_VERSIONS_INCREASE_STRING = 'version = "1.2.9"\n ' * 30
939MULTIPLE_VERSIONS_REDUCE_STRING = 'version = "1.2.10"\n ' * 30
9410
11+ TESTING_FILE_PREFIX = "tests/testing_version_files"
12+
9513
9614@pytest .fixture (scope = "function" )
9715def commitizen_config_file (tmpdir ):
9816 tmp_file = tmpdir .join ("pyproject.toml" )
99- tmp_file . write ( PYPROJECT )
17+ copyfile ( f" { TESTING_FILE_PREFIX } /sample_pyproject.toml" , str ( tmp_file ) )
10018 return str (tmp_file )
10119
10220
10321@pytest .fixture (scope = "function" )
10422def python_version_file (tmpdir ):
10523 tmp_file = tmpdir .join ("__verion__.py" )
106- tmp_file . write ( VERSION_PY )
24+ copyfile ( f" { TESTING_FILE_PREFIX } /sample_version.py" , str ( tmp_file ) )
10725 return str (tmp_file )
10826
10927
11028@pytest .fixture (scope = "function" )
11129def inconsistent_python_version_file (tmpdir ):
11230 tmp_file = tmpdir .join ("__verion__.py" )
113- tmp_file . write ( INCONSISTENT_VERSION_PY )
31+ copyfile ( f" { TESTING_FILE_PREFIX } /inconsistent_version.py" , str ( tmp_file ) )
11432 return str (tmp_file )
11533
11634
11735@pytest .fixture (scope = "function" )
11836def random_location_version_file (tmpdir ):
11937 tmp_file = tmpdir .join ("Cargo.lock" )
120- tmp_file . write ( CARGO_LOCK )
38+ copyfile ( f" { TESTING_FILE_PREFIX } /sample_cargo.lock" , str ( tmp_file ) )
12139 return str (tmp_file )
12240
12341
12442@pytest .fixture (scope = "function" )
12543def version_repeated_file (tmpdir ):
12644 tmp_file = tmpdir .join ("package.json" )
127- tmp_file . write ( REPEATED_VERSION_NUMBER )
45+ copyfile ( f" { TESTING_FILE_PREFIX } /repeated_version_number.json" , str ( tmp_file ) )
12846 return str (tmp_file )
12947
13048
@@ -145,14 +63,17 @@ def multiple_versions_reduce_string(tmpdir):
14563@pytest .fixture (scope = "function" )
14664def docker_compose_file (tmpdir ):
14765 tmp_file = tmpdir .join ("docker-compose.yaml" )
148- tmp_file . write ( DOCKER_COMPOSE )
66+ copyfile ( f" { TESTING_FILE_PREFIX } /sample_docker_compose.yaml" , str ( tmp_file ) )
14967 return str (tmp_file )
15068
15169
15270@pytest .fixture (scope = "function" )
15371def multiple_versions_to_update_poetry_lock (tmpdir ):
15472 tmp_file = tmpdir .join ("pyproject.toml" )
155- tmp_file .write (MULTIPLE_VERSIONS_TO_UPDATE_POETRY_LOCK )
73+ copyfile (
74+ f"{ TESTING_FILE_PREFIX } /multiple_versions_to_update_pyproject.toml" ,
75+ str (tmp_file ),
76+ )
15677 return str (tmp_file )
15778
15879
@@ -171,77 +92,73 @@ def version_files(
17192 ]
17293
17394
174- def test_update_version_in_files (version_files ):
95+ def test_update_version_in_files (version_files , file_regression ):
17596 old_version = "1.2.3"
17697 new_version = "2.0.0"
17798 bump .update_version_in_files (old_version , new_version , version_files )
99+
100+ file_contents = ""
178101 for filepath in version_files :
179102 with open (filepath , "r" ) as f :
180- data = f .read ()
181- assert new_version in data
103+ file_contents + = f .read ()
104+ file_regression . check ( file_contents , extension = ".txt" )
182105
183106
184- def test_partial_update_of_file (version_repeated_file ):
107+ def test_partial_update_of_file (version_repeated_file , file_regression ):
185108 old_version = "1.2.3"
186109 new_version = "2.0.0"
187110 regex = "version"
188111 location = f"{ version_repeated_file } :{ regex } "
189112
190113 bump .update_version_in_files (old_version , new_version , [location ])
191114 with open (version_repeated_file , "r" ) as f :
192- data = f .read ()
193- assert new_version in data
194- assert old_version in data
115+ file_regression .check (f .read (), extension = ".json" )
195116
196117
197- def test_random_location (random_location_version_file ):
118+ def test_random_location (random_location_version_file , file_regression ):
198119 old_version = "1.2.3"
199120 new_version = "2.0.0"
200121 location = f"{ random_location_version_file } :there-i-fixed-it.+\n version"
201122
202123 bump .update_version_in_files (old_version , new_version , [location ])
203124 with open (random_location_version_file , "r" ) as f :
204- data = f .read ()
205- assert len (re .findall (old_version , data )) == 2
206- assert len (re .findall (new_version , data )) == 1
125+ file_regression .check (f .read (), extension = ".lock" )
207126
208127
209- def test_duplicates_are_change_with_no_regex (random_location_version_file ):
128+ def test_duplicates_are_change_with_no_regex (
129+ random_location_version_file , file_regression
130+ ):
210131 old_version = "1.2.3"
211132 new_version = "2.0.0"
212133 location = f"{ random_location_version_file } :version"
213134
214135 bump .update_version_in_files (old_version , new_version , [location ])
215136 with open (random_location_version_file , "r" ) as f :
216- data = f .read ()
217- assert len (re .findall (old_version , data )) == 0
218- assert len (re .findall (new_version , data )) == 3
137+ file_regression .check (f .read (), extension = ".lock" )
219138
220139
221- def test_version_bump_increase_string_length (multiple_versions_increase_string ):
140+ def test_version_bump_increase_string_length (
141+ multiple_versions_increase_string , file_regression
142+ ):
222143 old_version = "1.2.9"
223144 new_version = "1.2.10"
224- version_bump_change_string_size (
225- multiple_versions_increase_string , old_version , new_version
226- )
145+ location = f"{ multiple_versions_increase_string } :version"
146+
147+ bump .update_version_in_files (old_version , new_version , [location ])
148+ with open (multiple_versions_increase_string , "r" ) as f :
149+ file_regression .check (f .read (), extension = ".txt" )
227150
228151
229- def test_version_bump_reduce_string_length (multiple_versions_reduce_string ):
152+ def test_version_bump_reduce_string_length (
153+ multiple_versions_reduce_string , file_regression
154+ ):
230155 old_version = "1.2.10"
231156 new_version = "2.0.0"
232- version_bump_change_string_size (
233- multiple_versions_reduce_string , old_version , new_version
234- )
235-
236-
237- def version_bump_change_string_size (filename , old_version , new_version ):
238- location = f"{ filename } :version"
157+ location = f"{ multiple_versions_reduce_string } :version"
239158
240159 bump .update_version_in_files (old_version , new_version , [location ])
241- with open (filename , "r" ) as f :
242- data = f .read ()
243- assert len (re .findall (old_version , data )) == 0
244- assert len (re .findall (new_version , data )) == 30
160+ with open (multiple_versions_reduce_string , "r" ) as f :
161+ file_regression .check (f .read (), extension = ".txt" )
245162
246163
247164def test_file_version_inconsistent_error (
0 commit comments