Use mockery to generate mocks compatible with testify/mock (#1190)

## Changes

This is the same approach we use in the Go SDK.

## Tests

Tests pass.
This commit is contained in:
Pieter Noordhuis 2024-02-08 16:18:53 +01:00 committed by GitHub
parent d638262665
commit 4073e45d4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 420 additions and 168 deletions

10
.mockery.yaml Normal file
View File

@ -0,0 +1,10 @@
with-expecter: true
filename: "mock_{{.InterfaceName | snakecase}}.go"
mockname: "Mock{{.InterfaceName}}"
outpkg: "mock{{.PackageName}}"
packages:
github.com/databricks/cli/libs/filer:
interfaces:
Filer:
config:
dir: "internal/mocks/libs/filer"

4
NOTICE
View File

@ -16,10 +16,6 @@ go-ini/ini - https://github.com/go-ini/ini
Copyright ini authors
License - https://github.com/go-ini/ini/blob/main/LICENSE
uber-go/mock - https://go.uber.org/mock
Copyright Google Inc.
License - https://github.com/uber-go/mock/blob/main/LICENSE
—--
This software contains code from the following open source projects, licensed under the MPL 2.0 license:

View File

@ -11,25 +11,24 @@ import (
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
mock "github.com/databricks/cli/internal/mocks/libs/filer"
mockfiler "github.com/databricks/cli/internal/mocks/libs/filer"
"github.com/databricks/cli/libs/filer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
func mockStateFilerForPull(t *testing.T, contents map[string]int, merr error) filer.Filer {
buf, err := json.Marshal(contents)
require.NoError(t, err)
ctrl := gomock.NewController(t)
mock := mock.NewMockFiler(ctrl)
mock.
f := mockfiler.NewMockFiler(t)
f.
EXPECT().
Read(gomock.Any(), gomock.Eq(TerraformStateFileName)).
Read(mock.Anything, TerraformStateFileName).
Return(io.NopCloser(bytes.NewReader(buf)), merr).
Times(1)
return mock
return f
}
func statePullTestBundle(t *testing.T) *bundle.Bundle {

View File

@ -8,25 +8,23 @@ import (
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
mock "github.com/databricks/cli/internal/mocks/libs/filer"
mockfiler "github.com/databricks/cli/internal/mocks/libs/filer"
"github.com/databricks/cli/libs/filer"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"github.com/stretchr/testify/mock"
)
func mockStateFilerForPush(t *testing.T, fn func(body io.Reader)) filer.Filer {
ctrl := gomock.NewController(t)
mock := mock.NewMockFiler(ctrl)
mock.
f := mockfiler.NewMockFiler(t)
f.
EXPECT().
Write(gomock.Any(), gomock.Any(), gomock.Any(), filer.CreateParentDirectories, filer.OverwriteIfExists).
Do(func(ctx context.Context, path string, reader io.Reader, mode ...filer.WriteMode) error {
Write(mock.Anything, mock.Anything, mock.Anything, filer.CreateParentDirectories, filer.OverwriteIfExists).
Run(func(ctx context.Context, path string, reader io.Reader, mode ...filer.WriteMode) {
fn(reader)
return nil
}).
Return(nil).
Times(1)
return mock
return f
}
func statePushTestBundle(t *testing.T) *bundle.Bundle {

5
go.mod
View File

@ -30,10 +30,7 @@ require (
gopkg.in/ini.v1 v1.67.0 // Apache 2.0
)
require (
go.uber.org/mock v0.4.0
gopkg.in/yaml.v3 v3.0.1
)
require gopkg.in/yaml.v3 v3.0.1
require (
cloud.google.com/go/compute v1.23.3 // indirect

2
go.sum generated
View File

@ -173,8 +173,6 @@ go.opentelemetry.io/otel/metric v1.21.0 h1:tlYWfeo+Bocx5kLEloTjbcDwBuELRrIFxwdQ3
go.opentelemetry.io/otel/metric v1.21.0/go.mod h1:o1p3CA8nNHW8j5yuQLdc1eeqEaPfzug24uvsyIEJRWM=
go.opentelemetry.io/otel/trace v1.21.0 h1:WD9i5gzvoUPuXIXH24ZNBudiarZDKuekPqi/E8fpfLc=
go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+BaslueVtS/qQ=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=

View File

@ -4,4 +4,7 @@ Use this directory to store mocks for interfaces in this repository.
Please use the same package structure for the mocks as the interface it is mocking.
See https://github.com/uber-go/mock for more information on how to generate mocks.
Refresh mocks by running:
```
go run github.com/vektra/mockery/v2@b9df18e0f7b94f0bc11af3f379c8a9aea1e1e8da
```

View File

@ -1,139 +0,0 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/databricks/cli/libs/filer (interfaces: Filer)
//
// Generated by this command:
//
// mockgen -destination filer_mock.go github.com/databricks/cli/libs/filer Filer
//
// Package mock_filer is a generated GoMock package.
package mock_filer
import (
context "context"
io "io"
fs "io/fs"
reflect "reflect"
filer "github.com/databricks/cli/libs/filer"
gomock "go.uber.org/mock/gomock"
)
// MockFiler is a mock of Filer interface.
type MockFiler struct {
ctrl *gomock.Controller
recorder *MockFilerMockRecorder
}
// MockFilerMockRecorder is the mock recorder for MockFiler.
type MockFilerMockRecorder struct {
mock *MockFiler
}
// NewMockFiler creates a new mock instance.
func NewMockFiler(ctrl *gomock.Controller) *MockFiler {
mock := &MockFiler{ctrl: ctrl}
mock.recorder = &MockFilerMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockFiler) EXPECT() *MockFilerMockRecorder {
return m.recorder
}
// Delete mocks base method.
func (m *MockFiler) Delete(arg0 context.Context, arg1 string, arg2 ...filer.DeleteMode) error {
m.ctrl.T.Helper()
varargs := []any{arg0, arg1}
for _, a := range arg2 {
varargs = append(varargs, a)
}
ret := m.ctrl.Call(m, "Delete", varargs...)
ret0, _ := ret[0].(error)
return ret0
}
// Delete indicates an expected call of Delete.
func (mr *MockFilerMockRecorder) Delete(arg0, arg1 any, arg2 ...any) *gomock.Call {
mr.mock.ctrl.T.Helper()
varargs := append([]any{arg0, arg1}, arg2...)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockFiler)(nil).Delete), varargs...)
}
// Mkdir mocks base method.
func (m *MockFiler) Mkdir(arg0 context.Context, arg1 string) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Mkdir", arg0, arg1)
ret0, _ := ret[0].(error)
return ret0
}
// Mkdir indicates an expected call of Mkdir.
func (mr *MockFilerMockRecorder) Mkdir(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Mkdir", reflect.TypeOf((*MockFiler)(nil).Mkdir), arg0, arg1)
}
// Read mocks base method.
func (m *MockFiler) Read(arg0 context.Context, arg1 string) (io.ReadCloser, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Read", arg0, arg1)
ret0, _ := ret[0].(io.ReadCloser)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Read indicates an expected call of Read.
func (mr *MockFilerMockRecorder) Read(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Read", reflect.TypeOf((*MockFiler)(nil).Read), arg0, arg1)
}
// ReadDir mocks base method.
func (m *MockFiler) ReadDir(arg0 context.Context, arg1 string) ([]fs.DirEntry, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ReadDir", arg0, arg1)
ret0, _ := ret[0].([]fs.DirEntry)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// ReadDir indicates an expected call of ReadDir.
func (mr *MockFilerMockRecorder) ReadDir(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReadDir", reflect.TypeOf((*MockFiler)(nil).ReadDir), arg0, arg1)
}
// Stat mocks base method.
func (m *MockFiler) Stat(arg0 context.Context, arg1 string) (fs.FileInfo, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Stat", arg0, arg1)
ret0, _ := ret[0].(fs.FileInfo)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Stat indicates an expected call of Stat.
func (mr *MockFilerMockRecorder) Stat(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Stat", reflect.TypeOf((*MockFiler)(nil).Stat), arg0, arg1)
}
// Write mocks base method.
func (m *MockFiler) Write(arg0 context.Context, arg1 string, arg2 io.Reader, arg3 ...filer.WriteMode) error {
m.ctrl.T.Helper()
varargs := []any{arg0, arg1, arg2}
for _, a := range arg3 {
varargs = append(varargs, a)
}
ret := m.ctrl.Call(m, "Write", varargs...)
ret0, _ := ret[0].(error)
return ret0
}
// Write indicates an expected call of Write.
func (mr *MockFilerMockRecorder) Write(arg0, arg1, arg2 any, arg3 ...any) *gomock.Call {
mr.mock.ctrl.T.Helper()
varargs := append([]any{arg0, arg1, arg2}, arg3...)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockFiler)(nil).Write), varargs...)
}

View File

@ -0,0 +1,390 @@
// Code generated by mockery v2.39.1. DO NOT EDIT.
package mockfiler
import (
context "context"
fs "io/fs"
filer "github.com/databricks/cli/libs/filer"
io "io"
mock "github.com/stretchr/testify/mock"
)
// MockFiler is an autogenerated mock type for the Filer type
type MockFiler struct {
mock.Mock
}
type MockFiler_Expecter struct {
mock *mock.Mock
}
func (_m *MockFiler) EXPECT() *MockFiler_Expecter {
return &MockFiler_Expecter{mock: &_m.Mock}
}
// Delete provides a mock function with given fields: ctx, path, mode
func (_m *MockFiler) Delete(ctx context.Context, path string, mode ...filer.DeleteMode) error {
_va := make([]interface{}, len(mode))
for _i := range mode {
_va[_i] = mode[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, path)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for Delete")
}
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, string, ...filer.DeleteMode) error); ok {
r0 = rf(ctx, path, mode...)
} else {
r0 = ret.Error(0)
}
return r0
}
// MockFiler_Delete_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Delete'
type MockFiler_Delete_Call struct {
*mock.Call
}
// Delete is a helper method to define mock.On call
// - ctx context.Context
// - path string
// - mode ...filer.DeleteMode
func (_e *MockFiler_Expecter) Delete(ctx interface{}, path interface{}, mode ...interface{}) *MockFiler_Delete_Call {
return &MockFiler_Delete_Call{Call: _e.mock.On("Delete",
append([]interface{}{ctx, path}, mode...)...)}
}
func (_c *MockFiler_Delete_Call) Run(run func(ctx context.Context, path string, mode ...filer.DeleteMode)) *MockFiler_Delete_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]filer.DeleteMode, len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(filer.DeleteMode)
}
}
run(args[0].(context.Context), args[1].(string), variadicArgs...)
})
return _c
}
func (_c *MockFiler_Delete_Call) Return(_a0 error) *MockFiler_Delete_Call {
_c.Call.Return(_a0)
return _c
}
func (_c *MockFiler_Delete_Call) RunAndReturn(run func(context.Context, string, ...filer.DeleteMode) error) *MockFiler_Delete_Call {
_c.Call.Return(run)
return _c
}
// Mkdir provides a mock function with given fields: ctx, path
func (_m *MockFiler) Mkdir(ctx context.Context, path string) error {
ret := _m.Called(ctx, path)
if len(ret) == 0 {
panic("no return value specified for Mkdir")
}
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, string) error); ok {
r0 = rf(ctx, path)
} else {
r0 = ret.Error(0)
}
return r0
}
// MockFiler_Mkdir_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Mkdir'
type MockFiler_Mkdir_Call struct {
*mock.Call
}
// Mkdir is a helper method to define mock.On call
// - ctx context.Context
// - path string
func (_e *MockFiler_Expecter) Mkdir(ctx interface{}, path interface{}) *MockFiler_Mkdir_Call {
return &MockFiler_Mkdir_Call{Call: _e.mock.On("Mkdir", ctx, path)}
}
func (_c *MockFiler_Mkdir_Call) Run(run func(ctx context.Context, path string)) *MockFiler_Mkdir_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(string))
})
return _c
}
func (_c *MockFiler_Mkdir_Call) Return(_a0 error) *MockFiler_Mkdir_Call {
_c.Call.Return(_a0)
return _c
}
func (_c *MockFiler_Mkdir_Call) RunAndReturn(run func(context.Context, string) error) *MockFiler_Mkdir_Call {
_c.Call.Return(run)
return _c
}
// Read provides a mock function with given fields: ctx, path
func (_m *MockFiler) Read(ctx context.Context, path string) (io.ReadCloser, error) {
ret := _m.Called(ctx, path)
if len(ret) == 0 {
panic("no return value specified for Read")
}
var r0 io.ReadCloser
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, string) (io.ReadCloser, error)); ok {
return rf(ctx, path)
}
if rf, ok := ret.Get(0).(func(context.Context, string) io.ReadCloser); ok {
r0 = rf(ctx, path)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(io.ReadCloser)
}
}
if rf, ok := ret.Get(1).(func(context.Context, string) error); ok {
r1 = rf(ctx, path)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockFiler_Read_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Read'
type MockFiler_Read_Call struct {
*mock.Call
}
// Read is a helper method to define mock.On call
// - ctx context.Context
// - path string
func (_e *MockFiler_Expecter) Read(ctx interface{}, path interface{}) *MockFiler_Read_Call {
return &MockFiler_Read_Call{Call: _e.mock.On("Read", ctx, path)}
}
func (_c *MockFiler_Read_Call) Run(run func(ctx context.Context, path string)) *MockFiler_Read_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(string))
})
return _c
}
func (_c *MockFiler_Read_Call) Return(_a0 io.ReadCloser, _a1 error) *MockFiler_Read_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockFiler_Read_Call) RunAndReturn(run func(context.Context, string) (io.ReadCloser, error)) *MockFiler_Read_Call {
_c.Call.Return(run)
return _c
}
// ReadDir provides a mock function with given fields: ctx, path
func (_m *MockFiler) ReadDir(ctx context.Context, path string) ([]fs.DirEntry, error) {
ret := _m.Called(ctx, path)
if len(ret) == 0 {
panic("no return value specified for ReadDir")
}
var r0 []fs.DirEntry
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, string) ([]fs.DirEntry, error)); ok {
return rf(ctx, path)
}
if rf, ok := ret.Get(0).(func(context.Context, string) []fs.DirEntry); ok {
r0 = rf(ctx, path)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]fs.DirEntry)
}
}
if rf, ok := ret.Get(1).(func(context.Context, string) error); ok {
r1 = rf(ctx, path)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockFiler_ReadDir_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ReadDir'
type MockFiler_ReadDir_Call struct {
*mock.Call
}
// ReadDir is a helper method to define mock.On call
// - ctx context.Context
// - path string
func (_e *MockFiler_Expecter) ReadDir(ctx interface{}, path interface{}) *MockFiler_ReadDir_Call {
return &MockFiler_ReadDir_Call{Call: _e.mock.On("ReadDir", ctx, path)}
}
func (_c *MockFiler_ReadDir_Call) Run(run func(ctx context.Context, path string)) *MockFiler_ReadDir_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(string))
})
return _c
}
func (_c *MockFiler_ReadDir_Call) Return(_a0 []fs.DirEntry, _a1 error) *MockFiler_ReadDir_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockFiler_ReadDir_Call) RunAndReturn(run func(context.Context, string) ([]fs.DirEntry, error)) *MockFiler_ReadDir_Call {
_c.Call.Return(run)
return _c
}
// Stat provides a mock function with given fields: ctx, name
func (_m *MockFiler) Stat(ctx context.Context, name string) (fs.FileInfo, error) {
ret := _m.Called(ctx, name)
if len(ret) == 0 {
panic("no return value specified for Stat")
}
var r0 fs.FileInfo
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, string) (fs.FileInfo, error)); ok {
return rf(ctx, name)
}
if rf, ok := ret.Get(0).(func(context.Context, string) fs.FileInfo); ok {
r0 = rf(ctx, name)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(fs.FileInfo)
}
}
if rf, ok := ret.Get(1).(func(context.Context, string) error); ok {
r1 = rf(ctx, name)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockFiler_Stat_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Stat'
type MockFiler_Stat_Call struct {
*mock.Call
}
// Stat is a helper method to define mock.On call
// - ctx context.Context
// - name string
func (_e *MockFiler_Expecter) Stat(ctx interface{}, name interface{}) *MockFiler_Stat_Call {
return &MockFiler_Stat_Call{Call: _e.mock.On("Stat", ctx, name)}
}
func (_c *MockFiler_Stat_Call) Run(run func(ctx context.Context, name string)) *MockFiler_Stat_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(string))
})
return _c
}
func (_c *MockFiler_Stat_Call) Return(_a0 fs.FileInfo, _a1 error) *MockFiler_Stat_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockFiler_Stat_Call) RunAndReturn(run func(context.Context, string) (fs.FileInfo, error)) *MockFiler_Stat_Call {
_c.Call.Return(run)
return _c
}
// Write provides a mock function with given fields: ctx, path, reader, mode
func (_m *MockFiler) Write(ctx context.Context, path string, reader io.Reader, mode ...filer.WriteMode) error {
_va := make([]interface{}, len(mode))
for _i := range mode {
_va[_i] = mode[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, path, reader)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for Write")
}
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, string, io.Reader, ...filer.WriteMode) error); ok {
r0 = rf(ctx, path, reader, mode...)
} else {
r0 = ret.Error(0)
}
return r0
}
// MockFiler_Write_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Write'
type MockFiler_Write_Call struct {
*mock.Call
}
// Write is a helper method to define mock.On call
// - ctx context.Context
// - path string
// - reader io.Reader
// - mode ...filer.WriteMode
func (_e *MockFiler_Expecter) Write(ctx interface{}, path interface{}, reader interface{}, mode ...interface{}) *MockFiler_Write_Call {
return &MockFiler_Write_Call{Call: _e.mock.On("Write",
append([]interface{}{ctx, path, reader}, mode...)...)}
}
func (_c *MockFiler_Write_Call) Run(run func(ctx context.Context, path string, reader io.Reader, mode ...filer.WriteMode)) *MockFiler_Write_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]filer.WriteMode, len(args)-3)
for i, a := range args[3:] {
if a != nil {
variadicArgs[i] = a.(filer.WriteMode)
}
}
run(args[0].(context.Context), args[1].(string), args[2].(io.Reader), variadicArgs...)
})
return _c
}
func (_c *MockFiler_Write_Call) Return(_a0 error) *MockFiler_Write_Call {
_c.Call.Return(_a0)
return _c
}
func (_c *MockFiler_Write_Call) RunAndReturn(run func(context.Context, string, io.Reader, ...filer.WriteMode) error) *MockFiler_Write_Call {
_c.Call.Return(run)
return _c
}
// NewMockFiler creates a new instance of MockFiler. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewMockFiler(t interface {
mock.TestingT
Cleanup(func())
}) *MockFiler {
mock := &MockFiler{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}