-
Notifications
You must be signed in to change notification settings - Fork 0
/
modfile_test.go
149 lines (133 loc) · 3.58 KB
/
modfile_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"bytes"
"testing"
qt "github.com/frankban/quicktest"
"golang.org/x/mod/modfile"
)
// The tests in this file cannot be ran in parallel with each other
// ie, no t.Parallel()
// This is because `modfile.File` is not safe for concurrent use.
// https://pkg.go.dev/golang.org/x/[email protected]/modfile#File
// Adding t.Parallel() here leads to the race detector not been happy
func Test_getModFile(t *testing.T) {
tests := []struct {
name string
gomodFile string
want string
}{
{
name: "mod1",
gomodFile: "testdata/modfiles/mod1/go.mod",
want: "testdata/modfiles/mod1",
},
}
for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
c := qt.New(t)
got, err := getModFile(tt.gomodFile)
c.Assert(err, qt.IsNil)
c.Assert(got.Module.Mod.Path, qt.DeepEquals, tt.want)
})
}
}
func Test_updateMod(t *testing.T) {
fmod1, _ := getModFile("testdata/modfiles/mod1/go.mod")
t.Cleanup(func() {
fmod1.Cleanup()
})
fmod4, _ := getModFile("testdata/modfiles/mod4/go.mod")
t.Cleanup(func() {
fmod4.Cleanup()
})
tests := []struct {
name string
trueTestModules []string
f *modfile.File
}{
{
name: "mod1",
trueTestModules: []string{"github.com/frankban/quicktest", "github.com/shirou/gopsutil"},
f: fmod1,
},
{
name: "mod4",
trueTestModules: []string{"github.com/benweissmann/memongo"},
f: fmod4,
},
}
for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
c := qt.New(t)
err := updateMod(tt.trueTestModules, tt.f)
c.Assert(err, qt.IsNil)
})
}
}
func Test_writeMod(t *testing.T) {
fmod1, _ := getModFile("testdata/modfiles/mod1/go.mod")
t.Cleanup(func() {
fmod1.Cleanup()
})
fmod4, _ := getModFile("testdata/modfiles/mod4/go.mod")
t.Cleanup(func() {
fmod4.Cleanup()
})
tests := []struct {
name string
trueTestModules []string
f *modfile.File
gomodFile string
readonly bool
want []string
}{
{
name: "mod1",
trueTestModules: []string{"github.com/frankban/quicktest", "github.com/shirou/gopsutil"},
f: fmod1,
gomodFile: "testdata/modfiles/mod1/go.mod",
readonly: true,
want: []string{
"module testdata/modfiles/mod1",
"github.com/frankban/quicktest v1.12.1 // test",
"github.com/shirou/gopsutil v2.20.9+incompatible // test",
},
},
{
name: "mod4",
trueTestModules: []string{"github.com/benweissmann/memongo"},
f: fmod4,
gomodFile: "testdata/modfiles/mod4/go.mod",
readonly: true,
want: []string{
"module testdata/modfiles/mod4",
"github.com/alexedwards/scs/v2 v2.4.0",
"github.com/aws/aws-sdk-go v1.38.31",
"github.com/benweissmann/memongo v0.1.1 // test",
"github.com/go-kit/kit v0.10.0",
"github.com/ishidawataru/sctp v0.0.0-20210226210310-f2269e66cdee",
"github.com/ory/herodot v0.9.5",
"github.com/rs/zerolog v1.21.0",
"github.com/sirupsen/logrus v1.8.1",
"github.com/zeebo/errs/v2 v2.0.3",
"go.uber.org/zap v1.13.0",
},
},
}
for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
c := qt.New(t)
err := updateMod(tt.trueTestModules, tt.f)
c.Assert(err, qt.IsNil)
w := &bytes.Buffer{}
errW := writeMod(tt.f, tt.gomodFile, w, tt.readonly)
c.Assert(errW, qt.IsNil)
for _, v := range tt.want {
c.Assert(w.String(), qt.Contains, v)
}
})
}
}