-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
common.sh
138 lines (108 loc) · 3.07 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
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
# Usage:
# source test/common.sh
# Include guard.
test -n "${__TEST_COMMON_SH:-}" && return
readonly __TEST_COMMON_SH=1
# 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.
MAX_PROCS=${MAX_PROCS:-$(( $(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() {
### Run a process and write a file with status and time
local out_file=$1
shift
# spec/wild tests only need two digits of precision
benchmarks/time_.py \
--tsv \
--time-fmt '%.2f' \
--output $out_file \
-- "$@" || 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"
}
test-func-manifest() {
### Shell funcs that start with 'test-' are cases that will pass or fail
compgen -A function | egrep '^test-'
}
run-test-funcs() {
test-func-manifest | while read t; do
$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=${3:-_tmp/other/${suite_name}.txt}
mkdir -p $(dirname $out)
echo
echo "*** Running test suite '$suite_name' ***"
echo
# I want to handle errors in $func_name while NOT changing its semantics.
# This requires a separate shell interpreter starts with $0, not just a
# separate process. I came up with this fix in gold/errexit-confusion.sh.
local status=0
set +o errexit
$0 $func_name >$out 2>&1
status=$? # pipefail makes this work.
set -o errexit
if test $status -eq 0; then
echo
log "Test suite '$suite_name' ran without errors. Wrote '$out'"
else
echo
die "Test suite '$suite_name' failed (running $func_name, wrote '$out')"
fi
}
date-and-git-info() {
date
echo
if test -d .git; then
local branch
branch=$(git rev-parse --abbrev-ref HEAD)
local hash
hash=$(git rev-parse $branch)
echo "oil repo: $hash on branch $branch"
else
echo "(not running from git repository)"
fi
echo
}
html-head() {
PYTHONPATH=. doctools/html_head.py "$@"
}
filename=$(basename $0)
if test "$filename" = 'common.sh'; then
"$@"
fi