1#! /bin/sh
2#
3# Copyright by The HDF Group.
4# Copyright by the Board of Trustees of the University of Illinois.
5# All rights reserved.
6#
7# This file is part of HDF5.  The full HDF5 copyright notice, including
8# terms governing use, modification, and redistribution, is contained in
9# the COPYING file, which can be found at the root of the source code
10# distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.
11# If you do not have access to either file, you may request a copy from
12# help@hdfgroup.org.
13#
14# Tests for the h5mkgrp tool
15#
16# Quincey Koziol (koziol@hdfgroup.org)
17# Tuesday, February 13, 2007
18#
19
20srcdir=@srcdir@
21
22TESTNAME=h5mkgrp
23EXIT_SUCCESS=0
24EXIT_FAILURE=1
25
26H5MKGRP=../../src/misc/h5mkgrp             # The tool name
27H5MKGRP_BIN=`pwd`/$H5MKGRP  # The path of the tool binary
28H5LS=../../src/h5ls/h5ls                   # The h5ls tool name
29H5LS_ARGS=-vr               # Arguments to the h5ls tool
30H5LS_BIN=`pwd`/$H5LS # The path of the h5ls tool binary
31
32RM='rm -rf'
33CMP='cmp -s'
34DIFF='diff -c'
35CP='cp'
36DIRNAME='dirname'
37LS='ls'
38AWK='awk'
39
40nerrors=0
41verbose=yes
42
43# source dirs
44SRC_TOOLS="$srcdir/../.."
45
46SRC_TOOLS_TESTFILES="$SRC_TOOLS/testfiles"
47# testfiles source dirs for tools
48SRC_H5MKGRP_TESTFILES="$SRC_TOOLS/test/misc/testfiles"
49
50TESTDIR=./testgrp
51test -d $TESTDIR || mkdir -p $TESTDIR
52
53######################################################################
54# test files
55# --------------------------------------------------------------------
56# All the test files copy from source directory to test directory
57# NOTE: Keep this framework to add/remove test files.
58#       Any test files from other tools can be used in this framework.
59#       This list are also used for checking exist.
60#       Comment '#' without space can be used.
61# --------------------------------------------------------------------
62
63#
64# copy test files and expected output files from source dirs to test dir
65#
66COPY_TESTFILES="
67$SRC_H5MKGRP_TESTFILES/h5mkgrp_help.txt
68$SRC_TOOLS_TESTFILES/h5mkgrp_single.ls
69$SRC_TOOLS_TESTFILES/h5mkgrp_single_v.ls
70$SRC_TOOLS_TESTFILES/h5mkgrp_single_p.ls
71$SRC_TOOLS_TESTFILES/h5mkgrp_single_l.ls
72$SRC_TOOLS_TESTFILES/h5mkgrp_several.ls
73$SRC_TOOLS_TESTFILES/h5mkgrp_several_v.ls
74$SRC_TOOLS_TESTFILES/h5mkgrp_several_p.ls
75$SRC_TOOLS_TESTFILES/h5mkgrp_several_l.ls
76$SRC_TOOLS_TESTFILES/h5mkgrp_nested_p.ls
77$SRC_TOOLS_TESTFILES/h5mkgrp_nested_lp.ls
78$SRC_TOOLS_TESTFILES/h5mkgrp_nested_mult_p.ls
79$SRC_TOOLS_TESTFILES/h5mkgrp_nested_mult_lp.ls
80"
81
82COPY_TESTFILES_TO_TESTDIR()
83{
84    # copy test files. Used -f to make sure get a new copy
85    for tstfile in $COPY_TESTFILES
86    do
87        # ignore '#' comment
88        echo $tstfile | tr -d ' ' | grep '^#' > /dev/null
89        RET=$?
90        if [ $RET -eq 1 ]; then
91            # skip cp if srcdir is same as destdir
92            # this occurs when build/test performed in source dir and
93            # make cp fail
94            SDIR=`$DIRNAME $tstfile`
95            INODE_SDIR=`$LS -i -d $SDIR | $AWK -F' ' '{print $1}'`
96            INODE_DDIR=`$LS -i -d $TESTDIR | $AWK -F' ' '{print $1}'`
97            if [ "$INODE_SDIR" != "$INODE_DDIR" ]; then
98                $CP -f $tstfile $TESTDIR
99                if [ $? -ne 0 ]; then
100                    echo "Error: FAILED to copy $tstfile ."
101
102                    # Comment out this to CREATE expected file
103                    exit $EXIT_FAILURE
104                fi
105            fi
106        fi
107    done
108}
109
110CLEAN_TESTFILES_AND_TESTDIR()
111{
112    # skip rm if srcdir is same as destdir
113    # this occurs when build/test performed in source dir and
114    # make cp fail
115    SDIR=$SRC_H5MKGRP_TESTFILES
116    INODE_SDIR=`$LS -i -d $SDIR | $AWK -F' ' '{print $1}'`
117    INODE_DDIR=`$LS -i -d $TESTDIR | $AWK -F' ' '{print $1}'`
118    if [ "$INODE_SDIR" != "$INODE_DDIR" ]; then
119        $RM $TESTDIR
120    fi
121}
122
123# Print a line-line message left justified in a field of 70 characters
124# beginning with the word "Testing".
125TESTING()
126{
127    SPACES="                                                               "
128    echo "Testing $* $SPACES" |cut -c1-70 |tr -d '\012'
129}
130
131# Source in the output filter function definitions.
132. $srcdir/../../../bin/output_filter.sh
133
134# Print a line-line message left justified in a field of 70 characters
135# beginning with the word "Verifying".
136#
137VERIFY_H5LS()
138{
139    SPACES="                                                               "
140    echo "Verifying h5ls file structure $* $SPACES" | cut -c1-70 | tr -d '\012'
141}
142
143# Run a test and print PASS or *FAIL*. If h5mkgrp can complete
144# with exit status 0, consider it pass. If a test fails then increment
145# the `nerrors' global variable.
146# Assumed arguments:
147# $* arguments for h5mkgrp.
148
149TOOLTEST()
150{
151    TESTING $H5MKGRP $@
152    (
153    cd $TESTDIR
154    $RUNSERIAL $H5MKGRP_BIN $@
155    ) > output.out
156    RET=$?
157    if [ $RET != 0 ]; then
158        echo "*FAILED*"
159        echo "failed result is:"
160        cat output.out
161        nerrors="`expr $nerrors + 1`"
162    else
163        echo " PASSED"
164
165        # Clean up output file
166        if test -z "$HDF5_NOCLEANUP"; then
167           rm -f output.out
168        fi
169    fi
170}
171
172# Call the h5ls tool to verify the correct output data in the destination file
173#
174H5LSTEST()
175{
176    expect="$TESTDIR/`basename $1 .h5`.ls"
177    actual="$TESTDIR/`basename $1 .h5`.out"
178    actual_sav=${actual}-sav
179
180    # Stderr is included in stdout so that the diff can detect
181    # any unexpected output from that stream too.
182    VERIFY_H5LS  $@
183    (
184      cd $TESTDIR
185      $RUNSERIAL $H5LS_BIN $H5LS_ARGS $@
186    ) 2>&1 |sed 's/Modified:.*/Modified:  XXXX-XX-XX XX:XX:XX XXX/' >$actual
187
188    # save actual in case it is needed later.
189    cp $actual $actual_sav
190    STDOUT_FILTER $actual
191    STDERR_FILTER $actual
192
193   if [ ! -f $expect ]; then
194      # Create the expect file if it doesn't yet exist.
195      echo " CREATED"
196      cp $actual $expect
197      echo "    Expected result (*.ls) missing"
198      nerrors="`expr $nerrors + 1`"
199   elif $CMP $expect $actual; then
200      echo " PASSED"
201   else
202      echo "*FAILED*"
203      echo "    Expected result (*.ls) differs from actual result (*.out)"
204      nerrors="`expr $nerrors + 1`"
205      test yes = "$verbose" && $DIFF $expect $actual |sed 's/^/    /'
206   fi
207
208   # Clean up output file
209   if test -z "$HDF5_NOCLEANUP"; then
210      rm -f $actual $actual_sav
211   fi
212}
213
214# Single run of tool
215#
216# Assumed arguments:
217# $1 is test file name
218# $2 is h5mkgrp options
219# $* are groups to create
220RUNTEST()
221{
222    FILEOUT=$1
223    shift
224    H5MKGRP_ARGS=$1
225    shift
226
227    # Remove any output file left over from previous test run
228    rm -f $FILEOUT
229
230    # Run test
231    TOOLTEST $H5MKGRP_ARGS $FILEOUT $@
232
233    # Verify that the file created above is correct
234    H5LSTEST $FILEOUT
235
236    # Remove output file created, if the "no cleanup" environment variable is
237    #   not defined
238    if test -z "$HDF5_NOCLEANUP"; then
239        rm -f $TESTDIR/$FILEOUT
240    fi
241}
242
243# Single run of tool
244#
245# Assumed arguments:
246# $1 is test expected output file
247# $2 is h5mkgrp options
248# $* are groups to create
249CMPTEST()
250{
251    FILEOUT=$1
252    expect="$TESTDIR/`basename $1 .h5`.txt"
253    actual="$TESTDIR/`basename $1 .h5`.out"
254    actual_err="$TESTDIR/`basename $1 .h5`.err"
255    shift
256
257    # Stderr is included in stdout so that the diff can detect
258    # any unexpected output from that stream too.
259    TESTING $H5MKGRP $@
260    (
261    cd $TESTDIR
262    $RUNSERIAL $H5MKGRP_BIN $@
263    ) >$actual 2>$actual_err
264    cat $actual_err >> $actual
265
266   if [ ! -f $expect ]; then
267    # Create the expect file if it doesn't yet exist.
268      echo " CREATED"
269      cp $actual $expect
270      echo "    Expected result (*.txt) missing"
271      nerrors="`expr $nerrors + 1`"
272   elif $CMP $expect $actual; then
273      echo " PASSED"
274   else
275      echo "*FAILED*"
276      echo "    Expected result (*.txt) differs from actual result (*.out)"
277      nerrors="`expr $nerrors + 1`"
278      test yes = "$verbose" && $DIFF $expect $actual |sed 's/^/    /'
279   fi
280
281   # Clean up output file
282   if test -z "$HDF5_NOCLEANUP"; then
283      rm -f $actual $actual_err
284   fi
285}
286
287##############################################################################
288###           T H E   T E S T S                                            ###
289##############################################################################
290# prepare for test
291COPY_TESTFILES_TO_TESTDIR
292
293# Check that help & version is displayed properly
294CMPTEST h5mkgrp_help.h5 "-h"
295#CMPTEST h5mkgrp_version.h5 "-V"
296
297# Create single group at root level
298RUNTEST h5mkgrp_single.h5 " " single
299RUNTEST h5mkgrp_single_v.h5 "-v" single
300RUNTEST h5mkgrp_single_p.h5 "-p" single
301RUNTEST h5mkgrp_single_l.h5 "-l" latest
302
303# Create several groups at root level
304RUNTEST h5mkgrp_several.h5 " " one two
305RUNTEST h5mkgrp_several_v.h5 "-v" one two
306RUNTEST h5mkgrp_several_p.h5 "-p" one two
307RUNTEST h5mkgrp_several_l.h5 "-l" one two
308
309# Create various nested groups
310RUNTEST h5mkgrp_nested_p.h5 "-p" /one/two
311RUNTEST h5mkgrp_nested_lp.h5 "-lp" /one/two
312RUNTEST h5mkgrp_nested_mult_p.h5 "-p" /one/two /three/four
313RUNTEST h5mkgrp_nested_mult_lp.h5 "-lp" /one/two /three/four
314
315# Clean up temporary files/directories
316CLEAN_TESTFILES_AND_TESTDIR
317
318if test $nerrors -eq 0 ; then
319    echo "All $TESTNAME tests passed."
320    exit $EXIT_SUCCESS
321else
322    echo "$TESTNAME tests failed with $nerrors errors."
323    exit $EXIT_FAILURE
324fi
325