]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
tests: Add function to test the ouput of a bash function
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Sun, 16 Jun 2024 16:02:34 +0000 (18:02 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 24 Aug 2024 12:19:56 +0000 (12:19 +0000)
Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/lib.sh

index af8c632cf1bb86492e13e844fbce11e68b9054f7..4110ed2d8976b18bccbb0caea2b6ead3e47e0803 100644 (file)
@@ -64,3 +64,32 @@ test_value_in_array() {
                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}"
+}