-
Notifications
You must be signed in to change notification settings - Fork 8
/
plugin_test.go
90 lines (81 loc) · 2.15 KB
/
plugin_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cmd
import (
"bytes"
"testing"
"github.com/krakendio/krakend-cobra/v2/plugin"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)
func Test_pluginFuncErr(t *testing.T) {
var buf bytes.Buffer
cmd := &cobra.Command{}
cmd.SetOutput(&buf)
localDescriber = func() plugin.Descriptor {
return plugin.Descriptor{
Go: goVersion,
Libc: libcVersion,
Deps: map[string]string{
"golang.org/x/mod": "v0.6.0-dev.0.20220419223038-86c51ed26bb4",
"github.com/Azure/azure-sdk-for-go": "v59.3.0+incompatible",
"cloud.google.com/go": "v0.100.2",
},
}
}
defer func() { localDescriber = plugin.Local }()
tests := map[string]struct {
goSum string
expected string
fix bool
err string
}{
"missing": {
goSum: "./testdata/missing-go.sum",
err: "open ./testdata/missing-go.sum: no such file or directory",
},
"matching": {
goSum: "./testdata/match-go.sum",
expected: "No incompatibilities found!\n",
},
"changes": {
goSum: "./testdata/changes-go.sum",
expected: `cloud.google.com/go
have: v0.100.3
want: v0.100.2
github.com/Azure/azure-sdk-for-go
have: v59.3.1+incompatible
want: v59.3.0+incompatible
golang.org/x/mod
have: v0.6.10-dev.0.20220419223038-86c51ed26bb4
want: v0.6.0-dev.0.20220419223038-86c51ed26bb4
`,
err: "3 incompatibilities found",
},
"fix": {
goSum: "./testdata/changes-go.sum",
fix: true,
expected: `go mod edit --replace cloud.google.com/go=cloud.google.com/[email protected]
go mod edit --replace github.com/Azure/azure-sdk-for-go=github.com/Azure/[email protected]+incompatible
go get golang.org/x/[email protected]
`,
err: "3 incompatibilities found",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
buf.Reset()
orig := goSum
goSum = tc.goSum
defer func() { goSum = orig }()
fix := gogetEnabled
gogetEnabled = tc.fix
defer func() { gogetEnabled = fix }()
err := pluginFuncErr(cmd, nil)
if tc.err != "" {
require.EqualError(t, err, tc.err)
} else {
require.NoError(t, err)
}
require.Equal(t, tc.expected, buf.String())
})
}
}