[11/21] tests: Add function to test the ouput of a bash function

Message ID 20240520090611.10406-12-jonatan.schlag@ipfire.org
State New
Headers
Series [01/21] test: Add bash lib for colors |

Commit Message

Jonatan Schlag May 20, 2024, 9:06 a.m. UTC
  Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
---
 tests/lib.sh | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
  

Patch

diff --git a/tests/lib.sh b/tests/lib.sh
index 81bc1439c..9ce0201b1 100644
--- a/tests/lib.sh
+++ b/tests/lib.sh
@@ -52,3 +52,32 @@  test_that_key_in_arry_has_value() {
 		return 1
 	fi
 }
+
+test_that_output_is(){
+	local reference_output_file="${1}"
+	local file_descriptor="${2}"
+	shift
+	shift
+
+	local command="$@"
+
+	local temp="$(mktemp)"
+
+	case "${file_descriptor}" in
+		"stdout"|"1")
+			$command 1> "${temp}" 2>/dev/null
+			;;
+		"stderr"|"2")
+			$command 2> "${temp}" 1>/dev/null
+			;;
+	esac
+
+	if diff -u "${temp}" "${reference_output_file}" &> /dev/null; then
+		log_test_succeded "The output of command '${command}' on file descriptor '${file_descriptor}' is equal to the reference output."
+	else
+		log_test_failed "The output of command '${command}' on file descriptor '${file_descriptor}' is unequal to the reference output."
+		diff -u --color "${reference_output_file}" "${temp}"
+	fi
+
+	rm "${temp}"
+}