Skip to content

Commit

Permalink
Fix output format for 'not_found' units
Browse files Browse the repository at this point in the history
  • Loading branch information
Journeycorner committed Nov 4, 2024
1 parent 3fbfad0 commit 50a0339
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl SystemCtl {
&'s self,
args: S,
) -> std::io::Result<String> {
let mut child = self.spawn_child(args)?;
let child = self.spawn_child(args)?;
let output = child.wait_with_output()?;
match output.status.code() {
Some(0) => {}, // success
Expand Down Expand Up @@ -84,7 +84,7 @@ impl SystemCtl {
},
}

let mut stdout: Vec<u8> = output.stdout;
let stdout: Vec<u8> = output.stdout;
let size = stdout.len();

if size > 0 {
Expand Down Expand Up @@ -262,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 @@ -836,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

0 comments on commit 50a0339

Please sign in to comment.