This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +44
-12
lines changed Expand file tree Collapse file tree 3 files changed +44
-12
lines changed Original file line number Diff line number Diff line change @@ -560,6 +560,29 @@ fn debug_print() {
560
560
"FOO": None,
561
561
}},
562
562
}},
563
+ {PIDFD}}}"#
564
+ )
565
+ ) ;
566
+
567
+ let mut command_with_cleared_env = Command :: new ( "boring-name" ) ;
568
+ command_with_cleared_env. env_clear ( ) . env ( "BAR" , "val" ) . env_remove ( "FOO" ) ;
569
+ assert_eq ! ( format!( "{command_with_cleared_env:?}" ) , r#"env -i BAR="val" "boring-name""# ) ;
570
+ assert_eq ! (
571
+ format!( "{command_with_cleared_env:#?}" ) ,
572
+ format!(
573
+ r#"Command {{
574
+ program: "boring-name",
575
+ args: [
576
+ "boring-name",
577
+ ],
578
+ env: CommandEnv {{
579
+ clear: true,
580
+ vars: {{
581
+ "BAR": Some(
582
+ "val",
583
+ ),
584
+ }},
585
+ }},
563
586
{PIDFD}}}"#
564
587
)
565
588
) ;
Original file line number Diff line number Diff line change @@ -558,20 +558,25 @@ impl fmt::Debug for Command {
558
558
if let Some ( ref cwd) = self . cwd {
559
559
write ! ( f, "cd {cwd:?} && " ) ?;
560
560
}
561
- // Removed env vars need a separate command.
562
- // We use a single `unset` command for all of them.
563
- let mut any_removed = false ;
564
- for ( key, value_opt) in self . get_envs ( ) {
565
- if value_opt. is_none ( ) {
566
- if !any_removed {
567
- write ! ( f, "unset " ) ?;
568
- any_removed = true ;
561
+ if self . env . does_clear ( ) {
562
+ write ! ( f, "env -i " ) ?;
563
+ // Altered env vars will be printed next, that should exactly work as expected.
564
+ } else {
565
+ // Removed env vars need a separate command.
566
+ // We use a single `unset` command for all of them.
567
+ let mut any_removed = false ;
568
+ for ( key, value_opt) in self . get_envs ( ) {
569
+ if value_opt. is_none ( ) {
570
+ if !any_removed {
571
+ write ! ( f, "unset " ) ?;
572
+ any_removed = true ;
573
+ }
574
+ write ! ( f, "{} " , key. to_string_lossy( ) ) ?;
569
575
}
570
- write ! ( f, "{} " , key. to_string_lossy( ) ) ?;
571
576
}
572
- }
573
- if any_removed {
574
- write ! ( f , "&& " ) ? ;
577
+ if any_removed {
578
+ write ! ( f , "&& " ) ? ;
579
+ }
575
580
}
576
581
// Altered env vars can just be added in front of the program.
577
582
for ( key, value_opt) in self . get_envs ( ) {
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