3a657d204faba97d96d3eb55ffd4214313209bd4
[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 tmpfile="/tmp/$$."
13 trap "rm -f $tmpfile; exit" 0 1 2 3 15
14
15 _cleanup()
16 {
17     :
18 }
19
20 SRC_GROUPS=`find tests -not -path tests -type d -printf "%f "`
21 usage()
22 {
23     echo "Usage $0 test_dir|test_dir_and_name"
24     echo "Available dirs are: $SRC_GROUPS"
25     exit
26 }
27
28 [ $# -eq 0 ] && usage
29
30 if echo "$1" | grep -q '/'; then
31         if [ -e "tests/$1" ]; then
32                 echo "$1: test already exists."
33                 exit 1
34         fi
35         tdir="tests/$(echo "$1" | cut -d '/' -f 1)"
36         id="$(echo "$1" | cut -d '/' -f 2)"
37 else
38         tdir=tests/$1
39         id="$(basename "$(./tools/nextid "$1")")"
40 fi
41
42 i=0
43 line=0
44 eof=1
45 [ -d "$tdir/" ] || usage
46
47 export AWK_PROG="$(type -P awk)"
48 [ "$AWK_PROG" = "" ] && { echo "awk not found"; exit; }
49
50 echo "Next test id is $id"
51 shift
52
53 read -p "Append a name to the ID? Test name will be $id-\$name. y,[n]: " -r
54 if [[ $REPLY = [Yy] ]]; then
55         # get the new name from user
56         name=""
57         while [ "$name" = "" ]; do
58                 read -p "Enter the name: "
59                 if [ "$REPLY" = "" ]; then
60                         echo "For canceling, use ctrl+c."
61                 elif echo "$id-$REPLY" | grep -q "^$VALID_TEST_NAME$"; then
62                         if [ -e "$tdir/$id-$REPLY" ]; then
63                                 echo "File '$id-$REPLY' already exists, use another one."
64                                 echo
65                         else
66                                 name="$REPLY"
67                         fi
68                 else
69                         echo "A name can contain only alphanumeric symbols and dash!"
70                         echo
71                 fi
72         done
73
74         id="$id-$name"
75 fi
76
77 echo "Creating test file '$id'"
78
79 if [ -f $tdir/$id ]
80 then
81     echo "Error: test $id already exists!"
82     _cleanup
83     exit 1
84 fi
85
86 # Extract group names from the documentation.
87 group_names() {
88         awk '/^[[:lower:][:digit:]_]/ {
89                 if ($1 != "" && $1 != "Group" && $2 != "Name:" && $1 != "all")
90                         printf("%s\n", $1);
91         }' doc/group-names.txt
92 }
93
94 # Make sure that the new test's groups fit the correct format and are listed
95 # in the group documentation file.
96 check_groups() {
97         for g in "$@"; do
98                 local inval="$(echo "${g}" | tr -d '[:lower:][:space:][:digit:]_')"
99                 if [ -n "${inval}" ]; then
100                         echo "Invalid characters in group(s): ${inval}"
101                         echo "Only lower cases, digits and underscore are allowed in groups, separated by space"
102                         return 1
103                 elif ! group_names | grep -q -w "${g}"; then
104                         echo "Warning: group \"${g}\" not defined in documentation"
105                         return 1
106                 fi
107         done
108
109         return 0
110 }
111
112 if [ $# -eq 0 ]; then
113         # interactive mode
114         prompt="Add to group(s) [other] (separate by space, ? for list): "
115         while true; do
116                 read -p "${prompt}" -a new_groups || exit 1
117                 case "${#new_groups[@]}" in
118                 0)
119                         new_groups=("other")
120                         ;;
121                 1)
122                         if [ "${new_groups[0]}" = "?" ]; then
123                                 echo $(group_names)
124                                 continue
125                         fi
126                         ;;
127                 esac
128                 check_groups "${new_groups[@]}" && break
129         done
130 else
131         # expert mode, groups are on the command line
132         new_groups=("$@")
133         check_groups "${new_groups[@]}" || exit 1
134 fi
135
136 echo -n "Creating skeletal script for you to edit ..."
137
138 year=`date +%Y`
139
140 cat <<End-of-File >$tdir/$id
141 #! /bin/bash
142 # SPDX-License-Identifier: GPL-2.0
143 # Copyright (c) $year YOUR NAME HERE.  All Rights Reserved.
144 #
145 # FS QA Test $id
146 #
147 # what am I here for?
148 #
149 . ./common/preamble
150 _begin_fstest ${new_groups[@]}
151
152 # Override the default cleanup function.
153 # _cleanup()
154 # {
155 #       cd /
156 #       rm -r -f \$tmp.*
157 # }
158
159 # Import common functions.
160 # . ./common/filter
161
162 # real QA test starts here
163
164 # Modify as appropriate.
165 _supported_fs generic
166 _require_test
167
168 # if error
169 exit
170
171 # optional stuff if your test has verbose output to help resolve problems
172 #echo
173 #echo "If failure, check \$seqres.full (this) and \$seqres.full.ok (reference)"
174
175 # success, all done
176 status=0
177 exit
178 End-of-File
179
180 sleep 2         # latency to read messages to this point
181 echo ""
182
183 chmod 755 $tdir/$id
184 ${EDITOR-vi} $tdir/$id
185
186 # Create default .out file
187 cat <<End-of-File >$tdir/$id.out
188 QA output created by $id
189 Silence is golden
190 End-of-File
191
192 echo " done."
193 exit 0