From 9f072b19f5cfc5f3b70122ef36ddac6cb262c2c2 Mon Sep 17 00:00:00 2001 From: Arkadiusz Osowski Date: Wed, 21 Aug 2024 12:18:09 +0200 Subject: [PATCH 1/2] bump golang ver => 1.22.5 --- go.mod | 9 ++++++--- go.sum | 11 ++++++----- main.go | 3 +-- pkg/moq/moq_test.go | 7 +++---- pkg/moq/testpackages/go.mod | 2 +- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index 435304e..e04213b 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,13 @@ module github.com/matryer/moq -go 1.18 +go 1.22.5 require ( github.com/pmezard/go-difflib v1.0.0 - golang.org/x/tools v0.17.0 + golang.org/x/tools v0.24.0 ) -require golang.org/x/mod v0.14.0 // indirect +require ( + golang.org/x/mod v0.20.0 // indirect + golang.org/x/sync v0.8.0 // indirect +) diff --git a/go.sum b/go.sum index c1e7da6..4a851ad 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= -golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= -golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc= -golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps= +golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0= +golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24= +golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ= diff --git a/main.go b/main.go index 89adb3d..e2a1fec 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "os" "path/filepath" @@ -109,5 +108,5 @@ func run(flags userFlags) error { return err } - return ioutil.WriteFile(flags.outFile, buf.Bytes(), 0o600) + return os.WriteFile(flags.outFile, buf.Bytes(), 0o600) } diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 2813238..71c62dd 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -5,7 +5,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -464,14 +463,14 @@ func matchGoldenFile(goldenFile string, actual []byte) error { if err := os.MkdirAll(filepath.Dir(goldenFile), 0o750); err != nil { return fmt.Errorf("create dir: %s", err) } - if err := ioutil.WriteFile(goldenFile, actual, 0o600); err != nil { + if err := os.WriteFile(goldenFile, actual, 0o600); err != nil { return fmt.Errorf("write: %s", err) } return nil } - expected, err := ioutil.ReadFile(goldenFile) + expected, err := os.ReadFile(goldenFile) if err != nil { return fmt.Errorf("read: %s: %s", goldenFile, err) } @@ -694,7 +693,7 @@ func TestMockError(t *testing.T) { } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - err := m.Mock(ioutil.Discard, tc.namePair) + err := m.Mock(io.Discard, tc.namePair) if err == nil { t.Errorf("expected error but got nil") return diff --git a/pkg/moq/testpackages/go.mod b/pkg/moq/testpackages/go.mod index b65ffdf..d7dab97 100644 --- a/pkg/moq/testpackages/go.mod +++ b/pkg/moq/testpackages/go.mod @@ -1,5 +1,5 @@ module github.com/matryer/moq/pkg/moq/testpackages -go 1.18 +go 1.22.5 require github.com/sudo-suhas/moq-test-pkgs/somerepo v0.0.0-20200816045313-d2f573eea6c7 From 8bd60ed62cea56aa97c4a47d2806e0f07ea046b2 Mon Sep 17 00:00:00 2001 From: Arkadiusz Osowski Date: Wed, 21 Aug 2024 17:17:43 +0200 Subject: [PATCH 2/2] add test case --- pkg/moq/moq_test.go | 6 ++ pkg/moq/testpackages/rangenum/rangenum.go | 13 ++++ .../rangenum/rangenum_moq.golden.go | 67 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 pkg/moq/testpackages/rangenum/rangenum.go create mode 100644 pkg/moq/testpackages/rangenum/rangenum_moq.golden.go diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 71c62dd..5d6230d 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -406,6 +406,12 @@ func TestMockGolden(t *testing.T) { interfaces: []string{"ResetStore"}, goldenFile: filepath.Join("testpackages/withresets", "withresets_moq.golden.go"), }, + { + name: "RangeNumber", + cfg: Config{SrcDir: "testpackages/rangenum"}, + interfaces: []string{"Magician"}, + goldenFile: filepath.Join("testpackages/rangenum", "rangenum_moq.golden.go"), + }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { diff --git a/pkg/moq/testpackages/rangenum/rangenum.go b/pkg/moq/testpackages/rangenum/rangenum.go new file mode 100644 index 0000000..d6e9fb2 --- /dev/null +++ b/pkg/moq/testpackages/rangenum/rangenum.go @@ -0,0 +1,13 @@ +package rangenum + +import "fmt" + +func DoMagic() { + for range 10 { + fmt.Println("abrakadabra") + } +} + +type Magician interface { + DoMagic() +} diff --git a/pkg/moq/testpackages/rangenum/rangenum_moq.golden.go b/pkg/moq/testpackages/rangenum/rangenum_moq.golden.go new file mode 100644 index 0000000..5fb191e --- /dev/null +++ b/pkg/moq/testpackages/rangenum/rangenum_moq.golden.go @@ -0,0 +1,67 @@ +// Code generated by moq; DO NOT EDIT. +// github.com/matryer/moq + +package rangenum + +import ( + "sync" +) + +// Ensure, that MagicianMock does implement Magician. +// If this is not the case, regenerate this file with moq. +var _ Magician = &MagicianMock{} + +// MagicianMock is a mock implementation of Magician. +// +// func TestSomethingThatUsesMagician(t *testing.T) { +// +// // make and configure a mocked Magician +// mockedMagician := &MagicianMock{ +// DoMagicFunc: func() { +// panic("mock out the DoMagic method") +// }, +// } +// +// // use mockedMagician in code that requires Magician +// // and then make assertions. +// +// } +type MagicianMock struct { + // DoMagicFunc mocks the DoMagic method. + DoMagicFunc func() + + // calls tracks calls to the methods. + calls struct { + // DoMagic holds details about calls to the DoMagic method. + DoMagic []struct { + } + } + lockDoMagic sync.RWMutex +} + +// DoMagic calls DoMagicFunc. +func (mock *MagicianMock) DoMagic() { + if mock.DoMagicFunc == nil { + panic("MagicianMock.DoMagicFunc: method is nil but Magician.DoMagic was just called") + } + callInfo := struct { + }{} + mock.lockDoMagic.Lock() + mock.calls.DoMagic = append(mock.calls.DoMagic, callInfo) + mock.lockDoMagic.Unlock() + mock.DoMagicFunc() +} + +// DoMagicCalls gets all the calls that were made to DoMagic. +// Check the length with: +// +// len(mockedMagician.DoMagicCalls()) +func (mock *MagicianMock) DoMagicCalls() []struct { +} { + var calls []struct { + } + mock.lockDoMagic.RLock() + calls = mock.calls.DoMagic + mock.lockDoMagic.RUnlock() + return calls +}