-
-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathtest_bump_update_version_in_files.py
More file actions
388 lines (303 loc) · 11.6 KB
/
test_bump_update_version_in_files.py
File metadata and controls
388 lines (303 loc) · 11.6 KB
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
import re
from collections.abc import Callable
from pathlib import Path
from shutil import copyfile
from typing import TypeAlias
import pytest
from _pytest.fixtures import FixtureRequest
from commitizen import bump
from commitizen.exceptions import CurrentVersionNotFoundError
MULTIPLE_VERSIONS_INCREASE_STRING = 'version = "1.2.9"\n' * 30
MULTIPLE_VERSIONS_REDUCE_STRING = 'version = "1.2.10"\n' * 30
SampleFileFixture: TypeAlias = Callable[[str, str], Path]
@pytest.fixture
def sample_file(tmp_path: Path, data_dir: Path) -> SampleFileFixture:
def fixture(source: str, destination: str) -> Path:
tmp_file = tmp_path / destination
copyfile(data_dir / source, tmp_file)
return tmp_file
return fixture
@pytest.fixture
def commitizen_config_file(sample_file: SampleFileFixture) -> Path:
return sample_file("sample_pyproject.toml", "pyproject.toml")
@pytest.fixture
def python_version_file(sample_file: SampleFileFixture) -> Path:
return sample_file("sample_version.py", "__version__.py")
@pytest.fixture
def inconsistent_python_version_file(sample_file: SampleFileFixture) -> Path:
return sample_file("inconsistent_version.py", "__version__.py")
@pytest.fixture
def random_location_version_file(sample_file: SampleFileFixture) -> Path:
return sample_file("sample_cargo.lock", "Cargo.lock")
@pytest.fixture
def version_repeated_file(sample_file: SampleFileFixture) -> Path:
return sample_file("repeated_version_number.json", "package.json")
@pytest.fixture
def docker_compose_file(sample_file: SampleFileFixture) -> Path:
return sample_file("sample_docker_compose.yaml", "docker-compose.yaml")
@pytest.fixture(
params=(
"multiple_versions_to_update_pyproject.toml",
"multiple_versions_to_update_pyproject_wo_eol.toml",
),
ids=("with_eol", "without_eol"),
)
def multiple_versions_to_update_poetry_lock(
sample_file: SampleFileFixture, request: FixtureRequest
) -> Path:
return sample_file(request.param, "pyproject.toml")
@pytest.fixture
def multiple_versions_increase_string(tmp_path: Path) -> str:
tmp_file = tmp_path / "anyfile"
tmp_file.write_text(MULTIPLE_VERSIONS_INCREASE_STRING)
return str(tmp_file)
@pytest.fixture
def multiple_versions_reduce_string(tmp_path: Path) -> str:
tmp_file = tmp_path / "anyfile"
tmp_file.write_text(MULTIPLE_VERSIONS_REDUCE_STRING)
return str(tmp_file)
@pytest.fixture
def version_files(
commitizen_config_file: Path,
python_version_file: Path,
version_repeated_file: Path,
docker_compose_file: Path,
) -> tuple[str, ...]:
return (
str(commitizen_config_file),
str(python_version_file),
str(version_repeated_file),
str(docker_compose_file),
)
def test_update_version_in_files(version_files, file_regression):
old_version = "1.2.3"
new_version = "2.0.0"
bump.update_version_in_files(
old_version,
new_version,
version_files,
check_consistency=False,
encoding="utf-8",
)
file_contents = ""
for filepath in version_files:
with open(filepath, encoding="utf-8") as f:
file_contents += f.read()
file_regression.check(file_contents, extension=".txt")
def test_partial_update_of_file(version_repeated_file, file_regression):
old_version = "1.2.3"
new_version = "2.0.0"
regex = "version"
location = f"{version_repeated_file}:{regex}"
bump.update_version_in_files(
old_version, new_version, [location], check_consistency=False, encoding="utf-8"
)
with version_repeated_file.open(encoding="utf-8") as f:
file_regression.check(f.read(), extension=".json")
def test_random_location(random_location_version_file, file_regression):
old_version = "1.2.3"
new_version = "2.0.0"
location = f"{random_location_version_file}:version.+Commitizen"
bump.update_version_in_files(
old_version, new_version, [location], check_consistency=False, encoding="utf-8"
)
with random_location_version_file.open(encoding="utf-8") as f:
file_regression.check(f.read(), extension=".lock")
def test_duplicates_are_change_with_no_regex(
random_location_version_file, file_regression
):
old_version = "1.2.3"
new_version = "2.0.0"
location = f"{random_location_version_file}:version"
bump.update_version_in_files(
old_version, new_version, [location], check_consistency=False, encoding="utf-8"
)
with random_location_version_file.open(encoding="utf-8") as f:
file_regression.check(f.read(), extension=".lock")
def test_version_bump_increase_string_length(
multiple_versions_increase_string, file_regression
):
old_version = "1.2.9"
new_version = "1.2.10"
location = f"{multiple_versions_increase_string}:version"
bump.update_version_in_files(
old_version, new_version, [location], check_consistency=False, encoding="utf-8"
)
with open(multiple_versions_increase_string, encoding="utf-8") as f:
file_regression.check(f.read(), extension=".txt")
def test_version_bump_reduce_string_length(
multiple_versions_reduce_string, file_regression
):
old_version = "1.2.10"
new_version = "2.0.0"
location = f"{multiple_versions_reduce_string}:version"
bump.update_version_in_files(
old_version, new_version, [location], check_consistency=False, encoding="utf-8"
)
with open(multiple_versions_reduce_string, encoding="utf-8") as f:
file_regression.check(f.read(), extension=".txt")
def test_file_version_inconsistent_error(
commitizen_config_file, inconsistent_python_version_file, version_repeated_file
):
version_files = [
commitizen_config_file,
inconsistent_python_version_file,
version_repeated_file,
]
old_version = "1.2.3"
new_version = "2.0.0"
with pytest.raises(
CurrentVersionNotFoundError,
match=re.escape(
f"Current version {old_version} is not found in {inconsistent_python_version_file}."
),
):
bump.update_version_in_files(
old_version,
new_version,
version_files,
check_consistency=True,
encoding="utf-8",
)
def test_multiple_versions_to_bump(
multiple_versions_to_update_poetry_lock, file_regression
):
old_version = "1.2.9"
new_version = "1.2.10"
location = f"{multiple_versions_to_update_poetry_lock}:version"
bump.update_version_in_files(
old_version, new_version, [location], check_consistency=False, encoding="utf-8"
)
with multiple_versions_to_update_poetry_lock.open(encoding="utf-8") as f:
file_regression.check(f.read(), extension=".toml")
def test_update_version_in_globbed_files(commitizen_config_file, file_regression):
old_version = "1.2.3"
new_version = "2.0.0"
other = commitizen_config_file.parent / "other.toml"
copyfile(commitizen_config_file, other)
# Prepend full path as test assume absolute paths or cwd-relative
version_files = [
str(file_path) for file_path in commitizen_config_file.parent.glob("*.toml")
]
bump.update_version_in_files(
old_version,
new_version,
version_files,
check_consistency=False,
encoding="utf-8",
)
for file in commitizen_config_file, other:
file_regression.check(file.read_text("utf-8"), extension=".toml")
def test_update_version_in_files_with_check_consistency_true(
version_files: tuple[str, ...],
):
"""Test update_version_in_files with check_consistency=True (success case)."""
old_version = "1.2.3"
new_version = "2.0.0"
# This should succeed because all files contain the current version
updated_files: list[str] = bump.update_version_in_files(
old_version,
new_version,
version_files,
check_consistency=True,
encoding="utf-8",
)
# Verify that all files were updated
assert set(updated_files) == set(version_files)
def test_update_version_in_files_with_check_consistency_true_failure(
commitizen_config_file, inconsistent_python_version_file
):
"""Test update_version_in_files with check_consistency=True (failure case)."""
old_version = "1.2.3"
new_version = "2.0.0"
version_files = [commitizen_config_file, inconsistent_python_version_file]
# This should fail because inconsistent_python_version_file doesn't contain the current version
with pytest.raises(
CurrentVersionNotFoundError,
match=re.escape(
f"Current version {old_version} is not found in {inconsistent_python_version_file}"
),
):
bump.update_version_in_files(
old_version,
new_version,
version_files,
check_consistency=True,
encoding="utf-8",
)
@pytest.mark.parametrize(
"encoding,filename",
[
("latin-1", "test_latin1.txt"),
("utf-16", "test_utf16.txt"),
],
ids=["latin-1", "utf-16"],
)
def test_update_version_in_files_with_different_encodings(tmp_path, encoding, filename):
"""Test update_version_in_files with different encodings."""
# Create a test file with the specified encoding
test_file = tmp_path / filename
content = f'version = "1.2.3"\n# This is a test file with {encoding} encoding\n'
test_file.write_text(content, encoding=encoding)
old_version = "1.2.3"
new_version = "2.0.0"
updated_files = bump.update_version_in_files(
old_version,
new_version,
[str(test_file)],
check_consistency=True,
encoding=encoding,
)
# Verify the file was updated
assert len(updated_files) == 1
assert str(test_file) in updated_files
# Verify the content was updated correctly
updated_content = test_file.read_text(encoding=encoding)
assert f'version = "{new_version}"' in updated_content
assert f'version = "{old_version}"' not in updated_content
def test_update_version_in_files_return_value(version_files):
"""Test that update_version_in_files returns the correct list of updated files."""
old_version = "1.2.3"
new_version = "2.0.0"
updated_files = bump.update_version_in_files(
old_version,
new_version,
version_files,
check_consistency=False,
encoding="utf-8",
)
# Verify return value is a list
assert isinstance(updated_files, list)
# Verify all files in the input are in the returned list
assert set(version_files) == set(updated_files)
# Verify the returned paths are strings
assert all(isinstance(file_path, str) for file_path in updated_files)
def test_update_version_in_files_return_value_partial_update(tmp_path):
"""Test return value when only some files are updated."""
# Create two test files
file1 = tmp_path / "file1.txt"
file2 = tmp_path / "file2.txt"
# File1 contains the version to update
file1.write_text('version = "1.2.3"\n')
# File2 doesn't contain the version
file2.write_text("some other content\n")
old_version = "1.2.3"
new_version = "2.0.0"
updated_files = bump.update_version_in_files(
old_version,
new_version,
[str(file1), str(file2)],
check_consistency=False,
encoding="utf-8",
)
# Verify return value
assert isinstance(updated_files, list)
assert len(updated_files) == 2 # Both files should be in the list
assert str(file1) in updated_files
assert str(file2) in updated_files
# Verify file1 was actually updated
content1 = file1.read_text(encoding="utf-8")
assert f'version = "{new_version}"' in content1
# Verify file2 was not changed
content2 = file2.read_text(encoding="utf-8")
assert content2 == "some other content\n"