Skip to contentSkip to conversation

test.sh: add SHOULD_PASS, SHOULD_FAIL functions

RFC PATCH New Stanislav Kholmanskikh 2 participants 3 messages Aug 19, 2016 – Aug 22, 2016

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 test.sh: add SHOULD_PASS, SHOULD_FAIL functions

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 introduce a couple of new functions to help with the described situation.

---
This is to help with situations like:

echo 1.0 > memory.limit_in_bytes 2> /dev/null
if [ $? -ne 0 ]; then
tst_resm TPASS "return value is $?"
else
tst_resm TFAIL "return value is 0"
fi

which could be transformed to:

SHOULD_FAIL echo 1.0 \> memory.limit_in_bytes

 testcases/lib/test.sh |   50 +++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 46 insertions(+), 4 deletions(-)
testcases/lib/test.sh +46 -4
index bd66109..ca2c00b 100644
@@ -220,12 +220,17 @@ ROD_SILENT()
 	fi
 }
 
-ROD()
+ROD_DISPATCHER()
 {
+	local act
 	local cmd
 	local arg
 	local file
 	local flag
+	local ret
+
+	act="$1"
+	shift
 
 	for arg; do
 		file="${arg#\>}"
@@ -251,9 +256,46 @@ ROD()
 		$@
 	fi
 
-	if [ $? -ne 0 ]; then
-		tst_brkm TBROK "$@ failed"
-	fi
+	ret=$?
+
+	case "$act" in
+		0) # break on failure
+		if [ $ret -ne 0 ]; then
+			tst_brkm TBROK "$@ failed"
+		fi;;
+
+		1) # the command should pass
+		if [ $ret -eq 0 ]; then
+			tst_resm TPASS "$@ passed as expected"
+		else
+			tst_resm TFAIL "$@ failed unexpectedly"
+		fi;;
+
+		2) # the command should fail
+		if [ $ret -ne 0 ]; then
+			tst_resm TPASS "$@ failed as expected"
+		else
+			tst_resm TFAIL "$@ passed unexpectedly"
+		fi;;
+
+		*) tst_brkm TBROK "unknown action '$act'";;
+	esac
+}
+
+ROD()
+{
+	ROD_DISPATCHER 0 $@
+}
+
+SHOULD_PASS()
+{
+	ROD_DISPATCHER 1 $@
+}
+
+SHOULD_FAIL()
+{
+	# redirect stderr since we expect the command to fail
+	ROD_DISPATCHER 2 $@ 2> /dev/null
 }
 
 tst_acquire_device()
Signature · 1 line
1.7.1
#1 lore raw

RFC PATCH 1/1 test.sh: add SHOULD_PASS, SHOULD_FAIL functions

Hi!

Quoted 19 lines

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 introduce a couple of new functions to help with the described situation.

---
This is to help with situations like:

echo 1.0 > memory.limit_in_bytes 2> /dev/null
if [ $? -ne 0 ]; then
tst_resm TPASS "return value is $?"
else
tst_resm TFAIL "return value is 0"
fi

Quoted 3 lines

which could be transformed to:

SHOULD_FAIL echo 1.0 \> memory.limit_in_bytes

This is quite nice. Maybe it would better be named EXPECT_FAIL and EXPECT_PASS or something but SHOULD_ prefix is fine as well.

We should include documentation for these in this commit as well.

A few comments to the implementation below.

Quoted 27 lines
 testcases/lib/test.sh |   50 +++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 46 insertions(+), 4 deletions(-)
testcases/lib/test.sh +6 -1
index bd66109..ca2c00b 100644
@@ -220,12 +220,17 @@ ROD_SILENT()
 	fi
 }
 
-ROD()
+ROD_DISPATCHER()
 {
+	local act
 	local cmd
 	local arg
 	local file
 	local flag
+	local ret
+
+	act="$1"
+	shift
 
 	for arg; do
 		file="${arg#\>}"

testcases/lib/test.sh ROD() · line 259

Quoted 7 lines
testcases/lib/test.sh +1 -3
@@ -251,9 +256,46 @@ ROD()
 		$@
 	fi
 
-	if [ $? -ne 0 ]; then
-		tst_brkm TBROK "$@ failed"
-	fi
+	ret=$?

Why don't we just return from this function here and let the caller examine the $?

Quoted 29 lines
testcases/lib/test.sh +29 -0
+	case "$act" in
+		0) # break on failure
+		if [ $ret -ne 0 ]; then
+			tst_brkm TBROK "$@ failed"
+		fi;;
+
+		1) # the command should pass
+		if [ $ret -eq 0 ]; then
+			tst_resm TPASS "$@ passed as expected"
+		else
+			tst_resm TFAIL "$@ failed unexpectedly"
+		fi;;
+
+		2) # the command should fail
+		if [ $ret -ne 0 ]; then
+			tst_resm TPASS "$@ failed as expected"
+		else
+			tst_resm TFAIL "$@ passed unexpectedly"
+		fi;;
+
+		*) tst_brkm TBROK "unknown action '$act'";;
+	esac
+}
+
+ROD()
+{
+	ROD_DISPATCHER 0 $@
+}
+

testcases/lib/test.sh ROD() · line 299

Quoted 10 lines
testcases/lib/test.sh +9 -0
+SHOULD_PASS()
+{
+	ROD_DISPATCHER 1 $@
+}
+
+SHOULD_FAIL()
+{
+	# redirect stderr since we expect the command to fail
+	ROD_DISPATCHER 2 $@ 2> /dev/null
 }

I think that we should quote the $@ in these functions.

Try for yourself:

8<------------------------------------

#!/bin/sh

d()

{
        echo "-------------"
        for i; do
                echo $i
        done
        echo "-------------"
}

dd()

{
        d "$@"
}

d a b c
dd a b c
d "a b c"
dd "a b c"

8<------------------------------------

If you unquote the $@ in dd() the "a b c" in the last call would be separated on spaces.

Signature · 2 lines
Cyril Hrubis
[email protected]
#2 lore raw

RFC PATCH 1/1 test.sh: add SHOULD_PASS, SHOULD_FAIL functions

Quoted 20 lines · Cyril Hrubis

Hi!

Quoted 19 lines

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 introduce a couple of new functions to help with the described situation.

---
This is to help with situations like:

echo 1.0 > memory.limit_in_bytes 2> /dev/null
if [ $? -ne 0 ]; then
tst_resm TPASS "return value is $?"
else
tst_resm TFAIL "return value is 0"
fi

Quoted 6 lines
Quoted 3 lines

which could be transformed to:

SHOULD_FAIL echo 1.0 \> memory.limit_in_bytes

This is quite nice. Maybe it would better be named EXPECT_FAIL and EXPECT_PASS or something but SHOULD_ prefix is fine as well.

Ok, will rename to EXPECT_*.

Quoted 2 lines

We should include documentation for these in this commit as well.

Ok.

Quoted 33 lines

A few comments to the implementation below.

Quoted 30 lines
 testcases/lib/test.sh |   50 +++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 46 insertions(+), 4 deletions(-)
testcases/lib/test.sh +6 -1
index bd66109..ca2c00b 100644
@@ -220,12 +220,17 @@ ROD_SILENT()
 	fi
 }
 
-ROD()
+ROD_DISPATCHER()
 {
+	local act
 	local cmd
 	local arg
 	local file
 	local flag
+	local ret
+
+	act="$1"
+	shift
 
 	for arg; do
 		file="${arg#\>}"
@@ -251,9 +256,46 @@ ROD()
 		$@
 	fi
 
Quoted 7 lines

testcases/lib/test.sh ROD() · line 259

Quoted 4 lines
testcases/lib/test.sh +1 -3
-	if [ $? -ne 0 ]; then
-		tst_brkm TBROK "$@ failed"
-	fi
+	ret=$?

Why don't we just return from this function here and let the caller examine the $?

No specific reason. Will put the handling of $? into the callers of ROD_DISPATCHER, and rename ROD_DISPATCHER to something like ROD_BASE, since now it doesn't "dispatch".

Quoted 69 lines
Quoted 30 lines
testcases/lib/test.sh +29 -0
+	case "$act" in
+		0) # break on failure
+		if [ $ret -ne 0 ]; then
+			tst_brkm TBROK "$@ failed"
+		fi;;
+
+		1) # the command should pass
+		if [ $ret -eq 0 ]; then
+			tst_resm TPASS "$@ passed as expected"
+		else
+			tst_resm TFAIL "$@ failed unexpectedly"
+		fi;;
+
+		2) # the command should fail
+		if [ $ret -ne 0 ]; then
+			tst_resm TPASS "$@ failed as expected"
+		else
+			tst_resm TFAIL "$@ passed unexpectedly"
+		fi;;
+
+		*) tst_brkm TBROK "unknown action '$act'";;
+	esac
+}
+
+ROD()
+{
+	ROD_DISPATCHER 0 $@
+}
+

testcases/lib/test.sh ROD() · line 299

Quoted 10 lines
testcases/lib/test.sh +9 -0
+SHOULD_PASS()
+{
+	ROD_DISPATCHER 1 $@
+}
+
+SHOULD_FAIL()
+{
+	# redirect stderr since we expect the command to fail
+	ROD_DISPATCHER 2 $@ 2> /dev/null
 }

I think that we should quote the $@ in these functions.

Try for yourself:

8<------------------------------------

#!/bin/sh

d()

{
        echo "-------------"
        for i; do
                echo $i
        done
        echo "-------------"
}

dd()

{
        d "$@"
}

d a b c
dd a b c
d "a b c"
dd "a b c"

Quoted 5 lines

8<------------------------------------

If you unquote the $@ in dd() the "a b c" in the last call would be separated on spaces.

Got it. Thank you.