File tree 4 files changed +68
-2
lines changed
4 files changed +68
-2
lines changed Original file line number Diff line number Diff line change @@ -789,7 +789,7 @@ impl Command {
789
789
/// or [`Command::envs`]. In addition, it will prevent the spawned child process from inheriting
790
790
/// any environment variable from its parent process.
791
791
///
792
- /// After calling [`Command::env_remove `], the iterator from [`Command::get_envs`] will be
792
+ /// After calling [`Command::env_clear `], the iterator from [`Command::get_envs`] will be
793
793
/// empty.
794
794
///
795
795
/// You can use [`Command::env_remove`] to clear a single mapping.
Original file line number Diff line number Diff line change @@ -537,7 +537,7 @@ fn env_empty() {
537
537
#[ test]
538
538
#[ cfg( not( windows) ) ]
539
539
#[ cfg_attr( any( target_os = "emscripten" , target_env = "sgx" ) , ignore) ]
540
- fn main ( ) {
540
+ fn debug_print ( ) {
541
541
const PIDFD : & ' static str =
542
542
if cfg ! ( target_os = "linux" ) { " create_pidfd: false,\n " } else { "" } ;
543
543
@@ -623,6 +623,51 @@ fn main() {
623
623
cwd: Some(
624
624
"/some/path",
625
625
),
626
+ {PIDFD}}}"#
627
+ )
628
+ ) ;
629
+
630
+ let mut command_with_removed_env = Command :: new ( "boring-name" ) ;
631
+ command_with_removed_env. env_remove ( "FOO" ) . env_remove ( "BAR" ) ;
632
+ assert_eq ! ( format!( "{command_with_removed_env:?}" ) , r#"env -u BAR -u FOO "boring-name""# ) ;
633
+ assert_eq ! (
634
+ format!( "{command_with_removed_env:#?}" ) ,
635
+ format!(
636
+ r#"Command {{
637
+ program: "boring-name",
638
+ args: [
639
+ "boring-name",
640
+ ],
641
+ env: CommandEnv {{
642
+ clear: false,
643
+ vars: {{
644
+ "BAR": None,
645
+ "FOO": None,
646
+ }},
647
+ }},
648
+ {PIDFD}}}"#
649
+ )
650
+ ) ;
651
+
652
+ let mut command_with_cleared_env = Command :: new ( "boring-name" ) ;
653
+ command_with_cleared_env. env_clear ( ) . env ( "BAR" , "val" ) . env_remove ( "FOO" ) ;
654
+ assert_eq ! ( format!( "{command_with_cleared_env:?}" ) , r#"env -i BAR="val" "boring-name""# ) ;
655
+ assert_eq ! (
656
+ format!( "{command_with_cleared_env:#?}" ) ,
657
+ format!(
658
+ r#"Command {{
659
+ program: "boring-name",
660
+ args: [
661
+ "boring-name",
662
+ ],
663
+ env: CommandEnv {{
664
+ clear: true,
665
+ vars: {{
666
+ "BAR": Some(
667
+ "val",
668
+ ),
669
+ }},
670
+ }},
626
671
{PIDFD}}}"#
627
672
)
628
673
) ;
Original file line number Diff line number Diff line change @@ -586,6 +586,23 @@ impl fmt::Debug for Command {
586
586
if let Some ( ref cwd) = self . cwd {
587
587
write ! ( f, "cd {cwd:?} && " ) ?;
588
588
}
589
+ if self . env . does_clear ( ) {
590
+ write ! ( f, "env -i " ) ?;
591
+ // Altered env vars will be printed next, that should exactly work as expected.
592
+ } else {
593
+ // Removed env vars need the command to be wrapped in `env`.
594
+ let mut any_removed = false ;
595
+ for ( key, value_opt) in self . get_envs ( ) {
596
+ if value_opt. is_none ( ) {
597
+ if !any_removed {
598
+ write ! ( f, "env " ) ?;
599
+ any_removed = true ;
600
+ }
601
+ write ! ( f, "-u {} " , key. to_string_lossy( ) ) ?;
602
+ }
603
+ }
604
+ }
605
+ // Altered env vars can just be added in front of the program.
589
606
for ( key, value_opt) in self . get_envs ( ) {
590
607
if let Some ( value) = value_opt {
591
608
write ! ( f, "{}={value:?} " , key. to_string_lossy( ) ) ?;
Original file line number Diff line number Diff line change @@ -80,6 +80,10 @@ impl CommandEnv {
80
80
self . vars . clear ( ) ;
81
81
}
82
82
83
+ pub fn does_clear ( & self ) -> bool {
84
+ self . clear
85
+ }
86
+
83
87
pub fn have_changed_path ( & self ) -> bool {
84
88
self . saw_path || self . clear
85
89
}
You can’t perform that action at this time.
0 commit comments