From da7edf1c0c92dc8b0b746b015da911d5820c64ba Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Thu, 3 Oct 2024 08:28:38 -0400 Subject: [PATCH] fix: don't prompt when using `Deno.permissions.request` with `--no-prompt` (#25811) --- runtime/permissions/lib.rs | 7 ++++++- tests/integration/run_tests.rs | 16 ++++++++++++++++ .../testdata/run/permission_request_no_prompt.ts | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/testdata/run/permission_request_no_prompt.ts diff --git a/runtime/permissions/lib.rs b/runtime/permissions/lib.rs index 77038ff2fa..efabd0b171 100644 --- a/runtime/permissions/lib.rs +++ b/runtime/permissions/lib.rs @@ -476,6 +476,9 @@ impl UnaryPermission { if state != PermissionState::Prompt { return state; } + if !self.prompt { + return PermissionState::Denied; + } let mut message = String::with_capacity(40); message.push_str(&format!("{} access", TQuery::flag_name())); if let Some(desc) = desc { @@ -3906,7 +3909,8 @@ mod tests { fn test_request() { set_prompter(Box::new(TestPrompter)); let parser = TestPermissionDescriptorParser; - let mut perms: Permissions = Permissions::none_without_prompt(); + let mut perms: Permissions = Permissions::none_with_prompt(); + let mut perms_no_prompt: Permissions = Permissions::none_without_prompt(); let read_query = |path: &str| parser.parse_path_query(path).unwrap().into_read(); let write_query = @@ -3955,6 +3959,7 @@ mod tests { assert_eq!(perms.run.query(None), PermissionState::Prompt); prompt_value.set(false); assert_eq!(perms.run.request(Some(&run_query)), PermissionState::Granted); + assert_eq!(perms_no_prompt.read.request(Some(&read_query("/foo"))), PermissionState::Denied); }; } diff --git a/tests/integration/run_tests.rs b/tests/integration/run_tests.rs index 1e1d6ed61b..b3ad15c813 100644 --- a/tests/integration/run_tests.rs +++ b/tests/integration/run_tests.rs @@ -3512,6 +3512,22 @@ itest!(no_prompt_flag { output_str: Some(""), }); +#[test] +fn permission_request_with_no_prompt() { + TestContext::default() + .new_command() + .env("NO_COLOR", "1") + .args_vec([ + "run", + "--quiet", + "--no-prompt", + "run/permission_request_no_prompt.ts", + ]) + .with_pty(|mut console| { + console.expect("PermissionStatus { state: \"denied\", onchange: null }"); + }); +} + #[test] fn deno_no_prompt_environment_variable() { let output = util::deno_cmd() diff --git a/tests/testdata/run/permission_request_no_prompt.ts b/tests/testdata/run/permission_request_no_prompt.ts new file mode 100644 index 0000000000..e33ffe3ce8 --- /dev/null +++ b/tests/testdata/run/permission_request_no_prompt.ts @@ -0,0 +1 @@ +console.log(await Deno.permissions.request({ name: "read" }));