Skip to content

Commit

Permalink
feat: improve test runner and accessor (#91)
Browse files Browse the repository at this point in the history
Signed-off-by: Tronje Krop <[email protected]>
  • Loading branch information
Tronje Krop authored Oct 6, 2024
1 parent 723c17e commit b05baaf
Show file tree
Hide file tree
Showing 16 changed files with 1,172 additions and 351 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.23.1
go-version: 1.23.2

- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -32,7 +32,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.23.1
go-version: 1.23.2

- name: Checkout code
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.20
0.0.21
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module github.com/tkrop/go-testing

go 1.23.1
go 1.23.2

require (
github.com/golang/mock v1.6.0
github.com/h2non/gock v1.2.0
github.com/huandu/go-clone v1.6.0
github.com/stretchr/testify v1.9.0
golang.org/x/text v0.13.0
golang.org/x/tools v0.25.0
golang.org/x/tools v0.26.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
2 changes: 1 addition & 1 deletion internal/mock/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (loader *CachedLoader) byPath(path string) *PkgResponse {
// file path targeting a directory. If the path is relative or targeting a
// file, the package is resolved but will not provide the package repository
// path nor the standardized package name by default. The method compensates
// this partially be calculating default package names on a best effort basis.
// this partially by calculating default package names on a best effort basis.
func (loader *CachedLoader) load(path string) *PkgResponse {
pkgs, err := packages.Load(loader.Config, path)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions internal/mock/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ 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 {
if pkgs[0].PkgPath == ReadFromFile {
Expand Down
40 changes: 20 additions & 20 deletions internal/mock/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"

. "github.com/tkrop/go-testing/internal/mock"
"github.com/tkrop/go-testing/test"
)
Expand All @@ -25,18 +24,6 @@ type ParseParams struct {
expectError []error
}

func testParse(t test.Test, param ParseParams) {
// Given
parser := NewParser(param.loader, param.target)

// When
mocks, errs := parser.Parse(param.args...)

// Then
assert.Equal(t, param.expectError, errs)
assert.Equal(t, param.expectMocks, mocks)
}

var testParseParams = map[string]ParseParams{
"no argument": {
loader: loaderTest,
Expand Down Expand Up @@ -480,12 +467,6 @@ var testParseParams = map[string]ParseParams{
},
}

// var testParseXParams = map[string]ParseParams{}

func TestParseMain(t *testing.T) {
test.Map(t, testParseParams).Run(testParse)
}

var testParseAddParams = map[string]ParseParams{
"package test": {
loader: loaderMock,
Expand Down Expand Up @@ -571,6 +552,25 @@ var testParseAddParams = map[string]ParseParams{
},
}

func testParse(t test.Test, param ParseParams) {
// Given
parser := NewParser(param.loader, param.target)

// When
mocks, errs := parser.Parse(param.args...)

// Then
assert.Equal(t, param.expectError, errs)
assert.Equal(t, param.expectMocks, mocks)
}

func TestParseMain(t *testing.T) {
test.Map(t, testParseParams).Run(testParse)
}

func TestParseAdd(t *testing.T) {
test.Map(t, testParseAddParams).Run(testParse)
test.Map(t, testParseAddParams).
// TODO: removed when Find feature migration implemented.
// Filter("package-test-file", true).
Run(testParse)
}
16 changes: 10 additions & 6 deletions internal/reflect/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"reflect"
"slices"
"unsafe"
)

Expand Down Expand Up @@ -34,7 +35,10 @@ var (

// FindArgOf find the first argument with one of the given field names matching
// the type matching the default argument type.
func FindArgOf[P any](param P, deflt any, names ...string) any {
//
// TODO: This function does not belong here and is tested insufficiently.
// TODO: This function may fail on pointer types.
func FindArgOf(param any, deflt any, names ...string) any {
t := reflect.TypeOf(param)
dt := reflect.TypeOf(deflt)
if t.Kind() != reflect.Struct {
Expand All @@ -50,10 +54,8 @@ func FindArgOf[P any](param P, deflt any, names ...string) any {
for i := 0; i < t.NumField(); i++ {
fv := v.Field(i)
if fv.Type().Kind() == dt.Kind() {
for _, name := range names {
if t.Field(i).Name == name {
return FieldArgOf(v, i)
}
if slices.Contains(names, t.Field(i).Name) {
return FieldArgOf(v, i)
}
if !found {
deflt = FieldArgOf(v, i)
Expand All @@ -65,6 +67,8 @@ func FindArgOf[P any](param P, deflt any, names ...string) any {
}

// FieldArgOf returns the argument of the `i`th field of the given value.
//
// TODO: This function does not belong here and is tested insufficiently.
func FieldArgOf(v reflect.Value, i int) any {
vf := v.Field(i)
if vf.CanInterface() {
Expand All @@ -80,7 +84,7 @@ func FieldArgOf(v reflect.Value, i int) any {
// #nosec G103 G115 -- is necessary.
rf := reflect.NewAt(vf.Type(),
unsafe.Pointer(vf.UnsafeAddr())).Elem()

// Create a new variable of the type P.
var value any
reflect.ValueOf(&value).Elem().Set(rf)
return value
Expand Down
Loading

0 comments on commit b05baaf

Please sign in to comment.