Skip to content

Commit 7635873

Browse files
committed
Fixed --ignore-missing-version and --ignore-missing-files options.
The CLI options were defaulting to `False` when missing. This overrode the configuration. Fixes #140
1 parent dbe6c1e commit 7635873

File tree

4 files changed

+162
-7
lines changed

4 files changed

+162
-7
lines changed

bumpversion/cli.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,14 @@ def cli(ctx: Context) -> None:
163163
),
164164
)
165165
@click.option(
166-
"--ignore-missing-files",
167-
is_flag=True,
166+
"--ignore-missing-files/--no-ignore-missing-files",
167+
default=None,
168168
envvar="BUMPVERSION_IGNORE_MISSING_FILES",
169169
help="Ignore any missing files when searching and replacing in files.",
170170
)
171171
@click.option(
172-
"--ignore-missing-version",
173-
is_flag=True,
172+
"--ignore-missing-version/--no-ignore-missing-version",
173+
default=None,
174174
envvar="BUMPVERSION_IGNORE_MISSING_VERSION",
175175
help="Ignore any Version Not Found errors when searching and replacing in files.",
176176
)

bumpversion/config/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
DEFAULTS = {
1818
"current_version": None,
1919
"parse": r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)",
20-
"serialize": ["{major}.{minor}.{patch}"],
20+
"serialize": ("{major}.{minor}.{patch}",),
2121
"search": "{current_version}",
2222
"replace": "{new_version}",
2323
"regex": False,

tests/test_config/test_create.py

+42-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,27 @@ def test_destination_without_prompt_returns_default_config(self, tmp_path: Path,
105105
default_config["current_version"] = "0.1.0"
106106
default_config["commit_args"] = ""
107107

108-
assert destination_config["tool"]["bumpversion"] == default_config
108+
assert destination_config["tool"]["bumpversion"]["allow_dirty"] == default_config["allow_dirty"]
109+
assert destination_config["tool"]["bumpversion"]["commit"] == default_config["commit"]
110+
assert destination_config["tool"]["bumpversion"]["commit_args"] == default_config["commit_args"]
111+
assert destination_config["tool"]["bumpversion"]["current_version"] == default_config["current_version"]
112+
assert (
113+
destination_config["tool"]["bumpversion"]["ignore_missing_files"] == default_config["ignore_missing_files"]
114+
)
115+
assert (
116+
destination_config["tool"]["bumpversion"]["ignore_missing_version"]
117+
== default_config["ignore_missing_version"]
118+
)
119+
assert destination_config["tool"]["bumpversion"]["message"] == default_config["message"]
120+
assert destination_config["tool"]["bumpversion"]["parse"] == default_config["parse"]
121+
assert destination_config["tool"]["bumpversion"]["regex"] == default_config["regex"]
122+
assert destination_config["tool"]["bumpversion"]["replace"] == default_config["replace"]
123+
assert destination_config["tool"]["bumpversion"]["search"] == default_config["search"]
124+
assert destination_config["tool"]["bumpversion"]["serialize"] == list(default_config["serialize"])
125+
assert destination_config["tool"]["bumpversion"]["sign_tags"] == default_config["sign_tags"]
126+
assert destination_config["tool"]["bumpversion"]["tag"] == default_config["tag"]
127+
assert destination_config["tool"]["bumpversion"]["tag_message"] == default_config["tag_message"]
128+
assert destination_config["tool"]["bumpversion"]["tag_name"] == default_config["tag_name"]
109129

110130
def test_prompt_true_asks_questions_and_updates_result(self, tmp_path: Path, default_config: dict, mocker):
111131
"""If prompt is True, the user should be prompted for the configuration."""
@@ -123,5 +143,25 @@ def test_prompt_true_asks_questions_and_updates_result(self, tmp_path: Path, def
123143

124144
default_config.update(answer)
125145

126-
assert destination_config["tool"]["bumpversion"] == default_config
146+
assert destination_config["tool"]["bumpversion"]["allow_dirty"] == default_config["allow_dirty"]
147+
assert destination_config["tool"]["bumpversion"]["commit"] == default_config["commit"]
148+
assert destination_config["tool"]["bumpversion"]["commit_args"] == default_config["commit_args"]
149+
assert destination_config["tool"]["bumpversion"]["current_version"] == default_config["current_version"]
150+
assert (
151+
destination_config["tool"]["bumpversion"]["ignore_missing_files"] == default_config["ignore_missing_files"]
152+
)
153+
assert (
154+
destination_config["tool"]["bumpversion"]["ignore_missing_version"]
155+
== default_config["ignore_missing_version"]
156+
)
157+
assert destination_config["tool"]["bumpversion"]["message"] == default_config["message"]
158+
assert destination_config["tool"]["bumpversion"]["parse"] == default_config["parse"]
159+
assert destination_config["tool"]["bumpversion"]["regex"] == default_config["regex"]
160+
assert destination_config["tool"]["bumpversion"]["replace"] == default_config["replace"]
161+
assert destination_config["tool"]["bumpversion"]["search"] == default_config["search"]
162+
assert destination_config["tool"]["bumpversion"]["serialize"] == list(default_config["serialize"])
163+
assert destination_config["tool"]["bumpversion"]["sign_tags"] == default_config["sign_tags"]
164+
assert destination_config["tool"]["bumpversion"]["tag"] == default_config["tag"]
165+
assert destination_config["tool"]["bumpversion"]["tag_message"] == default_config["tag_message"]
166+
assert destination_config["tool"]["bumpversion"]["tag_name"] == default_config["tag_name"]
127167
assert mocked_questionary.form.return_value.ask.call_count == 1

tests/test_config/test_utils.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)