|
| 1 | +"""Tests of the configuration utilities.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import tomlkit |
| 7 | +import pytest |
| 8 | +from pytest import param |
| 9 | + |
| 10 | +from bumpversion.config.utils import get_all_file_configs, get_all_part_configs, resolve_glob_files |
| 11 | +from bumpversion.config.models import FileChange |
| 12 | +from bumpversion.config import DEFAULTS |
| 13 | +from conftest import inside_dir |
| 14 | + |
| 15 | + |
| 16 | +def write_config(tmp_path: Path, overrides: dict) -> Path: |
| 17 | + """Write a configuration file.""" |
| 18 | + config_file = tmp_path / ".bumpversion.toml" |
| 19 | + defaults = DEFAULTS.copy() |
| 20 | + defaults.pop("parts") |
| 21 | + defaults.pop("scm_info") |
| 22 | + defaults["current_version"] = defaults["current_version"] or "1.2.3" |
| 23 | + defaults["commit_args"] = "" |
| 24 | + config = {"tool": {"bumpversion": {**defaults, **overrides}}} |
| 25 | + config_file.write_text(tomlkit.dumps(config)) |
| 26 | + return config_file |
| 27 | + |
| 28 | + |
| 29 | +class TestGetAllFileConfigs: |
| 30 | + """Tests for the get_all_file_configs function.""" |
| 31 | + |
| 32 | + def test_uses_defaults_for_missing_keys(self, tmp_path: Path): |
| 33 | + """Test the get_all_file_configs function.""" |
| 34 | + config_file = write_config(tmp_path, {"files": [{"filename": "setup.cfg"}]}) |
| 35 | + config_dict = tomlkit.loads(config_file.read_text())["tool"]["bumpversion"] |
| 36 | + file_configs = get_all_file_configs(config_dict) |
| 37 | + |
| 38 | + for key in FileChange.model_fields.keys(): |
| 39 | + global_key = key if key != "ignore_missing_file" else "ignore_missing_files" |
| 40 | + if key not in ["filename", "glob", "key_path"]: |
| 41 | + file_val = getattr(file_configs[0], key) |
| 42 | + assert file_val == DEFAULTS[global_key] |
| 43 | + |
| 44 | + @pytest.mark.parametrize( |
| 45 | + ["attribute", "global_value", "file_value"], |
| 46 | + [ |
| 47 | + param("parse", DEFAULTS["parse"], r"v(?P<major>\d+)", id="parse"), |
| 48 | + param("serialize", DEFAULTS["serialize"], ("v{major}",), id="serialize"), |
| 49 | + param("search", DEFAULTS["search"], "v{current_version}", id="search"), |
| 50 | + param("replace", DEFAULTS["replace"], "v{new_version}", id="replace"), |
| 51 | + param("ignore_missing_version", True, False, id="ignore_missing_version"), |
| 52 | + param("ignore_missing_files", True, False, id="ignore_missing_files"), |
| 53 | + param("regex", True, False, id="regex"), |
| 54 | + ], |
| 55 | + ) |
| 56 | + def test_overrides_defaults(self, tmp_path: Path, attribute: str, global_value: Any, file_value: Any): |
| 57 | + """Configured attributes in the file should override global options.""" |
| 58 | + file_attribute = attribute if attribute != "ignore_missing_files" else "ignore_missing_file" |
| 59 | + config_file = write_config( |
| 60 | + tmp_path, |
| 61 | + { |
| 62 | + attribute: global_value, |
| 63 | + "files": [ |
| 64 | + { |
| 65 | + "filename": "setup.cfg", |
| 66 | + file_attribute: file_value, |
| 67 | + } |
| 68 | + ], |
| 69 | + }, |
| 70 | + ) |
| 71 | + config_dict = tomlkit.loads(config_file.read_text())["tool"]["bumpversion"] |
| 72 | + file_configs = get_all_file_configs(config_dict) |
| 73 | + assert len(file_configs) == 1 |
| 74 | + assert file_configs[0].filename == "setup.cfg" |
| 75 | + assert getattr(file_configs[0], file_attribute) == file_value |
| 76 | + |
| 77 | + |
| 78 | +class TestResolveGlobFiles: |
| 79 | + """Tests for the resolve_glob_files function.""" |
| 80 | + |
| 81 | + def test_all_attributes_are_copied(self, tmp_path: Path): |
| 82 | + """Test that all attributes are copied.""" |
| 83 | + file1 = tmp_path.joinpath("setup.cfg") |
| 84 | + file2 = tmp_path.joinpath("subdir/setup.cfg") |
| 85 | + file1.touch() |
| 86 | + file2.parent.mkdir() |
| 87 | + file2.touch() |
| 88 | + |
| 89 | + file_cfg = FileChange( |
| 90 | + filename=None, |
| 91 | + glob="**/*.cfg", |
| 92 | + key_path=None, |
| 93 | + parse=r"v(?P<major>\d+)", |
| 94 | + serialize=("v{major}",), |
| 95 | + search="v{current_version}", |
| 96 | + replace="v{new_version}", |
| 97 | + ignore_missing_version=True, |
| 98 | + ignore_missing_file=True, |
| 99 | + regex=True, |
| 100 | + ) |
| 101 | + with inside_dir(tmp_path): |
| 102 | + resolved_files = resolve_glob_files(file_cfg) |
| 103 | + |
| 104 | + assert len(resolved_files) == 2 |
| 105 | + for resolved_file in resolved_files: |
| 106 | + assert resolved_file.filename.endswith("setup.cfg") |
| 107 | + assert resolved_file.glob is None |
| 108 | + assert resolved_file.key_path is None |
| 109 | + assert resolved_file.parse == r"v(?P<major>\d+)" |
| 110 | + assert resolved_file.serialize == ("v{major}",) |
| 111 | + assert resolved_file.search == "v{current_version}" |
| 112 | + assert resolved_file.replace == "v{new_version}" |
| 113 | + assert resolved_file.ignore_missing_version is True |
| 114 | + assert resolved_file.ignore_missing_file is True |
| 115 | + assert resolved_file.regex is True |
0 commit comments