@@ -534,6 +534,78 @@ fn maybe_wrap_shell_lc_with_snapshot_restores_codex_thread_id_from_env() {
534534 assert_eq ! ( String :: from_utf8_lossy( & output. stdout) , "nested-thread" ) ;
535535}
536536
537+ #[ test]
538+ fn maybe_wrap_shell_lc_with_snapshot_restores_permission_profile_from_env ( ) {
539+ let dir = tempdir ( ) . expect ( "create temp dir" ) ;
540+ let snapshot_path = dir. path ( ) . join ( "snapshot.sh" ) ;
541+ std:: fs:: write (
542+ & snapshot_path,
543+ "# Snapshot file\n export CODEX_PERMISSION_PROFILE='parent-profile'\n " ,
544+ )
545+ . expect ( "write snapshot" ) ;
546+ let ( session_shell, shell_snapshot) =
547+ shell_with_snapshot ( ShellType :: Bash , "/bin/bash" , snapshot_path. abs ( ) ) ;
548+ let command = vec ! [
549+ "/bin/bash" . to_string( ) ,
550+ "-lc" . to_string( ) ,
551+ "printenv CODEX_PERMISSION_PROFILE" . to_string( ) ,
552+ ] ;
553+ let env = HashMap :: from ( [ (
554+ CODEX_PERMISSION_PROFILE_ENV_VAR . to_string ( ) ,
555+ "current-profile" . to_string ( ) ,
556+ ) ] ) ;
557+ let rewritten = maybe_wrap_shell_lc_with_snapshot (
558+ & command,
559+ & session_shell,
560+ Some ( & shell_snapshot) ,
561+ & HashMap :: new ( ) ,
562+ & env,
563+ & RuntimePathPrepends :: default ( ) ,
564+ ) ;
565+ let output = Command :: new ( & rewritten[ 0 ] )
566+ . args ( & rewritten[ 1 ..] )
567+ . env ( CODEX_PERMISSION_PROFILE_ENV_VAR , "current-profile" )
568+ . output ( )
569+ . expect ( "run rewritten command" ) ;
570+
571+ assert ! ( output. status. success( ) , "command failed: {output:?}" ) ;
572+ assert_eq ! ( String :: from_utf8_lossy( & output. stdout) , "current-profile\n " ) ;
573+ }
574+
575+ #[ test]
576+ fn maybe_wrap_shell_lc_with_snapshot_unsets_absent_permission_profile ( ) {
577+ let dir = tempdir ( ) . expect ( "create temp dir" ) ;
578+ let snapshot_path = dir. path ( ) . join ( "snapshot.sh" ) ;
579+ std:: fs:: write (
580+ & snapshot_path,
581+ "# Snapshot file\n export CODEX_PERMISSION_PROFILE='stale-profile'\n " ,
582+ )
583+ . expect ( "write snapshot" ) ;
584+ let ( session_shell, shell_snapshot) =
585+ shell_with_snapshot ( ShellType :: Bash , "/bin/bash" , snapshot_path. abs ( ) ) ;
586+ let command = vec ! [
587+ "/bin/bash" . to_string( ) ,
588+ "-lc" . to_string( ) ,
589+ "printenv CODEX_PERMISSION_PROFILE" . to_string( ) ,
590+ ] ;
591+ let rewritten = maybe_wrap_shell_lc_with_snapshot (
592+ & command,
593+ & session_shell,
594+ Some ( & shell_snapshot) ,
595+ & HashMap :: new ( ) ,
596+ & HashMap :: new ( ) ,
597+ & RuntimePathPrepends :: default ( ) ,
598+ ) ;
599+ let output = Command :: new ( & rewritten[ 0 ] )
600+ . args ( & rewritten[ 1 ..] )
601+ . env_remove ( CODEX_PERMISSION_PROFILE_ENV_VAR )
602+ . output ( )
603+ . expect ( "run rewritten command" ) ;
604+
605+ assert_eq ! ( output. status. code( ) , Some ( 1 ) ) ;
606+ assert_eq ! ( output. stdout, b"" ) ;
607+ }
608+
537609#[ test]
538610fn maybe_wrap_shell_lc_with_snapshot_restores_proxy_env_from_process_env ( ) {
539611 let dir = tempdir ( ) . expect ( "create temp dir" ) ;
0 commit comments