From a8505507831550da7caca6d43cee5fb4d4234e87 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Wed, 18 Dec 2024 09:39:55 +0100 Subject: [PATCH] Don't fail with --compare when a .nix file is missing on disk The switch --compare is typically used during CI, to check whether all automatically generated files are up to date. When some files are missing (e.g. a ROS package was added but its Nix expression was not generated), ros2nix would fail with "FileNotFoundError" and would not return exit code 2 as it should. This commit fixes that. Missing files are reported with an error message and execution continues. Exit code 2 is correctly reported. --- ros2nix/ros2nix.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ros2nix/ros2nix.py b/ros2nix/ros2nix.py index 69235e9..29eb455 100755 --- a/ros2nix/ros2nix.py +++ b/ros2nix/ros2nix.py @@ -75,9 +75,16 @@ def file_writer(path: str, compare: bool): yield f finally: if compare: - ondisk = open(path, "r", encoding="utf-8").read() + global compare_failed + ondisk = None + try: + ondisk = open(path, "r", encoding="utf-8").read() + except Exception as e: + compare_failed = True + err(f'Cannot read {path}: {e}') + current = f.getvalue() - if current != ondisk: + if ondisk is not None and current != ondisk: err(f"{path} is not up-to-date") for line in difflib.unified_diff( ondisk.splitlines(), @@ -86,7 +93,6 @@ def file_writer(path: str, compare: bool): tofile="up-to-date", ): print(line) - global compare_failed compare_failed = True f.close()