diff --git a/cmd/xgo/option.go b/cmd/xgo/option.go index 3bb9e4c2..ea80da74 100644 --- a/cmd/xgo/option.go +++ b/cmd/xgo/option.go @@ -37,7 +37,9 @@ type options struct { // --options-from-file file optionsFromFile string - // rules from command line will take higher priorities + // --mock-rule: rules from command line + // amend the --options-from-file. + // it will take higher priority mockRules []string // dev only diff --git a/cmd/xgo/rule.go b/cmd/xgo/rule.go index 4f75093f..2d557b24 100644 --- a/cmd/xgo/rule.go +++ b/cmd/xgo/rule.go @@ -20,6 +20,7 @@ type Rule struct { MainModule *bool `json:"main_module"` Generic *bool `json:"generic"` Exported *bool `json:"exported"` + Closure *bool `json:"closure"` Action string `json:"action"` // include,exclude or empty } diff --git a/cmd/xgo/version.go b/cmd/xgo/version.go index 7a837d1e..7c25dda8 100644 --- a/cmd/xgo/version.go +++ b/cmd/xgo/version.go @@ -4,8 +4,8 @@ import "fmt" // auto updated const VERSION = "1.0.45" -const REVISION = "377e54a28f85da721ce413317ca99a91fc55626d+1" -const NUMBER = 292 +const REVISION = "dda3d723508dbe0490317e7af9ffba15de82b778+1" +const NUMBER = 293 // manually updated const CORE_VERSION = "1.0.43" diff --git a/patch/match/match.go b/patch/match/match.go index 8cf3138e..d952826b 100644 --- a/patch/match/match.go +++ b/patch/match/match.go @@ -16,6 +16,7 @@ type Rule struct { MainModule *bool `json:"main_module"` Generic *bool `json:"generic"` Exported *bool `json:"exported"` + Closure *bool `json:"closure"` Action string `json:"action"` // include,exclude or empty kinds []string @@ -106,6 +107,12 @@ func Match(rule *Rule, pkgPath string, isMainModule bool, funcDecl *info.DeclInf return false } } + if rule.Closure != nil { + hasAnyCondition = true + if *rule.Closure != funcDecl.Closure { + return false + } + } if hasAnyCondition { return true } diff --git a/runtime/test/mock/rule/rule_test.go b/runtime/test/mock/rule/rule_test.go new file mode 100644 index 00000000..9040efb4 --- /dev/null +++ b/runtime/test/mock/rule/rule_test.go @@ -0,0 +1,45 @@ +package rule + +import ( + "context" + "fmt" + "strings" + "testing" + + "github.com/xhd2015/xgo/runtime/core" + "github.com/xhd2015/xgo/runtime/trap" +) + +func getClosure() func() { + return func() { + + } +} +func TestClosureDefaultMock(t *testing.T) { + testClosureDefaultMock(t, "call getClosure\ncall getClosure.func1\n") +} + +// flags: --mock-rule '{"closure":true,"action":"exclude"}' +func TestClosureWithMockRuleNoMock(t *testing.T) { + testClosureDefaultMock(t, "call getClosure\n") +} + +func testClosureDefaultMock(t *testing.T, expect string) { + var buf strings.Builder + trap.AddInterceptor(&trap.Interceptor{ + Pre: func(ctx context.Context, f *core.FuncInfo, args, result core.Object) (data interface{}, err error) { + if f.Stdlib { + return + } + buf.WriteString(fmt.Sprintf("call %s\n", f.IdentityName)) + return + }, + }) + closure := getClosure() + closure() + + got := buf.String() + if got != expect { + t.Fatalf("interceptor expect: %q, actual: %q", expect, got) + } +} diff --git a/script/run-test/main.go b/script/run-test/main.go index 22c3d542..b8e4bac1 100644 --- a/script/run-test/main.go +++ b/script/run-test/main.go @@ -144,6 +144,16 @@ var extraSubTests = []*TestCase{ dir: "runtime/test/recover_no_trap", flags: []string{"--trap-stdlib"}, }, + { + name: "mock_rule_not_set", + dir: "runtime/test/mock/rule", + flags: []string{"-run", "TestClosureDefaultMock"}, + }, + { + name: "mock_rule_set", + dir: "runtime/test/mock/rule", + flags: []string{"-run", "TestClosureWithMockRuleNoMock", "--mock-rule", `{"closure":true,"action":"exclude"}`}, + }, { name: "xgo_integration", usePlainGo: true,