-
Notifications
You must be signed in to change notification settings - Fork 393
/
outputter_test.go
249 lines (223 loc) · 6.97 KB
/
outputter_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package pkg
import (
"context"
"errors"
"fmt"
"os"
"reflect"
"testing"
"github.com/chigopher/pathlib"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
pkgMocks "github.com/vektra/mockery/v2/mocks/github.com/vektra/mockery/v2/pkg"
"github.com/vektra/mockery/v2/pkg/config"
"github.com/vektra/mockery/v2/pkg/logging"
)
func TestFilenameBare(t *testing.T) {
out := FileOutputStreamProvider{InPackage: false, TestOnly: false}
assert.Equal(t, "name.go", out.filename("name"))
}
func TestFilenameMockOnly(t *testing.T) {
out := FileOutputStreamProvider{InPackage: true, TestOnly: false}
assert.Equal(t, "mock_name.go", out.filename("name"))
}
func TestFilenameMockOnlyWithSuffix(t *testing.T) {
out := FileOutputStreamProvider{InPackage: true, InPackageSuffix: true, TestOnly: false}
assert.Equal(t, "name_mock.go", out.filename("name"))
}
func TestFilenameMockTest(t *testing.T) {
out := FileOutputStreamProvider{InPackage: true, TestOnly: true}
assert.Equal(t, "mock_name_test.go", out.filename("name"))
}
func TestFilenameMockTestWithSuffix(t *testing.T) {
out := FileOutputStreamProvider{InPackage: true, InPackageSuffix: true, TestOnly: true}
assert.Equal(t, "name_mock_test.go", out.filename("name"))
}
func TestFilenameKeepTreeInPackage(t *testing.T) {
out := FileOutputStreamProvider{KeepTree: true, InPackage: true}
assert.Equal(t, "name.go", out.filename("name"))
}
func TestFilenameTest(t *testing.T) {
out := FileOutputStreamProvider{InPackage: false, TestOnly: true}
assert.Equal(t, "name_test.go", out.filename("name"))
}
func TestFilenameOverride(t *testing.T) {
out := FileOutputStreamProvider{InPackage: false, TestOnly: true, FileName: "override.go"}
assert.Equal(t, "override.go", out.filename("anynamehere"))
}
func TestUnderscoreCaseName(t *testing.T) {
assert.Equal(t, "notify_event", (&FileOutputStreamProvider{}).underscoreCaseName("NotifyEvent"))
assert.Equal(t, "repository", (&FileOutputStreamProvider{}).underscoreCaseName("Repository"))
assert.Equal(t, "http_server", (&FileOutputStreamProvider{}).underscoreCaseName("HTTPServer"))
assert.Equal(t, "awesome_http_server", (&FileOutputStreamProvider{}).underscoreCaseName("AwesomeHTTPServer"))
assert.Equal(t, "csv", (&FileOutputStreamProvider{}).underscoreCaseName("CSV"))
assert.Equal(t, "position0_size", (&FileOutputStreamProvider{}).underscoreCaseName("Position0Size"))
}
func Test_parseConfigTemplates(t *testing.T) {
mockPkg := func(t *testing.T) *pkgMocks.TypesPackage {
m := pkgMocks.NewTypesPackage(t)
m.EXPECT().Path().Return("github.com/user/project/package")
m.EXPECT().Name().Return("packageName")
return m
}
cwd, err := os.Getwd()
require.NoError(t, err)
type args struct {
c *config.Config
iface *Interface
}
tests := []struct {
name string
args args
disableWantCheck bool
// pkg is used to generate a mock types.Package object.
// It has to take in the testing.T object so we can
// assert expectations.
pkg func(t *testing.T) *pkgMocks.TypesPackage
want *config.Config
wantErr error
}{
{
name: "standards",
args: args{
c: &config.Config{
Dir: "{{.InterfaceDir}}/{{.PackagePath}}",
FileName: "{{.InterfaceName}}_{{.InterfaceNameCamel}}_{{.InterfaceNameSnake}}.go",
MockName: "{{.InterfaceNameLowerCamel}}",
Outpkg: "{{.PackageName}}",
},
iface: &Interface{
Name: "FooBar",
FileName: "path/to/foobar.go",
},
},
pkg: mockPkg,
want: &config.Config{
Dir: "path/to/github.com/user/project/package",
FileName: "FooBar_FooBar_foo_bar.go",
MockName: "fooBar",
Outpkg: "packageName",
},
},
{
name: "InterfaceDirRelative in current working directory",
args: args{
c: &config.Config{
Dir: "{{.InterfaceDirRelative}}",
},
iface: &Interface{
Name: "FooBar",
FileName: cwd + "/path/to/foobar.go",
},
},
pkg: mockPkg,
want: &config.Config{
Dir: "path/to",
},
},
{
name: "InterfaceDirRelative not in current working directory",
args: args{
c: &config.Config{
Dir: "mocks/{{.InterfaceDirRelative}}",
},
iface: &Interface{
Name: "FooBar",
FileName: "/path/to/foobar.go",
},
},
pkg: mockPkg,
want: &config.Config{
Dir: "mocks/github.com/user/project/package",
},
},
{
name: "infinite loop in template variables",
args: args{
c: &config.Config{
Dir: "{{.InterfaceDir}}/{{.PackagePath}}",
FileName: "{{.MockName}}.go",
MockName: "Mock{{.MockName}}",
Outpkg: "{{.PackageName}}",
},
iface: &Interface{
Name: "FooBar",
FileName: "path/to/foobar.go",
},
},
pkg: mockPkg,
disableWantCheck: true,
wantErr: ErrInfiniteLoop,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.iface.Pkg = tt.pkg(t)
err := parseConfigTemplates(context.Background(), tt.args.c, tt.args.iface)
if !errors.Is(err, tt.wantErr) {
t.Errorf("parseConfigTemplates() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.disableWantCheck && !reflect.DeepEqual(tt.args.c, tt.want) {
t.Errorf("*config.Config = %s\n, want %+v", spew.Sdump(tt.args.c), spew.Sdump(tt.want))
}
})
}
}
func TestOutputter_Generate(t *testing.T) {
type fields struct {
boilerplate string
config *config.Config
}
tests := []struct {
name string
packagePath string
fields fields
wantErr bool
}{
{
name: "generate normal",
packagePath: "github.com/vektra/mockery/v2/pkg/fixtures/example_project",
},
}
for _, tt := range tests {
if tt.fields.config == nil {
tt.fields.config = &config.Config{}
}
tt.fields.config.Dir = t.TempDir()
tt.fields.config.MockName = "Mock{{.InterfaceName}}"
tt.fields.config.FileName = "mock_{{.InterfaceName}}.go"
tt.fields.config.Outpkg = "{{.PackageName}}"
t.Run(tt.name, func(t *testing.T) {
m := &Outputter{
boilerplate: tt.fields.boilerplate,
config: tt.fields.config,
dryRun: true,
}
parser := NewParser([]string{})
log, err := logging.GetLogger("INFO")
require.NoError(t, err)
ctx := log.WithContext(context.Background())
confPath := pathlib.NewPath(t.TempDir()).Join("config.yaml")
ymlContents := fmt.Sprintf(`
packages:
%s:
config:
all: True
`, tt.packagePath)
require.NoError(t, confPath.WriteFile([]byte(ymlContents)))
m.config.Config = confPath.String()
require.NoError(t, parser.ParsePackages(ctx, []string{tt.packagePath}))
require.NoError(t, parser.Load())
for _, intf := range parser.Interfaces() {
t.Logf("generating interface: %s %s", intf.QualifiedName, intf.Name)
require.NoError(t, m.Generate(ctx, intf))
mockPath := pathlib.NewPath(tt.fields.config.Dir).Join("mock_" + intf.Name + ".go")
t.Logf("checking if path exists: %v", mockPath)
exists, err := mockPath.Exists()
require.NoError(t, err)
assert.True(t, exists)
}
})
}
}