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.
This commit is contained in:
Michal Sojka 2024-12-18 09:39:55 +01:00
parent c42d7ecaa1
commit a850550783

View file

@ -75,9 +75,16 @@ def file_writer(path: str, compare: bool):
yield f yield f
finally: finally:
if compare: 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() current = f.getvalue()
if current != ondisk: if ondisk is not None and current != ondisk:
err(f"{path} is not up-to-date") err(f"{path} is not up-to-date")
for line in difflib.unified_diff( for line in difflib.unified_diff(
ondisk.splitlines(), ondisk.splitlines(),
@ -86,7 +93,6 @@ def file_writer(path: str, compare: bool):
tofile="up-to-date", tofile="up-to-date",
): ):
print(line) print(line)
global compare_failed
compare_failed = True compare_failed = True
f.close() f.close()