Skip to content

Commit 3af45b8

Browse files
committed
lua plugins test
1 parent 6ecb088 commit 3af45b8

File tree

12 files changed

+251
-108
lines changed

12 files changed

+251
-108
lines changed

internal/luapi/lua_global/conda.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,24 +155,32 @@ func SearchVersions(sdkName string) (result map[string][]string) {
155155

156156
func SearchByConda(L *lua.LState) int {
157157
ud := L.ToUserData(1)
158+
158159
if ud == nil {
159-
return 0
160+
r := L.NewUserData()
161+
r.Value = nil
162+
L.Push(r)
163+
return 1
160164
}
161165
vl, ok := ud.Value.(VersionList)
162166

163167
if !ok || vl == nil {
164-
return 0
168+
r := L.NewUserData()
169+
r.Value = nil
170+
L.Push(r)
171+
return 1
165172
}
166173

167174
sdkName := L.ToString(2)
168175
if sdkName == "" {
169-
return 0
176+
r := L.NewUserData()
177+
r.Value = nil
178+
L.Push(r)
179+
return 1
170180
}
171181

172182
versions := SearchVersions(sdkName)
173183

174-
result := VersionList{}
175-
176184
for platform, versionList := range versions {
177185
pList := strings.Split(platform, "/")
178186
for _, vv := range versionList {
@@ -184,15 +192,15 @@ func SearchByConda(L *lua.LState) int {
184192
Arch: pList[1],
185193
Installer: "conda",
186194
}
187-
if _, ok := result[vv]; !ok {
188-
result[vv] = SDKVersion{}
195+
if _, ok := vl[vv]; !ok {
196+
vl[vv] = SDKVersion{}
189197
}
190-
result[vv] = append(result[vv], item)
198+
vl[vv] = append(vl[vv], item)
191199
}
192200
}
193201

194202
r := L.NewUserData()
195-
r.Value = result
203+
r.Value = vl
196204
L.Push(r)
197205
return 1
198206
}

internal/luapi/lua_global/conda_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package lua_global
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/gvcgo/version-manager/internal/utils"
78
"github.com/stretchr/testify/assert"
9+
lua "github.com/yuin/gopher-lua"
810
)
911

1012
func ExecuteLuaScript(script string) error {
@@ -19,13 +21,26 @@ func TestConda(t *testing.T) {
1921
return
2022
}
2123

22-
var condaScript = `print("-----------------conda-------------------")
23-
local vl = newVersionList()
24-
local result = vmrSearchByConda(vl, "php")
25-
print(result)
24+
var condaScript = `
25+
vl = vmrNewVersionList()
26+
vl = vmrSearchByConda(vl, "php")
27+
print(vl)
2628
`
27-
if err := ExecuteLuaScript(condaScript); err != nil {
29+
if l, err := ExecuteLuaScriptL(condaScript); err != nil {
2830
t.Error(err)
31+
} else {
32+
defer l.Close()
33+
v := l.GetGlobal("vl")
34+
35+
if v.Type() == lua.LTUserData {
36+
ud := v.(*lua.LUserData)
37+
if ud == nil {
38+
return
39+
}
40+
if vl, ok := ud.Value.(VersionList); ok {
41+
fmt.Println("versionList: ", vl)
42+
}
43+
}
2944
}
3045
}
3146

internal/luapi/lua_global/github.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import (
1010
)
1111

1212
func githubBoolFuncCall(L *lua.LState, cb *lua.LFunction, arg string) bool {
13+
defer func() {
14+
if err := recover(); err != nil {
15+
L.Push(lua.LBool(false))
16+
}
17+
}()
1318
if err := L.CallByParam(lua.P{
1419
Fn: cb,
1520
NRet: 1,
@@ -25,6 +30,11 @@ func githubBoolFuncCall(L *lua.LState, cb *lua.LFunction, arg string) bool {
2530
}
2631

2732
func githubStringFuncCall(L *lua.LState, cb *lua.LFunction, arg string) string {
33+
defer func() {
34+
if err := recover(); err != nil {
35+
L.Push(lua.LString(""))
36+
}
37+
}()
2838
if err := L.CallByParam(lua.P{
2939
Fn: cb,
3040
NRet: 1,
@@ -39,10 +49,20 @@ func githubStringFuncCall(L *lua.LState, cb *lua.LFunction, arg string) string {
3949
return ""
4050
}
4151

52+
/*
53+
lua:
54+
vl = vmrNewVersionList()
55+
vl = vmrGetGithubRelease(repoNameStr, tagFilterFunc, versionParserFunc, fileFilterFunc, archParserFunc, osParserFunc, installerGetterFunc)
56+
*/
4257
func GetGithubRelease(L *lua.LState) int {
4358
repoName := L.ToString(1)
59+
60+
result := VersionList{}
4461
if repoName == "" {
45-
return 0
62+
ud := L.NewUserData()
63+
ud.Value = result
64+
L.Push(ud)
65+
return 1
4666
}
4767
cfg := cnf.NewVMRConf()
4868
cfg.Load()
@@ -61,8 +81,6 @@ func GetGithubRelease(L *lua.LState) int {
6181
osParser := L.ToFunction(6)
6282
installerGetter := L.ToFunction(7)
6383

64-
result := make(VersionList)
65-
6684
for _, rItem := range rl {
6785
if !githubBoolFuncCall(L, tagFilter, rItem.TagName) {
6886
continue
@@ -98,7 +116,6 @@ func GetGithubRelease(L *lua.LState) int {
98116
}
99117
}
100118

101-
// fmt.Println("******$$$$$$$", result["1.2.2"])
102119
ud := L.NewUserData()
103120
ud.Value = result
104121
L.Push(ud)
Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,64 @@
11
package lua_global
22

3-
import "testing"
3+
import (
4+
"fmt"
5+
"testing"
46

5-
var githubScript = `print("------------github------------")
7+
lua "github.com/yuin/gopher-lua"
8+
)
69

10+
var githubScript = `
711
function tagFilter(str)
8-
local s = regexpFindString("v\\d+(.\\d+){2}", str)
12+
local s = vmrRegexpFindString("v\\d+(.\\d+){2}", str)
913
if s ~= "" then
1014
return true
1115
end
1216
return false
1317
end
1418
1519
function versionParser(str)
16-
local s = regexpFindString("v(\\d+)(.\\d+){2}", str)
17-
s = trimPrefix(s, "v")
20+
local s = vmrRegexpFindString("v(\\d+)(.\\d+){2}", str)
21+
s = vmrTrimPrefix(s, "v")
1822
return s
1923
end
2024
2125
function fileFilter(str)
22-
if contains(str, "profile") then
26+
if vmrContains(str, "profile") then
2327
return false
2428
end
25-
if contains(str, "baseline") then
29+
if vmrContains(str, "baseline") then
2630
return false
2731
end
28-
if hasSuffix(str, ".txt") then
32+
if vmrHasSuffix(str, ".txt") then
2933
return false
3034
end
31-
if hasSuffix(str, ".txt.asc") then
35+
if vmrHasSuffix(str, ".txt.asc") then
3236
return false
3337
end
34-
if hasSuffix(str, "musl.zip") then
38+
if vmrHasSuffix(str, "musl.zip") then
3539
return false
3640
end
3741
return true
3842
end
3943
4044
function archParser(str)
41-
if contains(str, "-x64") then
45+
if vmrContains(str, "-x64") then
4246
return "amd64"
4347
end
44-
if contains(str, "-aarch64") then
48+
if vmrContains(str, "-aarch64") then
4549
return "arm64"
4650
end
4751
return ""
4852
end
4953
5054
function osParser(str)
51-
if contains(str, "linux") then
55+
if vmrContains(str, "linux") then
5256
return "linux"
5357
end
54-
if contains(str, "darwin") then
58+
if vmrContains(str, "darwin") then
5559
return "darwin"
5660
end
57-
if contains(str, "windows") then
61+
if vmrContains(str, "windows") then
5862
return "windows"
5963
end
6064
return ""
@@ -64,16 +68,25 @@ function installerGetter(str)
6468
return "unarchiver"
6569
end
6670
67-
local result = getGithubRelease("oven-sh/bun", tagFilter, versionParser, fileFilter, archParser, osParser, installerGetter)
71+
result = vmrGetGithubRelease("oven-sh/bun", tagFilter, versionParser, fileFilter, archParser, osParser, installerGetter)
6872
print(result)
6973
`
7074

7175
func TestGithub(t *testing.T) {
72-
ll := NewLua()
73-
defer ll.Close()
74-
L := ll.GetLState()
75-
76-
if err := L.DoString(githubScript); err != nil {
76+
l, err := ExecuteLuaScriptL(githubScript)
77+
if err != nil {
7778
t.Error(err)
79+
} else {
80+
v := l.GetGlobal("result")
81+
82+
if v.Type() == lua.LTUserData {
83+
ud := v.(*lua.LUserData)
84+
if ud == nil {
85+
return
86+
}
87+
if vl, ok := ud.Value.(VersionList); ok {
88+
_, _ = fmt.Printf("version list: %+v\n", vl)
89+
}
90+
}
7891
}
7992
}

internal/luapi/lua_global/gjson.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func InitGJson(L *lua.LState) int {
1919
arg := L.ToUserData(1)
2020
if j, err := gjson.LoadJson(gconv.String(arg.Value)); err != nil {
2121
prepareResult(L, nil)
22-
return 0
22+
return 1
2323
} else {
2424
j.SetViolenceCheck(true)
2525
prepareResult(L, j)
@@ -30,12 +30,14 @@ func InitGJson(L *lua.LState) int {
3030
func GetGJsonString(L *lua.LState) int {
3131
j := checkGJson(L)
3232
if j == nil {
33-
return 0
33+
L.Push(lua.LString(""))
34+
return 1
3435
}
3536

3637
jPath := L.ToString(2)
3738
if jPath == "" {
38-
return 0
39+
L.Push(lua.LString(""))
40+
return 1
3941
}
4042
res := j.Get(jPath).String()
4143
L.Push(lua.LString(res))
@@ -45,12 +47,14 @@ func GetGJsonString(L *lua.LState) int {
4547
func GetGJsonInt(L *lua.LState) int {
4648
j := checkGJson(L)
4749
if j == nil {
48-
return 0
50+
L.Push(lua.LNumber(0))
51+
return 1
4952
}
5053

5154
jPath := L.ToString(2)
5255
if jPath == "" {
53-
return 0
56+
L.Push(lua.LNumber(0))
57+
return 1
5458
}
5559
res := j.Get(jPath).Int()
5660
L.Push(lua.LNumber(res))
@@ -89,15 +93,18 @@ func GetGJsonMapEach(L *lua.LState) int {
8993
func GetGJsonFromMapByKey(L *lua.LState) int {
9094
j := checkGJson(L)
9195
if j == nil {
92-
return 0
96+
L.Push(lua.LString(""))
97+
return 1
9398
}
9499
jPath := L.ToString(2)
95100
if jPath == "" {
96-
return 0
101+
L.Push(lua.LString(""))
102+
return 1
97103
}
98104
res := j.Get(jPath).Map()
99105
if res == nil {
100-
return 0
106+
L.Push(lua.LString(""))
107+
return 1
101108
}
102109

103110
key := L.ToString(3)
@@ -144,20 +151,24 @@ func GetGJsonSliceEach(L *lua.LState) int {
144151
func GetGJsonFromSliceByIndex(L *lua.LState) int {
145152
j := checkGJson(L)
146153
if j == nil {
147-
return 0
154+
L.Push(lua.LString(""))
155+
return 1
148156
}
149157
jPath := L.ToString(2)
150158
if jPath == "" {
151-
return 0
159+
L.Push(lua.LString(""))
160+
return 1
152161
}
153162
res := j.Get(jPath).Array()
154163
if res == nil {
155-
return 0
164+
L.Push(lua.LString(""))
165+
return 1
156166
}
157167

158168
index := L.ToInt(3)
159169
if index < 1 || index > len(res) {
160-
return 0
170+
L.Push(lua.LString(""))
171+
return 1
161172
}
162173
val := res[index-1]
163174
L.Push(lua.LString(gconv.String(val)))

0 commit comments

Comments
 (0)