Skip to content

Commit

Permalink
Merge pull request #142 from kozmod/feature/141_merge_mocks_script
Browse files Browse the repository at this point in the history
[#141] mockery: script to merge mocks
  • Loading branch information
kozmod authored Apr 3, 2024
2 parents 29e0408 + 149a63e commit 39a12d2
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 100 deletions.
46 changes: 0 additions & 46 deletions test/integration/internal/mock/mockery/mock_repository_test.go

This file was deleted.

46 changes: 0 additions & 46 deletions test/integration/internal/mock/mockery/mock_transactor_test.go

This file was deleted.

12 changes: 6 additions & 6 deletions test/integration/internal/mock/mockery/mockery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
func Test_mockery(t *testing.T) {
t.Run("assert_success", func(t *testing.T) {
ctx := context.Background()
transactorMock := new(transactorMock)
transactorMock := new(mockTransactor)
transactorMock.On(transactorMethodWithinTx,
ctx,
mock.MatchedBy(func(i any) bool {
Expand All @@ -28,10 +28,10 @@ func Test_mockery(t *testing.T) {
return assert.NoError(t, fn(ctx))
})).Return(nil)

repositoryMockA := new(repositoryMock)
repositoryMockA := new(mockRepository)
repositoryMockA.On(repositoryMethodInsert, ctx, textValue).Return(nil)

repositoryMockB := new(repositoryMock)
repositoryMockB := new(mockRepository)
repositoryMockB.On(repositoryMethodInsert, ctx, textValue).Return(nil)

useCase := UseCase{
Expand All @@ -51,7 +51,7 @@ func Test_mockery(t *testing.T) {
trsnsactorErr = fmt.Errorf("transactor_error")
)

transactorMock := new(transactorMock)
transactorMock := new(mockTransactor)
transactorMock.On(transactorMethodWithinTx,
ctx,
mock.MatchedBy(func(i any) bool {
Expand All @@ -61,10 +61,10 @@ func Test_mockery(t *testing.T) {
}),
).Return(trsnsactorErr)

repositoryMockA := new(repositoryMock)
repositoryMockA := new(mockRepository)
repositoryMockA.On(repositoryMethodInsert, ctx, textValue).Return(nil)

repositoryMockB := new(repositoryMock)
repositoryMockB := new(mockRepository)
repositoryMockB.On(repositoryMethodInsert, ctx, textValue).Return(expError)

useCase := UseCase{
Expand Down
83 changes: 83 additions & 0 deletions test/integration/internal/mock/mockery/mocks_test.go

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

46 changes: 46 additions & 0 deletions test/integration/internal/mock/mockery/scripts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Script to merge mockery (v2) generated mocks.


mock_pattern=mock_*.go
mock_merge_file=mocks_test.go

function update_mocks() {
path=${1:-.}
find ${path} -name "${mock_merge_file}" -delete

echo "merge mocks: ${path}"
$(merge_mocks $path)

echo "remove generated mocks by pattern: ${mock_pattern}"
$(rm_mocks $path)
}

function merge_mocks() {
path=${1:-.}
patter=${path}/${mock_pattern}
result_file=${path}/${mock_merge_file}

first=true
for f in ${patter}; do
if [ "${first}" = true ] ; then
(cat "${f}"; echo; echo;) >> ${result_file}
first=false
else
import_end_line=$(awk -v line=')' '$0 == line {print NR}' $f)
file_lines=$(wc -l < ${f})
tail_lines=$((${file_lines} - ${import_end_line}))
(cat "${f}" | tail -n ${tail_lines}; echo; echo;) >> ${result_file}
fi
done

exit 0
}

function rm_mocks() {
path=${1:-.}
find ${path} -name "${mock_pattern}" -delete

exit 0
}

"$@"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mockery

//go:generate mockery --dir=. --name=repository --outpkg=mockery --output=. --filename=mock_repository_test.go --structname=repositoryMock
//go:generate mockery --dir=. --name=transactor --outpkg=mockery --output=. --filename=mock_transactor_test.go --structname=transactorMock
//go:generate mockery --inpackage --all --outpkg=mockery --dir=. --outpkg=profile --output=.
//go:generate sh ./scripts.sh update_mocks .
//go:generate git add .

import (
Expand Down

0 comments on commit 39a12d2

Please sign in to comment.