Browse Source

Hard-code default config paths in help strings

This fixes a bug with fish completions. Before this commit,
`borgmatic --fish-completion` would generate a fish completion script
with an expanded XDG_CONFIG_HOME (if this variable is set).

This is problematic for package maintainers if the variable is set
during the creation of a package.

Moreover, it is problematic if the user has set this variable because
the fish completion scripts checks its own consistency with `borg
--fish-completion` during startup to detect version mismatches. If the
user has a packaged fish completion file with "~/.config/$HOME" in it,
this won't match the output of `borg --fish-completion` because the
latter contains the expansion of $XDG_CONFIG_HOME. As a result, the
script will incorrectly conclude that it is outdated.
Tim Ruffing 3 weeks ago
parent
commit
573405bb88
3 changed files with 10 additions and 22 deletions
  1. 4 5
      borgmatic/commands/arguments.py
  2. 6 9
      borgmatic/config/collect.py
  3. 0 8
      tests/unit/config/test_collect.py

+ 4 - 5
borgmatic/commands/arguments.py

@@ -568,8 +568,7 @@ def make_parsers(schema, unparsed_arguments):  # noqa: PLR0915
     ignoring actions, and the combined parser is handy for displaying help that includes everything:
     global flags, a list of actions, etc.
     '''
-    config_paths = collect.get_default_config_paths(expand_home=True)
-    unexpanded_config_paths = collect.get_default_config_paths(expand_home=False)
+    config_paths = collect.get_default_config_paths()
 
     # Using allow_abbrev=False here prevents the global parser from erroring about "ambiguous"
     # options like --encryption. Such options are intended for an action parser rather than the
@@ -582,7 +581,7 @@ def make_parsers(schema, unparsed_arguments):  # noqa: PLR0915
         '--config',
         dest='config_paths',
         action='append',
-        help=f"Configuration filename or directory, can specify flag multiple times, defaults to: -c {' -c '.join(unexpanded_config_paths)}",
+        help='Configuration filename or directory, can specify flag multiple times, defaults to "-c \'/etc/borgmatic/config.yaml\' -c \'/etc/borgmatic.d\' -c \'$XDG_CONFIG_HOME/borgmatic/config.yaml\' -c \'$XDG_CONFIG_HOME/borgmatic.d\'", where $XDG_CONFIG_HOME defaults to "$HOME/.config"',
     )
     global_group.add_argument(
         '-n',
@@ -1215,7 +1214,7 @@ def make_parsers(schema, unparsed_arguments):  # noqa: PLR0915
         '--destination',
         dest='destination_path',
         default=config_paths[0],
-        help=f'Destination configuration file (or directory if using --split), default: {unexpanded_config_paths[0]}',
+        help='Destination configuration file (or directory if using --split), default: /etc/borgmatic/config.yaml',
     )
     config_generate_group.add_argument(
         '--overwrite',
@@ -2015,7 +2014,7 @@ def parse_arguments(schema, *unparsed_arguments):
     )
 
     if not arguments['global'].config_paths:
-        arguments['global'].config_paths = collect.get_default_config_paths(expand_home=True)
+        arguments['global'].config_paths = collect.get_default_config_paths()
 
     for action_name in ('bootstrap', 'generate', 'validate'):
         if action_name in arguments and len(arguments) > HIGHLANDER_ACTION_ARGUMENTS_COUNT:

+ 6 - 9
borgmatic/config/collect.py

@@ -1,17 +1,14 @@
 import os
 
 
-def get_default_config_paths(expand_home=True):
+def get_default_config_paths():
     '''
-    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.
+    Return a list of default configuration paths. This includes both system-wide
+    configuration and configuration in the current user's home directory.
     '''
-    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)
+    user_config_directory = os.path.expandvars(
+        os.getenv('XDG_CONFIG_HOME') or os.path.join('$HOME', '.config')
+    )
 
     return [
         '/etc/borgmatic/config.yaml',

+ 0 - 8
tests/unit/config/test_collect.py

@@ -21,14 +21,6 @@ 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_yml_file_endings():
     config_paths = ('config.yaml', '/etc/borgmatic.d')
     mock_path = flexmock(module.os.path)