From 535f7a33fe49d6186e67160cbdbc1d926537431d Mon Sep 17 00:00:00 2001 From: Fuxino Date: Wed, 21 Oct 2026 22:16:47 +0200 Subject: [PATCH] Reject keep values below -1 A keep below -1 made remove_old_backups() delete every backup, including the new one, and then crash. --- simple_backup/simple_backup.py | 5 +++++ tests/test_cli.py | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/simple_backup/simple_backup.py b/simple_backup/simple_backup.py index 6760d2a..ed11a74 100755 --- a/simple_backup/simple_backup.py +++ b/simple_backup/simple_backup.py @@ -1082,6 +1082,11 @@ def simple_backup() -> int: ssh_host = args.ssh_host if args.ssh_host is not None else config_args['ssh_host'] ssh_user = args.ssh_user if args.ssh_user is not None else config_args['ssh_user'] + if keep < -1: + logger.critical('Invalid number of backups to keep: %d (use -1 to keep all)', keep) + + return 6 + if (ssh_host is None) != (ssh_user is None): logger.critical('Both SSH host and SSH user are needed for remote backup') diff --git a/tests/test_cli.py b/tests/test_cli.py index 0e99901..2ed5eb1 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -191,6 +191,18 @@ ssh_keyfile=/keys/id assert FakeBackup.instances == [] assert 'Both SSH host and SSH user are needed' in caplog.text + @pytest.mark.parametrize('config_keep, argv', [ + ('keep=-2', []), + ('', ['-k', '-2']), + ]) + def test_keep_below_minus_one(self, run_cli: RunCli, config: Callable[[str], str], + caplog: pytest.LogCaptureFixture, config_keep: str, argv: list[str]) -> None: + config_file = config(f'backup_dir=/backups\n{config_keep}') + + assert run_cli('-c', config_file, *argv) == 6 + assert FakeBackup.instances == [] + assert 'Invalid number of backups to keep: -2' in caplog.text + def test_bad_config(self, run_cli: RunCli, config: Callable[[str], str], caplog: pytest.LogCaptureFixture) -> None: config_file = config('backup_dir=/backups\nkeep=abc')