From d2519fdbeb37907d166cf13b8566141ee0a8bbc7 Mon Sep 17 00:00:00 2001 From: Xavier Claude Date: Fri, 8 Mar 2024 20:41:47 +0100 Subject: [PATCH 1/4] Fix output formatting for free The GNU free is using 11 chars wide for number, not 12. This was also causing a misalignement of the numbers with the title. --- src/uu/free/src/free.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 ); } From 3a5ec8e2885d3b0c4c6b942455e6f0246a898f54 Mon Sep 17 00:00:00 2001 From: Xavier Claude Date: Fri, 8 Mar 2024 22:04:49 +0100 Subject: [PATCH 2/4] Add tests for formating --- tests/by-util/test_free.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/by-util/test_free.rs b/tests/by-util/test_free.rs index 8fb36af1..e50bcf09 100644 --- a/tests/by-util/test_free.rs +++ b/tests/by-util/test_free.rs @@ -4,6 +4,7 @@ // file that was distributed with this source code. // spell-checker:ignore (words) symdir somefakedir +use pretty_assertions::assert_eq; use crate::common::util::TestScenario; #[test] @@ -23,3 +24,19 @@ fn test_free_wide() { assert!(result.stdout_str().contains("Mem:")); assert!(!result.stdout_str().contains("buff/cache")); } + +#[test] +fn test_free_column_format() { + let free_header = " total used free shared buff/cache available"; + let free_result = new_ucmd!().succeeds(); + assert_eq!(free_result.stdout_str().len(), 207); + assert_eq!(free_result.stdout_str().split("\n").next().unwrap(), free_header) +} + +#[test] +fn test_free_wide_column_format() { + let free_header = " total used free shared buffers cache available"; + let free_result = new_ucmd!().arg("--wide").succeeds(); + assert_eq!(free_result.stdout_str().len(), 231); + assert_eq!(free_result.stdout_str().split("\n").next().unwrap(), free_header) +} \ No newline at end of file From c7c4fd3187ef6c02eea3de4564ead63dcd2a77f0 Mon Sep 17 00:00:00 2001 From: Xavier Claude Date: Fri, 8 Mar 2024 23:01:08 +0100 Subject: [PATCH 3/4] cargo fmt for free tests --- tests/by-util/test_free.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/by-util/test_free.rs b/tests/by-util/test_free.rs index e50bcf09..0419148a 100644 --- a/tests/by-util/test_free.rs +++ b/tests/by-util/test_free.rs @@ -4,8 +4,8 @@ // file that was distributed with this source code. // spell-checker:ignore (words) symdir somefakedir -use pretty_assertions::assert_eq; use crate::common::util::TestScenario; +use pretty_assertions::assert_eq; #[test] fn test_invalid_arg() { @@ -27,10 +27,14 @@ fn test_free_wide() { #[test] fn test_free_column_format() { - let free_header = " total used free shared buff/cache available"; + let free_header = + " total used free shared buff/cache available"; let free_result = new_ucmd!().succeeds(); assert_eq!(free_result.stdout_str().len(), 207); - assert_eq!(free_result.stdout_str().split("\n").next().unwrap(), free_header) + assert_eq!( + free_result.stdout_str().split("\n").next().unwrap(), + free_header + ) } #[test] @@ -38,5 +42,8 @@ fn test_free_wide_column_format() { let free_header = " total used free shared buffers cache available"; let free_result = new_ucmd!().arg("--wide").succeeds(); assert_eq!(free_result.stdout_str().len(), 231); - assert_eq!(free_result.stdout_str().split("\n").next().unwrap(), free_header) -} \ No newline at end of file + assert_eq!( + free_result.stdout_str().split("\n").next().unwrap(), + free_header + ) +} From 73d698be21712a5aad3951c8287381843a124a17 Mon Sep 17 00:00:00 2001 From: Xavier Claude Date: Sat, 16 Mar 2024 13:23:56 +0100 Subject: [PATCH 4/4] Use regex for testing free output --- tests/by-util/test_free.rs | 57 +++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/tests/by-util/test_free.rs b/tests/by-util/test_free.rs index 0419148a..cda4a1e0 100644 --- a/tests/by-util/test_free.rs +++ b/tests/by-util/test_free.rs @@ -4,8 +4,10 @@ // file that was distributed with this source code. // spell-checker:ignore (words) symdir somefakedir -use crate::common::util::TestScenario; use pretty_assertions::assert_eq; +use regex::Regex; + +use crate::common::util::TestScenario; #[test] fn test_invalid_arg() { @@ -27,23 +29,46 @@ fn test_free_wide() { #[test] fn test_free_column_format() { - let free_header = - " total used free shared buff/cache available"; - let free_result = new_ucmd!().succeeds(); - assert_eq!(free_result.stdout_str().len(), 207); - assert_eq!( - free_result.stdout_str().split("\n").next().unwrap(), - free_header - ) + 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 free_header = " total used free shared buffers cache available"; - let free_result = new_ucmd!().arg("--wide").succeeds(); - assert_eq!(free_result.stdout_str().len(), 231); - assert_eq!( - free_result.stdout_str().split("\n").next().unwrap(), - free_header - ) + 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())); + } }