-
Notifications
You must be signed in to change notification settings - Fork 27
/
data_test.sh
executable file
·121 lines (100 loc) · 3.39 KB
/
data_test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash -u
#
# Copyright 2014 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
#
# Runs all tests defined in the "testdata" dir.
#
################################################################################
source "$(dirname $0)/test_util.sh"
readonly AUTOGEN="${1:-autogen}"
# If we're runnig via Bazel, find the source files via $TEST_SRCDIR;
# otherwise, default to dir of current file and search relative to that.
if [ -n "${TEST_SRCDIR:-}" ]; then
declare -r SRCDIR="${TEST_SRCDIR}/${TEST_WORKSPACE}"
else
declare -r SRCDIR="$(dirname $0)/.."
fi
declare -r TESTDATA="${SRCDIR}/tests/testdata"
# Set temp directory from environment, if running under Bazel.
declare -r TMPDIR="${TEST_TMPDIR:-${TEMPDIR:-${TMPDIR:-/tmp}}}"
# Set verbosity level; 0=mostly silent, higher numbers for more output.
declare -r VERBOSE="${VERBOSE:-0}"
# Args:
# $1: input file
# $2: expected stdout
# $3: expected stderr
function run_one_test() {
local input="$1"
local expected_out="$2"
local expected_err="$3"
local filebase="$(basename "${input%.in}")"
local actual_out="$(mktemp "${TMPDIR}/${filebase}.out.XXXXXX")"
local actual_err="$(mktemp "${TMPDIR}/${filebase}.err.XXXXXX")"
# Run tests in a hermetic environment such that they don't break every year.
bash -c "${SRCDIR}/${AUTOGEN} -y 2014 $(cat "${input}")" > "${actual_out}" 2> "${actual_err}"
local -i stdout=1
local -i stderr=1
if ! `diff "${expected_out}" "${actual_out}" > /dev/null 2>&1` ; then
stdout=0
fi
if ! `diff "${expected_err}" "${actual_err}" > /dev/null 2>&1` ; then
stderr=0
fi
local status_code=0
local status="${PASSED}"
if [ ${stdout} -eq 0 ] || [ ${stderr} -eq 0 ]; then
status_code=1
status="${FAILED}"
fi
if [ "${VERBOSE}" -ge 1 ] || [ ${status_code} -ne 0 ]; then
echo -e "${status} ${filebase}"
fi
if [ ${stdout} -eq 0 ]; then
echo " Differences in stdout; compare via: diff -u ${expected_out} ${actual_out}"
if [ "${VERBOSE}" -ge 2 ]; then
diff -u "${expected_out}" "${actual_out}"
fi
else
rm -f "${actual_out}"
fi
if [ ${stderr} -eq 0 ]; then
echo " Differences in stderr; compare via: diff -u ${expected_err} ${actual_err}"
if [ "${VERBOSE}" -ge 2 ]; then
diff -u "${expected_err}" "${actual_err}"
fi
else
rm -f "${actual_err}"
fi
return ${status_code}
}
function run_all_tests() {
local -r num_files="$(ls -1 ${TESTDATA}/*.in | wc -l)"
if [[ ${num_files} -eq 0 ]]; then
echo "No files found in ${TESTDATA}; exiting." >&2
exit 1
fi
for input in ${TESTDATA}/*.in; do
local expected_out="${input%.in}.out"
local expected_err="${input%.in}.err"
run_one_test "${input}" "${expected_out}" "${expected_err}" \
&& test_record_passed \
|| test_record_failed
done
test_print_status
test_return
}
run_all_tests