* [LTP] [PATCH] test.sh: add EXPECT_PASS, EXPECT_FAIL functions
@ 2016-08-22 13:53 Stanislav Kholmanskikh
2016-08-22 17:21 ` Cyril Hrubis
0 siblings, 1 reply; 3+ messages in thread
From: Stanislav Kholmanskikh @ 2016-08-22 13:53 UTC (permalink / raw)
To: ltp
Sometimes we need to execute a command and call tst_resm TPASS/TFAIL
based on the command's exit status.
The existing ROD() function can make 99% of the job, we just
need to let it know how the command's exit code should be
interpreted. This patch does it and introduces a couple of new
functions to help with the described situation.
Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
---
Changes since the RFC version:
* Rename SHOULD_* -> EXPECT_*
* Rename ROD_DISPATCHER -> ROD_BASE
* Move '$?' checks into the callers of ROD_BASE
* Pass $@ in the quoted form
* Add documentation
doc/test-writing-guidelines.txt | 15 +++++++++++++++
testcases/lib/test.sh | 27 ++++++++++++++++++++++++++-
2 files changed, 41 insertions(+), 1 deletions(-)
diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt
index 650f8b5..740e90c 100644
--- a/doc/test-writing-guidelines.txt
+++ b/doc/test-writing-guidelines.txt
@@ -1352,6 +1352,21 @@ Note the '>' is escaped with '\', this causes that the '>' and filename are
passed to the 'ROD' function as parameters and the 'ROD' function contains
code to split '$@' on '>' and redirects the output to the file.
+EXPECT_PASS and EXPECT_FAIL
++++++++++++++++++++++++++++
+
+[source,sh]
+-------------------------------------------------------------------------------
+EXPECT_PASS command arg1 arg2 ... [ \> file ]
+EXPECT_FAIL command arg1 arg2 ... [ \> file ]
+-------------------------------------------------------------------------------
+
+'EXPECT_PASS' calls 'tst_resm TPASS' if the command exited with 0 exit code,
+and 'tst_resm TFAIL' otherwise. 'EXPECT_FAIL' does vice versa.
+
+Output redirection rules are the same as for the 'ROD' function. In addition
+to that, 'EXPECT_FAIL' always redirects the command's stderr to '/dev/null'.
+
.tst_fs_has_free
[source,sh]
-------------------------------------------------------------------------------
diff --git a/testcases/lib/test.sh b/testcases/lib/test.sh
index bd66109..a1fa2d9 100644
--- a/testcases/lib/test.sh
+++ b/testcases/lib/test.sh
@@ -220,7 +220,7 @@ ROD_SILENT()
fi
}
-ROD()
+ROD_BASE()
{
local cmd
local arg
@@ -250,12 +250,37 @@ ROD()
else
$@
fi
+}
+ROD()
+{
+ ROD_BASE "$@"
if [ $? -ne 0 ]; then
tst_brkm TBROK "$@ failed"
fi
}
+EXPECT_PASS()
+{
+ ROD_BASE "$@"
+ if [ $? -eq 0 ]; then
+ tst_resm TPASS "$@ passed as expected"
+ else
+ tst_resm TFAIL "$@ failed unexpectedly"
+ fi
+}
+
+EXPECT_FAIL()
+{
+ # redirect stderr since we expect the command to fail
+ ROD_BASE "$@" 2> /dev/null
+ if [ $? -ne 0 ]; then
+ tst_resm TPASS "$@ failed as expected"
+ else
+ tst_resm TFAIL "$@ passed unexpectedly"
+ fi
+}
+
tst_acquire_device()
{
if [ -z ${TST_TMPDIR} ]; then
--
1.7.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [LTP] [PATCH] test.sh: add EXPECT_PASS, EXPECT_FAIL functions
2016-08-22 13:53 [LTP] [PATCH] test.sh: add EXPECT_PASS, EXPECT_FAIL functions Stanislav Kholmanskikh
@ 2016-08-22 17:21 ` Cyril Hrubis
2016-08-23 7:50 ` Stanislav Kholmanskikh
0 siblings, 1 reply; 3+ messages in thread
From: Cyril Hrubis @ 2016-08-22 17:21 UTC (permalink / raw)
To: ltp
Hi!
Acked.
--
Cyril Hrubis
chrubis@suse.cz
^ permalink raw reply [flat|nested] 3+ messages in thread
* [LTP] [PATCH] test.sh: add EXPECT_PASS, EXPECT_FAIL functions
2016-08-22 17:21 ` Cyril Hrubis
@ 2016-08-23 7:50 ` Stanislav Kholmanskikh
0 siblings, 0 replies; 3+ messages in thread
From: Stanislav Kholmanskikh @ 2016-08-23 7:50 UTC (permalink / raw)
To: ltp
On 08/22/2016 08:21 PM, Cyril Hrubis wrote:
> Hi!
> Acked.
>
Thank you. Pushed.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2016-08-23 7:50 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-22 13:53 [LTP] [PATCH] test.sh: add EXPECT_PASS, EXPECT_FAIL functions Stanislav Kholmanskikh
2016-08-22 17:21 ` Cyril Hrubis
2016-08-23 7:50 ` Stanislav Kholmanskikh
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox