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).
This commit is contained in:
Yoshiya Hinosawa 2020-02-24 23:13:03 +09:00 committed by GitHub
parent fb08cf7005
commit c080d7fba3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,6 +14,8 @@ use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicBool;
#[cfg(test)] #[cfg(test)]
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
#[cfg(test)]
use std::sync::Mutex;
use url::Url; use url::Url;
const PERMISSION_EMOJI: &str = "⚠️"; 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)] #[cfg(test)]
static STUB_PROMPT_VALUE: AtomicBool = AtomicBool::new(true); static STUB_PROMPT_VALUE: AtomicBool = AtomicBool::new(true);
@ -521,6 +529,7 @@ mod tests {
#[test] #[test]
fn test_permissions_request_run() { fn test_permissions_request_run() {
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
let mut perms0 = DenoPermissions::from_flags(&DenoFlags { let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
..Default::default() ..Default::default()
}); });
@ -532,10 +541,12 @@ mod tests {
}); });
set_prompt_result(false); set_prompt_result(false);
assert_eq!(perms1.request_run(), PermissionState::Deny); assert_eq!(perms1.request_run(), PermissionState::Deny);
drop(guard);
} }
#[test] #[test]
fn test_permissions_request_read() { fn test_permissions_request_read() {
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
let whitelist = vec![PathBuf::from("/foo/bar")]; let whitelist = vec![PathBuf::from("/foo/bar")];
let mut perms0 = DenoPermissions::from_flags(&DenoFlags { let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
read_whitelist: whitelist.clone(), read_whitelist: whitelist.clone(),
@ -568,10 +579,12 @@ mod tests {
perms2.request_read(&Some(Path::new("/foo/baz"))), perms2.request_read(&Some(Path::new("/foo/baz"))),
PermissionState::Deny PermissionState::Deny
); );
drop(guard);
} }
#[test] #[test]
fn test_permissions_request_write() { fn test_permissions_request_write() {
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
let whitelist = vec![PathBuf::from("/foo/bar")]; let whitelist = vec![PathBuf::from("/foo/bar")];
let mut perms0 = DenoPermissions::from_flags(&DenoFlags { let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
write_whitelist: whitelist.clone(), write_whitelist: whitelist.clone(),
@ -604,10 +617,12 @@ mod tests {
perms2.request_write(&Some(Path::new("/foo/baz"))), perms2.request_write(&Some(Path::new("/foo/baz"))),
PermissionState::Deny PermissionState::Deny
); );
drop(guard);
} }
#[test] #[test]
fn test_permission_request_net() { fn test_permission_request_net() {
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
let whitelist = svec!["localhost:8080"]; let whitelist = svec!["localhost:8080"];
let mut perms0 = DenoPermissions::from_flags(&DenoFlags { let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
@ -654,10 +669,12 @@ mod tests {
}); });
set_prompt_result(true); set_prompt_result(true);
assert!(perms3.request_net(&Some(":")).is_err()); assert!(perms3.request_net(&Some(":")).is_err());
drop(guard);
} }
#[test] #[test]
fn test_permissions_request_env() { fn test_permissions_request_env() {
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
let mut perms0 = DenoPermissions::from_flags(&DenoFlags { let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
..Default::default() ..Default::default()
}); });
@ -669,10 +686,12 @@ mod tests {
}); });
set_prompt_result(false); set_prompt_result(false);
assert_eq!(perms1.request_env(), PermissionState::Deny); assert_eq!(perms1.request_env(), PermissionState::Deny);
drop(guard);
} }
#[test] #[test]
fn test_permissions_request_plugin() { fn test_permissions_request_plugin() {
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
let mut perms0 = DenoPermissions::from_flags(&DenoFlags { let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
..Default::default() ..Default::default()
}); });
@ -684,10 +703,12 @@ mod tests {
}); });
set_prompt_result(false); set_prompt_result(false);
assert_eq!(perms1.request_plugin(), PermissionState::Deny); assert_eq!(perms1.request_plugin(), PermissionState::Deny);
drop(guard);
} }
#[test] #[test]
fn test_permissions_request_hrtime() { fn test_permissions_request_hrtime() {
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
let mut perms0 = DenoPermissions::from_flags(&DenoFlags { let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
..Default::default() ..Default::default()
}); });
@ -699,5 +720,6 @@ mod tests {
}); });
set_prompt_result(false); set_prompt_result(false);
assert_eq!(perms1.request_hrtime(), PermissionState::Deny); assert_eq!(perms1.request_hrtime(), PermissionState::Deny);
drop(guard);
} }
} }