forked from gkampitakis/coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoverage.go
61 lines (52 loc) · 1.11 KB
/
coverage.go
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
package coverage
import (
"fmt"
"io"
"os"
"runtime"
"strings"
"testing"
)
type testingM interface {
Run() int
}
var (
// used for overriding during unit tests
coverModeFunc = testing.CoverMode
coverageFunc = testing.Coverage
writer = io.Writer(os.Stdout)
exitFunc = os.Exit
)
func Run(t testingM, c float64, callbacks ...func()) {
var code int
defer func() {
exitFunc(code)
}()
code = t.Run()
if coverModeFunc() == "" {
fmt.Fprintf(
writer,
"\nFAIL coverage is not enabled. You can enable by using `-cover`\n\n",
)
code = 1
} else {
coverage := coverageFunc()
if coverage*100 < c {
code = 1
fmt.Fprintf(writer, "\nFAIL Coverage threshold not met %.1f >= %.1f for %s\n\n", c, coverage*100, packageName())
}
}
for _, callback := range callbacks {
callback()
}
}
func packageName() string {
pc, _, _, _ := runtime.Caller(2)
funcName := runtime.FuncForPC(pc).Name()
lastSlash := strings.LastIndexByte(funcName, '/')
if lastSlash < 0 {
lastSlash = 0
}
lastDot := strings.LastIndexByte(funcName[lastSlash:], '.') + lastSlash
return funcName[:lastDot]
}