From fa3ba2a6bdb08f80b8aa07a4b9e7fd1ea82a6aa4 Mon Sep 17 00:00:00 2001 From: Fuxino Date: Wed, 21 Oct 2026 22:26:55 +0200 Subject: [PATCH] Make inputs and backup_dir optional in the config file They can come from -i and -o; missing ones were rejected as a bad configuration file (exit code 6). --- simple_backup/simple_backup.py | 19 +++++++++++-------- tests/test_cli.py | 9 +++++++++ tests/test_config.py | 10 ++++++++-- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/simple_backup/simple_backup.py b/simple_backup/simple_backup.py index 60bae52..42c6c0a 100755 --- a/simple_backup/simple_backup.py +++ b/simple_backup/simple_backup.py @@ -946,19 +946,22 @@ def _read_config(config_file: str, user: str | None = None) -> ConfigArgs: config = configparser.ConfigParser(interpolation=None) config.read(config_file) - section = 'backup' - # Allow compatibility with previous version of config file - try: - inputs = config.get(section, 'inputs') - except configparser.NoSectionError: + if config.has_section('backup'): + section = 'backup' + elif config.has_section('default'): section = 'default' - inputs = config.get(section, 'inputs') + else: + raise configparser.NoSectionError('backup') - config_args['inputs'] = list(set(_expand_inputs([i.strip() for i in inputs.split(',')], user))) + # inputs and backup_dir are optional, since -i and -o can give them. Missing ones are reported by check_params() + inputs = config.get(section, 'inputs', fallback=None) + + if inputs is not None: + config_args['inputs'] = list(set(_expand_inputs([i.strip() for i in inputs.split(',')], user))) # '~' is expanded later, since for remote backups it refers to the home on the server - config_args['output'] = config.get(section, 'backup_dir') + config_args['output'] = config.get(section, 'backup_dir', fallback=None) exclude = config.get(section, 'exclude', fallback='') config_args['exclude'] = [e.strip() for e in exclude.split(',') if e.strip() != ''] diff --git a/tests/test_cli.py b/tests/test_cli.py index 2ed5eb1..ca2a8cc 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -170,6 +170,15 @@ ssh_keyfile=/keys/id assert b.keep == 0 assert (b.ssh_host, b.ssh_user, b.ssh_keyfile) == ('cli-host', 'cli-user', '/cli/key') + def test_inputs_and_output_only_from_cli(self, run_cli: RunCli, tmp_path: Path) -> None: + config_file = tmp_path / 'simple_backup.conf' + config_file.write_text('[backup]\nkeep=3\n') + + assert run_cli('-c', str(config_file), '-i', '/cli/in', '-o', '/cli/out') == 0 + + b = last_backup() + assert (b.inputs, b.output, b.keep) == (['/cli/in'], '/cli/out', 3) + def test_ssh_host_and_user_from_cli_and_config(self, run_cli: RunCli, config: Callable[[str], str]) -> None: config_file = config('backup_dir=/backups\n[server]\nssh_user=user') diff --git a/tests/test_config.py b/tests/test_config.py index 193ae52..a03bfe8 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -224,9 +224,15 @@ backup_dir=/backups sb._read_config(config_file, USER) +def test_inputs_and_backup_dir_optional(tmp_path: Path) -> None: + config_args = sb._read_config(write_config(tmp_path, '[backup]\nkeep=2\n'), USER) + + assert config_args['inputs'] is None + assert config_args['output'] is None + assert config_args['keep'] == 2 + + @pytest.mark.parametrize('text', [ - '[backup]\nbackup_dir=/backups\n', - '[backup]\ninputs=/tmp\n', '[other]\ninputs=/tmp\n', 'inputs=/tmp\n', ])