Skip to content

Commit 84a0627

Browse files
authored
Merge pull request #49 from KEINOS/cover-up-tests
Add test for gotuil.GetPackageVersion() function
2 parents e5b286d + df4536e commit 84a0627

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

internal/goutil/examples_test.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,29 @@ func ExampleGetLatestVer() {
6565
}
6666

6767
func ExampleGetPackageInformation() {
68-
pkgInfo := goutil.GetPackageInformation([]string{"../../cmd/testdata/check_success/gal"})
68+
// Prepare the path of go binary module for the example
69+
nameDirCheckSuccess := "check_success"
70+
nameFileBin := "gal"
71+
72+
if runtime.GOOS == "windows" {
73+
nameDirCheckSuccess = "check_success_for_windows"
74+
nameFileBin = "gal.exe" // remember the extension
75+
}
76+
77+
pathFileBin := filepath.Join("..", "..", "cmd", "testdata", nameDirCheckSuccess, nameFileBin)
78+
79+
pkgInfo := goutil.GetPackageInformation([]string{pathFileBin})
6980
if pkgInfo == nil {
7081
log.Fatal("example GetPackageInformation failed. The returned package information is nil")
7182
}
7283

7384
// Expected package information on Linux and macOS
7485
want := []string{
75-
"gal",
86+
nameFileBin,
7687
"github.com/nao1215/gal/cmd/gal",
7788
"github.com/nao1215/gal",
7889
}
7990

80-
// On Windows, paths are missing
81-
if runtime.GOOS == "windows" {
82-
want = []string{
83-
"gal", "", "",
84-
}
85-
}
86-
8791
// Actual package information
8892
got := []string{
8993
pkgInfo[0].Name,
@@ -99,8 +103,9 @@ func ExampleGetPackageInformation() {
99103
// Output: Example GetPackageInformation: OK
100104
}
101105

102-
func ExampleGetPackageVersion() {
106+
func ExampleGetPackageVersion_unknown() {
103107
// GetPackageVersion returns the version of the package installed via `go install`.
108+
// In this example, we specify a package that is not installed.
104109
got := goutil.GetPackageVersion("gup_dummy")
105110

106111
// Non existing binary returns "unknown"

internal/goutil/goutil_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"go/build"
77
"os"
88
"path/filepath"
9+
"runtime"
910
"strings"
1011
"testing"
1112

@@ -97,6 +98,39 @@ func TestGetPackageInformation_unknown_module(t *testing.T) {
9798
}
9899
}
99100

101+
func TestGetPackageVersion_golden(t *testing.T) {
102+
// Backaup and defer restore
103+
oldKeyGoBin := keyGoBin
104+
defer func() {
105+
keyGoBin = oldKeyGoBin
106+
}()
107+
108+
// Prepare the path of go binary module for testing
109+
nameDirCheckSuccess := "check_success"
110+
nameFileBin := "gal"
111+
want := "v1.1.1"
112+
113+
if runtime.GOOS == "windows" {
114+
nameDirCheckSuccess = "check_success_for_windows"
115+
nameFileBin = "gal.exe"
116+
want = "(devel)"
117+
}
118+
119+
pathDirBin := filepath.Join("..", "..", "cmd", "testdata", nameDirCheckSuccess)
120+
121+
// Mock the search directory path of go module binaries
122+
t.Setenv("GOBINTMP", pathDirBin)
123+
keyGoBin = "GOBINTMP"
124+
125+
// Get the package version of specified module
126+
got := GetPackageVersion(nameFileBin)
127+
128+
// Require to get the expected version of go module binary
129+
if want != got {
130+
t.Fatalf("GetPackageVersion() should return %v. got: %v", want, got)
131+
}
132+
}
133+
100134
func TestGetPackageVersion_getting_error_from_gobin(t *testing.T) {
101135
// Set env variable to temporary value and defer restore on Cleanup
102136
t.Setenv("GOBIN", "")

0 commit comments

Comments
 (0)