Skip to content

Commit

Permalink
Added configuration option ignore_missing_files
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed Feb 11, 2024
1 parent b2e4743 commit b473a19
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 192 deletions.
8 changes: 8 additions & 0 deletions bumpversion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,12 @@ def show(args: List[str], config_file: Optional[str], format_: str, increment: O
envvar="BUMPVERSION_IGNORE_MISSING_VERSION",
help="Ignore any Version Not Found errors when searching and replacing in files.",
)
@click.option(
"--ignore-missing-files",
is_flag=True,
envvar="BUMPVERSION_IGNORE_MISSING_FILES",
help="Ignore any missing files when searching and replacing in files.",
)
@click.option(
"--dry-run",
"-n",
Expand All @@ -465,6 +471,7 @@ def replace(
regex: bool,
no_configured_files: bool,
ignore_missing_version: bool,
ignore_missing_files: bool,
dry_run: bool,
) -> None:
"""
Expand Down Expand Up @@ -492,6 +499,7 @@ def replace(
message=None,
commit_args=None,
ignore_missing_version=ignore_missing_version,
ignore_missing_files=ignore_missing_files,
regex=regex,
)

Expand Down
1 change: 1 addition & 0 deletions bumpversion/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"replace": "{new_version}",
"regex": False,
"ignore_missing_version": False,
"ignore_missing_files": False,
"tag": False,
"sign_tags": False,
"tag_name": "v{new_version}",
Expand Down
1 change: 1 addition & 0 deletions bumpversion/config/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def update_config_file(
replace=config.replace,
regex=config.regex,
ignore_missing_version=True,
ignore_missing_file=True,
serialize=config.serialize,
parse=config.parse,
)
Expand Down
3 changes: 3 additions & 0 deletions bumpversion/config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class FileChange(BaseModel):
replace: str
regex: bool
ignore_missing_version: bool
ignore_missing_file: bool
filename: Optional[str] = None
glob: Optional[str] = None # Conflicts with filename. If both are specified, glob wins
key_path: Optional[str] = None # If specified, and has an appropriate extension, will be treated as a data file
Expand Down Expand Up @@ -84,6 +85,7 @@ class Config(BaseSettings):
replace: str
regex: bool
ignore_missing_version: bool
ignore_missing_files: bool
tag: bool
sign_tags: bool
tag_name: str
Expand Down Expand Up @@ -116,6 +118,7 @@ def add_files(self, filename: Union[str, List[str]]) -> None:
replace=self.replace,
regex=self.regex,
ignore_missing_version=self.ignore_missing_version,
ignore_missing_file=self.ignore_missing_files,
)
)
self.files = list(files)
Expand Down
1 change: 1 addition & 0 deletions bumpversion/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def get_all_file_configs(config_dict: dict) -> List[FileChange]:
"search": config_dict["search"],
"replace": config_dict["replace"],
"ignore_missing_version": config_dict["ignore_missing_version"],
"ignore_missing_file": config_dict["ignore_missing_files"],
"regex": config_dict["regex"],
}
files = [{k: v for k, v in filecfg.items() if v is not None} for filecfg in config_dict["files"]]
Expand Down
8 changes: 8 additions & 0 deletions bumpversion/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def __init__(
replace=replacement,
regex=file_change.regex or False,
ignore_missing_version=file_change.ignore_missing_version or False,
ignore_missing_file=file_change.ignore_missing_file or False,
filename=file_change.filename,
glob=file_change.glob,
key_path=file_change.key_path,
Expand Down Expand Up @@ -176,6 +177,12 @@ def make_file_change(
self.file_change.replace,
)
logger.indent()
if not os.path.exists(self.file_change.filename):
if self.file_change.ignore_missing_file:
logger.info("File not found, but ignoring")
logger.dedent()
return
raise FileNotFoundError(f"File not found: '{self.file_change.filename}'")
logger.debug("Serializing the current version")
logger.indent()
context["current_version"] = self.version_config.serialize(current_version, context)
Expand Down Expand Up @@ -273,6 +280,7 @@ def __init__(
search=search or file_change.search or version_config.search,
replace=replace or file_change.replace or version_config.replace,
regex=file_change.regex or False,
ignore_missing_file=file_change.ignore_missing_file or False,
ignore_missing_version=file_change.ignore_missing_version or False,
filename=file_change.filename,
glob=file_change.glob,
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/basic_cfg_expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'excluded_paths': [],
'files': [{'filename': 'setup.py',
'glob': None,
'ignore_missing_file': False,
'ignore_missing_version': False,
'key_path': None,
'parse': '(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(\\-(?P<release>[a-z]+))?',
Expand All @@ -15,6 +16,7 @@
'{major}.{minor}.{patch}')},
{'filename': 'bumpversion/__init__.py',
'glob': None,
'ignore_missing_file': False,
'ignore_missing_version': False,
'key_path': None,
'parse': '(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(\\-(?P<release>[a-z]+))?',
Expand All @@ -25,6 +27,7 @@
'{major}.{minor}.{patch}')},
{'filename': 'CHANGELOG.md',
'glob': None,
'ignore_missing_file': False,
'ignore_missing_version': False,
'key_path': None,
'parse': '(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(\\-(?P<release>[a-z]+))?',
Expand All @@ -33,6 +36,7 @@
'search': '**unreleased**',
'serialize': ('{major}.{minor}.{patch}-{release}',
'{major}.{minor}.{patch}')}],
'ignore_missing_files': False,
'ignore_missing_version': False,
'included_paths': [],
'message': 'Bump version: {current_version} → {new_version}',
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/basic_cfg_expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ excluded_paths:
files:
- filename: "setup.py"
glob: null
ignore_missing_file: false
ignore_missing_version: false
key_path: null
parse: "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(\\-(?P<release>[a-z]+))?"
Expand All @@ -18,6 +19,7 @@ files:
- "{major}.{minor}.{patch}"
- filename: "bumpversion/__init__.py"
glob: null
ignore_missing_file: false
ignore_missing_version: false
key_path: null
parse: "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(\\-(?P<release>[a-z]+))?"
Expand All @@ -29,6 +31,7 @@ files:
- "{major}.{minor}.{patch}"
- filename: "CHANGELOG.md"
glob: null
ignore_missing_file: false
ignore_missing_version: false
key_path: null
parse: "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(\\-(?P<release>[a-z]+))?"
Expand All @@ -38,6 +41,7 @@ files:
serialize:
- "{major}.{minor}.{patch}-{release}"
- "{major}.{minor}.{patch}"
ignore_missing_files: false
ignore_missing_version: false
included_paths:

Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/basic_cfg_expected_full.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{
"filename": "setup.py",
"glob": null,
"ignore_missing_file": false,
"ignore_missing_version": false,
"key_path": null,
"parse": "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(\\-(?P<release>[a-z]+))?",
Expand All @@ -22,6 +23,7 @@
{
"filename": "bumpversion/__init__.py",
"glob": null,
"ignore_missing_file": false,
"ignore_missing_version": false,
"key_path": null,
"parse": "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(\\-(?P<release>[a-z]+))?",
Expand All @@ -36,6 +38,7 @@
{
"filename": "CHANGELOG.md",
"glob": null,
"ignore_missing_file": false,
"ignore_missing_version": false,
"key_path": null,
"parse": "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(\\-(?P<release>[a-z]+))?",
Expand All @@ -48,6 +51,7 @@
]
}
],
"ignore_missing_files": false,
"ignore_missing_version": false,
"included_paths": [],
"message": "Bump version: {current_version} \u2192 {new_version}",
Expand Down
18 changes: 10 additions & 8 deletions tests/test_cli/test_bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def test_with_version_part_includes_new_version(self, tmp_path: Path, fixtures_p
"search={current_version}",
"replace={new_version}",
"regex=False",
"ignore_missing_files=False",
"ignore_missing_version=False",
"included_paths=[]",
"tag=True",
Expand All @@ -105,19 +106,19 @@ def test_with_version_part_includes_new_version(self, tmp_path: Path, fixtures_p
"'serialize': ('{major}.{minor}.{patch}-{release}', "
"'{major}.{minor}.{patch}'), 'search': '{current_version}', 'replace': "
"'{new_version}', 'regex': False, 'ignore_missing_version': False, "
"'filename': 'setup.py', 'glob': None, 'key_path': None}, {'parse': "
"'ignore_missing_file': False, 'filename': 'setup.py', 'glob': None, 'key_path': None}, {'parse': "
"'(?P<major>\\\\d+)\\\\.(?P<minor>\\\\d+)\\\\.(?P<patch>\\\\d+)(\\\\-(?P<release>[a-z]+))?', "
"'serialize': ('{major}.{minor}.{patch}-{release}', "
"'{major}.{minor}.{patch}'), 'search': '{current_version}', 'replace': "
"'{new_version}', 'regex': False, 'ignore_missing_version': False, "
"'filename': 'bumpversion/__init__.py', 'glob': None, 'key_path': None}, "
"'ignore_missing_file': False, 'filename': 'bumpversion/__init__.py', 'glob': None, 'key_path': None}, "
"{'parse': "
"'(?P<major>\\\\d+)\\\\.(?P<minor>\\\\d+)\\\\.(?P<patch>\\\\d+)(\\\\-(?P<release>[a-z]+))?', "
"'serialize': ('{major}.{minor}.{patch}-{release}', "
"'{major}.{minor}.{patch}'), 'search': '**unreleased**', 'replace': "
"'**unreleased**\\n**v{new_version}**', 'regex': False, "
"'ignore_missing_version': False, 'filename': 'CHANGELOG.md', 'glob': None, "
"'key_path': None}]"
"'ignore_missing_version': False, 'ignore_missing_file': False, 'filename': 'CHANGELOG.md', "
"'glob': None, 'key_path': None}]"
),
}

Expand Down Expand Up @@ -148,6 +149,7 @@ def test_without_version_part_excludes_new_version(self, tmp_path: Path, fixture
"search={current_version}",
"replace={new_version}",
"regex=False",
"ignore_missing_files=False",
"ignore_missing_version=False",
"included_paths=[]",
"tag=True",
Expand All @@ -164,19 +166,19 @@ def test_without_version_part_excludes_new_version(self, tmp_path: Path, fixture
"'serialize': ('{major}.{minor}.{patch}-{release}', "
"'{major}.{minor}.{patch}'), 'search': '{current_version}', 'replace': "
"'{new_version}', 'regex': False, 'ignore_missing_version': False, "
"'filename': 'setup.py', 'glob': None, 'key_path': None}, {'parse': "
"'ignore_missing_file': False, 'filename': 'setup.py', 'glob': None, 'key_path': None}, {'parse': "
"'(?P<major>\\\\d+)\\\\.(?P<minor>\\\\d+)\\\\.(?P<patch>\\\\d+)(\\\\-(?P<release>[a-z]+))?', "
"'serialize': ('{major}.{minor}.{patch}-{release}', "
"'{major}.{minor}.{patch}'), 'search': '{current_version}', 'replace': "
"'{new_version}', 'regex': False, 'ignore_missing_version': False, "
"'filename': 'bumpversion/__init__.py', 'glob': None, 'key_path': None}, "
"'ignore_missing_file': False, 'filename': 'bumpversion/__init__.py', 'glob': None, 'key_path': None}, "
"{'parse': "
"'(?P<major>\\\\d+)\\\\.(?P<minor>\\\\d+)\\\\.(?P<patch>\\\\d+)(\\\\-(?P<release>[a-z]+))?', "
"'serialize': ('{major}.{minor}.{patch}-{release}', "
"'{major}.{minor}.{patch}'), 'search': '**unreleased**', 'replace': "
"'**unreleased**\\n**v{new_version}**', 'regex': False, "
"'ignore_missing_version': False, 'filename': 'CHANGELOG.md', 'glob': None, "
"'key_path': None}]"
"'ignore_missing_version': False, 'ignore_missing_file': False, 'filename': 'CHANGELOG.md', "
"'glob': None, 'key_path': None}]"
),
}

Expand Down
Loading

0 comments on commit b473a19

Please sign in to comment.