Skip to content

Commit

Permalink
Merge pull request #34 from claudex/fix/free_formatting
Browse files Browse the repository at this point in the history
Fix output formatting for free
  • Loading branch information
sylvestre authored Mar 16, 2024
2 parents e809c19 + 73d698b commit 6afec4d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/uu/free/src/free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let used = mem_info.total - mem_info.free;

if wide {
println!(" total used free shared buffers cache available");
println!(" total used free shared buffers cache available");
println!(
"Mem: {:12} {:12} {:12} {:12} {:12} {:12} {:12}",
"Mem: {:11} {:11} {:11} {:11} {:11} {:11} {:11}",
mem_info.total,
used,
mem_info.free,
Expand All @@ -94,9 +94,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
mem_info.available
);
} else {
println!(" total used free shared buff/cache available");
println!(" total used free shared buff/cache available");
println!(
"Mem: {:12} {:12} {:12} {:12} {:12} {:12}",
"Mem: {:11} {:11} {:11} {:11} {:11} {:11}",
mem_info.total,
used,
mem_info.free,
Expand All @@ -106,7 +106,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
);
}
println!(
"Swap: {:12} {:12} {:12}",
"Swap: {:11} {:11} {:11}",
mem_info.swap_total, mem_info.swap_used, mem_info.swap_free
);
}
Expand Down
49 changes: 49 additions & 0 deletions tests/by-util/test_free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
// file that was distributed with this source code.
// spell-checker:ignore (words) symdir somefakedir

use pretty_assertions::assert_eq;
use regex::Regex;

use crate::common::util::TestScenario;

#[test]
Expand All @@ -23,3 +26,49 @@ fn test_free_wide() {
assert!(result.stdout_str().contains("Mem:"));
assert!(!result.stdout_str().contains("buff/cache"));
}

#[test]
fn test_free_column_format() {
let re_head_str = r"^ {15}total {8}used {8}free {6}shared {2}buff/cache {3}available$";
let re_mem_str = r"^Mem:( +\d+){6}$";
let re_swap_str = r"^Swap: ( +\d+){3}$";

let mut re_list = vec![];
re_list.push(Regex::new(re_head_str).unwrap());
re_list.push(Regex::new(re_mem_str).unwrap());
re_list.push(Regex::new(re_swap_str).unwrap());

let binding = new_ucmd!().succeeds();
let free_result = binding.stdout_str();
assert_eq!(free_result.len(), 207);

// Check the format for each line output
let mut free_lines = free_result.split("\n");
for re in re_list {
assert!(re.is_match(free_lines.next().unwrap()));
}
}

#[test]
fn test_free_wide_column_format() {
let re_head_str = r"^ {15}total {8}used {8}free {6}shared {5}buffers {7}cache {3}available$";
let re_mem_str = r"^Mem:( +\d+){7}$";
let re_swap_str = r"^Swap: ( +\d+){3}$";

let mut re_list = vec![];
re_list.push(Regex::new(re_head_str).unwrap());
re_list.push(Regex::new(re_mem_str).unwrap());
re_list.push(Regex::new(re_swap_str).unwrap());

let binding = new_ucmd!().arg("--wide").succeeds();
let free_result = binding.stdout_str();

// The total number of character is always fixed
assert_eq!(free_result.len(), 231);

// Check the format for each line output
let mut free_lines = free_result.split("\n");
for re in re_list {
assert!(re.is_match(free_lines.next().unwrap()));
}
}

0 comments on commit 6afec4d

Please sign in to comment.