Skip to content

Commit

Permalink
feat: added automatic snapshot testing
Browse files Browse the repository at this point in the history
  • Loading branch information
defaulterrr committed Mar 17, 2023
1 parent 548317b commit ad7b506
Show file tree
Hide file tree
Showing 19 changed files with 3,223 additions and 35 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
)

require (
github.com/bradleyjkemp/cupaloy v2.3.0+incompatible // indirect
github.com/kr/text v0.2.0 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sys v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bradleyjkemp/cupaloy v2.3.0+incompatible h1:UafIjBvWQmS9i/xRg+CamMrnLTKNzo+bdmT/oH34c2Y=
github.com/bradleyjkemp/cupaloy v2.3.0+incompatible/go.mod h1:Au1Xw1sgaJ5iSFktEhYsS0dbQiS1B0/XMXl+42y9Ilk=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down
133 changes: 133 additions & 0 deletions snapshot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package minimock_test

import (
"bytes"
"context"
"os"
"os/exec"
"testing"
"time"

"github.com/bradleyjkemp/cupaloy"
)

func mustRunMinimockWithParams(
t *testing.T,
interfacePattern string,
outputFile string,
) {
t.Helper()

var outBuffer bytes.Buffer

timeoutContext, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

cmd := exec.CommandContext(timeoutContext, "go", "run", "cmd/minimock/minimock.go", "-o", outputFile, "-i", interfacePattern)
cmd.Stdout = &outBuffer
cmd.Stderr = &outBuffer

t.Log(cmd.String())

if err := cmd.Run(); err != nil {
t.Log(outBuffer.String())
t.Fail()
}
}

func mustReadFile(
t *testing.T,
filename string,
) []byte {
t.Helper()

contents, err := os.ReadFile(filename)
if err != nil {
t.Errorf("failed to read the file: %v", err)
t.Fail()
}

return contents
}

func TestSnapshot(t *testing.T) {
snapshotter := cupaloy.New(
cupaloy.SnapshotSubdirectory("snapshots"),
cupaloy.SnapshotFileExtension(".go"),
)

type testCase struct {
name string
outputFile string
inputInterface string
expectedOutputFile string
}

testCases := []testCase{
{
name: "package reference",
outputFile: "./tests",
inputInterface: "github.com/gojuno/minimock/v3.Tester",
expectedOutputFile: "tests/tester_mock_test.go",
},
{
name: "relative reference",
inputInterface: "./tests.Formatter",
outputFile: "./tests/formatter_mock.go",
expectedOutputFile: "./tests/formatter_mock.go",
},
{
name: "generics with any used as param and return type",
inputInterface: "./tests.genericInout",
outputFile: "./tests/generic_inout.go",
expectedOutputFile: "./tests/generic_inout.go",
},
{
name: "generics with any used as return type",
inputInterface: "./tests.genericOut",
outputFile: "./tests/generic_out.go",
expectedOutputFile: "./tests/generic_out.go",
},
{
name: "generics with any used as param type",
inputInterface: "./tests.genericIn",
outputFile: "./tests/generic_in.go",
expectedOutputFile: "./tests/generic_in.go",
},
{
name: "generics with specific type used as a generic constraint",
inputInterface: "./tests.genericSpecific",
outputFile: "./tests/generic_specific.go",
expectedOutputFile: "./tests/generic_specific.go",
},
{
name: "generics with simple union used as a generic constraint",
inputInterface: "./tests.genericSimpleUnion",
outputFile: "./tests/generic_simple_union.go",
expectedOutputFile: "./tests/generic_simple_union.go",
},
{
name: "generics with complex union used as a generic constraint",
inputInterface: "./tests.genericComplexUnion",
outputFile: "./tests/generic_complex_union.go",
expectedOutputFile: "./tests/generic_complex_union.go",
},
{
name: "generics with complex inline union used as a generic constraint",
inputInterface: "./tests.genericInlineUnion",
outputFile: "./tests/generic_inline_union.go",
expectedOutputFile: "./tests/generic_inline_union.go",
},
}

for _, testCase := range testCases {
testCase := testCase

t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

mustRunMinimockWithParams(t, testCase.inputInterface, testCase.outputFile)
snapshotter.SnapshotT(t, mustReadFile(t, testCase.expectedOutputFile))
})
}
}
Loading

0 comments on commit ad7b506

Please sign in to comment.