Skip to content

Commit

Permalink
Use mockery to generate mocks compatible with testify/mock (#1190)
Browse files Browse the repository at this point in the history
## Changes

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

## Tests

Tests pass.
  • Loading branch information
pietern authored Feb 8, 2024
1 parent d638262 commit 4073e45
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 168 deletions.
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

0 comments on commit 4073e45

Please sign in to comment.