11use crate :: antithesis:: { TestLocation , is_running_in_antithesis} ;
2- use crate :: control:: { currently_in_test_context, with_test_context} ;
2+ use crate :: control:: { currently_in_test_context, panic_message , with_test_context} ;
33use crate :: protocol:: { Channel , Connection , HANDSHAKE_STRING , SERVER_CRASHED_MESSAGE } ;
44use crate :: test_case:: { ASSUME_FAIL_STRING , STOP_TEST_STRING , TestCase } ;
55use ciborium:: Value ;
@@ -145,11 +145,17 @@ fn take_panic_info() -> Option<(String, String, String, Backtrace)> {
145145/// Frame numbers are renumbered to start at 0.
146146fn format_backtrace ( bt : & Backtrace , full : bool ) -> String {
147147 let backtrace_str = format ! ( "{}" , bt) ;
148-
149148 if full {
150149 return backtrace_str;
151150 }
151+ filter_backtrace ( & backtrace_str)
152+ }
152153
154+ /// Filter a backtrace string to "short" format.
155+ ///
156+ /// Keeps only frames between `__rust_end_short_backtrace` and
157+ /// `__rust_begin_short_backtrace` markers, renumbering from 0.
158+ fn filter_backtrace ( backtrace_str : & str ) -> String {
153159 // Filter to short backtrace: keep lines between the markers
154160 // Frame groups look like:
155161 // N: function::name
@@ -266,20 +272,27 @@ fn init_panic_hook() {
266272}
267273
268274fn ensure_hegel_installed ( ) -> Result < String , String > {
269- let venv_dir = format ! ( "{HEGEL_SERVER_DIR}/venv" ) ;
275+ install_hegel_server ( HEGEL_SERVER_DIR , HEGEL_SERVER_VERSION )
276+ }
277+
278+ /// Install the hegel server into a directory using `uv`.
279+ ///
280+ /// Returns the path to the installed `hegel` binary on success.
281+ fn install_hegel_server ( server_dir : & str , version : & str ) -> Result < String , String > {
282+ let venv_dir = format ! ( "{server_dir}/venv" ) ;
270283 let version_file = format ! ( "{venv_dir}/hegel-version" ) ;
271284 let hegel_bin = format ! ( "{venv_dir}/bin/hegel" ) ;
272- let install_log = format ! ( "{HEGEL_SERVER_DIR }/install.log" ) ;
285+ let install_log = format ! ( "{server_dir }/install.log" ) ;
273286
274287 // Check cached version
275288 if let Ok ( cached) = std:: fs:: read_to_string ( & version_file) {
276- if cached. trim ( ) == HEGEL_SERVER_VERSION && std:: path:: Path :: new ( & hegel_bin) . is_file ( ) {
289+ if cached. trim ( ) == version && std:: path:: Path :: new ( & hegel_bin) . is_file ( ) {
277290 return Ok ( hegel_bin) ;
278291 }
279292 }
280293
281- std:: fs:: create_dir_all ( HEGEL_SERVER_DIR )
282- . map_err ( |e| format ! ( "Failed to create {HEGEL_SERVER_DIR }: {e}" ) ) ?;
294+ std:: fs:: create_dir_all ( server_dir )
295+ . map_err ( |e| format ! ( "Failed to create {server_dir }: {e}" ) ) ?;
283296
284297 let log_file = std:: fs:: File :: create ( & install_log)
285298 . map_err ( |e| format ! ( "Failed to create install log: {e}" ) ) ?;
@@ -310,7 +323,7 @@ fn ensure_hegel_installed() -> Result<String, String> {
310323 "install" ,
311324 "--python" ,
312325 & python_path,
313- & format ! ( "hegel-core=={HEGEL_SERVER_VERSION }" ) ,
326+ & format ! ( "hegel-core=={version }" ) ,
314327 ] )
315328 . stderr ( log_file. try_clone ( ) . unwrap ( ) )
316329 . stdout ( log_file)
@@ -319,7 +332,7 @@ fn ensure_hegel_installed() -> Result<String, String> {
319332 if !status. success ( ) {
320333 let log = std:: fs:: read_to_string ( & install_log) . unwrap_or_default ( ) ;
321334 return Err ( format ! (
322- "Failed to install hegel-core (version: {HEGEL_SERVER_VERSION }). \
335+ "Failed to install hegel-core (version: {version }). \
323336 Set {HEGEL_SERVER_COMMAND_ENV} to a hegel binary path to skip installation.\n \
324337 Install log:\n {log}"
325338 ) ) ;
@@ -329,7 +342,7 @@ fn ensure_hegel_installed() -> Result<String, String> {
329342 return Err ( format ! ( "hegel not found at {hegel_bin} after installation" ) ) ;
330343 }
331344
332- std:: fs:: write ( & version_file, HEGEL_SERVER_VERSION )
345+ std:: fs:: write ( & version_file, version )
333346 . map_err ( |e| format ! ( "Failed to write version file: {e}" ) ) ?;
334347
335348 Ok ( hegel_bin)
@@ -793,18 +806,7 @@ where
793806
794807 let test_failed = !passed || got_interesting. load ( Ordering :: SeqCst ) ;
795808
796- if is_running_in_antithesis ( ) {
797- #[ cfg( not( feature = "antithesis" ) ) ]
798- panic ! (
799- "When Hegel is run inside of Antithesis, it requires the `antithesis` feature. \
800- You can add it with {{ features = [\" antithesis\" ] }}."
801- ) ;
802-
803- #[ cfg( feature = "antithesis" ) ]
804- if let Some ( ref loc) = self . test_location {
805- crate :: antithesis:: emit_assertion ( loc, !test_failed) ;
806- }
807- }
809+ handle_antithesis_reporting ( self . test_location . as_ref ( ) , test_failed) ;
808810
809811 if test_failed {
810812 let msg = match & final_result {
@@ -909,14 +911,19 @@ fn run_test_case<F: FnMut(TestCase)>(
909911 tc_result
910912}
911913
912- /// Extract a message from a panic payload.
913- fn panic_message ( payload : & Box < dyn std:: any:: Any + Send > ) -> String {
914- if let Some ( s) = payload. downcast_ref :: < & str > ( ) {
915- s. to_string ( )
916- } else if let Some ( s) = payload. downcast_ref :: < String > ( ) {
917- s. clone ( )
918- } else {
919- "Unknown panic" . to_string ( )
914+ /// Report test results to Antithesis if running in that environment.
915+ fn handle_antithesis_reporting ( test_location : Option < & TestLocation > , test_failed : bool ) {
916+ if is_running_in_antithesis ( ) {
917+ #[ cfg( not( feature = "antithesis" ) ) ]
918+ panic ! (
919+ "When Hegel is run inside of Antithesis, it requires the `antithesis` feature. \
920+ You can add it with {{ features = [\" antithesis\" ] }}."
921+ ) ;
922+
923+ #[ cfg( feature = "antithesis" ) ]
924+ if let Some ( loc) = test_location {
925+ crate :: antithesis:: emit_assertion ( loc, !test_failed) ;
926+ }
920927 }
921928}
922929
0 commit comments