Suprisingly, not every Linux distribution comes with unzip installed.
Thankfully unzip does not appear to be needed for any other tests.
This patch cleans up the unzip test so that if zip or unzip does not
exist the test will not be built or run.

This patch changes the way the build and run portions of the script are spread
across the Makefile and unzip_tests.sh script. Previously part of the build was
performed during the test -- building the sample zip archive. Now that sample
archive is conditionally built using Make instead of shell script.

Also, as near as I can tell, the comments "describing" the test are
totally incorrect -- they appear to be a copy-paste artifact. So I
updated them.

Signed-off-by: Matt Helsley <[email protected]>
---

 runtest/commands                          |    2 
 testcases/commands/unzip/Makefile         |   33 ++++++++---
 testcases/commands/unzip/unzip_genfile.sh |   24 +-------
 testcases/commands/unzip/unzip_tests.sh   |   85 +++++++++++-------------------
 4 files changed, 60 insertions(+), 84 deletions(-)

Index: ltp-full-20090131/testcases/commands/unzip/Makefile
===================================================================
--- ltp-full-20090131.orig/testcases/commands/unzip/Makefile
+++ ltp-full-20090131/testcases/commands/unzip/Makefile
@@ -1,12 +1,27 @@
-all:
-       ./unzip_genfile.sh
+# Only build and install if we have the zip and unzip commands
+HAVE_ZIPTOOLS=$(shell which zip && which unzip && echo t)
+ifeq ($(HAVE_ZIPTOOLS),t)
+BINS := unzip_tests.sh
+DEPS := tst_unzip_file.zip $(BINS)
+else
+BINS :=
+DEPS :=
+endif
 
-install:
-       ln -f unzip_tests.sh ../../bin/unzip_tests.sh
-       ln -f tst_unzip_file.zip ../../bin/tst_unzip_file.zip
-       ln -fs $(PWD)/unzip/tst_unzip_file.zip /tmp/tst_unzip_file.zip
-       chmod 777 tst_unzip_file.zip
+all: $(DEPS)
+
+tst_unzip_file.zip: tst_unzip.dir
+       zip -r tst_unzip_file.zip tst_unzip.dir && chmod 777 tst_unzip_file.zip
+
+# This target should be auto-removed by Make
+.INTERMEDIATE: tst_unzip.dir
+tst_unzip.dir: unzip_genfile.sh
+       ./unzip_genfile.sh tst_unzip.dir
+
+install: all
+       for i in $(BINS) ; do \
+               ln -f $$i ../../bin/$$i ; \
+       done
 
 clean:
-       rm -f tst_unzip_file.zip
-       rm -f /tmp/tst_unzip_file.zip
+       rm -rf tst_unzip.dir tst_unzip_file.zip
Index: ltp-full-20090131/testcases/commands/unzip/unzip_genfile.sh
===================================================================
--- ltp-full-20090131.orig/testcases/commands/unzip/unzip_genfile.sh
+++ ltp-full-20090131/testcases/commands/unzip/unzip_genfile.sh
@@ -33,7 +33,7 @@
 
 numdirs=3                     # number of directories to create
 numfiles=3                    # number of file to create in each directory
-dirname=/tmp/tst_unzip.dir    # name of the base directory
+dirname=$1                   # name of the base directory
 dircnt=0                      # index into number of dirs created in loop
 fcnt=0                        # index into number of files created in loop
 RC=0                          # return value from commands
@@ -44,7 +44,7 @@ do
        mkdir -p $dirname || RC=$?
        if [ $RC -ne 0 ]
        then
-               echo "unzip_genfile.sh: ERROR: while creating $numdirs dirs." 
1>&2
+               echo "$0: ERROR: while creating $numdirs dirs." 1>&2
                exit $RC
        fi
        fcnt=0
@@ -53,28 +53,10 @@ do
                touch $dirname/f.$fcnt
                if [ $RC -ne 0 ]
                then
-                       echo "unzip_genfile.sh: ERROR: creating $numdirs dirs." 
1>&2
+                       echo "$0: ERROR: creating $numdirs dirs." 1>&2
                        exit $RC
                fi
                fcnt=$(($fcnt+1))
        done
        dircnt=$(($dircnt+1))
 done
-
-# Create ZIP file.
-
-zip -r tst_unzip_file.zip /tmp/tst_unzip.dir || RC=$?
-if [ $RC -ne 0 ]
-then
-       echo "unzip_genfile.sh: ERROR: creating tst_unzip_file.zip archive." 
1>&2
-       exit $RC
-fi
-
-rm -fr /tmp/tst_unzip.* || RC=$?
-if [ $RC -ne 0 ]
-then
-       echo "unzip_genfile.sh: ERROR: deleting tempory files." 1>&2
-       exit $RC
-fi
-
-exit $RC
Index: ltp-full-20090131/testcases/commands/unzip/unzip_tests.sh
===================================================================
--- ltp-full-20090131.orig/testcases/commands/unzip/unzip_tests.sh
+++ ltp-full-20090131/testcases/commands/unzip/unzip_tests.sh
@@ -21,18 +21,8 @@
 #
 # File :         unzip_tests.sh
 #
-# Description:   Test Basic functionality of unzip command.
-#                Test #1: Test that unzip -f <file.conf> rotates the logfile
-#                as per the specifications in the conf file. Create a file
-#                tst_logfile in /var/log/. Create a conf file such that this
-#                logfile is set for rotation every week. Execute the command
-#                unzip -f <file.conf>, check to see if it forced rotation.
-#                Test #2: Check if unzip running as a cronjob will rotate a
-#                logfile when it exceeds a specific size. Create two cronjobs 
-#                1. runs a command to log a string to a logfile. 2. runs
-#                unzip <file.conf> every minute. The conf file specifies
-#                that the rotation happen only if the log file exceeds 2k file
-#                size. 
+# Description:   Test Basic functionality of unzip command. Pass in the zip
+#               file to test.
 #
 # Author:        Manoj Iyer, [email protected]
 #
@@ -54,7 +44,7 @@
 chk_ifexists()
 {
        RC=0
-       which $2 > $LTPTMP/tst_unzip.err || RC=$?
+       which $2 > "$LTPTMP/tst_unzip.err" || RC=$?
        if [ $? -ne 0 ]
        then
                tst_brkm TBROK NULL "$1: command $2 not found."
@@ -71,11 +61,10 @@ chk_ifexists()
 #                              - non-zero on failure.
 cleanup()
 {
+       popd
        # remove all the temporary files created by this test.
-       tst_resm TINFO "CLEAN: removing $LTPTMP"
-       rm -fr $LTPTMP
-       tst_resm TINFO "CLEAN: removing $PWD/tmp"
-       rm -fr $PWD/tmp
+       tst_resm TINFO "CLEAN: removing \"$LTPTMP\""
+       rm -fr "$LTPTMP"
 }
 
 
@@ -96,16 +85,11 @@ init()
        export TST_COUNT=0
 
        # Inititalize cleanup function.
-       trap "cleanup" 0
 
        # create the temporary directory used by this testcase
-       if [ -d $TMP ]
-       then
-               LTPTMP=/tmp/tst_unzip.$$
-               TMP=/tmp
-       else
-               LTPTMP=$TMP/tst_unzip.$$
-       fi
+       LTPTMP=`mktemp -d $$.XXXXXX` || tst_resm TBROK "Unable to create 
temporary directory with: mktemp -d $$.XXXXXX"
+       trap "cleanup" 0
+       pushd "$LTPTMP"
 
        # check if commands tst_*, unzip, awk, etc exists.
        chk_ifexists INIT tst_resm  || return $RC
@@ -113,29 +97,22 @@ init()
        chk_ifexists INIT mkdir     || return $RC
        chk_ifexists INIT awk       || return $RC
 
-       mkdir -p $LTPTMP >/dev/null || RC=$?
-       if [ $RC -ne 0 ]
-       then
-                tst_brkm TBROK "INIT: Unable to create temporary directory"
-                return $RC
-       fi
-
        # create expected output files. tst_unzip.exp
        cat > $LTPTMP/tst_unzip.out.exp <<-EOF
        Archive:  $TMP/tst_unzip_file.zip
-    creating: tmp/tst_unzip.dir/
-    creating: tmp/tst_unzip.dir/d.0/
-    extracting: tmp/tst_unzip.dir/d.0/f.0  
-    extracting: tmp/tst_unzip.dir/d.0/f.1  
-    extracting: tmp/tst_unzip.dir/d.0/f.2  
-    creating: tmp/tst_unzip.dir/d.0/d.1/
-    extracting: tmp/tst_unzip.dir/d.0/d.1/f.0  
-    extracting: tmp/tst_unzip.dir/d.0/d.1/f.1  
-    extracting: tmp/tst_unzip.dir/d.0/d.1/f.2  
-    creating: tmp/tst_unzip.dir/d.0/d.1/d.2/
-    extracting: tmp/tst_unzip.dir/d.0/d.1/d.2/f.0  
-    extracting: tmp/tst_unzip.dir/d.0/d.1/d.2/f.1  
-    extracting: tmp/tst_unzip.dir/d.0/d.1/d.2/f.2  
+    creating: tst_unzip.dir/
+    creating: tst_unzip.dir/d.0/
+    extracting: tst_unzip.dir/d.0/f.0
+    extracting: tst_unzip.dir/d.0/f.1
+    extracting: tst_unzip.dir/d.0/f.2
+    creating: tst_unzip.dir/d.0/d.1/
+    extracting: tst_unzip.dir/d.0/d.1/f.0
+    extracting: tst_unzip.dir/d.0/d.1/f.1
+    extracting: tst_unzip.dir/d.0/d.1/f.2
+    creating: tst_unzip.dir/d.0/d.1/d.2/
+    extracting: tst_unzip.dir/d.0/d.1/d.2/f.0
+    extracting: tst_unzip.dir/d.0/d.1/d.2/f.1
+    extracting: tst_unzip.dir/d.0/d.1/d.2/f.2
        EOF
 
        return $RC
@@ -156,34 +133,35 @@ test01()
        count=0
        files=" "
        filesize=0
+       zipfile="$1"
 
        TCID=unzip01
        TST_COUNT=1
 
        tst_resm TINFO "Test #1: unzip command un-compresses a .zip file."
 
-       unzip $TMP/tst_unzip_file.zip >$LTPTMP/tst_unzip.out || RC=$?
+       unzip "${zipfile}" > "$LTPTMP/tst_unzip.out" || RC=$?
        if [ $RC -ne 0 ]
        then
-               tst_res TFAIL $LTPTMP/tst_unzip.out \
+               tst_res TFAIL "$LTPTMP/tst_unzip.out" \
                        "Test #1: unzip command failed. Return value = $RC. 
Details:"
                return $RC
        else
-               diff -iwB $LTPTMP/tst_unzip.out $LTPTMP/tst_unzip.out.exp > 
$LTPTMP/tst_unzip.out.err || RC=$?
+               diff -iwB "$LTPTMP/tst_unzip.out" "$LTPTMP/tst_unzip.out.exp" > 
"$LTPTMP/tst_unzip.out.err" || RC=$?
                if [ $RC -ne 0 ]
                then
-                       tst_res TFAIL $LTPTMP/tst_unzip.out.err \
+                       tst_res TFAIL "$LTPTMP/tst_unzip.out.err" \
                                "Test #1: unzip output differs from expected 
output. Details"
                else
-                       tst_resm TINFO "Test #1: check if 
$PWD/tmp/tst_unzip.dir exits."
-                       if ! [ -d $PWD/tmp/tst_unzip.dir ]
+                       tst_resm TINFO "Test #1: check if 
\"$PWD/tst_unzip.dir\" exits."
+                       if ! [ -d $PWD/tst_unzip.dir ]
                        then
                                tst_resm TFAIL \
                                        "Test #1: unzip did not uncompress the 
.zip file"
                                $((RC+1))
                        else
                                tst_resm TINFO \
-                                       "Test #1: $PWD/tmp/tst_unzip.dir was 
created by unzip"
+                                       "Test #1: \"$PWD/tst_unzip.dir\" was 
created by unzip"
                                tst_resm TPASS \
                                   "Test #1: unzip can uncompress .zip file 
correctly."
                        fi
@@ -201,8 +179,9 @@ test01()
 #               - non-zero on failure.
 
 RC=0
+stat "$1" || exit $?
 init || exit $?
 
-test01 || RC=$?
+test01 "$1" || RC=$?
 
 exit $RC
Index: ltp-full-20090131/runtest/commands
===================================================================
--- ltp-full-20090131.orig/runtest/commands
+++ ltp-full-20090131/runtest/commands
@@ -10,7 +10,7 @@ cron export TCdat=$LTPROOT/testcases/bin
 logrotate export TCdat=$LTPROOT/testcases/bin; logrotate_tests.sh
 mail export TCdat=$LTPROOT/testcases/bin; mail_tests.sh
 cpio export TCdat=$LTPROOT/testcases/bin; cpio_tests.sh
-unzip01 unzip_tests.sh
+unzip01 unzip_tests.sh $LTPROOT/testcases/commands/unzip/tst_unzip_file.zip
 gzip01 gzip_tests.sh
 cp_tests01 cp_tests.sh
 ln_tests01 ln_tests.sh

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to