-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-lib.sh
executable file
·65 lines (55 loc) · 1.31 KB
/
test-lib.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
#!/bin/bash
root="$(git rev-parse --show-toplevel)"
function yellow() {
echo "$(tput setaf 3)$*$(tput sgr0)"
}
# kills child processes
function cleanTrap() {
rc=$?
set +x
trap - EXIT SIGINT SIGTERM
pkill -P $(pgrep -P $$ -d, making)
rm -rf "$workdir" 2>/dev/null || yellow "$workdir not deleted (too fast?)"
return $rc
}
function fail() {
echo "TEST FAILED: $*" >&2
exit 1
}
function polling() {
max=$(($1 * 1000000000))
now=$(date +%s%N)
if [ -z "$polling_start" ]; then
polling_start="$now"
fi
if (( now - polling_start > max )); then
unset polling_start
return 1
fi
sleep .01
}
# checks that the file $1 is created
function test_created() {
while polling 1; do
if test -f "$1"; then
return
fi
done
fail "$1 was NOT created"
}
# checks that the file $1 contains the string $2
function test_content() {
while polling 1; do
if grep -F "$2" "$1" >/dev/null; then
return
fi
done
fail "$1 doesn't contain $2"
}
# creates a directory where I can test making
making="$(git rev-parse --show-toplevel)/making"
trap "cleanTrap" EXIT SIGINT SIGTERM
workdir="$(mktemp -d --suffix -making-test-bed)"
cd "$workdir"
cp $root/test-project/* "$workdir"
git init >/dev/null 2>&1