README: fsgqa needs a home directory too
[xfstests-dev.git] / new
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2000-2005 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # Make a new test
6 #
7
8 # generic initialization
9 iam=new
10 . ./common/test_names
11
12 trap "rm -f /tmp/$$.; exit" 0 1 2 3 15
13
14 _cleanup()
15 {
16     :
17 }
18
19 SRC_GROUPS=`find tests -not -path tests -type d -printf "%f "`
20 usage()
21 {
22     echo "Usage $0 test_dir"
23     echo "Available dirs are: $SRC_GROUPS"
24     exit
25 }
26
27 [ $# -eq 0 ] && usage
28 tdir=tests/$1
29 shift
30
31 if [ ! -f $tdir/group ]
32 then
33     echo "Creating the $tdir/group index ..."
34     cat <<'End-of-File' >$tdir/group
35 # QA groups control
36 #
37 # define groups and default group owners
38 # do not start group name with a digit
39 #
40
41 # catch-all
42 #
43 other           some-user-login
44
45 # test-group association ... one line per test
46 #
47 End-of-File
48 fi
49
50 if [ ! -w $tdir/group ]
51 then
52     chmod u+w $tdir/group
53     echo "Warning: making the index file \"$tdir/group\" writeable"
54 fi
55
56 if make
57 then
58     :
59 else
60     echo "Warning: make failed -- some tests may be missing"
61 fi
62
63 i=0
64 line=0
65 eof=1
66 [ -f "$tdir/group" ] || usage
67
68 export AWK_PROG="$(type -P awk)"
69 [ "$AWK_PROG" = "" ] && { echo "awk not found"; exit; }
70
71 for found in `cat $tdir/group | tr - ' ' | $AWK_PROG '{ print $1 }'`
72 do
73     line=$((line+1))
74     if [ -z "$found" ] || [ "$found" == "#" ]; then
75         continue
76     elif ! echo "$found" | grep -q "^$VALID_TEST_NAME$"; then
77         # this one is for tests not named by a number
78         continue
79     fi
80     i=$((i+1))
81     id=`printf "%03d" $i`
82     if [ "$id" != "$found" ];then
83         eof=0
84         break
85     fi
86 done
87 if [ $eof -eq 1 ]; then
88    line=$((line+1))
89    i=$((i+1))
90    id=`printf "%03d" $i`
91 fi
92
93 echo "Next test id is $id"
94
95 read -p "Append a name to the ID? Test name will be $id-\$name. y,[n]: " -r
96 if [[ $REPLY = [Yy] ]]; then
97         # get the new name from user
98         name=""
99         while [ "$name" = "" ]; do
100                 read -p "Enter the name: "
101                 if [ "$REPLY" = "" ]; then
102                         echo "For canceling, use ctrl+c."
103                 elif echo "$id-$REPLY" | grep -q "^$VALID_TEST_NAME$"; then
104                         if [ -e "$tdir/$id-$REPLY" ]; then
105                                 echo "File '$id-$REPLY' already exists, use another one."
106                                 echo
107                         else
108                                 name="$REPLY"
109                         fi
110                 else
111                         echo "A name can contain only alphanumeric symbols and dash!"
112                         echo
113                 fi
114         done
115
116         # now find where to insert this name
117         eof=1
118         for found in `tail -n +$line $tdir/group | $AWK_PROG '{ print $1 }'`; do
119                 found_id=$(echo "$found" | cut -d "-" -f 1 - )
120                 line=$((line+1))
121                 if [ -z "$found" ] || [ "$found" == "#" ]; then
122                         continue
123                 elif [ $found_id -gt $id ]; then
124                         eof=0
125                         break
126                 fi
127         done
128         if [ $eof -eq 0 ]; then
129                 # If place wasn't found, let $line be the end of the file
130                 line=$((line-1))
131         fi
132         id="$id-$name"
133 fi
134 echo "Creating test file '$id'"
135
136 if [ -f $tdir/$id ]
137 then
138     echo "Error: test $id already exists!"
139     _cleanup
140     exit 1
141 fi
142
143 echo -n "Creating skeletal script for you to edit ..."
144
145 year=`date +%Y`
146
147 cat <<End-of-File >$tdir/$id
148 #! /bin/bash
149 # SPDX-License-Identifier: GPL-2.0
150 # Copyright (c) $year YOUR NAME HERE.  All Rights Reserved.
151 #
152 # FS QA Test $id
153 #
154 # what am I here for?
155 #
156 seq=\`basename \$0\`
157 seqres=\$RESULT_DIR/\$seq
158 echo "QA output created by \$seq"
159
160 here=\`pwd\`
161 tmp=/tmp/\$\$
162 status=1        # failure is the default!
163 trap "_cleanup; exit \\\$status" 0 1 2 3 15
164
165 _cleanup()
166 {
167         cd /
168         rm -f \$tmp.*
169 }
170
171 # get standard environment, filters and checks
172 . ./common/rc
173 . ./common/filter
174
175 # remove previous \$seqres.full before test
176 rm -f \$seqres.full
177
178 # real QA test starts here
179
180 # Modify as appropriate.
181 _supported_fs generic
182 _supported_os Linux
183 _require_test
184
185 # if error
186 exit
187
188 # optional stuff if your test has verbose output to help resolve problems
189 #echo
190 #echo "If failure, check \$seqres.full (this) and \$seqres.full.ok (reference)"
191
192 # success, all done
193 status=0
194 exit
195 End-of-File
196
197 sleep 2         # latency to read messages to this point
198 echo ""
199
200 chmod 755 $tdir/$id
201 ${EDITOR-vi} $tdir/$id
202
203 # Create default .out file
204 cat <<End-of-File >$tdir/$id.out
205 QA output created by $id
206 Silence is golden
207 End-of-File
208
209 if [ $# -eq 0 ]
210 then
211
212     while true
213     do
214         echo -n "Add to group(s) [other] (separate by space, ? for list): "
215         read ans
216         [ -z "$ans" ] && ans=other
217         if [ "X$ans" = "X?" ]
218         then
219             for d in $SRC_GROUPS; do
220                 l=$(sed -n < tests/$d/group \
221                     -e 's/#.*//' \
222                     -e 's/$/ /' \
223                     -e 's;\(^[0-9][0-9][0-9]\)\(.*$\);\2;p')
224                 grpl="$grpl $l"
225             done
226             lst=`for word in $grpl; do echo $word; done | sort| uniq `
227             echo $lst
228         else
229             # only allow lower cases, spaces, digits and underscore in group
230             inval=`echo $ans | tr -d '[:lower:][:space:][:digit:]_'`
231             if [ "$inval" != "" ]; then
232                 echo "Invalid characters in group(s): $inval"
233                 echo "Only lower cases, digits and underscore are allowed in groups, separated by space"
234                 continue
235             else
236                 # remove redundant spaces/tabs
237                 ans=`echo "$ans" | sed 's/\s\+/ /g'`
238                 break
239             fi
240         fi
241     done
242 else
243     # expert mode, groups are on the command line
244     #
245     for g in $*
246     do
247         if grep "^$g[   ]" $tdir/group >/dev/null
248         then
249             :
250         else
251             echo "Warning: group \"$g\" not defined in $tdir/group"
252         fi
253     done
254     ans="$*"
255 fi
256
257 echo -n "Adding $id to group index ..."
258 head -n $(($line-1)) $tdir/group > /tmp/$$.group
259 echo "$id $ans" >> /tmp/$$.group
260 tail -n +$((line)) $tdir/group >> /tmp/$$.group
261 mv /tmp/$$.group $tdir/group
262 echo " done."
263
264 exit 0