Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load cli args before config file #232

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions bumpversion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
DESCRIPTION = "{}: v{} (using Python v{})".format(
__title__,
__version__,
sys.version.split("\n")[0].split(" ")[0]
sys.version.split("\n", maxsplit=1)[0].split(" ", maxsplit=1)[0]
)
VCS = [Git, Mercurial]

Expand Down Expand Up @@ -86,12 +86,13 @@ def main(original_args=None):
if hasattr(known_args, "config_file"):
explicit_config = known_args.config_file
config_file = _determine_config_file(explicit_config)
config, config_file_exists, config_newlines, part_configs, files = _load_configuration(
config_file, explicit_config, defaults,
)
config, config_file_exists, config_newlines = _load_configuration_file(config_file,
explicit_config,
defaults)
known_args, parser2, remaining_argv = _parse_arguments_phase_2(
args, known_args, defaults, root_parser
)
part_configs, files = _load_configuration(config, defaults)
version_config = _setup_versionconfig(known_args, part_configs)
current_version = version_config.parse(known_args.current_version)
context = dict(
Expand Down Expand Up @@ -247,7 +248,7 @@ def _determine_config_file(explicit_config):
return ".bumpversion.cfg"


def _load_configuration(config_file, explicit_config, defaults):
def _load_configuration_file(config_file, explicit_config, defaults):
# setup.cfg supports interpolation - for compatibility we must do the same.
if os.path.basename(config_file) == "setup.cfg":
config = ConfigParser("")
Expand All @@ -263,7 +264,7 @@ def _load_configuration(config_file, explicit_config, defaults):
if explicit_config:
raise argparse.ArgumentTypeError(message)
logger.info(message)
return config, config_file_exists, None, {}, []
return config, config_file_exists, None

logger.info("Reading config file %s:", config_file)

Expand Down Expand Up @@ -302,6 +303,10 @@ def _load_configuration(config_file, explicit_config, defaults):
except NoOptionError:
pass # no default value then ;)

return config, config_file_exists, config_newlines


def _load_configuration(config, defaults):
part_configs = {}
files = []

Expand Down Expand Up @@ -370,7 +375,7 @@ def _load_configuration(config_file, explicit_config, defaults):
files.append(ConfiguredFile(filename_glob, version_config))
else:
files.append(ConfiguredFile(filename, version_config))
return config, config_file_exists, config_newlines, part_configs, files
return part_configs, files


def _parse_arguments_phase_2(args, known_args, defaults, root_parser):
Expand Down Expand Up @@ -475,7 +480,8 @@ def _parse_arguments_phase_3(remaining_argv, positionals, defaults, parser2):
action="store_true",
default=False,
dest="no_configured_files",
help="Only replace the version in files specified on the command line, ignoring the files from the configuration file.",
help="Only replace the version in files specified on the command line, "
"ignoring the files from the configuration file.",
)
parser3.add_argument(
"--dry-run",
Expand Down
40 changes: 38 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _mock_calls_to_string(called_mock):
[--tag-message TAG_MESSAGE]
[--message COMMIT_MSG]
part
[file [file ...]]
[file ...]
""".strip().splitlines()

EXPECTED_USAGE = (r"""
Expand Down Expand Up @@ -247,7 +247,7 @@ def test_defaults_in_usage_with_config(tmpdir, capsys):
assert "New version that should be in the files (default: 19)" in out
assert "[--current-version VERSION]" in out
assert "[--new-version VERSION]" in out
assert "[file [file ...]]" in out
assert "[file ...]" in out


def test_missing_explicit_config_file(tmpdir):
Expand Down Expand Up @@ -1261,6 +1261,42 @@ def test_multiple_serialize_two_part_patch_configfile(tmpdir):

assert '0.6.1' == tmpdir.join("fileD").read()

def test_serialize_via_cli_takes_priority_over_bumpversion_cfg(tmpdir):
tmpdir.join("fileD").write("0.6")
tmpdir.chdir()

tmpdir.join(".bumpversion.cfg").write(r"""[bumpversion]
current_version = 0.6
serialize =
{major}.{minor}
parse = (?P<major>\d+)\.(?P<minor>\d+)(\.(?P<patch>\d+))?
[bumpversion:file:fileD]
""")

main(['minor',
'--serialize', '{major}.{minor}.{patch}'])

assert '0.7.0' == tmpdir.join("fileD").read()
assert '0.7.0' in tmpdir.join(".bumpversion.cfg").read()

def test_serialize_via_cli_takes_priority_over_bumpversion_cfg_with_env_variable(tmpdir):
tmpdir.join("fileD").write("0.6")
tmpdir.chdir()

tmpdir.join(".bumpversion.cfg").write(r"""[bumpversion]
current_version = 0.6
serialize = {major}.{minor}
parse = (?P<major>\d+)\.(?P<minor>\d+)(\.(?P<patch>\d+))?
[bumpversion:file:fileD]
""")

os.environ['BUILD_ID'] = 'pr_123'

main(['minor',
'--serialize', '{major}.{minor}.{patch}+{$BUILD_ID}'])

assert '0.7.0+pr_123' == tmpdir.join("fileD").read()
assert '0.7.0+pr_123' in tmpdir.join(".bumpversion.cfg").read()

def test_search_uses_shortest_possible_custom_search_pattern(tmpdir):
config = dedent(r"""
Expand Down