Skip to contentSkip to conversation

commands/which: Added new testcase to test which(1).

PATCH New Guangwen Feng 2 participants 7 messages Nov 26, 2015 – Jan 5, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 commands/which: Added new testcase to test which(1).

Test which(1) command with some basic options.

---
 runtest/commands                    |   1 +
 testcases/commands/which/Makefile   |  22 ++++++
 testcases/commands/which/which01.sh | 142 ++++++++++++++++++++++++++++++++++++
 3 files changed, 165 insertions(+)
 create mode 100644 testcases/commands/which/Makefile
 create mode 100755 testcases/commands/which/which01.sh
runtest/commands +1 -0
index ab600dc..b05ae7b 100644
@@ -39,3 +39,4 @@ mkfs01_msdos mkfs01.sh -f msdos
 mkfs01_vfat mkfs01.sh -f vfat
 mkfs01_ntfs mkfs01.sh -f ntfs
 mkswap01 mkswap01.sh
+which01 which01.sh
testcases/commands/which/Makefile new file +22 -0
new file mode 100644
index 0000000..d9b4d12
@@ -0,0 +1,22 @@
+#
+#    Copyright (c) 2015 Fujitsu Ltd.
+#    Author:Guangwen Feng <[email protected]>
+#
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+
+top_srcdir		?= ../../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+
+INSTALL_TARGETS		:= which01.sh
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/commands/which/which01.sh new file +142 -0

142 lines not on this page · raw message

Signature · 1 line
1.8.4.2
#1 lore raw

PATCH 1/1 commands/which: Added new testcase to test which(1).

Hi!

testcases/commands/which/which01.sh line 25

Quoted 3 lines
testcases/commands/which/which01.sh new file +3 -0
+setup()
+{
+	tst_require_root

Do we really need root for the test?

Quoted 51 lines
testcases/commands/which/which01.sh new file +51 -0
+	tst_check_cmds which
+
+	tst_tmpdir
+
+	TST_CLEANUP="cleanup"
+
+	touch programname
+	chmod +x programname
+	PATH=$PATH:.
+
+	mkdir bin
+	touch bin/programname
+	chmod +x bin/programname
+	PATH=$PATH:./bin
+
+	alias programname='programname -i'
+}
+
+cleanup()
+{
+	tst_rmdir
+}
+
+which_verify()
+{
+	local which_op=$1
+	local prog_name=$2
+
+	if [ -z "$which_op" ]; then
+		grep -q `pwd`/"$prog_name" temp
+		if [ $? -ne 0 ]; then
+			return 1
+		fi
+	elif [ "$which_op" = "--all" ] || [ "$which_op" = "-a" ]; then
+		grep -q `pwd`/"$prog_name" temp
+		if [ $? -ne 0 ]; then
+			return 1
+		fi
+		grep -q `pwd`/bin/"$prog_name" temp
+		if [ $? -ne 0 ]; then
+			return 1
+		fi
+	elif [ "$which_op" = "--read-alias" ] || [ "$which_op" = "-i" ]; then
+		grep -q "programname='programname -i'" temp
+		if [ $? -ne 0 ]; then
+			return 1
+		fi
+		grep -q `pwd`/"$prog_name" temp
+		if [ $? -ne 0 ]; then
+			return 1
+		fi

testcases/commands/which/which01.sh line 87

Quoted 10 lines
testcases/commands/which/which01.sh new file +10 -0
+	elif [ "$which_op" = "--skip-alias" ]; then
+		grep -q "programname='programname -i'" temp
+		if [ $? -eq 0 ]; then
+			return 1
+		fi
+		grep -q `pwd`/"$prog_name" temp
+		if [ $? -ne 0 ]; then
+			return 1
+		fi
+	fi

Again, why don't we rather pass the expected paths as the parameters to the which_test function? For example:

which_test "-a" "programname" "$PWD/programname" $PWD/bin/programname"

Then we will do shift twice in the which_test() and loop over the rest of the $@ doing grep in the temp for each iteration.

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

PATCH 1/1 commands/which: Added new testcase to test which(1).

Hi!
Thanks for your review!

Quoted 7 lines · Cyril Hrubis

Hi!

testcases/commands/which/which01.sh line 25

Quoted 3 lines
testcases/commands/which/which01.sh new file +3 -0
+setup()
+{
+	tst_require_root

Do we really need root for the test?

It's not necessary indeed, I'll drop it.

Quoted 65 lines
Quoted 51 lines
testcases/commands/which/which01.sh new file +51 -0
+	tst_check_cmds which
+
+	tst_tmpdir
+
+	TST_CLEANUP="cleanup"
+
+	touch programname
+	chmod +x programname
+	PATH=$PATH:.
+
+	mkdir bin
+	touch bin/programname
+	chmod +x bin/programname
+	PATH=$PATH:./bin
+
+	alias programname='programname -i'
+}
+
+cleanup()
+{
+	tst_rmdir
+}
+
+which_verify()
+{
+	local which_op=$1
+	local prog_name=$2
+
+	if [ -z "$which_op" ]; then
+		grep -q `pwd`/"$prog_name" temp
+		if [ $? -ne 0 ]; then
+			return 1
+		fi
+	elif [ "$which_op" = "--all" ] || [ "$which_op" = "-a" ]; then
+		grep -q `pwd`/"$prog_name" temp
+		if [ $? -ne 0 ]; then
+			return 1
+		fi
+		grep -q `pwd`/bin/"$prog_name" temp
+		if [ $? -ne 0 ]; then
+			return 1
+		fi
+	elif [ "$which_op" = "--read-alias" ] || [ "$which_op" = "-i" ]; then
+		grep -q "programname='programname -i'" temp
+		if [ $? -ne 0 ]; then
+			return 1
+		fi
+		grep -q `pwd`/"$prog_name" temp
+		if [ $? -ne 0 ]; then
+			return 1
+		fi

testcases/commands/which/which01.sh line 87

Quoted 10 lines
testcases/commands/which/which01.sh new file +10 -0
+	elif [ "$which_op" = "--skip-alias" ]; then
+		grep -q "programname='programname -i'" temp
+		if [ $? -eq 0 ]; then
+			return 1
+		fi
+		grep -q `pwd`/"$prog_name" temp
+		if [ $? -ne 0 ]; then
+			return 1
+		fi
+	fi

Again, why don't we rather pass the expected paths as the parameters to the which_test function? For example:

Quoted 5 lines

which_test "-a" "programname" "$PWD/programname" $PWD/bin/programname"

Then we will do shift twice in the which_test() and loop over the rest of the $@ doing grep in the temp for each iteration.

Yeah, it sounds better, I'll rewrite this according to your suggestion.

Best Regards,
Guangwen Feng

#3 lore raw

PATCH v2 1/1 commands/which: Added new testcase to test which(1).

Test which(1) command with some basic options.

---
 runtest/commands                    |   1 +
 testcases/commands/which/Makefile   |  22 +++++++
 testcases/commands/which/which01.sh | 121 ++++++++++++++++++++++++++++++++++++
 3 files changed, 144 insertions(+)
 create mode 100644 testcases/commands/which/Makefile
 create mode 100755 testcases/commands/which/which01.sh
runtest/commands +1 -0
index ab600dc..b05ae7b 100644
@@ -39,3 +39,4 @@ mkfs01_msdos mkfs01.sh -f msdos
 mkfs01_vfat mkfs01.sh -f vfat
 mkfs01_ntfs mkfs01.sh -f ntfs
 mkswap01 mkswap01.sh
+which01 which01.sh
testcases/commands/which/Makefile new file +22 -0
new file mode 100644
index 0000000..d9b4d12
@@ -0,0 +1,22 @@
+#
+#    Copyright (c) 2015 Fujitsu Ltd.
+#    Author:Guangwen Feng <[email protected]>
+#
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+
+top_srcdir		?= ../../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+
+INSTALL_TARGETS		:= which01.sh
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/commands/which/which01.sh new file +121 -0

121 lines not on this page · raw message

Signature · 1 line
1.8.4.2
#4 lore raw

PATCH v2 1/1 commands/which: Added new testcase to test which(1).

Hi!

testcases/commands/which/which01.sh line 61

Quoted 7 lines
testcases/commands/which/which01.sh new file +7 -0
+		grep -q "$1" temp
+		if [ $? -ne 0 ]; then
+			if [ "$which_op" = "--skip-alias" ] && \
+				[ "$1" = "pname='pname -i'" ]; then
+				shift
+				continue
+			fi

Hmm why don't we remove the "pname='pname -i'" from the parameters?

If we silently ignore the failure it's the same as if we haven't checked it in the first place...

Also it would be good to be a bit more verbose here. I.e. do:

echo "'$1' not found in:
cat temp
echo

Quoted 22 lines
testcases/commands/which/which01.sh new file +22 -0
+			return 1
+		fi
+		shift
+	done
+}
+
+which_test()
+{
+	local which_op=$1
+	local prog_name=$2
+
+	local which_cmd="which $which_op $prog_name"
+
+	if [ "$which_op" = "--read-alias" ] || [ "$which_op" = "-i" ] || \
+		[ "$which_op" = "--skip-alias" ]; then
+		which_cmd="alias | $which_cmd"
+	fi
+
+	eval ${which_cmd} >temp 2>&1
+	if [ $? -ne 0 ]; then
+		grep -q -E "unknown option|invalid option|Usage" temp
+		if [ $? -eq 0 ]; then

testcases/commands/which/which01.sh line 93

Quoted 10 lines
testcases/commands/which/which01.sh new file +10 -0
+			tst_resm TCONF "'${which_cmd}' not supported."
+			return
+		fi
+
+		tst_resm TFAIL "'${which_cmd}' failed."
+		cat temp
+		return
+	fi
+
+	if [ -n "$prog_name" ]; then

I would rather do 'if [ $# -gt 2 ]; then' but that is very minor.

testcases/commands/which/which01.sh line 103

Quoted 10 lines
testcases/commands/which/which01.sh new file +10 -0
+		shift 2
+		which_verify "$which_op" "$@"
+		if [ $? -ne 0 ]; then
+			tst_resm TFAIL "'${which_cmd}' failed, not expected."
+			return
+		fi
+	fi
+
+	tst_resm TPASS "'${which_cmd}' passed."
+}

The rest looks good.

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

PATCH v3 1/1 commands/which: Added new testcase to test which(1).

Test which(1) command with some basic options.

---
 runtest/commands                    |   1 +
 testcases/commands/which/Makefile   |  22 +++++++
 testcases/commands/which/which01.sh | 119 ++++++++++++++++++++++++++++++++++++
 3 files changed, 142 insertions(+)
 create mode 100644 testcases/commands/which/Makefile
 create mode 100755 testcases/commands/which/which01.sh
runtest/commands +1 -0
index ab600dc..b05ae7b 100644
@@ -39,3 +39,4 @@ mkfs01_msdos mkfs01.sh -f msdos
 mkfs01_vfat mkfs01.sh -f vfat
 mkfs01_ntfs mkfs01.sh -f ntfs
 mkswap01 mkswap01.sh
+which01 which01.sh
testcases/commands/which/Makefile new file +22 -0
new file mode 100644
index 0000000..d9b4d12
@@ -0,0 +1,22 @@
+#
+#    Copyright (c) 2015 Fujitsu Ltd.
+#    Author:Guangwen Feng <[email protected]>
+#
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+
+top_srcdir		?= ../../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+
+INSTALL_TARGETS		:= which01.sh
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/commands/which/which01.sh new file +119 -0

119 lines not on this page · raw message

Signature · 1 line
1.8.4.2
#6 lore raw

PATCH v3 1/1 commands/which: Added new testcase to test which(1).

Hi!
Pushed with this change:

@@ -47,9 +47,6 @@ cleanup()

which_verify()
{
- local which_op=$1
-
- shift

        until [ -z "$1" ]
        do
                grep -q "$1" temp

@@ -90,7 +87,7 @@ which_test()

        if [ $# -gt 2 ]; then
                shift 2
-               which_verify "$which_op" "$@"
+               which_verify "$@"
                if [ $? -ne 0 ]; then
                        tst_resm TFAIL "'${which_cmd}' failed, not expected."
                        return

Since the $which_op was unused in the fucntion.

Thanks.

Signature · 2 lines
Cyril Hrubis
[email protected]