-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
68 lines (65 loc) · 1.51 KB
/
utils_test.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
62
63
64
65
66
67
68
package build_info_utils
import (
"runtime/debug"
"testing"
)
func mockReadBuildInfo(parseBuildInfoError bool) func() (info *debug.BuildInfo, ok bool) {
return func() (*debug.BuildInfo, bool) {
info := &debug.BuildInfo{
Deps: []*debug.Module{
{
Path: "foo",
Version: "v1.2.3",
},
{
Path: "bar",
Version: "v4.5.6",
},
},
}
return info, !parseBuildInfoError
}
}
func TestGetDependencyVersion(t *testing.T) {
tests := []struct {
name string
dependencyPath string
parseBuildInfoError bool
want string
wantErr bool
}{
{
name: "Error parsing build info",
parseBuildInfoError: true,
wantErr: true,
},
{
name: "Dependency not found",
dependencyPath: "test",
wantErr: true,
},
{
name: "Finds and returns version successfully (1)",
dependencyPath: "foo",
want: "v1.2.3",
},
{
name: "Finds and returns version successfully (2)",
dependencyPath: "bar",
want: "v4.5.6",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
readBuildInfoFunc = mockReadBuildInfo(tt.parseBuildInfoError)
got, err := GetDependencyVersion(tt.dependencyPath)
if (err != nil) != tt.wantErr {
t.Errorf("GetDependencyVersion() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GetDependencyVersion() got = %v, want %v", got, tt.want)
}
})
}
}