Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(contexts): Add Windows OS version to OS context #499

Merged
merged 1 commit into from Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"),
}
}
}