Skip to content

Commit

Permalink
fix: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gkampitakis committed Jan 15, 2023
1 parent 368ab0d commit ab96890
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func TestMain(m *testing.M) {

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.

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.
In case you have "clean" up code after running your tests `coverage.Run` support passing a callback function `func()` for running your code.

e.g.

```go
func TestMain(m *testing.M) {
coverage.Run(m, 91, func(t *testing.M) {
coverage.Run(m, 91, func() {
fmt.Println("tests are done")
})
}
Expand Down
30 changes: 23 additions & 7 deletions coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,49 @@ package coverage

import (
"fmt"
"io"
"os"
"runtime"
"strings"
"testing"
)

func Run(t *testing.M, c float64, callbacks ...func(t *testing.M)) {
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() {
os.Exit(code)
exitFunc(code)
}()

code = t.Run()

if testing.CoverMode() == "" {
fmt.Printf("\nFAIL coverage is not enabled. You can enable by using `-cover`\n\n")
if coverModeFunc() == "" {
fmt.Fprintf(
writer,
"\nFAIL coverage is not enabled. You can enable by using `-cover`\n\n",
)
code = 1
} else {
coverage := testing.Coverage()
coverage := coverageFunc()
if coverage*100 < c {
code = 1
fmt.Printf("\nFAIL Coverage threshold not met %.1f >= %.1f for %s\n\n", c, coverage*100, packageName())
fmt.Fprintf(writer, "\nFAIL Coverage threshold not met %.1f >= %.1f for %s\n\n", c, coverage*100, packageName())
}
}

for _, callback := range callbacks {
callback(t)
callback()
}
}

Expand Down
79 changes: 79 additions & 0 deletions coverage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package coverage

import (
"strings"
"testing"
)

func mockExit(t *testing.T, code int) func(int) {
t.Helper()

return func(c int) {
if code != c {
t.Errorf("[expected]: %d \n[received]:%d\n", code, c)
}
}
}

type mockTestingM struct {
code int
}

func (m mockTestingM) Run() int {
return m.code
}

func TestCoverage(t *testing.T) {
t.Run("should run tests successfully", func(t *testing.T) {
exitFunc = mockExit(t, 0)
coverModeFunc = func() string { return "mock-mode" }
coverageFunc = func() float64 { return 1 }
m := mockTestingM{code: 0}

Run(m, 100)
})

t.Run("should fail if no cover enabled", func(t *testing.T) {
exitFunc = mockExit(t, 1)
coverModeFunc = func() string { return "" }
coverageFunc = func() float64 { return 1 }
m := mockTestingM{code: 0}
s := strings.Builder{}
writer = &s

Run(m, 100)
if s.String() != "\nFAIL coverage is not enabled. You can enable by using `-cover`\n\n" {
t.Errorf("expected coverage not enabled msg but got: %s\f", s.String())
}
})

t.Run("should fail if coverage is not met", func(t *testing.T) {
exitFunc = mockExit(t, 1)
coverModeFunc = func() string { return "mock-mode" }
coverageFunc = func() float64 { return 0.5 }
m := mockTestingM{code: 0}
s := strings.Builder{}
writer = &s

Run(m, 51)
if s.String() != "\nFAIL Coverage threshold not met 51.0 >= 50.0 for coverage.TestCoverage\n\n" {
t.Errorf("expected coverage threshold error but got: %s\n", s.String())
}
})

t.Run("should call callback funcs", func(t *testing.T) {
exitFunc = mockExit(t, 0)
coverModeFunc = func() string { return "mock-mode" }
coverageFunc = func() float64 { return 1 }
m := mockTestingM{code: 0}
called := 0

Run(m, 100, func() {
called++
})

if called != 1 {
t.Errorf("callback expected to be called once but called: %d\n", called)
}
})
}

0 comments on commit ab96890

Please sign in to comment.