From e0fff32b787ec10d8c27b655f546a8a43e090b61 Mon Sep 17 00:00:00 2001 From: xhd2015 Date: Wed, 3 Apr 2024 20:35:47 +0800 Subject: [PATCH] fix nil arguments when patching --- cmd/xgo/version.go | 6 +++--- runtime/core/version.go | 6 +++--- runtime/mock/patch.go | 7 +++++-- runtime/test/patch/patch_test.go | 21 +++++++++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/cmd/xgo/version.go b/cmd/xgo/version.go index 0fddfe2f..50fc367d 100644 --- a/cmd/xgo/version.go +++ b/cmd/xgo/version.go @@ -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) diff --git a/runtime/core/version.go b/runtime/core/version.go index 2d0608a6..b9c7d40f 100644 --- a/runtime/core/version.go +++ b/runtime/core/version.go @@ -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 = "" diff --git a/runtime/mock/patch.go b/runtime/mock/patch.go index 7776fd56..6b866ec8 100644 --- a/runtime/mock/patch.go +++ b/runtime/mock/patch.go @@ -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++ } @@ -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 diff --git a/runtime/test/patch/patch_test.go b/runtime/test/patch/patch_test.go index b73c4d4b..cd87c861 100644 --- a/runtime/test/patch/patch_test.go +++ b/runtime/test/patch/patch_test.go @@ -1,6 +1,7 @@ package patch import ( + "context" "strings" "testing" @@ -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") +}