mirror of
https://github.com/denoland/deno
synced 2024-11-05 18:45:24 +00:00
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:
parent
fb08cf7005
commit
c080d7fba3
1 changed files with 22 additions and 0 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue