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

Fix issues with '--all' argument #34

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Crate to manage and monitor services through `systemctl`
//! Homepage: <https://github.com/gwbres/systemctl>
#![doc=include_str!("../README.md")]
use std::io::{Error, ErrorKind, Read};
use std::io::{Error, ErrorKind};
use std::process::{Child, ExitStatus};
use std::str::FromStr;
use strum_macros::EnumString;
Expand Down Expand Up @@ -56,8 +56,9 @@ impl SystemCtl {
&'s self,
args: S,
) -> std::io::Result<String> {
let mut child = self.spawn_child(args)?;
match child.wait()?.code() {
let child = self.spawn_child(args)?;
let output = child.wait_with_output()?;
match output.status.code() {
Some(0) => {}, // success
Some(1) => {}, // success -> Ok(Unit not found)
Some(3) => {}, // success -> Ok(unit is inactive and/or dead)
Expand All @@ -83,8 +84,8 @@ impl SystemCtl {
},
}

let mut stdout: Vec<u8> = Vec::new();
let size = child.stdout.unwrap().read_to_end(&mut stdout)?;
let stdout: Vec<u8> = output.stdout;
let size = stdout.len();

if size > 0 {
if let Ok(s) = String::from_utf8(stdout) {
Expand Down Expand Up @@ -261,7 +262,10 @@ impl SystemCtl {
.filter(|line| line.contains('.') && !line.ends_with('.'));

for l in lines {
let parsed: Vec<&str> = l.split_ascii_whitespace().collect();
// fixes format for not found units
let slice = if l.starts_with("● ") { &l[3..] } else { l };

let parsed: Vec<&str> = slice.split_ascii_whitespace().collect();

let description = parsed
.split_at(4)
Expand Down Expand Up @@ -835,6 +839,28 @@ mod test {
}
}

/// Test valid results for the --all argument
/// Example of broken output:
/// ```text
/// UnitService {
/// unit_name: "●",
/// loaded: "syslog.service",
/// state: "not-found",
/// sub_state: "inactive",
/// description: " dead syslog.service",
/// }
///```
#[test]
fn test_list_units_full_all() {
let ctl = SystemCtl::builder()
.additional_args(vec![String::from("--all")])
.build();
let units = ctl.list_units_full(None, None, None).unwrap(); // all units
for unit in units {
assert_ne!("●", unit.unit_name);
}
}

#[cfg(feature = "serde")]
#[test]
fn test_serde_for_unit() {
Expand Down
Loading