1 | #!/bin/bash |
---|
2 | # ------------------------------------------------------------------------------ |
---|
3 | # (C) British Crown Copyright 2006-17 Met Office. |
---|
4 | # |
---|
5 | # This file is part of FCM, tools for managing and building source code. |
---|
6 | # |
---|
7 | # FCM is free software: you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation, either version 3 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # FCM is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | # ------------------------------------------------------------------------------ |
---|
20 | # NAME |
---|
21 | # test_header |
---|
22 | # |
---|
23 | # SYNOPSIS |
---|
24 | # . $FCM_HOME/t/lib/bash/test_header |
---|
25 | # |
---|
26 | # DESCRIPTION |
---|
27 | # Provide bash shell functions for writing tests for "fcm" commands to |
---|
28 | # output in Perl's TAP format. Add "set -eu". Create a temporary working |
---|
29 | # directory $TEST_DIR and change to it. Automatically increment test number. |
---|
30 | # If $FCM_HOME is not specified, set it to point to the "fcm" source tree |
---|
31 | # containing this script. Add $FCM_HOME/bin to the front of $PATH. |
---|
32 | # |
---|
33 | # FUNCTIONS |
---|
34 | # tests N |
---|
35 | # echo "1..$N". |
---|
36 | # skip N REASON |
---|
37 | # echo "ok $((++T)) # skip REASON" N times, where T is the test number. |
---|
38 | # skip_all REASON |
---|
39 | # echo "1..0 # SKIP $REASON" and exit. |
---|
40 | # pass TEST_KEY |
---|
41 | # echo "ok $T - $TEST_KEY" where T is the current test number. |
---|
42 | # fail TEST_KEY |
---|
43 | # echo "not ok $T - $TEST_KEY" where T is the current test number. |
---|
44 | # run_pass TEST_KEY COMMAND ... |
---|
45 | # Run $COMMAND. pass/fail $TEST_KEY if $COMMAND returns true/false. |
---|
46 | # Write STDOUT and STDERR in $TEST_KEY.out and $TEST_KEY.err. |
---|
47 | # run_fail TEST_KEY COMMAND ... |
---|
48 | # Run $COMMAND. pass/fail $TEST_KEY if $COMMAND returns false/true. |
---|
49 | # Write STDOUT and STDERR in $TEST_KEY.out and $TEST_KEY.err. |
---|
50 | # file_cmp TEST_KEY FILE_ACTUAL [$FILE_EXPECT] |
---|
51 | # Compare contents in $FILE_ACTUAL and $FILE_EXPECT. pass/fail |
---|
52 | # $TEST_KEY if contents are identical/different. If $FILE_EXPECT is "-" |
---|
53 | # or not defined, compare $FILE_ACTUAL with STDIN to this function. |
---|
54 | # file_test TEST_KEY FILE [OPTION] |
---|
55 | # pass/fail $TEST_KEY if "test $OPTION $FILE" returns 0/1. $OPTION is |
---|
56 | # -e if not specified. |
---|
57 | # file_grep TEST_KEY PATTERN FILE |
---|
58 | # Run "grep -q PATTERN FILE". pass/fail $TEST_KEY accordingly. |
---|
59 | # branch_tidy INPUT_FILE |
---|
60 | # Standardise branch-create output between Subversion 1.8 and 1.9. |
---|
61 | # commit_sort INPUT_FILE OUTPUT_FILE |
---|
62 | # Sort status and transmitting info within the commit output. |
---|
63 | # diff_sort INPUT_FILE OUTPUT_FILE |
---|
64 | # Sort Subversion diff output by filename. |
---|
65 | # diff_svn_version_filter |
---|
66 | # Preprocess stdin to stdout based on relevant '#IF SVN1.X' prefixes. |
---|
67 | # status_sort INPUT_FILE OUTPUT_FILE |
---|
68 | # Sort Subversion status lines. |
---|
69 | # merge_sort INPUT_FILE OUTPUT_FILE |
---|
70 | # Sort Subversion merge status lines. |
---|
71 | # check_svn_version |
---|
72 | # Check Subversion version and skip tests if not compatible. |
---|
73 | # FINALLY |
---|
74 | # This is run on EXIT or INT to remove the temporary working directory |
---|
75 | # for the test. Call FINALLY_MORE if it is declared. |
---|
76 | # |
---|
77 | # VARIABLES |
---|
78 | # FCM_HOME |
---|
79 | # Root of FCM's installation. (Exported.) |
---|
80 | # SIGNALS |
---|
81 | # List of signals trapped by FINALLY, currently EXIT and INT. |
---|
82 | # TEST_DIR |
---|
83 | # Temporary directory that is also the working directory for this test. |
---|
84 | # TEST_KEY_BASE |
---|
85 | # Base root name of current test file. |
---|
86 | # TEST_NUMBER |
---|
87 | # Test number of latest test. |
---|
88 | # TEST_SOURCE_DIR |
---|
89 | # Directory containing the current test file. |
---|
90 | #------------------------------------------------------------------------------- |
---|
91 | set -eu |
---|
92 | |
---|
93 | SIGNALS="EXIT INT" |
---|
94 | TEST_DIR= |
---|
95 | function FINALLY() { |
---|
96 | for S in $SIGNALS; do |
---|
97 | trap '' $S |
---|
98 | done |
---|
99 | if [[ -n $TEST_DIR ]]; then |
---|
100 | cd ~ |
---|
101 | rm -rf $TEST_DIR |
---|
102 | fi |
---|
103 | if declare -F FINALLY_MORE >/dev/null; then |
---|
104 | FINALLY_MORE |
---|
105 | fi |
---|
106 | |
---|
107 | } |
---|
108 | for S in $SIGNALS; do |
---|
109 | trap "FINALLY $S" $S |
---|
110 | done |
---|
111 | |
---|
112 | TEST_NUMBER=0 |
---|
113 | |
---|
114 | function tests() { |
---|
115 | echo "1..$1" |
---|
116 | } |
---|
117 | |
---|
118 | function skip() { |
---|
119 | local N_SKIPS=$1 |
---|
120 | shift 1 |
---|
121 | local I=0 |
---|
122 | while ((I++ < N_SKIPS)); do |
---|
123 | echo "ok $((++TEST_NUMBER)) # skip $@" |
---|
124 | done |
---|
125 | } |
---|
126 | |
---|
127 | function skip_all() { |
---|
128 | echo "1..0 # SKIP $@" |
---|
129 | exit |
---|
130 | } |
---|
131 | |
---|
132 | function pass() { |
---|
133 | echo "ok $((++TEST_NUMBER)) - $@" |
---|
134 | } |
---|
135 | |
---|
136 | function fail() { |
---|
137 | echo "not ok $((++TEST_NUMBER)) - $@" |
---|
138 | } |
---|
139 | |
---|
140 | function run_pass() { |
---|
141 | local TEST_KEY=$1 |
---|
142 | shift 1 |
---|
143 | if ! "$@" 1>$TEST_KEY.out 2>$TEST_KEY.err; then |
---|
144 | fail $TEST_KEY |
---|
145 | return |
---|
146 | fi |
---|
147 | pass $TEST_KEY |
---|
148 | } |
---|
149 | |
---|
150 | function run_fail() { |
---|
151 | local TEST_KEY=$1 |
---|
152 | shift 1 |
---|
153 | if "$@" 1>$TEST_KEY.out 2>$TEST_KEY.err; then |
---|
154 | fail $TEST_KEY |
---|
155 | return |
---|
156 | fi |
---|
157 | pass $TEST_KEY |
---|
158 | } |
---|
159 | |
---|
160 | function file_cmp() { |
---|
161 | local TEST_KEY=$1 |
---|
162 | local FILE_ACTUAL=$2 |
---|
163 | local FILE_EXPECT=${3:--} |
---|
164 | if diff -u $FILE_EXPECT $FILE_ACTUAL >&2; then |
---|
165 | pass $TEST_KEY |
---|
166 | return |
---|
167 | fi |
---|
168 | fail $TEST_KEY |
---|
169 | } |
---|
170 | |
---|
171 | function file_test() { |
---|
172 | local TEST_KEY=$1 |
---|
173 | local FILE=$2 |
---|
174 | local OPTION=${3:--e} |
---|
175 | if test $OPTION $FILE; then |
---|
176 | pass $TEST_KEY |
---|
177 | else |
---|
178 | fail $TEST_KEY |
---|
179 | fi |
---|
180 | } |
---|
181 | |
---|
182 | function file_grep() { |
---|
183 | local TEST_KEY=$1 |
---|
184 | local PATTERN=$2 |
---|
185 | local FILE=$3 |
---|
186 | if grep -q -e "$PATTERN" $FILE; then |
---|
187 | pass $TEST_KEY |
---|
188 | return |
---|
189 | fi |
---|
190 | fail $TEST_KEY |
---|
191 | } |
---|
192 | |
---|
193 | function branch_tidy() { |
---|
194 | local INPUT_FILE=$TEST_DIR/$1 |
---|
195 | sed -i "/^Committing transaction/d; /^$/d" "$INPUT_FILE" |
---|
196 | } |
---|
197 | |
---|
198 | function commit_sort() { |
---|
199 | local INPUT_FILE=$1 |
---|
200 | local OUTPUT_FILE=$2 |
---|
201 | local TMP_OUTPUT_FILE=$(mktemp) |
---|
202 | # Sort the svn status part of the message |
---|
203 | status_sort $INPUT_FILE $TMP_OUTPUT_FILE |
---|
204 | # Sort the 'Adding/Deleting', etc part of the message |
---|
205 | python -c 'import re, sys |
---|
206 | text = sys.stdin.read() |
---|
207 | sending_lines = re.findall("^\w+ing +.*$", text, re.M) |
---|
208 | prefix = text[:text.index(sending_lines[0])] |
---|
209 | suffix = text[(text.index(sending_lines[-1]) + len(sending_lines[-1])):] |
---|
210 | sending_lines.sort() |
---|
211 | print prefix + "\n".join(sending_lines) + suffix.rstrip() |
---|
212 | ' <"$TMP_OUTPUT_FILE" >"$OUTPUT_FILE" |
---|
213 | rm "$TMP_OUTPUT_FILE" |
---|
214 | # Remove 1.8 to 1.9 specific changes (transmitting, transaction lines). |
---|
215 | sed -i "/^Transmitting file data/d; /^Committing transaction/d; /^$/d" \ |
---|
216 | "$OUTPUT_FILE" |
---|
217 | } |
---|
218 | |
---|
219 | function diff_sort() { |
---|
220 | local INPUT_FILE=$1 |
---|
221 | local OUTPUT_FILE=$2 |
---|
222 | # Sort the diff file order. |
---|
223 | python -c 'import re, sys |
---|
224 | text = sys.stdin.read() |
---|
225 | print "\nIndex: ".join( |
---|
226 | [l.strip() for l in sorted(re.compile("^Index: ", re.M).split(text))]) |
---|
227 | ' <"$INPUT_FILE" >"$OUTPUT_FILE" |
---|
228 | # In 1.9, new files are (nonexistent) rather than (working copy). |
---|
229 | sed -i "s/(nonexistent)/(working copy)/" "$OUTPUT_FILE" |
---|
230 | } |
---|
231 | |
---|
232 | function diff_svn_version_filter() { |
---|
233 | if "$SVN_VERSION_IS_19"; then |
---|
234 | sed "s/^#IF SVN1.9 //g; /^#IF SVN1.8 /d" |
---|
235 | else |
---|
236 | sed "s/^#IF SVN1.8 //g; /^#IF SVN1.9 /d" |
---|
237 | fi |
---|
238 | } |
---|
239 | |
---|
240 | function status_sort() { |
---|
241 | local INPUT_FILE=$1 |
---|
242 | local OUTPUT_FILE=$2 |
---|
243 | python -c 'import re, sys |
---|
244 | text = sys.stdin.read() |
---|
245 | status_lines = re.findall("^.{7} [\w./].*$", text, re.M) |
---|
246 | prefix = text[:text.index(status_lines[0])] |
---|
247 | suffix = text[(text.index(status_lines[-1]) + len(status_lines[-1])):] |
---|
248 | status_lines.sort() |
---|
249 | print prefix + "\n".join(status_lines) + suffix.rstrip() |
---|
250 | ' <"$INPUT_FILE" >"$OUTPUT_FILE" |
---|
251 | } |
---|
252 | |
---|
253 | function merge_sort() { |
---|
254 | local INPUT_FILE=$1 |
---|
255 | local OUTPUT_FILE=$2 |
---|
256 | python -c 'import re, sys |
---|
257 | text = sys.stdin.read() |
---|
258 | status_lines = [] |
---|
259 | for line in text.splitlines(): |
---|
260 | if line.startswith("Enter \"y\"") and ": " in line: |
---|
261 | head, tail = line.split(": ", 1) |
---|
262 | if status_lines: |
---|
263 | print "\n".join(sorted(status_lines)) |
---|
264 | status_lines = [] |
---|
265 | print head + ": " |
---|
266 | if tail: |
---|
267 | line = tail |
---|
268 | if re.search("^.{4} [\w./].*$", line): |
---|
269 | status_lines.append(line) |
---|
270 | elif status_lines: |
---|
271 | print "\n".join(sorted(status_lines)) |
---|
272 | print line |
---|
273 | status_lines = [] |
---|
274 | else: |
---|
275 | print line |
---|
276 | if status_lines: |
---|
277 | print "\n".join(sorted(status_lines)) |
---|
278 | ' <"$INPUT_FILE" >"$OUTPUT_FILE" |
---|
279 | } |
---|
280 | |
---|
281 | function check_svn_version() { |
---|
282 | if ! svn --version | head -1 | grep -q "^svn, version 1\.\(8\|9\)"; then |
---|
283 | skip_all "Tests require Subversion 1.8 or 1.9" |
---|
284 | exit 0 |
---|
285 | fi |
---|
286 | } |
---|
287 | |
---|
288 | function fcm_make_build_hello_tests() { |
---|
289 | local TEST_KEY=$1 |
---|
290 | local HELLO_EXT=${2:-} |
---|
291 | shift 2 |
---|
292 | rm -fr \ |
---|
293 | .fcm-make \ |
---|
294 | build \ |
---|
295 | fcm-make-as-parsed.cfg \ |
---|
296 | fcm-make-on-success.cfg \ |
---|
297 | fcm-make.log |
---|
298 | run_pass "$TEST_KEY" fcm make "$@" |
---|
299 | file_test "$TEST_KEY.hello$HELLO_EXT" "$PWD/build/bin/hello$HELLO_EXT" |
---|
300 | "$PWD/build/bin/hello$HELLO_EXT" >"$TEST_KEY.hello$HELLO_EXT.out" |
---|
301 | file_cmp "$TEST_KEY.hello$HELLO_EXT.out" \ |
---|
302 | "$TEST_KEY.hello$HELLO_EXT.out" <<'__OUT__' |
---|
303 | Hello World! |
---|
304 | __OUT__ |
---|
305 | } |
---|
306 | |
---|
307 | FCM_HOME=${FCM_HOME:-$(cd $(dirname $(readlink -f $BASH_SOURCE))/../../.. && pwd)} |
---|
308 | export FCM_HOME |
---|
309 | PATH=$FCM_HOME/bin:$PATH |
---|
310 | |
---|
311 | SVN_VERSION_IS_19=false |
---|
312 | if svn --version 1>/dev/null 2>&1; then |
---|
313 | SVN_VERSION=$(svn --version | \ |
---|
314 | sed -n "s/^svn, version \([0-9.]\+\) .*/\1/p") |
---|
315 | if [[ $SVN_VERSION =~ "1.9." ]]; then |
---|
316 | SVN_VERSION_IS_19=true |
---|
317 | fi |
---|
318 | fi |
---|
319 | |
---|
320 | TEST_KEY_BASE=$(basename $0 .t) |
---|
321 | TEST_SOURCE_DIR=$(cd $(dirname $0) && pwd) |
---|
322 | TEST_DIR=$(mktemp -d) |
---|
323 | export LC_ALL=C |
---|
324 | export LANG=C |
---|
325 | cd $TEST_DIR |
---|
326 | |
---|
327 | set +e |
---|