-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.tests-utils.sh
executable file
·148 lines (113 loc) · 3.6 KB
/
.tests-utils.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
#!/usr/bin/env bash
set -e
USAGE="
Usage $(basename "$0") COMMAND
Avaliable commands:
coverage-report Run unit tests and generate coverage.txt file (useful for codecov)
e2e Run e2e tests
"
RESET_COLOR="\033[0m"
function coverage_report() {
echo "" > coverage.txt
for d in $(go list ./... | grep -v vendor); do
go test -race -coverprofile=profile.out -covermode=atomic $d
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
done
}
function echo_green(){
echo -e "\033[32m$1${RESET_COLOR}"
}
function echo_red() {
echo -e "\033[1;31m$1${RESET_COLOR}"
}
function echo_yellow() {
echo -e "\033[1;33m$1${RESET_COLOR}"
}
function check_result() {
local got="$1"
local expected="$2"
if [ "$got" != "$expected" ]; then
echo_red "expected $expected, got $got"
exit 1
fi
}
function _e2e_single_file() {
printf "Testing with a single file... "
local cmd="./sif Cgo _tests/golang.txt"
check_result "$($cmd | wc -l | tr -d '[:space:]')" "3"
check_result "$($cmd | head -n 1)" "_tests/golang.txt"
check_result "$($cmd | tail -n 1)" "12: Cgo is not Go."
echo_green OK
}
function _e2e_dir(){
printf "Testing with a dir... "
local cmd="./sif better _tests"
check_result "$($cmd | grep -v '[0-9]' | xargs)" "_tests/golang.txt _tests/python.txt"
check_result "$($cmd | wc -l | tr -d '[:space:]')" "13"
check_result "$($cmd | tail -n 1)" "18: Although never is often better than *right* now."
echo_green "OK"
}
function _e2e_build() {
printf "build app... "
make build
echo_green "OK"
}
function _e2e_without_target() {
printf "Testing without a target... "
local cmd="./sif Hello"
check_result "$($cmd | wc -l | tr -d '[:space:]')" "5"
check_result "$($cmd | grep -v '[0-9]' | xargs)" "$(basename $0) _tests/subdir/hello.go"
echo_green "OK"
}
function _e2e_with_two_targets(){
printf "Testing with two targets... "
local cmd="./sif better _tests/python.txt _tests/golang.txt"
check_result "$($cmd | grep -v '[0-9]' | xargs)" "_tests/python.txt _tests/golang.txt"
check_result "$($cmd | wc -l | tr -d '[:space:]')" "13"
check_result "$($cmd | tail -n 1)" "14: Clear is better than clever."
echo_green "OK"
}
function _e2e_case_insensitive_flag() {
printf "Testing with -i flag (case insensitive)... "
local cmd="./sif -i cgo _tests/golang.txt"
check_result "$($cmd | wc -l | tr -d '[:space:]')" "3"
check_result "$($cmd | head -n 1)" "_tests/golang.txt"
check_result "$($cmd | tail -n 1)" "12: Cgo is not Go."
echo_green "OK"
}
function _e2e_only_filenames_flag() {
printf "Testing with -l flag (print only file names)... "
local cmd="./sif -l Cgo"
check_result "$($cmd | wc -l | tr -d '[:space:]')" "2"
check_result "$($cmd | head -n 1)" "$(basename $0)"
check_result "$($cmd | tail -n 1)" "_tests/golang.txt"
echo_green "OK"
}
function _e2e_invalid_pattern() {
printf "Testing with invalid pattern (don't panic)..."
local cmd="./sif \["
check_result "$($cmd | grep 'Invalid pattern' | wc -l | tr -d '[:space:]')" "1"
echo_green "OK"
}
function e2e() {
echo_yellow "START E2E TESTS"
_e2e_build
_e2e_without_target
_e2e_single_file
_e2e_dir
_e2e_with_two_targets
_e2e_invalid_pattern
echo_yellow " - FLAGS TESTS"
_e2e_case_insensitive_flag
_e2e_only_filenames_flag
rm sif
echo_yellow "FINISH"
}
case "$1" in
"coverage-report") coverage_report ;;
"e2e") e2e ;;
*) echo "$USAGE" ;;
esac