Skip to content

Commit

Permalink
feat(contexts): Add Windows OS version to OS context (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpraneis committed Sep 12, 2022
1 parent bb81c88 commit a433f63
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 41 deletions.
3 changes: 3 additions & 0 deletions sentry-contexts/Cargo.toml
Expand Up @@ -21,6 +21,9 @@ hostname = "0.3.0"
[target."cfg(not(windows))".dependencies]
uname = "0.1.1"

[target."cfg(windows)".dependencies]
os_info = "3.5.0"

[build-dependencies]
rustc_version = "0.4.0"

Expand Down
111 changes: 70 additions & 41 deletions sentry-contexts/src/utils.rs
Expand Up @@ -115,52 +115,59 @@ pub fn server_name() -> Option<String> {
}

/// Returns the OS context
#[cfg(not(windows))]
pub fn os_context() -> Option<Context> {
#[cfg(not(windows))]
{
use uname::uname;
if let Ok(info) = uname() {
#[cfg(target_os = "macos")]
{
Some(
OsContext {
name: Some("macOS".into()),
kernel_version: Some(info.version),
version: model_support::get_macos_version(),
build: model_support::get_macos_build(),
..Default::default()
}
.into(),
)
}
#[cfg(not(target_os = "macos"))]
{
Some(
OsContext {
name: Some(info.sysname),
kernel_version: Some(info.version),
version: Some(info.release),
..Default::default()
}
.into(),
)
}
} else {
None
use uname::uname;
if let Ok(info) = uname() {
#[cfg(target_os = "macos")]
{
Some(
OsContext {
name: Some("macOS".into()),
kernel_version: Some(info.version),
version: model_support::get_macos_version(),
build: model_support::get_macos_build(),
..Default::default()
}
.into(),
)
}
}
#[cfg(windows)]
{
Some(
OsContext {
name: Some(PLATFORM.into()),
..Default::default()
}
.into(),
)
#[cfg(not(target_os = "macos"))]
{
Some(
OsContext {
name: Some(info.sysname),
kernel_version: Some(info.version),
version: Some(info.release),
..Default::default()
}
.into(),
)
}
} else {
None
}
}

/// Returns the OS context
#[cfg(windows)]
pub fn os_context() -> Option<Context> {
use os_info::Version;
let version = match os_info::get().version() {
Version::Unknown => None,
version => Some(version.to_string()),
};

Some(
OsContext {
name: Some(PLATFORM.into()),
version,
..Default::default()
}
.into(),
)
}

/// Returns the rust info.
pub fn rust_context() -> Context {
RuntimeContext {
Expand All @@ -187,3 +194,25 @@ pub fn device_context() -> Context {
}
.into()
}

#[cfg(test)]
mod tests {
use super::*;
#[cfg(windows)]
#[test]
fn windows_os_version_not_empty() {
let context = os_context();
match context {
Some(Context::Os(os_context)) => {
// verify the version is a non-empty string
let version = os_context.version.expect("OS version to be some");
assert!(!version.is_empty());

// verify the version is not equal to the unknown OS version
let unknown_version = os_info::Version::Unknown.to_string();
assert_ne!(version, unknown_version);
}
_ => unreachable!("os_context() should return a Context::Os"),
}
}
}

0 comments on commit a433f63

Please sign in to comment.