Skip to content

Commit

Permalink
support print() in lua script
Browse files Browse the repository at this point in the history
  • Loading branch information
vearne committed Oct 14, 2024
1 parent 365f83a commit 5e67df6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/vearne/autotest

go 1.21.0
go 1.22

require (
github.com/antchfx/jsonquery v1.3.4
Expand Down
4 changes: 2 additions & 2 deletions internal/command/http_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ func (m *HttpTestCallable) Call(ctx context.Context) *executor.GPResult {
}

// 3. render
zaplog.Info("before render()", zap.Uint64("testCaseId", m.testcase.ID),
zaplog.Debug("before render()", zap.Uint64("testCaseId", m.testcase.ID),
zap.Any("request", m.testcase.Request))
req, err := renderRequestHttp(m.testcase.Request)
tcResult.Request = req
zaplog.Info("after render()", zap.Uint64("testCaseId", m.testcase.ID),
zaplog.Debug("after render()", zap.Uint64("testCaseId", m.testcase.ID),
zap.Any("request", tcResult.Request))
if err != nil {
tcResult.State = model.StateFailed
Expand Down
16 changes: 16 additions & 0 deletions internal/rule/init.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package rule

import (
"fmt"
lua "github.com/yuin/gopher-lua"
luajson "layeh.com/gopher-json"
"os"
"sync"
)

Expand All @@ -17,8 +19,22 @@ var LuaVMLock sync.Mutex
func init() {
L = lua.NewState()
//defer L.Close()
// 设置自定义的 print 函数
L.SetGlobal("print", L.NewFunction(customPrint))
// register json lib
luajson.Preload(L)
registerHttpRespType(L)
registerGrocRespType(L)
}

func customPrint(L *lua.LState) int {
top := L.GetTop()
for i := 1; i <= top; i++ {
fmt.Fprint(os.Stdout, L.ToString(i)) // 输出到 os.Stdout
if i != top {
fmt.Fprint(os.Stdout, "\t")
}
}
fmt.Fprintln(os.Stdout) // 换行
return 0
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
version = "v0.1.3"
version = "v0.1.4"
)

func main() {
Expand Down

0 comments on commit 5e67df6

Please sign in to comment.