fstests: groupfile generation needs force overwrite
[xfstests-dev.git] / tools / mkgroupfile
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2021 Oracle.  All Rights Reserved.
4 #
5 # Generate a group file from the _begin_fstest call in each test.
6
7 if [ "$1" = "--help" ]; then
8         echo "Usage: (cd tests/XXX/ ; ../../tools/mkgroupfile [output])"
9         exit 1
10 fi
11
12 test_dir="$PWD"
13 groupfile="$1"
14 GROUPNAME_DOC_FILE="$(readlink -m ../../doc/group-names.txt)"
15 export GROUPNAME_DOC_FILE
16
17 if [ ! -x ../../check ]; then
18         echo "$0: Run this from tests/XXX/."
19         exit 1
20 fi
21
22 cleanup() {
23         test -z "$groupfile" && return
24         test -z "$ngroupfile" && return
25
26         if [ $ret -eq 0 ]; then
27                 mv -f "$ngroupfile" "$groupfile"
28         else
29                 rm -f "$ngroupfile"
30         fi
31 }
32
33 ret=1   # trigger cleanup of temporary files unless we succeed
34 trap 'cleanup; exit $ret' EXIT INT TERM QUIT
35
36 generate_groupfile() {
37         cat << ENDL
38 # QA groups control file, automatically generated.
39 # See _begin_fstest in each test for details.
40
41 ENDL
42         cd ../../
43         export GENERATE_GROUPS=yes
44         grep -R -l "^_begin_fstest" "$test_dir/" 2>/dev/null | while read testfile; do
45                 test -x "$testfile" && "$testfile" || return 1
46         done | sort -g
47         ret="${PIPESTATUS[1]}"
48         cd "$test_dir"
49 }
50
51 if [ -z "$groupfile" ] || [ "$groupfile" = "-" ]; then
52         # Dump the group file to stdout and exit
53         unset groupfile
54         generate_groupfile
55 else
56         # Otherwise, write the group file to disk somewhere.
57         ngroupfile="${groupfile}.new"
58         rm -f "$ngroupfile"
59         generate_groupfile >> "$ngroupfile"
60         # let cleanup rename or delete ngroupfile
61 fi