Skip to content

Commit

Permalink
Merge pull request #2 from sg0hsmt/increase-test-coverage
Browse files Browse the repository at this point in the history
Increase test coverage
  • Loading branch information
sg0hsmt committed Mar 8, 2020
2 parents dd9e6d7 + 38e0253 commit 60f9761
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
14 changes: 10 additions & 4 deletions discover.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package goversion

import (
"fmt"
"errors"
"os/exec"
"regexp"
"strconv"
Expand All @@ -10,17 +10,23 @@ import (
)

// ErrDevelopVersion develop version is not supported.
var ErrDevelopVersion = fmt.Errorf("develop version is not supported")
var ErrDevelopVersion = errors.New("develop version is not supported")

// ErrVersionSyntax version syntax parse failed.
var ErrVersionSyntax = fmt.Errorf("version syntax parse failed")
var ErrVersionSyntax = errors.New("version syntax parse failed")

// nolint: gochecknoglobals
var mu sync.Mutex

// nolint: gochecknoglobals
var cache *Version

// execCmd returns go command execute results.
// nolint: gochecknoglobals
var execCmd = func() ([]byte, error) {
return exec.Command("go", "version").Output()
}

// Discover returns version instance from go command execute result.
// Execution results are cached and reused.
func Discover() (*Version, error) {
Expand All @@ -41,7 +47,7 @@ func Discover() (*Version, error) {

// discover returns version instance from go command execute result.
func discover() (*Version, error) {
out, err := exec.Command("go", "version").Output()
out, err := execCmd()
if err != nil {
return nil, err
}
Expand Down
18 changes: 18 additions & 0 deletions discover_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goversion_test

import (
"errors"
"go/build"
"reflect"
"runtime"
Expand Down Expand Up @@ -31,6 +32,13 @@ func TestDiscover(t *testing.T) {
first = res
})

var testErr = errors.New("test for execution failure")

// replace internal function
goversion.SetExecCmd(func() ([]byte, error) {
return nil, testErr // always error
})

t.Run("second call (get from cache)", func(t *testing.T) {
res, err := goversion.Discover()
if err != nil {
Expand All @@ -41,4 +49,14 @@ func TestDiscover(t *testing.T) {
t.Errorf("second is not equal first, first %#v, second %#v", first, res)
}
})

// clear internal cache
goversion.ResetCache()

t.Run("third call (from go command)", func(t *testing.T) {
_, err := goversion.Discover()
if err != testErr {
t.Errorf("unmatch error, want %v, got %v", testErr, err)
}
})
}
13 changes: 13 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package goversion

// ResetCache clear internal cache for testing.
func ResetCache() {
mu.Lock()
cache = nil
mu.Unlock()
}

// SetExecCmd replace internal functions for testing.
func SetExecCmd(f func() ([]byte, error)) {
execCmd = f
}

0 comments on commit 60f9761

Please sign in to comment.