| 1 |
import re |
| 2 |
from collections.abc import Callable |
| 3 |
from pathlib import Path |
| 4 |
from shutil import copyfile |
| 5 |
from typing import TypeAlias |
| 6 |
|
| 7 |
import pytest |
| 8 |
from _pytest.fixtures import FixtureRequest |
| 9 |
|
| 10 |
from commitizen import bump |
| 11 |
from commitizen.exceptions import CurrentVersionNotFoundError |
| 12 |
|
| 13 |
MULTIPLE_VERSIONS_INCREASE_STRING = 'version = "1.2.9"\n' * 30 |
| 14 |
MULTIPLE_VERSIONS_REDUCE_STRING = 'version = "1.2.10"\n' * 30 |
| 15 |
|
| 16 |
|
| 17 |
SampleFileFixture: TypeAlias = Callable[[str, str], Path] |
| 18 |
|
| 19 |
|
| 20 |
@pytest.fixture |
| 21 |
def sample_file(tmp_path: Path, data_dir: Path) -> SampleFileFixture: |
| 22 |
def fixture(source: str, destination: str) -> Path: |
| 23 |
tmp_file = tmp_path / destination |
| 24 |
copyfile(data_dir / source, tmp_file) |
| 25 |
return tmp_file |
| 26 |
|
| 27 |
return fixture |
| 28 |
|
| 29 |
|
| 30 |
@pytest.fixture |
| 31 |
def commitizen_config_file(sample_file: SampleFileFixture) -> Path: |
| 32 |
return sample_file("sample_pyproject.toml", "pyproject.toml") |
| 33 |
|
| 34 |
|
| 35 |
@pytest.fixture |
| 36 |
def python_version_file(sample_file: SampleFileFixture) -> Path: |
| 37 |
return sample_file("sample_version.py", "__version__.py") |
| 38 |
|
| 39 |
|
| 40 |
@pytest.fixture |
| 41 |
def inconsistent_python_version_file(sample_file: SampleFileFixture) -> Path: |
| 42 |
return sample_file("inconsistent_version.py", "__version__.py") |
| 43 |
|
| 44 |
|
| 45 |
@pytest.fixture |
| 46 |
def random_location_version_file(sample_file: SampleFileFixture) -> Path: |
| 47 |
return sample_file("sample_cargo.lock", "Cargo.lock") |
| 48 |
|
| 49 |
|
| 50 |
@pytest.fixture |
| 51 |
def version_repeated_file(sample_file: SampleFileFixture) -> Path: |
| 52 |
return sample_file("repeated_version_number.json", "package.json") |
| 53 |
|
| 54 |
|
| 55 |
@pytest.fixture |
| 56 |
def docker_compose_file(sample_file: SampleFileFixture) -> Path: |
| 57 |
return sample_file("sample_docker_compose.yaml", "docker-compose.yaml") |
| 58 |
|
| 59 |
|
| 60 |
@pytest.fixture( |
| 61 |
params=( |
| 62 |
"multiple_versions_to_update_pyproject.toml", |
| 63 |
"multiple_versions_to_update_pyproject_wo_eol.toml", |
| 64 |
), |
| 65 |
ids=("with_eol", "without_eol"), |
| 66 |
) |
| 67 |
def multiple_versions_to_update_poetry_lock( |
| 68 |
sample_file: SampleFileFixture, request: FixtureRequest |
| 69 |
) -> Path: |
| 70 |
return sample_file(request.param, "pyproject.toml") |
| 71 |
|
| 72 |
|
| 73 |
@pytest.fixture |
| 74 |
def multiple_versions_increase_string(tmp_path: Path) -> str: |
| 75 |
tmp_file = tmp_path / "anyfile" |
| 76 |
tmp_file.write_text(MULTIPLE_VERSIONS_INCREASE_STRING) |
| 77 |
return str(tmp_file) |
| 78 |
|
| 79 |
|
| 80 |
@pytest.fixture |
| 81 |
def multiple_versions_reduce_string(tmp_path: Path) -> str: |
| 82 |
tmp_file = tmp_path / "anyfile" |
| 83 |
tmp_file.write_text(MULTIPLE_VERSIONS_REDUCE_STRING) |
| 84 |
return str(tmp_file) |
| 85 |
|
| 86 |
|
| 87 |
@pytest.fixture |
| 88 |
def version_files( |
| 89 |
commitizen_config_file: Path, |
| 90 |
python_version_file: Path, |
| 91 |
version_repeated_file: Path, |
| 92 |
docker_compose_file: Path, |
| 93 |
) -> tuple[str, ...]: |
| 94 |
return ( |
| 95 |
str(commitizen_config_file), |
| 96 |
str(python_version_file), |
| 97 |
str(version_repeated_file), |
| 98 |
str(docker_compose_file), |
| 99 |
) |
| 100 |
|
| 101 |
|
| 102 |
def test_update_version_in_files(version_files, file_regression): |
| 103 |
old_version = "1.2.3" |
| 104 |
new_version = "2.0.0" |
| 105 |
bump.update_version_in_files( |
| 106 |
old_version, |
| 107 |
new_version, |
| 108 |
version_files, |
| 109 |
check_consistency=False, |
| 110 |
encoding="utf-8", |
| 111 |
) |
| 112 |
|
| 113 |
file_contents = "" |
| 114 |
for filepath in version_files: |
| 115 |
file_contents += Path(filepath).read_text(encoding="utf-8") |
| 116 |
file_regression.check(file_contents, extension=".txt") |
| 117 |
|
| 118 |
|
| 119 |
def test_partial_update_of_file(version_repeated_file, file_regression): |
| 120 |
old_version = "1.2.3" |
| 121 |
new_version = "2.0.0" |
| 122 |
regex = "version" |
| 123 |
location = f"{version_repeated_file}:{regex}" |
| 124 |
|
| 125 |
bump.update_version_in_files( |
| 126 |
old_version, new_version, [location], check_consistency=False, encoding="utf-8" |
| 127 |
) |
| 128 |
file_regression.check( |
| 129 |
version_repeated_file.read_text(encoding="utf-8"), extension=".json" |
| 130 |
) |
| 131 |
|
| 132 |
|
| 133 |
def test_random_location(random_location_version_file, file_regression): |
| 134 |
old_version = "1.2.3" |
| 135 |
new_version = "2.0.0" |
| 136 |
location = f"{random_location_version_file}:version.+Commitizen" |
| 137 |
|
| 138 |
bump.update_version_in_files( |
| 139 |
old_version, new_version, [location], check_consistency=False, encoding="utf-8" |
| 140 |
) |
| 141 |
file_regression.check( |
| 142 |
random_location_version_file.read_text(encoding="utf-8"), extension=".lock" |
| 143 |
) |
| 144 |
|
| 145 |
|
| 146 |
def test_duplicates_are_change_with_no_regex( |
| 147 |
random_location_version_file, file_regression |
| 148 |
): |
| 149 |
old_version = "1.2.3" |
| 150 |
new_version = "2.0.0" |
| 151 |
location = f"{random_location_version_file}:version" |
| 152 |
|
| 153 |
bump.update_version_in_files( |
| 154 |
old_version, new_version, [location], check_consistency=False, encoding="utf-8" |
| 155 |
) |
| 156 |
file_regression.check( |
| 157 |
random_location_version_file.read_text(encoding="utf-8"), extension=".lock" |
| 158 |
) |
| 159 |
|
| 160 |
|
| 161 |
def test_version_bump_increase_string_length( |
| 162 |
multiple_versions_increase_string, file_regression |
| 163 |
): |
| 164 |
old_version = "1.2.9" |
| 165 |
new_version = "1.2.10" |
| 166 |
location = f"{multiple_versions_increase_string}:version" |
| 167 |
|
| 168 |
bump.update_version_in_files( |
| 169 |
old_version, new_version, [location], check_consistency=False, encoding="utf-8" |
| 170 |
) |
| 171 |
file_regression.check( |
| 172 |
Path(multiple_versions_increase_string).read_text(encoding="utf-8"), |
| 173 |
extension=".txt", |
| 174 |
) |
| 175 |
|
| 176 |
|
| 177 |
def test_version_bump_reduce_string_length( |
| 178 |
multiple_versions_reduce_string, file_regression |
| 179 |
): |
| 180 |
old_version = "1.2.10" |
| 181 |
new_version = "2.0.0" |
| 182 |
location = f"{multiple_versions_reduce_string}:version" |
| 183 |
|
| 184 |
bump.update_version_in_files( |
| 185 |
old_version, new_version, [location], check_consistency=False, encoding="utf-8" |
| 186 |
) |
| 187 |
file_regression.check( |
| 188 |
Path(multiple_versions_reduce_string).read_text(encoding="utf-8"), |
| 189 |
extension=".txt", |
| 190 |
) |
| 191 |
|
| 192 |
|
| 193 |
def test_file_version_inconsistent_error( |
| 194 |
commitizen_config_file, inconsistent_python_version_file, version_repeated_file |
| 195 |
): |
| 196 |
version_files = [ |
| 197 |
commitizen_config_file, |
| 198 |
inconsistent_python_version_file, |
| 199 |
version_repeated_file, |
| 200 |
] |
| 201 |
old_version = "1.2.3" |
| 202 |
new_version = "2.0.0" |
| 203 |
with pytest.raises( |
| 204 |
CurrentVersionNotFoundError, |
| 205 |
match=re.escape( |
| 206 |
f"Current version {old_version} is not found in {inconsistent_python_version_file}.\n" |
| 207 |
"The version defined in commitizen configuration and the ones in " |
| 208 |
"version_files are possibly inconsistent." |
| 209 |
), |
| 210 |
): |
| 211 |
bump.update_version_in_files( |
| 212 |
old_version, |
| 213 |
new_version, |
| 214 |
version_files, |
| 215 |
check_consistency=True, |
| 216 |
encoding="utf-8", |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
def test_multiple_versions_to_bump( |
| 221 |
multiple_versions_to_update_poetry_lock, file_regression |
| 222 |
): |
| 223 |
old_version = "1.2.9" |
| 224 |
new_version = "1.2.10" |
| 225 |
location = f"{multiple_versions_to_update_poetry_lock}:version" |
| 226 |
|
| 227 |
bump.update_version_in_files( |
| 228 |
old_version, new_version, [location], check_consistency=False, encoding="utf-8" |
| 229 |
) |
| 230 |
file_regression.check( |
| 231 |
multiple_versions_to_update_poetry_lock.read_text(encoding="utf-8"), |
| 232 |
extension=".toml", |
| 233 |
) |
| 234 |
|
| 235 |
|
| 236 |
def test_update_version_in_globbed_files(commitizen_config_file, file_regression): |
| 237 |
old_version = "1.2.3" |
| 238 |
new_version = "2.0.0" |
| 239 |
other = commitizen_config_file.parent / "other.toml" |
| 240 |
|
| 241 |
copyfile(commitizen_config_file, other) |
| 242 |
|
| 243 |
# Prepend full path as test assume absolute paths or cwd-relative |
| 244 |
version_files = [ |
| 245 |
str(file_path) for file_path in commitizen_config_file.parent.glob("*.toml") |
| 246 |
] |
| 247 |
|
| 248 |
bump.update_version_in_files( |
| 249 |
old_version, |
| 250 |
new_version, |
| 251 |
version_files, |
| 252 |
check_consistency=False, |
| 253 |
encoding="utf-8", |
| 254 |
) |
| 255 |
|
| 256 |
for file in commitizen_config_file, other: |
| 257 |
file_regression.check(file.read_text("utf-8"), extension=".toml") |
| 258 |
|
| 259 |
|
| 260 |
def test_update_version_in_files_with_check_consistency_true( |
| 261 |
version_files: tuple[str, ...], |
| 262 |
): |
| 263 |
"""Test update_version_in_files with check_consistency=True (success case).""" |
| 264 |
old_version = "1.2.3" |
| 265 |
new_version = "2.0.0" |
| 266 |
|
| 267 |
# This should succeed because all files contain the current version |
| 268 |
updated_files: list[str] = bump.update_version_in_files( |
| 269 |
old_version, |
| 270 |
new_version, |
| 271 |
version_files, |
| 272 |
check_consistency=True, |
| 273 |
encoding="utf-8", |
| 274 |
) |
| 275 |
|
| 276 |
# Verify that all files were updated |
| 277 |
assert set(updated_files) == set(version_files) |
| 278 |
|
| 279 |
|
| 280 |
def test_update_version_in_files_with_check_consistency_true_failure( |
| 281 |
commitizen_config_file, inconsistent_python_version_file |
| 282 |
): |
| 283 |
"""Test update_version_in_files with check_consistency=True (failure case).""" |
| 284 |
old_version = "1.2.3" |
| 285 |
new_version = "2.0.0" |
| 286 |
version_files = [commitizen_config_file, inconsistent_python_version_file] |
| 287 |
|
| 288 |
# This should fail because inconsistent_python_version_file doesn't contain the current version |
| 289 |
with pytest.raises( |
| 290 |
CurrentVersionNotFoundError, |
| 291 |
match=re.escape( |
| 292 |
f"Current version {old_version} is not found in {inconsistent_python_version_file}.\n" |
| 293 |
"The version defined in commitizen configuration and the ones in " |
| 294 |
"version_files are possibly inconsistent." |
| 295 |
), |
| 296 |
): |
| 297 |
bump.update_version_in_files( |
| 298 |
old_version, |
| 299 |
new_version, |
| 300 |
version_files, |
| 301 |
check_consistency=True, |
| 302 |
encoding="utf-8", |
| 303 |
) |
| 304 |
|
| 305 |
|
| 306 |
@pytest.mark.parametrize( |
| 307 |
("encoding", "filename"), |
| 308 |
[ |
| 309 |
("latin-1", "test_latin1.txt"), |
| 310 |
("utf-16", "test_utf16.txt"), |
| 311 |
], |
| 312 |
ids=["latin-1", "utf-16"], |
| 313 |
) |
| 314 |
def test_update_version_in_files_with_different_encodings(tmp_path, encoding, filename): |
| 315 |
"""Test update_version_in_files with different encodings.""" |
| 316 |
# Create a test file with the specified encoding |
| 317 |
test_file = tmp_path / filename |
| 318 |
content = f'version = "1.2.3"\n# This is a test file with {encoding} encoding\n' |
| 319 |
test_file.write_text(content, encoding=encoding) |
| 320 |
|
| 321 |
old_version = "1.2.3" |
| 322 |
new_version = "2.0.0" |
| 323 |
|
| 324 |
updated_files = bump.update_version_in_files( |
| 325 |
old_version, |
| 326 |
new_version, |
| 327 |
[str(test_file)], |
| 328 |
check_consistency=True, |
| 329 |
encoding=encoding, |
| 330 |
) |
| 331 |
|
| 332 |
# Verify the file was updated |
| 333 |
assert len(updated_files) == 1 |
| 334 |
assert str(test_file) in updated_files |
| 335 |
|
| 336 |
# Verify the content was updated correctly |
| 337 |
updated_content = test_file.read_text(encoding=encoding) |
| 338 |
assert f'version = "{new_version}"' in updated_content |
| 339 |
assert f'version = "{old_version}"' not in updated_content |
| 340 |
|
| 341 |
|
| 342 |
def test_update_version_in_files_return_value(version_files): |
| 343 |
"""Test that update_version_in_files returns the correct list of updated files.""" |
| 344 |
old_version = "1.2.3" |
| 345 |
new_version = "2.0.0" |
| 346 |
|
| 347 |
updated_files = bump.update_version_in_files( |
| 348 |
old_version, |
| 349 |
new_version, |
| 350 |
version_files, |
| 351 |
check_consistency=False, |
| 352 |
encoding="utf-8", |
| 353 |
) |
| 354 |
|
| 355 |
# Verify return value is a list |
| 356 |
assert isinstance(updated_files, list) |
| 357 |
|
| 358 |
# Verify all files in the input are in the returned list |
| 359 |
assert set(version_files) == set(updated_files) |
| 360 |
|
| 361 |
# Verify the returned paths are strings |
| 362 |
assert all(isinstance(file_path, str) for file_path in updated_files) |
| 363 |
|
| 364 |
|
| 365 |
def test_update_version_in_files_return_value_partial_update(tmp_path): |
| 366 |
"""Test return value when only some files are updated.""" |
| 367 |
# Create two test files |
| 368 |
file1 = tmp_path / "file1.txt" |
| 369 |
file2 = tmp_path / "file2.txt" |
| 370 |
|
| 371 |
# File1 contains the version to update |
| 372 |
file1.write_text('version = "1.2.3"\n') |
| 373 |
|
| 374 |
# File2 doesn't contain the version |
| 375 |
file2.write_text("some other content\n") |
| 376 |
|
| 377 |
old_version = "1.2.3" |
| 378 |
new_version = "2.0.0" |
| 379 |
|
| 380 |
updated_files = bump.update_version_in_files( |
| 381 |
old_version, |
| 382 |
new_version, |
| 383 |
[str(file1), str(file2)], |
| 384 |
check_consistency=False, |
| 385 |
encoding="utf-8", |
| 386 |
) |
| 387 |
|
| 388 |
# Verify return value |
| 389 |
assert isinstance(updated_files, list) |
| 390 |
assert len(updated_files) == 2 # Both files should be in the list |
| 391 |
assert str(file1) in updated_files |
| 392 |
assert str(file2) in updated_files |
| 393 |
|
| 394 |
# Verify file1 was actually updated |
| 395 |
content1 = file1.read_text(encoding="utf-8") |
| 396 |
assert f'version = "{new_version}"' in content1 |
| 397 |
|
| 398 |
# Verify file2 was not changed |
| 399 |
content2 = file2.read_text(encoding="utf-8") |
| 400 |
assert content2 == "some other content\n" |