Skip to content

Commit 9a24ab8

Browse files
authored
Merge pull request #1026 from LalatenduMohanty/fix/get-distributions-unbound-error
fix(build_environment): re-raise exception for the JSON parse failure
2 parents b5df8e2 + 2f588af commit 9a24ab8

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/fromager/build_environment.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ def get_distributions(self) -> typing.Mapping[str, Version]:
241241
mapping = json.loads(result.strip())
242242
except Exception:
243243
logger.exception("failed to de-serialize JSON: %s", result)
244+
raise
244245
return {name: Version(version) for name, version in sorted(mapping.items())}
245246

246247

tests/test_build_environment.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import json
12
import textwrap
23
from unittest.mock import Mock, patch
34

5+
import pytest
46
from packaging.requirements import Requirement
57
from packaging.version import Version
68

@@ -81,3 +83,24 @@ def test_missing_dependency_pattern_resolution_impossible() -> None:
8183
""")
8284
match = build_environment._uv_missing_dependency_pattern.search(msg)
8385
assert match is not None
86+
87+
88+
def test_get_distributions_valid_json() -> None:
89+
"""get_distributions returns a mapping of package names to versions."""
90+
env = Mock(spec=build_environment.BuildEnvironment)
91+
env.python = "/fake/python3"
92+
env.run.return_value = json.dumps({"setuptools": "69.5.1", "pip": "24.0"})
93+
94+
result = build_environment.BuildEnvironment.get_distributions(env)
95+
96+
assert result == {"pip": Version("24.0"), "setuptools": Version("69.5.1")}
97+
98+
99+
def test_get_distributions_invalid_json_raises() -> None:
100+
"""get_distributions raises on invalid JSON instead of UnboundLocalError."""
101+
env = Mock(spec=build_environment.BuildEnvironment)
102+
env.python = "/fake/python3"
103+
env.run.return_value = "not valid json"
104+
105+
with pytest.raises(json.JSONDecodeError):
106+
build_environment.BuildEnvironment.get_distributions(env)

0 commit comments

Comments
 (0)