Skip to content

Commit a7f8717

Browse files
tronicalogoffart
andcommitted
testing: Permit tests to override the detected operating system
Co-Authored-By: Olivier Goffart <[email protected]>
1 parent 72c3e77 commit a7f8717

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

internal/core/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,20 @@ pub type Coord = i32;
103103
#[repr(C)]
104104
pub struct InternalToken;
105105

106+
#[cfg(feature = "std")]
107+
thread_local!(
108+
/// Permit testing code to force an OS type
109+
pub static OPERATING_SYSTEM_OVERRIDE: core::cell::Cell<Option<OperatingSystemType>> =
110+
Default::default();
111+
);
112+
106113
#[cfg(not(target_family = "wasm"))]
107114
pub fn detect_operating_system() -> OperatingSystemType {
115+
#[cfg(feature = "std")]
116+
if let Some(os_override) = OPERATING_SYSTEM_OVERRIDE.with(|os_override| os_override.get()) {
117+
return os_override;
118+
}
119+
108120
if cfg!(target_os = "android") {
109121
OperatingSystemType::Android
110122
} else if cfg!(target_os = "ios") {
@@ -122,6 +134,10 @@ pub fn detect_operating_system() -> OperatingSystemType {
122134

123135
#[cfg(target_family = "wasm")]
124136
pub fn detect_operating_system() -> OperatingSystemType {
137+
if let Some(os_override) = OPERATING_SYSTEM_OVERRIDE.with(|os_override| os_override.get()) {
138+
return os_override;
139+
}
140+
125141
let mut user_agent =
126142
web_sys::window().and_then(|w| w.navigator().user_agent().ok()).unwrap_or_default();
127143
user_agent.make_ascii_lowercase();

0 commit comments

Comments
 (0)