@@ -56,68 +56,52 @@ use std::process::Output;
5656use tempfile;
5757
5858/// Install environment for Bear
59+ ///
60+ /// Uses `scripts/install.sh` to create a proper installation layout in a temp
61+ /// directory. This exercises the install script on every test run.
5962pub struct InstallEnvironment {
6063 #[ allow( dead_code) ]
6164 install_dir : tempfile:: TempDir ,
62- #[ allow( dead_code) ]
63- bin_dir : tempfile:: TempDir ,
6465 bear : PathBuf ,
6566}
6667
6768impl InstallEnvironment {
6869 pub fn new ( ) -> Result < Self > {
6970 let install_dir = tempfile:: TempDir :: new ( ) . with_context ( || "install dir tempdir failed" ) ?;
70- let bin_dir = tempfile:: TempDir :: new ( ) . with_context ( || "bin dir tempdir failed" ) ?;
71-
72- let driver = Self :: prepare_install_dir ( install_dir. path ( ) ) ?;
73- let bear = Self :: prepare_entry_script ( bin_dir. path ( ) , & driver) ?;
74-
75- Ok ( Self { install_dir, bin_dir, bear } )
76- }
77-
78- fn prepare_install_dir ( path : & Path ) -> Result < PathBuf > {
79- let bin_dir = path. join ( "bin" ) ;
80- std:: fs:: create_dir ( & bin_dir) . with_context ( || format ! ( "create dir={:?}" , & bin_dir) ) ?;
8171
82- let driver = & bin_dir. join ( DRIVER_EXECUTABLE ) ;
83- std:: fs:: copy ( DRIVER_EXECUTABLE_PATH , driver)
84- . with_context ( || format ! ( "copy from={}, to={:?}" , DRIVER_EXECUTABLE_PATH , & driver) ) ?;
72+ Self :: run_install_script ( install_dir. path ( ) ) ?;
8573
86- let wrapper = & bin_dir. join ( WRAPPER_EXECUTABLE ) ;
87- std:: fs:: copy ( WRAPPER_EXECUTABLE_PATH , wrapper)
88- . with_context ( || format ! ( "copy from={}, to={:?}" , WRAPPER_EXECUTABLE_PATH , & wrapper) ) ?;
74+ let bear = install_dir. path ( ) . join ( "bin" ) . join ( "bear" ) ;
8975
90- let lib_dir = path. join ( env ! ( "INTERCEPT_LIBDIR" ) ) ;
91- std:: fs:: create_dir ( & lib_dir) . with_context ( || format ! ( "create dir={:?}" , & lib_dir) ) ?;
92-
93- let library = & lib_dir. join ( PRELOAD_LIBRARY ) ;
94- std:: fs:: copy ( PRELOAD_LIBRARY_PATH , library)
95- . with_context ( || format ! ( "copy from={}, to={:?}" , PRELOAD_LIBRARY_PATH , & library) ) ?;
96-
97- Ok ( bin_dir. join ( env ! ( "DRIVER_EXECUTABLE" ) ) )
76+ Ok ( Self { install_dir, bear } )
9877 }
9978
100- #[ cfg( unix) ]
101- fn prepare_entry_script ( path : & Path , driver : & Path ) -> Result < PathBuf > {
102- let file = path. join ( "bear" ) ;
103- let script = format ! ( "#!/usr/bin/sh\n {driver:?} $@\n " ) ;
104-
105- std:: fs:: write ( & file, script) . with_context ( || format ! ( "writing file={:?}" , & file) ) ?;
106-
107- use std:: os:: unix:: fs:: PermissionsExt ;
108- std:: fs:: set_permissions ( & file, std:: fs:: Permissions :: from_mode ( 0o755 ) ) . unwrap ( ) ;
109-
110- Ok ( file)
111- }
79+ fn run_install_script ( destdir : & Path ) -> Result < ( ) > {
80+ // Find the build artifact directory (same logic as build.rs)
81+ let source_dir = Path :: new ( DRIVER_EXECUTABLE_PATH )
82+ . parent ( )
83+ . with_context ( || "cannot determine artifact directory from DRIVER_EXECUTABLE_PATH" ) ?;
11284
113- #[ cfg( not( unix) ) ]
114- fn prepare_entry_script ( path : & Path , driver : & Path ) -> Result < PathBuf > {
115- let file = path. join ( "bear.bat" ) ;
116- let script = format ! ( "@echo off\r \n {driver:?} %*\r \n " ) ;
85+ let output = std:: process:: Command :: new ( "sh" )
86+ . arg ( INSTALL_SCRIPT_PATH )
87+ . env ( "DESTDIR" , destdir)
88+ . env ( "INTERCEPT_LIBDIR" , INTERCEPT_LIBDIR )
89+ . env ( "SOURCE_DIR" , source_dir)
90+ . output ( )
91+ . with_context ( || "failed to run install.sh" ) ?;
11792
118- std:: fs:: write ( & file, script) . with_context ( || "writing the script failed" ) ?;
93+ if !output. status . success ( ) {
94+ let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
95+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
96+ anyhow:: bail!(
97+ "install.sh failed with exit code {:?}\n stdout: {}\n stderr: {}" ,
98+ output. status. code( ) ,
99+ stdout,
100+ stderr
101+ ) ;
102+ }
119103
120- Ok ( file )
104+ Ok ( ( ) )
121105 }
122106
123107 pub fn path ( & self ) -> & Path {
0 commit comments