From d0c533555e39dd938c6b5bd45436a4c030d0b6d6 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Thu, 2 Jan 2020 10:37:31 -0800 Subject: [PATCH] In "borgmatic --help", don't expand $HOME in listing of default "--config" paths. --- NEWS | 3 +++ borgmatic/commands/arguments.py | 5 +++-- borgmatic/config/collect.py | 10 ++++++---- setup.py | 2 +- tests/unit/config/test_collect.py | 8 ++++++++ 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 8e8adce4f..6ed814d2b 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,6 @@ +1.4.22.dev0 + * In "borgmatic --help", don't expand $HOME in listing of default "--config" paths. + 1.4.21 * #268: Override particular configuration options from the command-line via "--override" flag. See the documentation for more information: diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index 630300668..142c3a7e1 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -106,7 +106,8 @@ def parse_arguments(*unparsed_arguments): Given command-line arguments with which this script was invoked, parse the arguments and return them as a dict mapping from subparser name (or "global") to an argparse.Namespace instance. ''' - config_paths = collect.get_default_config_paths() + config_paths = collect.get_default_config_paths(expand_home=True) + unexpanded_config_paths = collect.get_default_config_paths(expand_home=False) global_parser = ArgumentParser(add_help=False) global_group = global_parser.add_argument_group('global arguments') @@ -118,7 +119,7 @@ def parse_arguments(*unparsed_arguments): dest='config_paths', default=config_paths, help='Configuration filenames or directories, defaults to: {}'.format( - ' '.join(config_paths) + ' '.join(unexpanded_config_paths) ), ) global_group.add_argument( diff --git a/borgmatic/config/collect.py b/borgmatic/config/collect.py index 59d7dfe5c..ef04bd499 100644 --- a/borgmatic/config/collect.py +++ b/borgmatic/config/collect.py @@ -1,15 +1,17 @@ import os -def get_default_config_paths(): +def get_default_config_paths(expand_home=True): ''' Based on the value of the XDG_CONFIG_HOME and HOME environment variables, return a list of default configuration paths. This includes both system-wide configuration and configuration in the current user's home directory. + + Don't expand the home directory ($HOME) if the expand home flag is False. ''' - user_config_directory = os.getenv('XDG_CONFIG_HOME') or os.path.expandvars( - os.path.join('$HOME', '.config') - ) + user_config_directory = os.getenv('XDG_CONFIG_HOME') or os.path.join('$HOME', '.config') + if expand_home: + user_config_directory = os.path.expandvars(user_config_directory) return [ '/etc/borgmatic/config.yaml', diff --git a/setup.py b/setup.py index d3ae21683..15dfed55c 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import find_packages, setup -VERSION = '1.4.21' +VERSION = '1.4.22.dev0' setup( diff --git a/tests/unit/config/test_collect.py b/tests/unit/config/test_collect.py index 2cb5b584d..4c4420a47 100644 --- a/tests/unit/config/test_collect.py +++ b/tests/unit/config/test_collect.py @@ -21,6 +21,14 @@ def test_get_default_config_paths_prefers_xdg_config_home_for_user_config_path() assert '/home/user/.etc/borgmatic/config.yaml' in config_paths +def test_get_default_config_paths_does_not_expand_home_when_false(): + flexmock(module.os, environ={'HOME': '/home/user'}) + + config_paths = module.get_default_config_paths(expand_home=False) + + assert '$HOME/.config/borgmatic/config.yaml' in config_paths + + def test_collect_config_filenames_collects_given_files(): config_paths = ('config.yaml', 'other.yaml') flexmock(module.os.path).should_receive('isdir').and_return(False)