From: Michael Rappazzo <rappazzo@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, sunshine@sunshineco.com, pclouds@gmail.com,
szeder@ira.uka.de, peff@peff.net,
Michael Rappazzo <rappazzo@gmail.com>
Subject: [PATCH v2 1/2] test-lib: add a function to compare an expection with stdout from a command
Date: Sat, 16 Apr 2016 12:13:49 -0400 [thread overview]
Message-ID: <1460823230-45692-2-git-send-email-rappazzo@gmail.com> (raw)
In-Reply-To: <1460823230-45692-1-git-send-email-rappazzo@gmail.com>
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@peff.net>
Signed-off-by: Michael Rappazzo <rappazzo@gmail.com>
---
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
next prev parent reply other threads:[~2016-04-16 16:14 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-04-16 16:13 [PATCH v2 0/2] t1500-rev-parse: re-write t1500 Michael Rappazzo
2016-04-16 16:13 ` Michael Rappazzo [this message]
2016-04-17 3:07 ` [PATCH v2 1/2] test-lib: add a function to compare an expection with stdout from a command Eric Sunshine
2016-04-17 3:54 ` Jeff King
2016-04-17 6:36 ` Eric Sunshine
2016-04-17 6:41 ` Jeff King
2016-04-17 15:19 ` Johannes Sixt
2016-04-17 16:22 ` Eric Sunshine
2016-04-16 16:13 ` [PATCH v2 2/2] t1500-rev-parse: rewrite each test to run in isolation Michael Rappazzo
2016-04-17 5:59 ` Eric Sunshine
2016-04-17 15:05 ` Johannes Sixt
2016-04-17 9:42 ` SZEDER Gábor
2016-04-17 16:15 ` Eric Sunshine
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1460823230-45692-2-git-send-email-rappazzo@gmail.com \
--to=rappazzo@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=pclouds@gmail.com \
--cc=peff@peff.net \
--cc=sunshine@sunshineco.com \
--cc=szeder@ira.uka.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).