/
common.sh
96 lines (77 loc) · 2.1 KB
/
common.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
#!/bin/bash
#
# Usage:
# ./common.sh <function name>
# TODO: Remove/rename this. The release process might use the release binary
# instead of this dev binary. test/spec.sh already has its own scheme.
# This is analogous to $OSH_OVM in benchmarks/common.sh.
readonly OSH=${OSH:-bin/osh}
# For xargs -P in spec-runner.sh, wild-runner.sh.
readonly JOBS=$(( $(nproc) - 1 ))
readonly R_PATH=~/R # Like PYTHONPATH, but for running R scripts
log() {
echo "$@" 1>&2
}
die() {
log "$@"
exit 1
}
fail() {
echo 'TEST FAILURE ' "$@"
exit 1
}
# NOTE: Could use BASH_SOURCE and so forth for a better error message.
assert() {
test "$@" || die "'$@' failed"
}
run-task-with-status() {
local out_file=$1
shift
# --quiet suppresses a warning message
/usr/bin/env time \
--output $out_file \
--format '%x %e' \
-- "$@" || true # suppress failure
# Hack to get around the fact that --quiet is Debian-specific:
# http://lists.oilshell.org/pipermail/oil-dev-oilshell.org/2017-March/000012.html
#
# Long-term solution: our xargs should have --format.
sed -i '/Command exited with non-zero status/d' $out_file
# TODO: Use rows like this with oil
# '{"status": %x, "wall_secs": %e, "user_secs": %U, "kernel_secs": %S}' \
}
run-task-with-status-test() {
run-task-with-status _tmp/status.txt sh -c 'sleep 0.1; exit 1' || true
cat _tmp/status.txt
test "$(wc -l < _tmp/status.txt)" = '1' || die "Expected only one line"
}
# Each test file should define PASSING
run-all() {
for t in "$@"; do
# fail calls 'exit 1'
$t
echo "OK $t"
done
echo
echo "All $0 tests passed."
}
# A quick and dirty function to show logs
run-other-suite-for-release() {
local suite_name=$1
local func_name=$2
local out=_tmp/other/${suite_name}.txt
mkdir -p $(dirname $out)
echo
echo "*** Running test suite '$suite_name' ***"
echo
if $func_name 2>&1 | tee $out; then
echo
log "Test suite '$suite_name' ran without errors. Wrote $out"
else
echo
die "Test suite '$suite_name' failed (running $func_name)"
fi
}
if test "$(basename $0)" = 'common.sh'; then
"$@"
fi