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