-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassert.sh
116 lines (102 loc) · 2.44 KB
/
assert.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
#
# asserts.
#
# assert_fails COMMAND -- assert that command fails
# assert_succeeds COMMAND -- assert that command succeeds
# assert_equals VALUE VALUE -- check that values are equal
#
# assert_grep REGEXP FILE -- check that FILE matches REGEXP
# assert_grepv REGEXP FILE -- check that FILE doesn't match REGEXP
#
# assert_exists NAMES... -- check that all NAMES are existing file names
#
bashfoo_require run
source_log_error()
{
SCRIPT_NAME="${BASH_SOURCE[2]}:${BASH_LINENO[1]}" log_error "$@"
}
source_log_info()
{
SCRIPT_NAME="${BASH_SOURCE[2]}:${BASH_LINENO[1]}" log_info "$@"
}
assert_fails()
{
"$@"
local r=$?
if [ "$r" = 0 ] ; then
source_log_error "expected '$@' to fail, but it succeeded (exit_code=$r)"
exit 1
else
source_log_info "success: $@ failed (exit_code=$r) as expected"
fi
}
assert_succeeds()
{
"$@"
local r=$?
if [ "$r" != 0 ] ; then
source_log_error "expected '$@' to succeed, but it failed (exit_code=$r)"
exit 1
else
source_log_info "success: $@ succeeded as expected"
fi
}
assert_variable_present()
{
local val=`eval echo \\${$1}`
if [ -z "$val" ] ; then
source_log_error "variable setting '$1' is missing $2"
exit 1
else
source_log_info "variable $1 value: $val"
fi
}
assert_equals()
{
local expected="$1"
local actual="$2"
if [ "$expected" != "$actual" ] ; then
source_log_error "expected '$expected', but actual value is '$actual'"
exit 1
fi
}
assert_files_equal()
{
local expected="$1"
local actual="$2"
if ! quiet_if_success -q diff -u "$expected" "$actual" ; then
source_log_error "file '$actual'(+) is different from '$expected'(-), diff above"
exit 1
fi
}
show_file()
{
source_log_info "contents of '$1' after last command ($last_command)"
egrep -H "^" $1 >&2
}
assert_grep() {
egrep -q "$@" || {
source_log_error "expected '$1' in '$2' not found"
show_file $2
exit 1
}
}
assert_grepv() {
egrep -qv "$@" || {
source_log_error "not expected '$1' found in $2"
show_file $2
exit 1
}
}
assert_exists() {
local r=0
for f in "$@" ; do
if ! test -f $f ; then
source_log_error "expected file '$f' doesn't exist"
r=1
fi
done
if [ "$r" = 1 ] ; then
exit 1
fi
}