Repository: GuillaumeGomez/sysinfo
Pull request #1248 (2024-04-09 21:47:03)

Add regression test for components
Follow-up of #1246.


Patch:
diff --git a/README.md b/README.md
index 945e44e..eb76214 100644
--- a/README.md
+++ b/README.md
@@ -108,7 +108,7 @@ use sysinfo::System;
 let mut sys = System::new();
 
 loop {
-    sys.refresh_cpu_usage(); // Refreshing CPU usage.
+    sys.refresh_cpu(); // Refreshing CPU information.
     for cpu in sys.cpus() {
         print!("{}% ", cpu.cpu_usage());
     }
diff --git a/benches/basic.rs b/benches/basic.rs
index 04d1217..3412400 100644
--- a/benches/basic.rs
+++ b/benches/basic.rs
@@ -114,12 +114,11 @@ fn bench_refresh_memory(b: &mut test::Bencher) {
 }
 
 #[bench]
-fn bench_refresh_cpu_usage(b: &mut test::Bencher) {
+fn bench_refresh_cpu(b: &mut test::Bencher) {
     let mut s = sysinfo::System::new();
 
-    s.refresh_cpu_usage();
     b.iter(move || {
-        s.refresh_cpu_usage();
+        s.refresh_cpu();
     });
 }
 
diff --git a/examples/simple.rs b/examples/simple.rs
index afd351c..15ce7be 100644
--- a/examples/simple.rs
+++ b/examples/simple.rs
@@ -179,7 +179,7 @@ fn interpret_input(
         }
         "refresh_cpu" => {
             writeln!(&mut io::stdout(), "Refreshing CPUs...");
-            sys.refresh_cpu_all();
+            sys.refresh_cpu();
             writeln!(&mut io::stdout(), "Done.");
         }
         "signals" => {
diff --git a/src/common.rs b/src/common.rs
index ba98761..62b4d33 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -201,9 +201,7 @@ impl System {
 
     /// Refreshes all information related to CPUs information.
     ///
-    /// If you only want the CPU usage, use [`System::refresh_cpu_usage`] instead.
-    ///
-    /// ⚠️ Please note that the result will be inaccurate at the first call.
+    /// ⚠️ Please note that the result will very likely be inaccurate at the first call.
     /// You need to call this method at least twice (with a bit of time between each call, like
     /// 200 ms, take a look at [`MINIMUM_CPU_UPDATE_INTERVAL`] for more information)
     /// to get accurate value as it uses previous results to compute the next value.
@@ -215,11 +213,11 @@ impl System {
     /// use sysinfo::System;
     ///
     /// let mut s = System::new_all();
-    /// s.refresh_cpu_all();
+    /// s.refresh_cpu();
     /// ```
     ///
     /// [`MINIMUM_CPU_UPDATE_INTERVAL`]: crate::MINIMUM_CPU_UPDATE_INTERVAL
-    pub fn refresh_cpu_all(&mut self) {
+    pub fn refresh_cpu(&mut self) {
         self.refresh_cpu_specifics(CpuRefreshKind::everything())
     }
 
@@ -495,7 +493,7 @@ impl System {
 
     /// Returns "global" CPUs information (aka the addition of all the CPUs).
     ///
-    /// To have up-to-date information, you need to call [`System::refresh_cpu_specifics`] or
+    /// To have up-to-date information, you need to call [`System::refresh_cpu`] or
     /// [`System::refresh_specifics`] with `cpu` enabled.
     ///
     /// **⚠️ Important ⚠️**
@@ -521,7 +519,7 @@ impl System {
 
     /// Returns the list of the CPUs.
     ///
-    /// By default, the list of CPUs is empty until you call [`System::refresh_cpu_specifics`] or
+    /// By default, the list of CPUs is empty until you call [`System::refresh_cpu`] or
     /// [`System::refresh_specifics`] with `cpu` enabled.
     ///
     /// ```no_run
@@ -3972,7 +3970,7 @@ impl Component {
 /// // Wait a bit because CPU usage is based on diff.
 /// std::thread::sleep(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL);
 /// // Refresh CPUs again.
-/// s.refresh_cpu_all();
+/// s.refresh_cpu();
 ///
 /// for cpu in s.cpus() {
 ///     println!("{}%", cpu.cpu_usage());
@@ -3998,7 +3996,7 @@ impl Cpu {
     /// // Wait a bit because CPU usage is based on diff.
     /// std::thread::sleep(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL);
     /// // Refresh CPUs again.
-    /// s.refresh_cpu_all();
+    /// s.refresh_cpu();
     ///
     /// for cpu in s.cpus() {
     ///     println!("{}%", cpu.cpu_usage());


Test patch:
diff --git a/tests/components.rs b/tests/components.rs
new file mode 100644
index 0000000..27cadf3
--- /dev/null
+++ b/tests/components.rs
@@ -0,0 +1,17 @@
+// Take a look at the license at the top of the repository in the LICENSE file.
+
+#[test]
+fn test_components() {
+    use std::env::var;
+
+    let mut c = sysinfo::Components::new();
+    assert!(c.is_empty());
+
+    // Unfortunately, we can't get components in the CI...
+    if !sysinfo::IS_SUPPORTED_SYSTEM || cfg!(windows) || var("CI").is_ok() {
+        return;
+    }
+
+    c.refresh_list();
+    assert!(!c.is_empty());
+}
diff --git a/tests/cpu.rs b/tests/cpu.rs
index 72fa93b..14a8806 100644
--- a/tests/cpu.rs
+++ b/tests/cpu.rs
@@ -11,7 +11,7 @@ fn test_cpu() {
         return;
     }
 
-    s.refresh_cpu_all();
+    s.refresh_cpu();
     assert!(!s.cpus().is_empty());
 
     let s = sysinfo::System::new_all();
@@ -42,7 +42,7 @@ fn test_global_cpu_info_not_set() {
     assert_eq!(s.global_cpu_info().vendor_id(), "");
     assert_eq!(s.global_cpu_info().brand(), "");
     assert_eq!(s.global_cpu_info().frequency(), 0);
-    s.refresh_cpu_all();
+    s.refresh_cpu();
     assert_eq!(s.global_cpu_info().vendor_id(), "");
     assert_eq!(s.global_cpu_info().brand(), "");
     assert_eq!(s.global_cpu_info().frequency(), 0);
@@ -57,8 +57,8 @@ fn test_too_rapid_cpu_refresh() {
         return;
     }
 
-    s.refresh_cpu_all();
-    s.refresh_cpu_all();
+    s.refresh_cpu();
+    s.refresh_cpu();
 
     assert!(s.cpus().iter().any(|c| !c.cpu_usage().is_nan()));
 }