@@ -131,3 +131,48 @@ def test_external_command_output_prefix(caplog: pytest.LogCaptureFixture) -> Non
131131 finally :
132132 # Restore the original log record factory
133133 logging .setLogRecordFactory (old_factory )
134+
135+
136+ def test_external_commands_error_includes_package_name (caplog ) -> None :
137+ """Test that package name is included in error logs when context var is set"""
138+ logging .setLogRecordFactory (log .FromagerLogRecord )
139+
140+ req = Requirement ("test-package==1.0.0" )
141+ version = Version ("1.0.0" )
142+
143+ with log .req_ctxvar_context (req , version ):
144+ with caplog .at_level (logging .ERROR ):
145+ with pytest .raises (subprocess .CalledProcessError ):
146+ external_commands .run (["sh" , "-c" , "exit 1" ])
147+
148+ error_logs = [
149+ record .message for record in caplog .records if record .levelname == "ERROR"
150+ ]
151+ assert len (error_logs ) > 0
152+ assert any ("test-package-1.0.0:" in msg for msg in error_logs ), (
153+ f"Expected package name in error logs, got: { error_logs } "
154+ )
155+
156+
157+ def test_format_exception_formats_chained_exceptions () -> None :
158+ """Test that _format_exception formats chained exceptions correctly"""
159+ from fromager import __main__
160+
161+ # Test basic exception formatting
162+ exception_without_cause = subprocess .CalledProcessError (
163+ 1 , ["command" ], output = "some output"
164+ )
165+ message = __main__ ._format_exception (exception_without_cause )
166+ assert "Command '['command']' returned non-zero exit status 1" in message
167+
168+ # Test chained exception formatting with "because"
169+ try :
170+ try :
171+ raise ValueError ("Root cause" )
172+ except ValueError as e :
173+ raise RuntimeError ("Higher level error" ) from e
174+ except RuntimeError as chained_exc :
175+ formatted = __main__ ._format_exception (chained_exc )
176+ assert "Higher level error" in formatted
177+ assert "because" in formatted
178+ assert "Root cause" in formatted
0 commit comments