fstests: faster group file creation
[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 new_groups="/tmp/groups.$$"
15 GROUPNAME_DOC_FILE="$(readlink -m ../../doc/group-names.txt)"
16
17 if [ ! -x ../../check ]; then
18         echo "$0: Run this from tests/XXX/."
19         exit 1
20 fi
21
22 cleanup()
23 {
24         rm -f $new_groups.check
25         rm -f $new_groups
26 }
27
28 ret=1   # trigger cleanup of temporary files unless we succeed
29 trap 'cleanup; exit $ret' EXIT INT TERM QUIT
30
31 # Make sure each group is in the documentation file.
32 _check_groups() {
33         test -n "$GROUPNAME_DOC_FILE" || return 0
34
35         local groups="$1"
36         declare -a missing=()
37
38         for group in `grep -v '#' $groups`; do
39                 if ! grep -q "^${group}[[:space:]]" "$GROUPNAME_DOC_FILE"; then
40                         missing+=("\"${group}\"")
41                 fi
42         done
43         test "${#missing}" -eq 0 && return 0
44
45         local suffix=
46         test "${#missing}" -gt 1 && suffix="s"
47         echo "group$suffix ${missing[@]} not mentioned in documentation." 1>&2
48         ret=1
49         exit 1
50 }
51
52 generate_groupfile() {
53         cat << ENDL > $new_groups
54 # QA groups control file, automatically generated.
55 # See _begin_fstest in each test for details.
56
57 ENDL
58
59         cd ../../
60
61         # Aggregate the groups each test belongs to for the group file
62         grep -I -R "^_begin_fstest" $test_dir/ | \
63                 sed -e 's/^.*\/\([0-9]*\):_begin_fstest/\1/' >> $new_groups
64
65         # Create the list of unique groups for existence checking
66         grep -I -R "^_begin_fstest" $test_dir/ | \
67                 sed -e 's/^.*_begin_fstest //' -e 's/ /\n/g' | \
68                 sort -u > $new_groups.check
69
70         _check_groups $new_groups.check
71
72         cd "$test_dir"
73 }
74
75 if [ -z "$groupfile" ] || [ "$groupfile" = "-" ]; then
76         # Dump the group file to stdout and exit
77         unset groupfile
78         generate_groupfile
79         cat $new_groups
80 else
81         # Otherwise, write the group file to disk somewhere.
82         generate_groupfile
83         mv -f "$new_groups" "$groupfile"
84 fi
85
86 # Success!
87 ret=0