[PATCH v2 1/2] test-lib: add a function to compare an expection with stdout from a command

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



test_stdout accepts an expection and a command to execute.  It will execute
the command and then compare the stdout from that command to an expectation.
If the expectation is not met, a mock diff output is written to stderr.

Based-on-a-patch-by: Jeff King <peff@xxxxxxxx>
Signed-off-by: Michael Rappazzo <rappazzo@xxxxxxxxx>
---
 t/test-lib-functions.sh | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 8d99eb3..95e54b2 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -941,3 +941,37 @@ mingw_read_file_strip_cr_ () {
 		eval "$1=\$$1\$line"
 	done
 }
+
+#	test_stdout is a helper function to compare expected output with
+#	the standard output of a command execution
+#
+#	Args:
+#		1: The expected output
+#		2: The command to run
+#
+#	You can use it like:
+#
+#	test_expect_success 'foo works' '
+#		test_cmp "This is expected" cmd_to_run arg1 arg2 ... argN
+#	'
+#
+#	The output when there is a mismatch mimics diff output, but this
+#	can break down for a multi-line result
+test_stdout () {
+	expect=$1
+	shift
+	if ! actual=$("$@")
+	then
+		echo "test_stdout: command failed: '$*'" >&2
+		return 1
+	fi
+	if test "$expect" != "$actual"
+	then
+		echo "test_stdout: unexpected output for '$*'" >&2
+		echo "@@ -N +N @@" >&2
+		echo "-$expect" >&2
+		echo "+$actual" >&2
+		return 1
+	fi
+	return 0
+}
-- 
2.8.0

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]