Skip to content

Commit

Permalink
fix nil arguments when patching
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Apr 3, 2024
1 parent d7fa92e commit e0fff32
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
6 changes: 3 additions & 3 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import "fmt"

const VERSION = "1.0.17"
const REVISION = "77848295e3d73a3eba8ce2bbd1b95d8c988929d5+1"
const NUMBER = 156
const VERSION = "1.0.18"
const REVISION = "d7fa92e870f1be98c43f42107ad24f4a3152ef5a+1"
const NUMBER = 157

func getRevision() string {
return fmt.Sprintf("%s %s BUILD_%d", VERSION, REVISION, NUMBER)
Expand Down
6 changes: 3 additions & 3 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os"
)

const VERSION = "1.0.17"
const REVISION = "77848295e3d73a3eba8ce2bbd1b95d8c988929d5+1"
const NUMBER = 156
const VERSION = "1.0.18"
const REVISION = "d7fa92e870f1be98c43f42107ad24f4a3152ef5a+1"
const NUMBER = 157

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
7 changes: 5 additions & 2 deletions runtime/mock/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func buildInterceptorFromPatch(recvPtr interface{}, replacer interface{}) func(c
} else {
// set receiver
if nIn > 0 {
callArgs[dst] = reflect.ValueOf(args.GetFieldIndex(0).Value())
callArgs[dst] = reflect.ValueOf(args.GetFieldIndex(0).Ptr()).Elem()
dst++
src++
}
Expand All @@ -111,7 +111,10 @@ func buildInterceptorFromPatch(recvPtr interface{}, replacer interface{}) func(c
dst++
}
for i := 0; i < nIn-dst; i++ {
callArgs[dst+i] = reflect.ValueOf(args.GetFieldIndex(src + i).Value())
// fail if with the following setup:
// reflect: Call using zero Value argument
// callArgs[dst+i] = reflect.ValueOf(args.GetFieldIndex(src + i).Value())
callArgs[dst+i] = reflect.ValueOf(args.GetFieldIndex(src + i).Ptr()).Elem()
}

// call the function
Expand Down
21 changes: 21 additions & 0 deletions runtime/test/patch/patch_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package patch

import (
"context"
"strings"
"testing"

Expand Down Expand Up @@ -58,3 +59,23 @@ func TestPatchMethod(t *testing.T) {
t.Fatalf("expect patched result to be %q, actual: %q", "mock world", res)
}
}

func TestPatchNilArg(t *testing.T) {
var haveCalledMock bool
var argCtx context.Context
mock.Patch(nilCtx, func(a int, ctx context.Context) {
argCtx = ctx
haveCalledMock = true
})
nilCtx(0, nil)
if !haveCalledMock {
t.Fatalf("expect have called mock,actually not")
}
if argCtx != nil {
t.Fatalf("expect arg ctx to be nil, actual: %v", argCtx)
}
}

func nilCtx(a int, ctx context.Context) {
panic("nilCtx should be mocked")
}

0 comments on commit e0fff32

Please sign in to comment.