Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 2 additions & 6 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ 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:

hashicopr/go-version - https://github.com/hashicorp/go-version
Copyright 2014 HashiCorp, Inc.
Copyright 2014 HashiCorp, Inc.
License - https://github.com/hashicorp/go-version/blob/main/LICENSE

hashicorp/hc-install - https://github.com/hashicorp/hc-install
Expand Down Expand Up @@ -81,7 +77,7 @@ License - https://github.com/fatih/color/blob/main/LICENSE.md
ghodss/yaml - https://github.com/ghodss/yaml
Copyright (c) 2014 Sam Ghods
License - https://github.com/ghodss/yaml/blob/master/LICENSE

mattn/go-isatty - https://github.com/mattn/go-isatty
Copyright (c) Yasuhiro MATSUMOTO <mattn.jp@gmail.com>
https://github.com/mattn/go-isatty/blob/master/LICENSE
Expand Down
13 changes: 6 additions & 7 deletions bundle/deploy/terraform/state_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 7 additions & 9 deletions bundle/deploy/terraform/state_push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion internal/mocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
139 changes: 0 additions & 139 deletions internal/mocks/libs/filer/filer_mock.go

This file was deleted.

Loading
Loading