-
-
Notifications
You must be signed in to change notification settings - Fork 145
/
unit.sh
executable file
·244 lines (190 loc) · 4.9 KB
/
unit.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env bash
#
# Run unit tests. Sets PYTHONPATH.
#
# Usage:
# test/unit.sh <function name>
#
# Examples:
#
# test/unit.sh unit frontend/lexer_test.py
# test/unit.sh all
# test/unit.sh minimal
set -o nounset
set -o pipefail
set -o errexit
shopt -s strict:all 2>/dev/null || true # dogfood for OSH
source test/common.sh
source test/tsv-lib.sh
export PYTHONPATH='.:vendor' # repo root and vendor subdir
# For auto-complete
unit() {
#$py "$@"
"$@"
}
delete-pyc() {
find . -name '*.pyc' | xargs --no-run-if-empty -- rm || true
}
readonly PY_273=~/src/languages/Python-2.7.3/python
readonly PY_272=~/src/languages/Python-2.7.2/python
readonly PY_27=~/src/languages/Python-2.7/python
# WTF, fixes native_test issue
#export PYTHONDONTWRITEBYTECODE=1
banner() {
echo -----
echo "$@"
echo -----
}
readonly -a PY2_UNIT_TESTS=( {asdl,build,core,doctools,frontend,lazylex,native,oil_lang,osh,pylib,qsn_,test,tools}/*_test.py )
readonly -a PY3_UNIT_TESTS=( mycpp/*_test.py spec/stateful/*_test.py )
py2-tests() {
local minimal=${1:-}
for t in "${PY2_UNIT_TESTS[@]}"; do
# For Travis after build/dev.sh minimal: if we didn't build fastlex.so,
# then skip a unit test that will fail.
if test -n "$minimal"; then
if test $t = 'native/fastlex_test.py' && ! test -e 'fastlex.so'; then
continue
fi
# doctools/cmark.sh makes that shared library
if test $t = 'doctools/cmark_test.py' && ! test -e '_deps/libcmark.so'; then
continue
fi
fi
echo $t
done
}
py3-tests() {
for t in "${PY3_UNIT_TESTS[@]}"; do
echo $t
done
}
all-tests() {
py2-tests "$@"
# TODO: This only PRINTS the tests. It doesn't actually run them, but we
# need a different PYTHONPATH here.
py3-tests
}
# Exits 255 if a test fails.
run-test-and-maybe-abort() {
local t=$1
echo
echo "[$t]"
if ! $t >/dev/null; then
echo
echo "*** $t FAILED ***"
echo
return 255 # xargs aborts
fi
#echo "OK $t"
}
all() {
### Run unit tests after build/dev.sh all
time all-tests "$@" | xargs -n 1 -- $0 run-test-and-maybe-abort
echo
echo "All unit tests passed."
}
minimal() {
### Run unit tests after build/dev.sh minimal
time py2-tests T | xargs -n 1 -- $0 run-test-and-maybe-abort
echo
echo "Minimal unit tests passed."
}
soil-run() {
if test -n "${TRAVIS_SKIP:-}"; then
echo "TRAVIS_SKIP: Skipping $0"
return
fi
minimal
}
# Run all unit tests in one process.
all-in-one() {
# -s: start dir -t: top level are defaults
# NOTE: without the pattern, it finds byterun unit tests called 'test*'.
_OVM_RESOURCE_ROOT=$PWD python -m unittest discover \
--failfast --verbose --pattern '*_test.py'
# This style fails due to the arguments being filenames and not Python module
# names.
#tests-to-run | xargs python -m unittest
}
# NOTE: Show options like this:
# python -m unittest discover -h
#
# For _release/VERSION
#
run-test-and-log() {
local tasks_tsv=$1
local rel_path=$2
local log=_tmp/unit/$rel_path.txt
mkdir -p "$(dirname $log)"
time-tsv --append --out $tasks_tsv \
--field $rel_path --field "$rel_path.txt" -- \
$rel_path >$log 2>&1
}
run-all-and-log() {
local out_dir=_tmp/unit
mkdir -p $out_dir
rm -r -f $out_dir/*
local tasks_tsv=$out_dir/tasks.tsv
local status=0
# TODO: I need to write a schema too? Or change csv2html.py to support HREF
# in NullSchema.
tsv-row 'status' 'elapsed_secs' 'test' 'test_HREF' > $tasks_tsv
# There are no functions here, so disabline errexit is safe.
# Note: In Oil, this could use shopt { }.
set +o errexit
time all-tests | xargs -n 1 -- $0 run-test-and-log $tasks_tsv
status=$?
set -o errexit
if test $status -ne 0; then
cat $tasks_tsv
echo
echo "*** Some tests failed. See $tasks_tsv ***"
echo
return $status
fi
#tree _tmp/unit
echo
echo "All unit tests passed."
}
# TODO: It would be nice to have timestamps of the underlying CSV files and
# timestamp of running the report. This is useful for benchmarks too.
print-report() {
local in_dir=${1:-_tmp/unit}
local base_url='../../web'
html-head --title 'Oil Unit Test Results' \
"$base_url/table/table-sort.js" \
"$base_url/table/table-sort.css" \
"$base_url/base.css" \
"$base_url/benchmarks.css"
# NOTE: Using benchmarks for now.
cat <<EOF
<body class="width40">
<p id="home-link">
<a href="/">oilshell.org</a>
</p>
<h2>Unit Test Results</h2>
EOF
tsv2html $in_dir/report.tsv
cat <<EOF
</body>
</html>
EOF
}
# Presentation changes:
#
# - elapsed seconds -> milliseconds
# - Need a link to the log for the test name (done, but no schema)
# - schema for right-justifying numbers
write-report() {
local out=_tmp/unit/index.html
R_LIBS_USER=$R_PATH test/report.R unit _tmp/unit _tmp/unit
print-report > $out
echo "Wrote $out"
}
# Called by scripts/release.sh.
run-for-release() {
run-all-and-log
write-report
}
"$@"