Skip to content

Commit ab96890

Browse files
committed
fix: add unit tests
1 parent 368ab0d commit ab96890

File tree

3 files changed

+104
-9
lines changed

3 files changed

+104
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ func TestMain(m *testing.M) {
1919

2020
What `coverage.Run` does, it runes the tests with `t.Run()` and then depending on the coverage fails the tests and calls `os.Exit` with correct exit code.
2121

22-
In case you have "clean" up code after running your tests `coverage.Run` support passing a callback function `func(t *testing.M)` for running your code.
22+
In case you have "clean" up code after running your tests `coverage.Run` support passing a callback function `func()` for running your code.
2323

2424
e.g.
2525

2626
```go
2727
func TestMain(m *testing.M) {
28-
coverage.Run(m, 91, func(t *testing.M) {
28+
coverage.Run(m, 91, func() {
2929
fmt.Println("tests are done")
3030
})
3131
}

coverage.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,49 @@ package coverage
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"runtime"
78
"strings"
89
"testing"
910
)
1011

11-
func Run(t *testing.M, c float64, callbacks ...func(t *testing.M)) {
12+
type testingM interface {
13+
Run() int
14+
}
15+
16+
var (
17+
// used for overriding during unit tests
18+
coverModeFunc = testing.CoverMode
19+
coverageFunc = testing.Coverage
20+
writer = io.Writer(os.Stdout)
21+
exitFunc = os.Exit
22+
)
23+
24+
func Run(t testingM, c float64, callbacks ...func()) {
1225
var code int
1326
defer func() {
14-
os.Exit(code)
27+
exitFunc(code)
1528
}()
1629

1730
code = t.Run()
1831

19-
if testing.CoverMode() == "" {
20-
fmt.Printf("\nFAIL coverage is not enabled. You can enable by using `-cover`\n\n")
32+
if coverModeFunc() == "" {
33+
fmt.Fprintf(
34+
writer,
35+
"\nFAIL coverage is not enabled. You can enable by using `-cover`\n\n",
36+
)
2137
code = 1
2238
} else {
23-
coverage := testing.Coverage()
39+
coverage := coverageFunc()
2440
if coverage*100 < c {
2541
code = 1
26-
fmt.Printf("\nFAIL Coverage threshold not met %.1f >= %.1f for %s\n\n", c, coverage*100, packageName())
42+
fmt.Fprintf(writer, "\nFAIL Coverage threshold not met %.1f >= %.1f for %s\n\n", c, coverage*100, packageName())
2743
}
2844
}
2945

3046
for _, callback := range callbacks {
31-
callback(t)
47+
callback()
3248
}
3349
}
3450

coverage_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package coverage
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func mockExit(t *testing.T, code int) func(int) {
9+
t.Helper()
10+
11+
return func(c int) {
12+
if code != c {
13+
t.Errorf("[expected]: %d \n[received]:%d\n", code, c)
14+
}
15+
}
16+
}
17+
18+
type mockTestingM struct {
19+
code int
20+
}
21+
22+
func (m mockTestingM) Run() int {
23+
return m.code
24+
}
25+
26+
func TestCoverage(t *testing.T) {
27+
t.Run("should run tests successfully", func(t *testing.T) {
28+
exitFunc = mockExit(t, 0)
29+
coverModeFunc = func() string { return "mock-mode" }
30+
coverageFunc = func() float64 { return 1 }
31+
m := mockTestingM{code: 0}
32+
33+
Run(m, 100)
34+
})
35+
36+
t.Run("should fail if no cover enabled", func(t *testing.T) {
37+
exitFunc = mockExit(t, 1)
38+
coverModeFunc = func() string { return "" }
39+
coverageFunc = func() float64 { return 1 }
40+
m := mockTestingM{code: 0}
41+
s := strings.Builder{}
42+
writer = &s
43+
44+
Run(m, 100)
45+
if s.String() != "\nFAIL coverage is not enabled. You can enable by using `-cover`\n\n" {
46+
t.Errorf("expected coverage not enabled msg but got: %s\f", s.String())
47+
}
48+
})
49+
50+
t.Run("should fail if coverage is not met", func(t *testing.T) {
51+
exitFunc = mockExit(t, 1)
52+
coverModeFunc = func() string { return "mock-mode" }
53+
coverageFunc = func() float64 { return 0.5 }
54+
m := mockTestingM{code: 0}
55+
s := strings.Builder{}
56+
writer = &s
57+
58+
Run(m, 51)
59+
if s.String() != "\nFAIL Coverage threshold not met 51.0 >= 50.0 for coverage.TestCoverage\n\n" {
60+
t.Errorf("expected coverage threshold error but got: %s\n", s.String())
61+
}
62+
})
63+
64+
t.Run("should call callback funcs", func(t *testing.T) {
65+
exitFunc = mockExit(t, 0)
66+
coverModeFunc = func() string { return "mock-mode" }
67+
coverageFunc = func() float64 { return 1 }
68+
m := mockTestingM{code: 0}
69+
called := 0
70+
71+
Run(m, 100, func() {
72+
called++
73+
})
74+
75+
if called != 1 {
76+
t.Errorf("callback expected to be called once but called: %d\n", called)
77+
}
78+
})
79+
}

0 commit comments

Comments
 (0)