Skip to content

Commit

Permalink
Merge pull request #389 from nokyan/more-tests
Browse files Browse the repository at this point in the history
Implement more unit tests
  • Loading branch information
nokyan authored Oct 30, 2024
2 parents fd8c58c + cc531e7 commit 31c7c40
Show file tree
Hide file tree
Showing 19 changed files with 1,052 additions and 190 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/flatpak.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
flatpak:
runs-on: ubuntu-latest
container:
image: bilelmoussaoui/flatpak-github-actions:gnome-44
image: bilelmoussaoui/flatpak-github-actions:gnome-47
options: --privileged
steps:
- uses: actions/checkout@v4
Expand Down
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name = "resources"
version = "1.6.0"
authors = ["nokyan <[email protected]>"]
edition = "2021"
rust-version = "1.80.0"
homepage = "https://apps.gnome.org/app/net.nokyan.Resources/"
license = "GPL-3.0-or-later"

[profile.dev]
opt-level = 1
Expand Down Expand Up @@ -44,3 +47,6 @@ rust-ini = "0.21.1"
strum = "0.26.3"
strum_macros = "0.26.4"
sysconf = "0.3.4"

[dev-dependencies]
pretty_assertions = "1.4.1"
23 changes: 23 additions & 0 deletions lib/process_data/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/process_data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name = "process-data"
version = "1.6.0"
authors = ["nokyan <[email protected]>"]
edition = "2021"
rust-version = "1.80.0"
homepage = "https://apps.gnome.org/app/net.nokyan.Resources/"
license = "GPL-3.0-or-later"

[profile.dev]
opt-level = 1
Expand All @@ -27,3 +30,6 @@ syscalls = { version = "0.6.18", features = ["all"] }
sysconf = "0.3.4"
unescape = "0.1.0"
uzers = "0.12.1"

[dev-dependencies]
pretty_assertions = "1.4.1"
14 changes: 14 additions & 0 deletions lib/process_data/src/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cargo_options = [
'--manifest-path', meson.project_source_root() / 'lib' / 'process_data' / 'Cargo.toml',
]
cargo_options += [
'--target-dir', meson.project_build_root() / 'lib' / 'process_data' / 'src',
]

test(
'Cargo tests (process_data)',
cargo,
args: ['test', cargo_options],
timeout: 600,
env: cargo_env,
)
2 changes: 2 additions & 0 deletions lib/process_data/src/pci_slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ impl Display for PciSlot {
mod test {
use std::str::FromStr;

use pretty_assertions::assert_eq;

use super::PciSlot;

#[test]
Expand Down
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ endif
subdir('data')
subdir('po')
subdir('src')
subdir('lib/process_data/src')

gnome.post_install(
gtk_update_icon_cache: true,
glib_compile_schemas: true,
update_desktop_database: true,
)
)
8 changes: 8 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ endif

cargo_env = ['CARGO_HOME=' + meson.project_build_root() / 'cargo']

test(
'Cargo tests (main application)',
cargo,
args: ['test', cargo_options],
timeout: 600,
env: cargo_env,
)

cargo_build = custom_target(
'cargo-build',
depends: resources,
Expand Down
4 changes: 2 additions & 2 deletions src/ui/pages/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ impl ResNetwork {

imp.manufacturer.set_subtitle(
&network_interface
.vendor
.clone()
.device
.map(|device| device.vendor().name().to_string())
.unwrap_or_else(|| i18n("N/A")),
);

Expand Down
99 changes: 81 additions & 18 deletions src/utils/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl CpuData {
}
}

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, PartialEq)]
pub struct CpuInfo {
pub model_name: Option<String>,
pub architecture: Option<String>,
Expand All @@ -128,21 +128,8 @@ fn trade_mark_symbols<S: AsRef<str>>(s: S) -> String {
.replace("(TM)", "™")
}

/// Returns a `CPUInfo` struct populated with values gathered from `lscpu`.
///
/// # Errors
///
/// Will return `Err` if the are problems during reading or parsing
/// of the `lscpu` command
pub fn cpu_info() -> Result<CpuInfo> {
let lscpu_output = String::from_utf8(
std::process::Command::new("lscpu")
.env("LC_ALL", "C")
.output()
.context("unable to run lscpu, is util-linux installed?")?
.stdout,
)
.context("unable to parse lscpu output to UTF-8")?;
fn parse_lscpu<S: AsRef<str>>(lscpu_output: S) -> CpuInfo {
let lscpu_output = lscpu_output.as_ref();

let model_name = RE_LSCPU_MODEL_NAME
.captures(&lscpu_output)
Expand Down Expand Up @@ -193,15 +180,33 @@ pub fn cpu_info() -> Result<CpuInfo> {
})
});

Ok(CpuInfo {
CpuInfo {
model_name,
architecture,
logical_cpus,
physical_cpus,
sockets,
virtualization,
max_speed,
})
}
}

/// Returns a `CPUInfo` struct populated with values gathered from `lscpu`.
///
/// # Errors
///
/// Will return `Err` if the are problems during reading or parsing
/// of the `lscpu` command
pub fn cpu_info() -> Result<CpuInfo> {
String::from_utf8(
std::process::Command::new("lscpu")
.env("LC_ALL", "C")
.output()
.context("unable to run lscpu, is util-linux installed?")?
.stdout,
)
.context("unable to parse lscpu output to UTF-8")
.map(|output| parse_lscpu(output))
}

/// Returns the frequency of the given CPU `core`
Expand Down Expand Up @@ -297,3 +302,61 @@ fn read_sysfs_thermal<P: AsRef<Path>>(path: P) -> Result<f32> {
.with_context(|| format!("unable to parse {}", path.display()))
.map(|t| t / 1000f32)
}

#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;

use crate::utils::cpu::CpuInfo;

use super::parse_lscpu;

const LSCPU_OUTPUT: &str = concat!(
"Architecture: x86_64\n",
" CPU op-mode(s): 32-bit, 64-bit\n",
" Address sizes: 48 bits physical, 48 bits virtual\n",
" Byte Order: Little Endian\n",
"CPU(s): 16\n",
" On-line CPU(s) list: 0-7\n",
"Vendor ID: UnauthenticIngenuineManufacturer\n",
" Model name: UIM(R) Abacus(tm) 10\n",
" CPU family: 1\n",
" Model: 2\n",
" Thread(s) per core: 2\n",
" Core(s) per socket: 4\n",
" Socket(s): 2\n",
" Stepping: 2\n",
" Frequency boost: enabled\n",
" CPU(s) scaling MHz: 100%\n",
" CPU max MHz: 3.0000\n",
" CPU min MHz: 2.0000\n",
" BogoMIPS: 0.0\n",
"Virtualization features: \n",
" Virtualization: Abacus-V\n",
"Caches (sum of all): \n",
" L1d: 256 KiB (8 instances)\n",
" L1i: 256 KiB (8 instances)\n",
" L2: 4 MiB (8 instances)\n",
" L3: 32 MiB (1 instance)\n",
"NUMA: \n",
" NUMA node(s): 1\n",
" NUMA node0 CPU(s): 0-15\n",
);

#[test]
fn lscpu_complex() {
let parsed = parse_lscpu(LSCPU_OUTPUT);

let expected = CpuInfo {
model_name: Some("UIM® Abacus™ 10".into()),
architecture: Some("x86_64".into()),
logical_cpus: Some(16),
physical_cpus: Some(8),
sockets: Some(2),
virtualization: Some("Abacus-V".into()),
max_speed: Some(3000000.0),
};

assert_eq!(parsed, expected)
}
}
34 changes: 13 additions & 21 deletions src/utils/gpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ use log::{debug, info};
use process_data::pci_slot::PciSlot;

use std::{
collections::HashMap,
path::{Path, PathBuf},
str::FromStr,
};

use glob::glob;

use crate::{i18n::i18n, utils::pci::Device};
use crate::{
i18n::i18n,
utils::{pci::Device, read_uevent},
};

use self::{amd::AmdGpu, intel::IntelGpu, nvidia::NvidiaGpu, other::OtherGpu};

Expand Down Expand Up @@ -235,24 +237,16 @@ impl Gpu {

fn from_sysfs_path<P: AsRef<Path>>(path: P) -> Result<Gpu> {
let sysfs_device_path = path.as_ref().join("device");
let mut uevent_contents: HashMap<String, String> = HashMap::new();
let uevent_raw = std::fs::read_to_string(sysfs_device_path.join("uevent"))?;

for line in uevent_raw.lines() {
let (k, v) = line
.split_once('=')
.context("unable to correctly read uevent file")?;
uevent_contents.insert(k.to_owned(), v.to_owned());
}
let uevent_contents = read_uevent(sysfs_device_path.join("uevent"))?;

let mut vid: u16 = 0;
let mut pid: u16 = 0;

if let Some(pci_line) = uevent_contents.get("PCI_ID") {
let split = pci_line.split(':').collect::<Vec<&str>>();
vid = u16::from_str_radix(split[0], 16)?;
pid = u16::from_str_radix(split[1], 16)?;
}
let (device, vid, pid) = if let Some(pci_line) = uevent_contents.get("PCI_ID") {
let (vid_str, pid_str) = pci_line.split_once(':').unwrap_or(("0", "0"));
let vid = u16::from_str_radix(vid_str, 16).unwrap_or_default();
let pid = u16::from_str_radix(pid_str, 16).unwrap_or_default();
(Device::from_vid_pid(vid, pid), vid, pid)
} else {
(None, 0, 0)
};

let mut hwmon_vec: Vec<PathBuf> = Vec::new();
for hwmon in glob(&format!(
Expand All @@ -266,8 +260,6 @@ impl Gpu {
hwmon_vec.push(hwmon);
}

let device = Device::from_vid_pid(vid, pid);

let pci_slot = PciSlot::from_str(
&uevent_contents
.get("PCI_SLOT_NAME")
Expand Down
Loading

0 comments on commit 31c7c40

Please sign in to comment.