diff --git a/bumpversion/config.py b/bumpversion/config.py index 660915c2..fa552591 100644 --- a/bumpversion/config.py +++ b/bumpversion/config.py @@ -201,7 +201,7 @@ def find_config_file(explicit_file: Union[str, Path, None] = None) -> Union[Path """ search_paths = [Path(explicit_file)] if explicit_file else CONFIG_FILE_SEARCH_ORDER return next( - (config_file for config_file in search_paths if config_file.exists()), + (cfg_file for cfg_file in search_paths if cfg_file.exists() and "bumpversion]" in cfg_file.read_text()), None, ) diff --git a/tests/fixtures/basic_cfg.toml b/tests/fixtures/basic_cfg.toml index f9e9d21e..a1b14936 100644 --- a/tests/fixtures/basic_cfg.toml +++ b/tests/fixtures/basic_cfg.toml @@ -23,8 +23,8 @@ tag = true current_version = "1.0.0" parse = "(?P\\d+)\\.(?P\\d+)\\.(?P\\d+)(\\-(?P[a-z]+))?" serialize = [ - "{major}.{minor}.{patch}-{release}", - "{major}.{minor}.{patch}" + "{major}.{minor}.{patch}-{release}", + "{major}.{minor}.{patch}" ] [[tool.bumpversion.files]] filename = "setup.py" diff --git a/tests/test_config.py b/tests/test_config.py index fadba1e1..78bde117 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -112,15 +112,28 @@ def test_file_keyword_with_suffix_is_accepted(tmp_path: Path, cfg_file: str, cfg def test_multiple_config_files(tmp_path: Path): + """If there are multiple config files, the first one with content wins.""" setup_cfg = tmp_path / "setup.cfg" - setup_cfg.write_text("[bumpversion]\n" "current_version: 0.10.2\n" "tag: true\n") + setup_cfg.write_text("[metadata]\nname: just-a-name\n") bumpversion_cfg = tmp_path / ".bumpversion.cfg" - bumpversion_cfg.write_text("[bumpversion]\n" "current_version: 0.10.2\n" "tag: false\n") - - cfg_file = config.find_config_file() - cfg = config.get_configuration(cfg_file) + bumpversion_cfg.write_text("\n") + pyproject_toml = tmp_path / "pyproject.toml" + pyproject_toml.write_text( + "[tool.bumpversion]\n" + 'current_version = "0.10.5"\n' + 'parse = "(?P\\\\d+)\\\\.(?P\\\\d+)\\\\.(?P\\\\d+)(\\\\-(?P[a-z]+))?"\n' + "serialize = [\n" + ' "{major}.{minor}.{patch}-{release}",\n' + ' "{major}.{minor}.{patch}"\n' + "]\n" + ) + with inside_dir(tmp_path): + cfg_file = config.find_config_file() + cfg = config.get_configuration(cfg_file) - assert cfg.tag is True + assert cfg.current_version == "0.10.5" + assert cfg.parse == "(?P\\d+)\\.(?P\\d+)\\.(?P\\d+)(\\-(?P[a-z]+))?" + assert cfg.serialize == ["{major}.{minor}.{patch}-{release}", "{major}.{minor}.{patch}"] def test_utf8_message_from_config_file(tmp_path: Path, cfg_file):