Skip to content

Commit 6ecb088

Browse files
committed
lua plugins test
1 parent fc9ccdc commit 6ecb088

File tree

9 files changed

+192
-48
lines changed

9 files changed

+192
-48
lines changed

internal/luapi/lua_global/conda.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"runtime"
7+
"slices"
78
"strings"
89

910
"github.com/gvcgo/goutils/pkgs/gtea/gprint"
@@ -79,7 +80,7 @@ var CondaSearchCommand = []string{
7980

8081
func GetVersionForPlatform(platform, sdkName string) (vlist []string) {
8182
homeDir, _ := os.UserHomeDir()
82-
_cmd := append([]string{}, CondaSearchCommand...)
83+
_cmd := slices.Clone(CondaSearchCommand)
8384
_cmd = append(_cmd, "--subdir", platform, "--full-name", sdkName)
8485
r, err := gutils.ExecuteSysCommand(true, homeDir, _cmd...)
8586
if err == nil {

internal/luapi/lua_global/goquery.go

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ func initDocument(resp string) *goquery.Document {
3131
return doc
3232
}
3333

34+
/*
35+
lua:
36+
url = "https://www.bing.com"
37+
timeout = 10
38+
headers = { ["User-Agent"] ="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"}
39+
resp = vmrGetResponse(url, timeout, headers)
40+
selection = vmrInitSelection(resp, "li")
41+
*/
3442
func InitSelection(L *lua.LState) int {
3543
resp := L.ToUserData(1)
3644
if resp == nil {
@@ -47,6 +55,15 @@ func InitSelection(L *lua.LState) int {
4755
return 1
4856
}
4957

58+
/*
59+
lua:
60+
url = "https://www.bing.com"
61+
timeout = 10
62+
headers = { ["User-Agent"] ="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"}
63+
resp = vmrGetResponse(url, timeout, headers)
64+
selection = vmrInitSelection(resp, "li")
65+
selection = vmrFind(selection, "a")
66+
*/
5067
func Find(L *lua.LState) int {
5168
s := checkSelection(L)
5269
if s == nil {
@@ -59,6 +76,15 @@ func Find(L *lua.LState) int {
5976
return 1
6077
}
6178

79+
/*
80+
lua:
81+
url = "https://www.bing.com"
82+
timeout = 10
83+
headers = { ["User-Agent"] ="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"}
84+
resp = vmrGetResponse(url, timeout, headers)
85+
selection = vmrInitSelection(resp, "li")
86+
selection = vmrEq(selection, 0)
87+
*/
6288
func Eq(L *lua.LState) int {
6389
s := checkSelection(L)
6490
if s == nil {
@@ -71,18 +97,36 @@ func Eq(L *lua.LState) int {
7197
return 1
7298
}
7399

100+
/*
101+
lua:
102+
url = "https://www.bing.com"
103+
timeout = 10
104+
headers = { ["User-Agent"] ="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"}
105+
resp = vmrGetResponse(url, timeout, headers)
106+
selection = vmrInitSelection(resp, "a")
107+
string = vmrAttr(selection, "href")
108+
*/
74109
func Attr(L *lua.LState) int {
75110
s := checkSelection(L)
76111
if s == nil {
77-
prepareResult(L, nil)
78-
return 0
112+
L.Push(lua.LString(""))
113+
return 1
79114
}
80115
attrName := L.ToString(2)
81116
value := s.AttrOr(attrName, "")
82117
L.Push(lua.LString(value))
83118
return 1
84119
}
85120

121+
/*
122+
lua:
123+
url = "https://www.bing.com"
124+
timeout = 10
125+
headers = { ["User-Agent"] ="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"}
126+
resp = vmrGetResponse(url, timeout, headers)
127+
selection = vmrInitSelection(resp, "a")
128+
string = vmrText(selection)
129+
*/
86130
func Text(L *lua.LState) int {
87131
s := checkSelection(L)
88132
if s == nil {
@@ -94,6 +138,22 @@ func Text(L *lua.LState) int {
94138
return 1
95139
}
96140

141+
/*
142+
lua:
143+
url = "https://www.bing.com"
144+
timeout = 10
145+
headers = { ["User-Agent"] ="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"}
146+
resp = vmrGetResponse(url, timeout, headers)
147+
s = initSelection(resp, "li")
148+
function parseLiItem(i, ss)
149+
150+
local node = vmrFind(ss, "a")
151+
local href = vmrAttr(node, "href")
152+
print(href)
153+
154+
end
155+
vmrEach(s, parseLiItem)
156+
*/
97157
func Each(L *lua.LState) int {
98158
s := checkSelection(L)
99159
if s == nil {

internal/luapi/lua_global/goquery_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import "testing"
55
var goqueryScript = `local headers = {}
66
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
77
8-
local resp = getResponse("https://www.bing.com", 10, headers)
9-
local s = initSelection(resp, "li")
8+
local resp = vmrGetResponse("https://www.bing.com", 10, headers)
9+
local s = vmrInitSelection(resp, "li")
1010
1111
function parseLiItem(i, ss)
12-
local node = find(ss, "a")
13-
local href = attr(node, "href")
14-
print(href)
12+
local node = vmrFind(ss, "a")
13+
local text = vmrText(node)
14+
local href = vmrAttr(node, "href")
15+
local s = vmrSprintf("%s: %s", {text, href})
16+
print(s)
1517
end
16-
17-
print("------------------goquery------------------")
18-
each(s, parseLiItem)
18+
vmrEach(s, parseLiItem)
1919
`
2020

2121
func TestGoQuery(t *testing.T) {

internal/luapi/lua_global/installer.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func NewInstallerConfig() (ic *InstallerConfig) {
5555
return
5656
}
5757

58+
/*
59+
lua: ic = vmrNewInstallerConfig()
60+
*/
5861
func NewInstallerConf(L *lua.LState) int {
5962
ud := L.NewUserData()
6063
ud.Value = NewInstallerConfig()
@@ -70,6 +73,12 @@ func checkInstallerConfig(L *lua.LState, n int) *InstallerConfig {
7073
return nil
7174
}
7275

76+
/*
77+
lua:
78+
ic = vmrNewInstallerConfig()
79+
os, arch = vmrGetOsArch()
80+
ic = vmrAddFlagFiles(ic, os, {"a", "b", "c", ...})
81+
*/
7382
func AddFlagFiles(L *lua.LState) int {
7483
ic := checkInstallerConfig(L, 1)
7584
if ic == nil {
@@ -106,6 +115,11 @@ func AddFlagFiles(L *lua.LState) int {
106115
return 1
107116
}
108117

118+
/*
119+
lua:
120+
ic = vmrNewInstallerConfig()
121+
ic = vmrEnableFlagDirExcepted(ic)
122+
*/
109123
func EnableFlagDirExcepted(L *lua.LState) int {
110124
ic := checkInstallerConfig(L, 1)
111125
if ic == nil {
@@ -119,6 +133,12 @@ func EnableFlagDirExcepted(L *lua.LState) int {
119133
return 1
120134
}
121135

136+
/*
137+
lua:
138+
ic = vmrNewInstallerConfig()
139+
os, arch = vmrGetOsArch()
140+
ic = vmrAddBinaryDirs(ic, os, {"usr", "bin", ...})
141+
*/
122142
func AddBinaryDirs(L *lua.LState) int {
123143
ic := checkInstallerConfig(L, 1)
124144
if ic == nil {
@@ -156,6 +176,14 @@ func AddBinaryDirs(L *lua.LState) int {
156176
return 1
157177
}
158178

179+
/*
180+
lua:
181+
ic = vmrNewInstallerConfig()
182+
os, arch = vmrGetOsArch()
183+
envPath = {"usr", "bin", ...}
184+
limitedVersions = "<=1.8"
185+
ic = vmrAddAdditionalEnvs(ic, envNameStr, envPath, limitedVersions)
186+
*/
159187
func AddAdditionalEnvs(L *lua.LState) int {
160188
ic := checkInstallerConfig(L, 1)
161189
if ic == nil {

internal/luapi/lua_global/installer_test.go

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,34 @@ import (
55
"testing"
66
)
77

8-
var installerScript = `
9-
print("----------installer_config----------")
10-
11-
ic = newInstallerConfig()
12-
ic = addFlagFiles(ic, "windows", {"windows"})
13-
ic = addFlagFiles(ic, "linux", {"linux"})
14-
ic = addFlagFiles(ic, "darwin", {"osx"})
15-
ic = addBinaryDirs(ic, "windows", {"windows", "bin"})
16-
ic = addBinaryDirs(ic, "linux", {"linux", "bin64"})
17-
ic = addBinaryDirs(ic, "darwin", {"osx", "bin"})
18-
ic = addAdditionalEnvs(ic, "PATH", {"xxx", "bin"}, ">=1.0.0")
19-
ic = enableFlagDirExcepted(ic)
8+
func TestInstaller(t *testing.T) {
9+
script := `
10+
print("----------installer_config----------")
2011
21-
print(ic)
22-
`
12+
ic = vmrNewInstallerConfig()
13+
ic = vmrAddFlagFiles(ic, "windows", {"windows"})
14+
ic = vmrAddFlagFiles(ic, "linux", {"linux"})
15+
ic = vmrAddFlagFiles(ic, "darwin", {"osx"})
16+
ic = vmrAddBinaryDirs(ic, "windows", {"windows", "bin"})
17+
ic = vmrAddBinaryDirs(ic, "linux", {"linux", "bin64"})
18+
ic = vmrAddBinaryDirs(ic, "darwin", {"osx", "bin"})
19+
ic = vmrAddAdditionalEnvs(ic, "PATH", {"xxx", "bin"}, ">=1.0.0")
20+
ic = vmrEnableFlagDirExcepted(ic)
2321
24-
func TestInstaller(t *testing.T) {
25-
ll := NewLua()
26-
defer ll.Close()
27-
L := ll.GetLState()
22+
print(ic)
23+
`
2824

29-
if err := L.DoString(installerScript); err != nil {
25+
if l, err := ExecuteLuaScriptL(script); err != nil {
26+
if l != nil {
27+
l.Close()
28+
}
3029
t.Error(err)
30+
} else {
31+
ic := GetInstallerConfig(l)
32+
// fmt.Println("isntaller_config: ", ic)
33+
fmt.Println("installer_config_flagFiles: ", ic.FlagFiles)
34+
fmt.Println("installer_config_flagDirExcepted: ", ic.FlagDirExcepted)
35+
fmt.Println("installer_config_binaryDirs: ", ic.BinaryDirs)
36+
fmt.Println("installer_config_additionalEnvs: ", ic.AdditionalEnvs)
3137
}
32-
33-
ic := GetInstallerConfig(L)
34-
// fmt.Println("isntaller_config: ", ic)
35-
fmt.Println("installer_config_flagFiles: ", ic.FlagFiles)
36-
fmt.Println("installer_config_flagDirExcepted: ", ic.FlagDirExcepted)
37-
fmt.Println("installer_config_binaryDirs: ", ic.BinaryDirs)
38-
fmt.Println("installer_config_additionalEnvs: ", ic.AdditionalEnvs)
3938
}

internal/luapi/lua_global/lua.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ func (l *Lua) SetGlobal(name string, fn lua.LGFunction) {
3535
func (l *Lua) init() {
3636
l.SetGlobal("vmrGetResponse", GetResponse)
3737
// goquery
38-
l.SetGlobal("initSelection", InitSelection)
39-
l.SetGlobal("find", Find)
40-
l.SetGlobal("eq", Eq)
41-
l.SetGlobal("attr", Attr)
42-
l.SetGlobal("text", Text)
43-
l.SetGlobal("each", Each)
38+
l.SetGlobal("vmrInitSelection", InitSelection)
39+
l.SetGlobal("vmrFind", Find)
40+
l.SetGlobal("vmrEq", Eq)
41+
l.SetGlobal("vmrAttr", Attr)
42+
l.SetGlobal("vmrText", Text)
43+
l.SetGlobal("vmrEach", Each)
4444
// gjson
4545
l.SetGlobal("initGJson", InitGJson)
4646
l.SetGlobal("getString", GetGJsonString)
@@ -69,11 +69,11 @@ func (l *Lua) init() {
6969
// github
7070
l.SetGlobal("getGithubRelease", GetGithubRelease)
7171
// installer_config
72-
l.SetGlobal("newInstallerConfig", NewInstallerConf)
73-
l.SetGlobal("addFlagFiles", AddFlagFiles)
74-
l.SetGlobal("enableFlagDirExcepted", EnableFlagDirExcepted)
75-
l.SetGlobal("addBinaryDirs", AddBinaryDirs)
76-
l.SetGlobal("addAdditionalEnvs", AddAdditionalEnvs)
72+
l.SetGlobal("vmrNewInstallerConfig", NewInstallerConf)
73+
l.SetGlobal("vmrAddFlagFiles", AddFlagFiles)
74+
l.SetGlobal("vmrEnableFlagDirExcepted", EnableFlagDirExcepted)
75+
l.SetGlobal("vmrAddBinaryDirs", AddBinaryDirs)
76+
l.SetGlobal("vmrAddAdditionalEnvs", AddAdditionalEnvs)
7777
// conda
7878
l.SetGlobal("vmrSearchByConda", SearchByConda)
7979
}

internal/luapi/lua_global/req.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ const (
1313
ProxyEnvName string = "VCOLLECTOR_PROXY"
1414
)
1515

16+
/*
17+
lua:
18+
timeout = 10
19+
response_str = vmrGetResponse(url, timeout, { ["User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36" })
20+
*/
1621
func GetResponse(L *lua.LState) int {
1722
dUrl := L.ToString(1)
1823
timeout := L.ToInt(2)

0 commit comments

Comments
 (0)