Skip to content

Commit

Permalink
allow skip trapping closure
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Jul 11, 2024
1 parent dda3d72 commit ab32ad1
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
4 changes: 3 additions & 1 deletion cmd/xgo/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/xgo/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions patch/match/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
45 changes: 45 additions & 0 deletions runtime/test/mock/rule/rule_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
10 changes: 10 additions & 0 deletions script/run-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ab32ad1

Please sign in to comment.