diff --git a/src/uu/free/src/free.rs b/src/uu/free/src/free.rs index 8e24f0c7..4806d9e7 100644 --- a/src/uu/free/src/free.rs +++ b/src/uu/free/src/free.rs @@ -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, @@ -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, @@ -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 ); } diff --git a/tests/by-util/test_free.rs b/tests/by-util/test_free.rs index 8fb36af1..cda4a1e0 100644 --- a/tests/by-util/test_free.rs +++ b/tests/by-util/test_free.rs @@ -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] @@ -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())); + } +}