From c080d7fba34fa9114e191b171d6a810312d74343 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Mon, 24 Feb 2020 23:13:03 +0900 Subject: [PATCH] fix: add mutex guard for permission_prompt tests (#4105) This PR introduces the mutex guard for the test cases which depends on the permission prompt mocking utility. permission_request test cases depend on the mocked (dummy) value of the permission_prompt result. The value is stored at static STUB_PROMPT_VALUE: AtomicBool and these test cases share this value. So we should lock at the start of these test cases. Before this change cargo test permission failed 6 times out of 20. After this change, 0 times out of 20 (on my mac). --- cli/permissions.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cli/permissions.rs b/cli/permissions.rs index d99daa005f..a6d0210ea4 100644 --- a/cli/permissions.rs +++ b/cli/permissions.rs @@ -14,6 +14,8 @@ use std::path::{Path, PathBuf}; use std::sync::atomic::AtomicBool; #[cfg(test)] use std::sync::atomic::Ordering; +#[cfg(test)] +use std::sync::Mutex; use url::Url; const PERMISSION_EMOJI: &str = "⚠️"; @@ -325,6 +327,12 @@ fn permission_prompt(message: &str) -> bool { } } +#[cfg(test)] +lazy_static! { + /// Lock this when you use `set_prompt_result` in a test case. + static ref PERMISSION_PROMPT_GUARD: Mutex<()> = Mutex::new(()); +} + #[cfg(test)] static STUB_PROMPT_VALUE: AtomicBool = AtomicBool::new(true); @@ -521,6 +529,7 @@ mod tests { #[test] fn test_permissions_request_run() { + let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); let mut perms0 = DenoPermissions::from_flags(&DenoFlags { ..Default::default() }); @@ -532,10 +541,12 @@ mod tests { }); set_prompt_result(false); assert_eq!(perms1.request_run(), PermissionState::Deny); + drop(guard); } #[test] fn test_permissions_request_read() { + let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); let whitelist = vec![PathBuf::from("/foo/bar")]; let mut perms0 = DenoPermissions::from_flags(&DenoFlags { read_whitelist: whitelist.clone(), @@ -568,10 +579,12 @@ mod tests { perms2.request_read(&Some(Path::new("/foo/baz"))), PermissionState::Deny ); + drop(guard); } #[test] fn test_permissions_request_write() { + let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); let whitelist = vec![PathBuf::from("/foo/bar")]; let mut perms0 = DenoPermissions::from_flags(&DenoFlags { write_whitelist: whitelist.clone(), @@ -604,10 +617,12 @@ mod tests { perms2.request_write(&Some(Path::new("/foo/baz"))), PermissionState::Deny ); + drop(guard); } #[test] fn test_permission_request_net() { + let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); let whitelist = svec!["localhost:8080"]; let mut perms0 = DenoPermissions::from_flags(&DenoFlags { @@ -654,10 +669,12 @@ mod tests { }); set_prompt_result(true); assert!(perms3.request_net(&Some(":")).is_err()); + drop(guard); } #[test] fn test_permissions_request_env() { + let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); let mut perms0 = DenoPermissions::from_flags(&DenoFlags { ..Default::default() }); @@ -669,10 +686,12 @@ mod tests { }); set_prompt_result(false); assert_eq!(perms1.request_env(), PermissionState::Deny); + drop(guard); } #[test] fn test_permissions_request_plugin() { + let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); let mut perms0 = DenoPermissions::from_flags(&DenoFlags { ..Default::default() }); @@ -684,10 +703,12 @@ mod tests { }); set_prompt_result(false); assert_eq!(perms1.request_plugin(), PermissionState::Deny); + drop(guard); } #[test] fn test_permissions_request_hrtime() { + let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); let mut perms0 = DenoPermissions::from_flags(&DenoFlags { ..Default::default() }); @@ -699,5 +720,6 @@ mod tests { }); set_prompt_result(false); assert_eq!(perms1.request_hrtime(), PermissionState::Deny); + drop(guard); } }