From 716f0c4e1800df5d279366369f3356732853da90 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Mon, 28 Sep 2026 09:58:40 +0100 Subject: [PATCH] nixos-rebuild-ng: handle JSONDecodeError --- .../nixos-rebuild-ng/src/nixos_rebuild/nix.py | 5 +- .../ni/nixos-rebuild-ng/src/tests/test_nix.py | 67 +++++++++---------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py index d157839a38b7..de6d5ff8b60e 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/nixos_rebuild/nix.py @@ -511,15 +511,16 @@ def list_generations(profile: Profile) -> list[GenerationJson]: profile.path.parent / f"{profile.path.name}-{generation.id}-link" ) - j: NixOSVersionJson = {} + j: NixOSVersionJson try: result = run_wrapper( [generation_path / "sw/bin/nixos-version", "--json"], capture_output=True, ).stdout j = json.loads(result) - except (OSError, CalledProcessError) as ex: + except (OSError, CalledProcessError, json.JSONDecodeError) as ex: logger.debug("could not get configuration revision: %s", ex) + j = {} return GenerationJson( generation=generation.id, diff --git a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py index 3b243678bf07..e65531090a2e 100644 --- a/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py +++ b/pkgs/by-name/ni/nixos-rebuild-ng/src/tests/test_nix.py @@ -590,65 +590,64 @@ def test_list_generations( mock_get_generations: Mock, tmp_path: Path, ) -> None: - mock_run.return_value = CompletedProcess( - args=[], - returncode=0, - stdout=json.dumps({"nixosVersion": "26.11.20260926.dirty"}), - ) - assert n.list_generations(m.Profile("system", tmp_path)) == [ - { - "configurationRevision": "Unknown", - "current": True, - "date": "2024-11-07 23:54:17", - "generation": 2, - "kernelVersion": "Unknown", - "nixosVersion": "26.11.20260926.dirty", - "specialisations": [], - }, - { - "configurationRevision": "Unknown", - "current": False, - "date": "2024-11-07 23:54:17", - "generation": 1, - "kernelVersion": "Unknown", - "nixosVersion": "26.11.20260926.dirty", - "specialisations": [], - }, - ] - + # happy path mock_run.return_value = CompletedProcess( args=[], returncode=0, stdout=json.dumps( { - "configurationRevision": "dirty", + "configurationRevision": "3f0180ea99a4c8277961825ec5fca2b50a0eca75", "kernelVersion": "7.2.8", - "nixosVersion": "26.11.20260926.dirty", - "specialisations": [], + "nixosVersion": "26.11.20260925.e94cb15", + "specialisations": ["foo", "bar"], } ), ) assert n.list_generations(m.Profile("system", tmp_path)) == [ { - "configurationRevision": "dirty", + "configurationRevision": "3f0180ea99a4c8277961825ec5fca2b50a0eca75", "current": True, "date": "2024-11-07 23:54:17", "generation": 2, "kernelVersion": "7.2.8", - "nixosVersion": "26.11.20260926.dirty", - "specialisations": [], + "nixosVersion": "26.11.20260925.e94cb15", + "specialisations": ["foo", "bar"], }, { - "configurationRevision": "dirty", + "configurationRevision": "3f0180ea99a4c8277961825ec5fca2b50a0eca75", "current": False, "date": "2024-11-07 23:54:17", "generation": 1, "kernelVersion": "7.2.8", - "nixosVersion": "26.11.20260926.dirty", + "nixosVersion": "26.11.20260925.e94cb15", + "specialisations": ["foo", "bar"], + }, + ] + + # parsing invalid JSON + mock_run.return_value = CompletedProcess(args=[], returncode=0, stdout="garbage") + assert n.list_generations(m.Profile("system", tmp_path)) == [ + { + "configurationRevision": "Unknown", + "current": True, + "date": "2024-11-07 23:54:17", + "generation": 2, + "kernelVersion": "Unknown", + "nixosVersion": "Unknown", + "specialisations": [], + }, + { + "configurationRevision": "Unknown", + "current": False, + "date": "2024-11-07 23:54:17", + "generation": 1, + "kernelVersion": "Unknown", + "nixosVersion": "Unknown", "specialisations": [], }, ] + # error calling nixos-version mock_run.side_effect = CalledProcessError(returncode=1, cmd=[]) assert n.list_generations(m.Profile("system", tmp_path)) == [ {