From d50cb24c50986198e5fd27550c982e9179945a1c Mon Sep 17 00:00:00 2001 From: kevin zhao Date: Mon, 1 Dec 2025 19:42:05 -0500 Subject: [PATCH] inlining and cleanup tests --- codex-rs/core/src/exec_policy.rs | 65 +++++++++++++++----------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/codex-rs/core/src/exec_policy.rs b/codex-rs/core/src/exec_policy.rs index 154f5a312a..ad3ab561c5 100644 --- a/codex-rs/core/src/exec_policy.rs +++ b/codex-rs/core/src/exec_policy.rs @@ -25,7 +25,8 @@ use crate::sandboxing::SandboxPermissions; use crate::tools::sandboxing::ApprovalRequirement; const FORBIDDEN_REASON: &str = "execpolicy forbids this command"; -const PROMPT_CONFLICT_REASON: &str = "execpolicy requires approval for this command, but AskForApproval is set to Never"; +const PROMPT_CONFLICT_REASON: &str = + "execpolicy requires approval for this command, but AskForApproval is set to Never"; const PROMPT_REASON: &str = "execpolicy requires approval for this command"; const POLICY_DIR_NAME: &str = "policy"; const POLICY_EXTENSION: &str = "codexpolicy"; @@ -204,7 +205,7 @@ pub(crate) async fn create_approval_requirement_for_command( }, } } - + /// Only return PROMPT_REASON when an execpolicy rule drove the prompt decision fn derive_prompt_reason(evaluation: &Evaluation) -> Option { evaluation.matched_rules.iter().find_map(|rule_match| { @@ -296,9 +297,9 @@ mod tests { }], }, policy - .read() - .await - .check_multiple(commands.iter(), &|_| Decision::Allow) + .read() + .await + .check_multiple(commands.iter(), &|_| Decision::Allow) ); assert!(!temp_dir.path().join(POLICY_DIR_NAME).exists()); } @@ -339,9 +340,9 @@ mod tests { }], }, policy - .read() - .await - .check_multiple(command.iter(), &|_| Decision::Allow) + .read() + .await + .check_multiple(command.iter(), &|_| Decision::Allow) ); } @@ -367,9 +368,9 @@ mod tests { }], }, policy - .read() - .await - .check_multiple(command.iter(), &|_| Decision::Allow) + .read() + .await + .check_multiple(command.iter(), &|_| Decision::Allow) ); } @@ -503,18 +504,16 @@ prefix_rule(pattern=["rm"], decision="forbidden") "apple | orange".to_string(), ]; - let requirement = create_approval_requirement_for_command( - &policy, - &Features::with_defaults(), - &command, - AskForApproval::UnlessTrusted, - &SandboxPolicy::DangerFullAccess, - SandboxPermissions::UseDefault, - ) - .await; - assert_eq!( - requirement, + create_approval_requirement_for_command( + &policy, + &Features::with_defaults(), + &command, + AskForApproval::UnlessTrusted, + &SandboxPolicy::DangerFullAccess, + SandboxPermissions::UseDefault, + ) + .await, ApprovalRequirement::NeedsApproval { reason: None, allow_prefix: Some(vec!["orange".to_string()]) @@ -522,7 +521,7 @@ prefix_rule(pattern=["rm"], decision="forbidden") ); } - #[tokio::test] +#[tokio::test] async fn append_allow_prefix_rule_updates_policy_and_file() { let codex_home = tempdir().expect("create temp dir"); let current_policy = Arc::new(RwLock::new(Policy::empty())); @@ -690,18 +689,16 @@ prefix_rule(pattern=["rm"], decision="forbidden") "python && echo ok".to_string(), ]; - let requirement = create_approval_requirement_for_command( - &policy, - &Features::with_defaults(), - &command, - AskForApproval::UnlessTrusted, - &SandboxPolicy::ReadOnly, - SandboxPermissions::UseDefault, - ) - .await; - assert_eq!( - requirement, + create_approval_requirement_for_command( + &policy, + &Features::with_defaults(), + &command, + AskForApproval::UnlessTrusted, + &SandboxPolicy::ReadOnly, + SandboxPermissions::UseDefault, + ) + .await, ApprovalRequirement::Skip { bypass_sandbox: true }