fstests: add tool migrate group membership data to test files
[xfstests-dev.git] / tools / convert-group
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2021 Oracle.  All Rights Reserved.
4
5 # Move group tags from the groups file into the test files themselves.
6
7 if [ -z "$1" ] || [ "$1" = "--help" ]; then
8         echo "Usage: $0 test_dir [test_dirs...]"
9         exit 1
10 fi
11
12 obliterate_group_file() {
13         sed -e 's/^#.*$//g' < group | while read test groups; do
14                 if [ -z "$test" ]; then
15                         continue;
16                 elif [ ! -e "$test" ]; then
17                         echo "Ignoring unknown test file \"$test\"."
18                         continue
19                 fi
20
21                 # Replace all the open-coded test preparation code with a
22                 # single call to _begin_fstest.
23                 sed -e '/^seqres=\$RESULT_DIR\/\$seq$/d' \
24                     -e '/^seqres=\"\$RESULT_DIR\/\$seq\"$/d' \
25                     -e '/^echo "QA output created by \$seq"$/d' \
26                     -e '/^here=`pwd`$/d' \
27                     -e '/^here=\$(pwd)$/d' \
28                     -e '/^here=\$PWD$/d' \
29                     -e '/^here=\"`pwd`\"$/d' \
30                     -e '/^tmp=\/tmp\/\$\$$/d' \
31                     -e '/^status=1.*failure.*is.*the.*default/d' \
32                     -e '/^status=1.*FAILure.*is.*the.*default/d' \
33                     -e '/^status=1.*success.*is.*the.*default/d' \
34                     -e '/^status=1.*default.*failure/d' \
35                     -e '/^echo.*QA output created by.*seq/d' \
36                     -e '/^# remove previous \$seqres.full before test/d' \
37                     -e '/^rm -f \$seqres.full/d' \
38                     -e 's|^# get standard environment, filters and checks|# Import common functions.|g' \
39                     -e '/^\. \.\/common\/rc/d' \
40                     -e '/^\. common\/rc/d' \
41                     -e 's|^seq=.*$|. ./common/preamble\n_begin_fstest '"$groups"'|g' \
42                     -i "$test"
43
44                 # Replace the open-coded trap calls that register cleanup code
45                 # with a call to _register_cleanup.
46                 #
47                 # For tests that registered empty-string cleanups or open-coded
48                 # calls to remove $tmp files, remove the _register_cleanup
49                 # calls entirely because the default _cleanup does that for us.
50                 #
51                 # For tests that now have a _register_cleanup call for the
52                 # _cleanup function, remove the explicit call because
53                 # _begin_fstest already registers that for us.
54                 #
55                 # For tests that override _cleanup, insert a comment noting
56                 # that it is overriding the default, to match the ./new
57                 # template.
58                 sed -e 's|^trap "exit \\\$status" 0 1 2 3 15|_register_cleanup ""|g' \
59                     -e 's|^trap "\(.*\)[[:space:]]*; exit \\\$status" 0 1 2 3 15|_register_cleanup "\1"|g' \
60                     -e 's|^trap "\(.*\)[[:space:]]*; exit \\\$status" 1 2 3 15|_register_cleanup "\1"|g' \
61                     -e 's|^trap '"'"'\(.*\)[[:space:]]*; exit \$status'"'"' 0 1 2 3 15|_register_cleanup "\1"|g' \
62                     -e 's|^trap "\(.*\)[[:space:]]*; exit \\\$status" 0 1 2 3 7 15|_register_cleanup "\1" BUS|g' \
63                     -e 's|^_register_cleanup "[[:space:]]*\([^[:space:]]*\)[[:space:]]*"|_register_cleanup "\1"|g' \
64                     -e '/^_register_cleanup ""$/d' \
65                     -e '/^_register_cleanup "rm -f \$tmp.*"$/d' \
66                     -e '/^_register_cleanup "_cleanup"$/d' \
67                     -e 's|^_cleanup()|# Override the default cleanup function.\n_cleanup()|g' \
68                     -i "$test"
69
70                 # If the test doesn't import any common functionality,
71                 # get rid of the pointless comment.
72                 if ! grep -q '^\. .*common' "$test"; then
73                         sed -e '/^# Import common functions.$/d' -i "$test"
74                 fi
75
76                 # Replace the "status=1" lines that don't have the usual
77                 # "failure is the default" message if there's no other code
78                 # between _begin_fstest and status=1.
79                 if grep -q '^status=1$' "$test"; then
80                         awk '
81 BEGIN {
82         saw_groupinfo = 0;
83 }
84 {
85         if ($0 ~ /^_begin_fstest/) {
86                 saw_groupinfo = 1;
87                 printf("%s\n", $0);
88         } else if ($0 ~ /^status=1$/) {
89                 if (saw_groupinfo == 0) {
90                         printf("%s\n", $0);
91                 }
92         } else if ($0 == "") {
93                 printf("\n");
94         } else {
95                 saw_groupinfo = 0;
96                 printf("%s\n", $0);
97         }
98 }
99 ' < "$test" > "$test.new"
100                         cat "$test.new" > "$test"
101                         rm -f "$test.new"
102                 fi
103
104                 # Get rid of _cleanup functions that match the standard one.
105                 # Thanks to Eric Biggers for providing this.
106                 sed -z -E \
107                         -e 's/(#[^#\n]*\n)*_cleanup\(\)\n\{\n(\s+cd \/\n)?\s+rm -r?f "?\$tmp"?\.\*\n\}\n\n?//' \
108                         -e 's/(#[^#\n]*\n)*_cleanup\(\)\n\{\n(\s+cd \/\n)?\s+rm -fr "?\$tmp"?\.\*\n\}\n\n?//' \
109                         -i "$test"
110
111                 # Collapse sequences of blank lines to a single blank line.
112                 awk '
113 BEGIN {
114         saw_blank = 0;
115 }
116 {
117         if ($0 ~ /^$/) {
118                 if (saw_blank == 0) {
119                         printf("\n");
120                         saw_blank = 1;
121                 }
122         } else {
123                 printf("%s\n", $0);
124                 saw_blank = 0;
125         }
126 }
127 ' < "$test" > "$test.new"
128                 cat "$test.new" > "$test"
129                 rm -f "$test.new"
130         done
131 }
132
133 curr_dir="$PWD"
134 for tdir in "$@"; do
135         cd "tests/$tdir"
136         obliterate_group_file
137         cd "$curr_dir"
138 done