@@ -55,6 +55,20 @@ impl TestCommand {
5555 // `CARGO_*` and `NEXTEST_*` variables set directly on `cmd` below.
5656 lctx. env . apply_env ( & mut cmd) ;
5757
58+ if let Some ( out_dir) = lctx
59+ . rust_build_meta
60+ . build_script_out_dirs
61+ . get ( package. id ( ) . repr ( ) )
62+ {
63+ // Convert the output directory to an absolute path.
64+ let out_dir = lctx. rust_build_meta . target_directory . join ( out_dir) ;
65+ cmd. env ( "OUT_DIR" , & out_dir) ;
66+
67+ // Apply the user-provided environment variables from the build script. This is
68+ // supported by cargo test, but discouraged.
69+ apply_build_script_env ( & mut cmd, & out_dir) ;
70+ }
71+
5872 cmd. current_dir ( cwd)
5973 // This environment variable is set to indicate that tests are being run under nextest.
6074 . env ( "NEXTEST" , "1" )
@@ -70,17 +84,6 @@ impl TestCommand {
7084
7185 apply_ld_dyld_env ( & mut cmd, lctx. dylib_path ) ;
7286
73- if let Some ( out_dir) = lctx
74- . rust_build_meta
75- . build_script_out_dirs
76- . get ( package. id ( ) . repr ( ) )
77- {
78- // Convert the output directory to an absolute path.
79- let out_dir = lctx. rust_build_meta . target_directory . join ( out_dir) ;
80- cmd. env ( "OUT_DIR" , & out_dir) ;
81- apply_build_script_env ( & mut cmd, out_dir) ;
82- }
83-
8487 // Expose paths to non-test binaries at runtime so that relocated paths work.
8588 // These paths aren't exposed by Cargo at runtime, so use a NEXTEST_BIN_EXE prefix.
8689 for ( name, path) in non_test_binaries {
@@ -196,7 +199,7 @@ fn apply_package_env(cmd: &mut std::process::Command, package: &PackageMetadata<
196199}
197200
198201/// Applies environment variables spcified by the build script via `cargo::rustc-env`
199- fn apply_build_script_env ( cmd : & mut std:: process:: Command , out_dir : Utf8PathBuf ) {
202+ fn apply_build_script_env ( cmd : & mut std:: process:: Command , out_dir : & Utf8Path ) {
200203 let Some ( out_dir_parent) = out_dir. parent ( ) else {
201204 warn ! ( "could not determine parent directory of output directory {out_dir}" ) ;
202205 return ;
@@ -225,7 +228,7 @@ where
225228{
226229 for line in out_file. lines ( ) {
227230 let Ok ( line) = line else {
228- warn ! ( "found line with invalid UTF8 in build script output file at {out_file_path}" ) ;
231+ warn ! ( "in build script output ` {out_file_path}`, found line with invalid UTF-8 " ) ;
229232 continue ;
230233 } ;
231234 // `cargo::rustc-env` is the official syntax since `cargo` 1.77, `cargo:rustc-env` is
@@ -236,11 +239,11 @@ where
236239 else {
237240 continue ;
238241 } ;
239- let Some ( split ) = key_val. find ( '=' ) else {
242+ let Some ( ( k , v ) ) = key_val. split_once ( '=' ) else {
240243 warn ! ( "rustc-env variable '{key_val}' has no value in {out_file_path}, skipping" ) ;
241244 continue ;
242245 } ;
243- callback ( & key_val [ ..split ] , & key_val [ split + 1 .. ] ) ;
246+ callback ( k , v ) ;
244247 }
245248}
246249
@@ -292,21 +295,35 @@ pub(crate) fn apply_ld_dyld_env(cmd: &mut std::process::Command, dylib_path: &Os
292295
293296#[ cfg( test) ]
294297mod tests {
298+ use super :: * ;
299+ use indoc:: indoc;
300+
295301 #[ test]
296- fn parse_build_script_output ( ) {
297- let out_file = "some_other_line\n cargo::rustc-env=NEW_VAR=new_val\n cargo:rustc-env=OLD_VAR=old_val\n cargo::rustc-env=NEW_MISSING_VALUE\n cargo:rustc-env=OLD_MISSING_VALUE" ;
302+ fn parse_build_script ( ) {
303+ let out_file = indoc ! { "
304+ some_other_line
305+ cargo::rustc-env=NEW_VAR=new_val
306+ cargo:rustc-env=OLD_VAR=old_val
307+ cargo::rustc-env=NEW_MISSING_VALUE
308+ cargo:rustc-env=OLD_MISSING_VALUE
309+ cargo:rustc-env=NEW_EMPTY_VALUE=
310+ " } ;
298311
299312 let mut key_vals = Vec :: new ( ) ;
300- super :: parse_build_script_output (
301- std :: io :: BufReader :: new ( std:: io:: Cursor :: new ( out_file) ) ,
302- camino :: Utf8Path :: new ( "<test input>" ) ,
313+ parse_build_script_output (
314+ BufReader :: new ( std:: io:: Cursor :: new ( out_file) ) ,
315+ Utf8Path :: new ( "<test input>" ) ,
303316 |key, val| key_vals. push ( ( key. to_owned ( ) , val. to_owned ( ) ) ) ,
304317 ) ;
305318
306- assert_eq ! ( key_vals. len( ) , 2 ) ;
307- assert_eq ! ( key_vals[ 0 ] . 0 , "NEW_VAR" ) ;
308- assert_eq ! ( key_vals[ 0 ] . 1 , "new_val" ) ;
309- assert_eq ! ( key_vals[ 1 ] . 0 , "OLD_VAR" ) ;
310- assert_eq ! ( key_vals[ 1 ] . 1 , "old_val" ) ;
319+ assert_eq ! (
320+ key_vals,
321+ vec![
322+ ( "NEW_VAR" . to_owned( ) , "new_val" . to_owned( ) ) ,
323+ ( "OLD_VAR" . to_owned( ) , "old_val" . to_owned( ) ) ,
324+ ( "NEW_EMPTY_VALUE" . to_owned( ) , "" . to_owned( ) ) ,
325+ ] ,
326+ "parsed key-value pairs match"
327+ ) ;
311328 }
312329}
0 commit comments