This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 605
Expand file tree
/
Copy pathgotests.sh
More file actions
executable file
·116 lines (95 loc) · 3.25 KB
/
Copy pathgotests.sh
File metadata and controls
executable file
·116 lines (95 loc) · 3.25 KB
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
#!/bin/bash
set -o errexit
# make relative paths work.
cd $(dirname $0)/..
COVERAGE_FOLDER="./go/src"
COVMERGE="./go/bin/gocovmerge"
KD=$(pwd)
action="sh -c"
# action="echo"
# compile uses -coverpkg flag to test with coverage
# this satisfies to cover the subpackages of tests
function compile() {
# list the packages
export PKGS=$(go list $1 | grep -v /vendor/)
# make comma-separated
export PKGS_DELIM=$(echo "$PKGS" | paste -sd "," -)
go list -f '{{if len .TestGoFiles}}"go test -race -v -cover -c {{.ImportPath}} -o={{.Dir}}/{{.Name}}.test -coverpkg='$PKGS_DELIM' "{{end}}' $PKGS | grep -v vendor | xargs -L 1 -I{} $action "{}$COMPILE_FLAGS"
}
# run runs the binary file that created before (with compile function)
# this function runs the binary with given RUN_FLAGS
# RUN_FLAGS values might be config file or any value if required like
# ...('-kite-init' required for collaboration tests)
function run() {
go list -f '{{if len .TestGoFiles}}"{{.Dir}}/{{.Name}}.test -test.coverprofile={{.Dir}}/coverage.txt "{{end}}' $1 | grep -v vendor | xargs -L 1 -I{} $action "{}$RUN_FLAGS"
}
function merge() {
local folder=$(createFolder $1)
go list -f '{{if len .TestGoFiles}}"{{.Dir}}/coverage.txt"{{end}}' $1 | grep -v vendor | xargs $COVMERGE >"$folder"
}
# runWithCD runs like run function.
# But this function changes the directory while running.
function runWithCD() {
go list -f '{{if len .TestGoFiles}}"cd {{.Dir}} && ./{{.Name}}.test -test.coverprofile={{.Dir}}/coverage.txt "{{end}}' $1 | grep -v vendor | xargs -L 1 -I{} $action "{}$RUN_FLAGS"
}
# clean removes the .tests files after creation
function clean() {
go list -f '{{if len .TestGoFiles}}"rm {{.Dir}}/{{.Name}}.test "{{end}}' $1 | xargs -L 1 $action
}
function runAll() {
compile $@
run $@
merge $@
removeCoverProfiles $@
clean $@
}
# removeCoverProfiles deletes the coverage.txt files after merging all
# coverages into 1 coverage.txt(merged coverage file won't be deleted)
function removeCoverProfiles() {
go list -f '{{if len .TestGoFiles}}"{{.Dir}}/coverage.txt"{{end}}' $1 | grep -v vendor | xargs rm
}
# commandExists checks if the given parameter command is exits or not
function commandExists() {
command -v $1 >/dev/null 2>&1
}
# generateFolderName generates a random string with given parameter
# according to given parameter & command checking
function generateFolderName() {
if commandExists md5; then
echo $1 | md5
elif commandExists md5sum; then
echo -n $1 | md5sum | awk '{print $1}'
else
date +%s%N
fi
}
# createFolder creates a '/coverage' folder if doesn't exists
# then appends the generated random string as folder name
# e.g:
# assume random string : $generatedName
# path will be -> koding/go/src/coverage/$generatedName
function createFolder() {
name=$(generateFolderName $1)
mkdir -p "$KD/go/src/coverages/$name"
touch "$KD/go/src/coverages/$name/coverage.txt"
echo "$KD/go/src/coverages/$name/coverage.txt"
}
if [ "$1" == "kites" ]; then
shift
compile $@
runWithCD $@
merge $@
removeCoverProfiles $@
clean $@
elif [ "$1" == "socialapi" ]; then
shift
export RUN_FLAGS=${RUN_FLAGS:-"-c=$KONFIG_SOCIALAPI_CONFIGFILEPATH"}
runAll $@
# useful after a refactoring, just to see if there is any error
elif [ "$1" == "compile" ]; then
shift
compile $@
clean $@
else
runAll $@
fi