From b54f7e1817cd6f510c07b9ba9ca0598d217c6651 Mon Sep 17 00:00:00 2001 From: Fuxino Date: Wed, 21 Oct 2026 22:37:55 +0200 Subject: [PATCH] Exit with simple_backup()'s return code when run as a script --- simple_backup/simple_backup.py | 2 +- tests/test_cli.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/simple_backup/simple_backup.py b/simple_backup/simple_backup.py index c6a0fac..2aede59 100755 --- a/simple_backup/simple_backup.py +++ b/simple_backup/simple_backup.py @@ -1192,4 +1192,4 @@ def simple_backup() -> int: if __name__ == '__main__': - simple_backup() + sys.exit(simple_backup()) diff --git a/tests/test_cli.py b/tests/test_cli.py index ca2a8cc..c3c5847 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2,6 +2,8 @@ import os import pwd +import subprocess +import sys from collections.abc import Callable from pathlib import Path from typing import Any @@ -325,6 +327,20 @@ class TestDetectUser: assert sb._detect_user() == USER +def test_run_as_script(tmp_path: Path) -> None: + """Running the module directly passes simple_backup()'s return code to the shell""" + + repo = Path(sb.__file__).resolve().parents[1] + env = {**os.environ, 'PYTHONPATH': str(repo), 'HOME': str(tmp_path)} + + # No config file and no inputs: check_params() returns 1 + result = subprocess.run([sys.executable, '-m', 'simple_backup.simple_backup', '--no-syslog', + '-c', str(tmp_path / 'missing.conf')], env=env, cwd=tmp_path, + capture_output=True, check=False) + + assert result.returncode == 1 + + class TestTargetUid: @pytest.mark.usefixtures('as_user') def test_non_root(self) -> None: