summaryrefslogtreecommitdiffstats
path: root/vendor/os_info/src/macos/mod.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
commit10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch)
treebdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/os_info/src/macos/mod.rs
parentReleasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff)
downloadrustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz
rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/os_info/src/macos/mod.rs')
-rw-r--r--vendor/os_info/src/macos/mod.rs106
1 files changed, 106 insertions, 0 deletions
diff --git a/vendor/os_info/src/macos/mod.rs b/vendor/os_info/src/macos/mod.rs
new file mode 100644
index 000000000..0c93a9713
--- /dev/null
+++ b/vendor/os_info/src/macos/mod.rs
@@ -0,0 +1,106 @@
+use std::process::Command;
+
+use log::{trace, warn};
+
+use crate::{bitness, matcher::Matcher, Info, Type, Version};
+
+pub fn current_platform() -> Info {
+ trace!("macos::current_platform is called");
+
+ let info = Info {
+ os_type: Type::Macos,
+ version: version(),
+ bitness: bitness::get(),
+ ..Default::default()
+ };
+ trace!("Returning {:?}", info);
+ info
+}
+
+fn version() -> Version {
+ match product_version() {
+ None => Version::Unknown,
+ Some(val) => Version::from_string(val),
+ }
+}
+
+fn product_version() -> Option<String> {
+ match Command::new("sw_vers").output() {
+ Ok(val) => {
+ let output = String::from_utf8_lossy(&val.stdout);
+ trace!("sw_vers command returned {:?}", output);
+ parse(&output)
+ }
+ Err(e) => {
+ warn!("sw_vers command failed with {:?}", e);
+ None
+ }
+ }
+}
+
+fn parse(sw_vers_output: &str) -> Option<String> {
+ Matcher::PrefixedVersion {
+ prefix: "ProductVersion:",
+ }
+ .find(sw_vers_output)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use pretty_assertions::{assert_eq, assert_ne};
+
+ #[test]
+ fn os_type() {
+ let version = current_platform();
+ assert_eq!(Type::Macos, version.os_type());
+ }
+
+ #[test]
+ fn os_version() {
+ let version = version();
+ assert_ne!(Version::Unknown, version);
+ }
+
+ #[test]
+ fn string_product_version() {
+ let version = product_version();
+ assert!(version.is_some());
+ }
+
+ #[test]
+ fn parse_version() {
+ let parse_output = parse(sw_vers_output());
+ assert_eq!(parse_output, Some("10.10.5".to_string()));
+ }
+
+ fn sw_vers_output() -> &'static str {
+ "ProductName: Mac OS X\n\
+ ProductVersion: 10.10.5\n\
+ BuildVersion: 14F27"
+ }
+
+ #[test]
+ fn parse_beta_version() {
+ let parse_output = parse(sw_vers_output_beta());
+ assert_eq!(parse_output, Some("10.15".to_string()));
+ }
+
+ fn sw_vers_output_beta() -> &'static str {
+ "ProductName: Mac OS X\n\
+ ProductVersion: 10.15\n\
+ BuildVersion: 19A546d"
+ }
+
+ #[test]
+ fn parse_double_digit_patch_version() {
+ let parse_output = parse(sw_vers_output_double_digit_patch_version());
+ assert_eq!(parse_output, Some("10.15.21".to_string()));
+ }
+
+ fn sw_vers_output_double_digit_patch_version() -> &'static str {
+ "ProductName: Mac OS X\n\
+ ProductVersion: 10.15.21\n\
+ BuildVersion: ABCD123"
+ }
+}