Skip to content

Commit

Permalink
refactor: basic test concepts to introduce test timeout (#93)
Browse files Browse the repository at this point in the history
Signed-off-by: Tronje Krop <[email protected]>
  • Loading branch information
Tronje Krop committed Oct 16, 2024
1 parent 9f2193c commit 6968c1f
Show file tree
Hide file tree
Showing 27 changed files with 1,670 additions and 1,451 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ require (
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
go.uber.org/mock v0.4.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand All @@ -48,6 +53,9 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand All @@ -65,5 +73,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
20 changes: 15 additions & 5 deletions internal/mock/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ const (
dirTop = "../../.."
dirMock = "mock"

dirTest = "./test"
dirSubTest = "./test"
dirOther = "../other"
dirUnknown = "../unknown"
dirTesting = "../../test"
dirTest = "../../test"

pathMock = "github.com/tkrop/go-testing/internal/mock"
pathTest = "github.com/tkrop/go-testing/internal/mock/test"
Expand All @@ -43,7 +43,7 @@ const (
fileOther = "mock_other_test.go"
fileTemplate = "mock_template_test.go"
fileUnknown = "unnkown_test.go"
fileTesting = "testing.go"
fileContext = "context.go"

aliasMock = "mock_" + pkgTest
aliasInt = "internal_" + aliasMock
Expand All @@ -60,6 +60,8 @@ const (
var (
errAny = errors.New("any error")

absUnknown, _ = filepath.Abs(dirUnknown)

nameIFace = &Type{Name: iface}
nameIFaceMock = &Type{Name: ifaceMock}

Expand Down Expand Up @@ -126,7 +128,7 @@ func methodsMockIFaceFunc(mocktest, test, mock string) []*Method {
}, {
Name: "CallC",
Params: []*Param{{
Name: "test", Type: aliasType(test, "Tester"),
Name: "test", Type: aliasType(test, "Context"),
}},
Results: []*Param{},
Variadic: false,
Expand All @@ -136,7 +138,7 @@ func methodsMockIFaceFunc(mocktest, test, mock string) []*Method {
var (
// Use two different singleton loaders.
loaderMock = NewLoader(DirDefault)
loaderTest = NewLoader(dirTest)
loaderTest = NewLoader(dirSubTest)
loaderFail = NewLoader(dirUnknown)

// Use singleton template for testing.
Expand Down Expand Up @@ -170,6 +172,14 @@ var (
pathTest, pathTesting, pathMock)

methodsTestTest = []*Method{{
Name: "Deadline",
Params: []*Param{},
Results: []*Param{
{Name: "deadline", Type: "time.Time"},
{Name: "ok", Type: "bool"},
},
Variadic: false,
}, {
Name: "Errorf",
Params: []*Param{
{Name: "format", Type: "string"},
Expand Down
12 changes: 9 additions & 3 deletions internal/mock/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/tools/go/packages"

"github.com/tkrop/go-testing/test"

Expand All @@ -36,7 +37,7 @@ var (
return dir
}()

fileFailure = filepath.Join(testDirGenerate, dirTest, fileUnknown)
fileFailure = filepath.Join(testDirGenerate, dirSubTest, fileUnknown)
)

var testGenerateParams = map[string]GenerateParams{
Expand All @@ -49,8 +50,13 @@ var testGenerateParams = map[string]GenerateParams{
"failure parsing": {
file: filepath.Join(testDirGenerate, MockFileDefault),
args: []string{pathUnknown},
expectStderr: "argument invalid [pos: 3, arg: " + pathUnknown +
"]: not found\n",
expectStderr: NewErrArgFailure(3, ".",
NewErrPackageParsing(pathUnknown, []*packages.Package{
{Errors: []packages.Error{{
Msg: "no required module provides package " + pathUnknown +
"; to add it:\n\tgo get " + pathUnknown,
}}},
})).Error() + "\n",
expectCode: 1,
},

Expand Down
4 changes: 2 additions & 2 deletions internal/mock/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ var testLoaderLoadParams = map[string]LoaderLoadParams{
"failure loading": {
loader: loaderFail,
source: targetTest.With(&Type{
File: filepath.Join(dirUp, dirMock, dirTest, fileIFace),
File: filepath.Join(dirUp, dirMock, dirSubTest, fileIFace),
}),
expectError: NewErrLoading(pathTest, fmt.Errorf(
"err: chdir %s: no such file or directory: stderr: ",
Expand Down Expand Up @@ -315,7 +315,7 @@ var testLoaderIFacesParams = map[string]LoaderIFacesParams{
"failure loading": {
loader: loaderFail,
source: targetTest.With(&Type{
File: filepath.Join(dirUp, dirMock, dirTest, fileIFace),
File: filepath.Join(dirUp, dirMock, dirSubTest, fileIFace),
}),
expectError: NewErrLoading(pathTest, fmt.Errorf(
"err: chdir %s: no such file or directory: stderr: ",
Expand Down
4 changes: 2 additions & 2 deletions internal/mock/mock_iface_test.gox
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ func (mr *MockIFaceRecorder) CallB() *gomock.Call {
}

// CallC is the mock method to capture a coresponding call.
func (m *MockIFace) CallC(test testing_test.Tester) {
func (m *MockIFace) CallC(test testing_test.Context) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "CallC", test)
}

// CallC is the recorder method to indicates an expected call.
func (mr *MockIFaceRecorder) CallC(test testing_test.Tester) *gomock.Call {
func (mr *MockIFaceRecorder) CallC(test testing_test.Context) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CallC",
reflect.TypeOf((*MockIFace)(nil).CallC), test)
Expand Down
25 changes: 8 additions & 17 deletions internal/mock/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func (parser *Parser) Parse(args ...string) ([]*Mock, []error) {
// value and returns the argument type with the remaining argument value.
func (parser *Parser) argType(arg string) (argType, string) {
if strings.Index(arg, "--") == 0 {
if !strings.Contains(arg, "=") {
return argTypeUnknown, arg
}
return parser.argTypeParse(arg)
}
return parser.argTypeGuess(arg)
Expand Down Expand Up @@ -217,26 +220,14 @@ func (parser *Parser) argTypeGuess(arg string) (argType, string) {
}
}

// TODO: Reconsider the early failure handling approach here.
//
// This early failure handing may not be necessary and undesired. It has
// the drawback to prevents generating mocks for broken code, as well as
// for partially loaded packages defined by incomplete sets of files.
//
// It looks not to complicated to drop the early failure handling and
// change the four effected tests. The alternative solution to drop support
// for partial package loading via single file loading or automatically
// load the whole package is not very desirable and complicates code.
//
// pkgs, _ := parser.loader.Load(arg).Get()
// if len(pkgs) > 0 {
pkgs, err := parser.loader.Load(arg).Get()
if len(pkgs) > 0 && err == nil {
pkgs, _ := parser.loader.Load(arg).Get()
if len(pkgs) > 0 {
if pkgs[0].PkgPath == ReadFromFile {
return argTypeSourceFile, arg
}
return argTypeSourcePath, arg
}
// #no-cover: impossible to reach this code?
return argTypeNotFound, arg
}

Expand Down Expand Up @@ -270,7 +261,7 @@ func (state *ParseState) ensureSource() *ParseState {
// interfaces in the source package.
func (state *ParseState) ensureIFace(pos int) {
if state.source.IsPartial() {
state.creatMocks(pos, "")
state.creatMocks(pos, ".")
}
}

Expand All @@ -282,7 +273,7 @@ func (state *ParseState) ensureState(arg string) {
state.source.Update(state.loader)
state.target.Update(state.loader)

if arg == "" {
if arg == "." {
state.source.Name = MatchPatternDefault
state.target.Name = MockPatternDefault
return
Expand Down
59 changes: 41 additions & 18 deletions internal/mock/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"
. "github.com/tkrop/go-testing/internal/mock"
"github.com/tkrop/go-testing/test"
"golang.org/x/tools/go/packages"
)

var (
Expand All @@ -34,11 +35,17 @@ var testParseParams = map[string]ParseParams{
Methods: methodsLoadIFace,
}},
},
"invalid argument": {
"invalid argument flag": {
loader: loaderTest,
args: []string{"--test"},
expectError: []error{NewErrArgInvalid(0, "--test")},
},
"invalid argument unknown": {
loader: loaderTest,
args: []string{"--unknown=any"},
expectError: []error{NewErrArgInvalid(0, "--unknown=any")},
},
// TODO: add test case for invalid argument for guessed type not found.

"default file": {
loader: loaderTest,
Expand Down Expand Up @@ -140,13 +147,19 @@ var testParseParams = map[string]ParseParams{
loader: loaderMock,
args: []string{pathUnknown},
expectError: []error{
NewErrArgNotFound(0, pathUnknown),
NewErrArgFailure(0, ".",
NewErrPackageParsing(absUnknown, []*packages.Package{{
Errors: []packages.Error{{
Msg: "stat " + absUnknown + ": directory not found",
}},
}}),
),
},
},

"source file explicit": {
loader: loaderMock,
args: []string{"--source-file=" + dirTest + "/" + fileIFace},
args: []string{"--source-file=" + dirSubTest + "/" + fileIFace},
expectMocks: []*Mock{{
Source: sourceIFaceAny,
Target: targetMockTestIFace,
Expand All @@ -155,7 +168,7 @@ var testParseParams = map[string]ParseParams{
},
"source file derived": {
loader: loaderMock,
args: []string{"--source=" + dirTest + "/" + fileIFace},
args: []string{"--source=" + dirSubTest + "/" + fileIFace},
expectMocks: []*Mock{{
Source: sourceIFaceAny,
Target: targetMockTestIFace,
Expand All @@ -174,7 +187,7 @@ var testParseParams = map[string]ParseParams{
},
"source file guessed": {
loader: loaderMock,
args: []string{dirTest + "/" + fileIFace},
args: []string{dirSubTest + "/" + fileIFace},
expectMocks: []*Mock{{
Source: sourceIFaceAny,
Target: targetMockTestIFace,
Expand All @@ -185,13 +198,20 @@ var testParseParams = map[string]ParseParams{
loader: loaderMock,
args: []string{fileUnknown},
expectError: []error{
NewErrArgNotFound(0, fileUnknown),
NewErrArgFailure(0, ".",
NewErrPackageParsing(fileUnknown, []*packages.Package{{
Errors: []packages.Error{{
Msg: "no required module provides package " + fileUnknown +
"; to add it:\n\tgo get " + fileUnknown,
}},
}}),
),
},
},

"source directory explicit": {
loader: loaderMock,
args: []string{"--source-file=" + dirTest},
args: []string{"--source-file=" + dirSubTest},
expectMocks: []*Mock{{
Source: sourceIFaceAny,
Target: targetMockTestIFace,
Expand All @@ -200,7 +220,7 @@ var testParseParams = map[string]ParseParams{
},
"source directory derived": {
loader: loaderMock,
args: []string{"--source=" + dirTest},
args: []string{"--source=" + dirSubTest},
expectMocks: []*Mock{{
Source: sourceIFaceAny,
Target: targetMockTestIFace,
Expand All @@ -209,7 +229,7 @@ var testParseParams = map[string]ParseParams{
},
"source directory guessed": {
loader: loaderMock,
args: []string{dirTest},
args: []string{dirSubTest},
expectMocks: []*Mock{{
Source: sourceIFaceAny,
Target: targetMockTestIFace,
Expand All @@ -220,7 +240,13 @@ var testParseParams = map[string]ParseParams{
loader: loaderMock,
args: []string{dirUnknown},
expectError: []error{
NewErrArgNotFound(0, dirUnknown),
NewErrArgFailure(0, ".",
NewErrPackageParsing(dirUnknown, []*packages.Package{{
Errors: []packages.Error{{
Msg: "stat " + absUnknown + ": directory not found",
}},
}}),
),
},
},

Expand Down Expand Up @@ -457,10 +483,10 @@ var testParseParams = map[string]ParseParams{
"failure loading": {
loader: loaderFail,
args: []string{
"--source-file=" + filepath.Join(dirUp, dirMock, dirTest, fileIFace),
"--source-file=" + filepath.Join(dirUp, dirMock, dirSubTest, fileIFace),
},
expectError: []error{
NewErrArgFailure(0, "", NewErrLoading("", fmt.Errorf(
NewErrArgFailure(0, ".", NewErrLoading("", fmt.Errorf(
"err: chdir %s: no such file or directory: stderr: ",
dirUnknown))),
},
Expand Down Expand Up @@ -488,7 +514,7 @@ var testParseAddParams = map[string]ParseParams{
"package test path": {
loader: loaderMock,
args: []string{
dirTesting, "Test",
dirTest, "Test",
"--target=" + pkgMock, "Reporter=Reporter",
},
expectMocks: []*Mock{{
Expand All @@ -505,7 +531,7 @@ var testParseAddParams = map[string]ParseParams{
"package test file": {
loader: loaderMock,
args: []string{
dirTesting + "/" + fileTesting, "Test",
dirTest + "/" + fileContext, "Test",
"--target=" + pkgMock, "Reporter=Reporter",
},
expectMocks: []*Mock{{
Expand Down Expand Up @@ -569,8 +595,5 @@ func TestParseMain(t *testing.T) {
}

func TestParseAdd(t *testing.T) {
test.Map(t, testParseAddParams).
// TODO: removed when Find feature migration implemented.
// Filter("package-test-file", true).
Run(testParse)
test.Map(t, testParseAddParams).Run(testParse)
}
Loading

0 comments on commit 6968c1f

Please sign in to comment.