Skip to content

Commit a573185

Browse files
committed
lua plugins test
1 parent 3af45b8 commit a573185

File tree

12 files changed

+400
-355
lines changed

12 files changed

+400
-355
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package gh
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/gvcgo/version-manager/internal/cnf"
8+
)
9+
10+
func TestGh(t *testing.T) {
11+
cfg := cnf.NewVMRConf()
12+
cfg.Load()
13+
14+
if cfg.GithubToken == "" {
15+
cfg.GithubToken = GetDefaultReadOnly()
16+
}
17+
18+
client := NewGh("oven-sh/bun", cfg.GithubToken, cfg.ProxyUri, cfg.ReverseProxy)
19+
rl := client.GetReleases()
20+
if len(rl) == 0 {
21+
t.Error("test github release failed")
22+
} else {
23+
fmt.Printf("release items: %+v\n", rl)
24+
}
25+
}

internal/luapi/lua_global/goquery.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
lua "github.com/yuin/gopher-lua"
99
)
1010

11-
func prepareResult(L *lua.LState, result interface{}) {
11+
func prepareResult(L *lua.LState, result any) {
1212
ud := L.NewUserData()
1313
ud.Value = result
1414
L.Push(ud)
@@ -42,12 +42,13 @@ selection = vmrInitSelection(resp, "li")
4242
func InitSelection(L *lua.LState) int {
4343
resp := L.ToUserData(1)
4444
if resp == nil {
45-
return 0
45+
prepareResult(L, nil)
46+
return 1
4647
}
4748
doc := initDocument(gconv.String(resp.Value))
4849
if doc == nil {
4950
prepareResult(L, nil)
50-
return 0
51+
return 1
5152
}
5253
selector := L.ToString(2)
5354
s := doc.Find(selector)
@@ -68,7 +69,7 @@ func Find(L *lua.LState) int {
6869
s := checkSelection(L)
6970
if s == nil {
7071
prepareResult(L, nil)
71-
return 0
72+
return 1
7273
}
7374
selector := L.ToString(2)
7475
s = s.Find(selector)
@@ -89,7 +90,7 @@ func Eq(L *lua.LState) int {
8990
s := checkSelection(L)
9091
if s == nil {
9192
prepareResult(L, nil)
92-
return 0
93+
return 1
9394
}
9495
index := L.ToInt(2)
9596
s = s.Eq(index)
@@ -131,7 +132,7 @@ func Text(L *lua.LState) int {
131132
s := checkSelection(L)
132133
if s == nil {
133134
prepareResult(L, nil)
134-
return 0
135+
return 1
135136
}
136137
value := s.Text()
137138
L.Push(lua.LString(value))
@@ -157,7 +158,6 @@ vmrEach(s, parseLiItem)
157158
func Each(L *lua.LState) int {
158159
s := checkSelection(L)
159160
if s == nil {
160-
prepareResult(L, nil)
161161
return 0
162162
}
163163

internal/luapi/lua_global/utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,7 @@ func LenString(L *lua.LState) int {
147147
L.Push(lua.LNumber(len(str)))
148148
return 1
149149
}
150+
151+
// TODO: execute system command
152+
153+
// TODO: check prequisite

internal/luapi/plugin/download.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func copyPlugins() {
6767
}
6868
}
6969

70-
func updateInfo() {
70+
func GetPluginFileList() []gh.RepoFile {
7171
cfg := cnf.NewVMRConf()
7272
cfg.Load()
7373

@@ -76,8 +76,11 @@ func updateInfo() {
7676
}
7777

7878
github := gh.NewGh(PluginRepo, cfg.GithubToken, cfg.ProxyUri, cfg.ReverseProxy)
79-
fileList := github.GetFileList()
79+
return github.GetFileList()
80+
}
8081

82+
func updateInfo() {
83+
fileList := GetPluginFileList()
8184
result, _ := json.Marshal(fileList)
8285
infoFilePath := filepath.Join(cnf.GetPluginDir(), PluginInfoFileName)
8386
os.WriteFile(infoFilePath, result, os.ModePerm)

internal/luapi/plugin/download_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package plugin
22

3+
import (
4+
"testing"
5+
)
6+
37
// func TestDownload(t *testing.T) {
48
// fmt.Println("test download aaaaa")
59
// if err := downloadPlugins(); err != nil {
@@ -13,3 +17,10 @@ package plugin
1317
// t.Error(err)
1418
// }
1519
// }
20+
21+
func TestGetPluginFileList(t *testing.T) {
22+
fileList := GetPluginFileList()
23+
if len(fileList) == 0 {
24+
t.Error("no plugin files found")
25+
}
26+
}

internal/luapi/plugin/fromlua.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package plugin
22

33
import (
4+
"github.com/gvcgo/version-manager/internal/luapi/lua_global"
45
lua "github.com/yuin/gopher-lua"
56
)
67

@@ -12,9 +13,12 @@ const (
1213
PluginVersion LuaConfItem = "plugin_version"
1314
Prequisite LuaConfItem = "prequisite"
1415
Homepage LuaConfItem = "homepage"
16+
Crawler LuaConfItem = "crawl"
1517
)
1618

17-
func GetConfItemFromLua(L *lua.LState, item LuaConfItem) (result string) {
19+
var InstallerConfig LuaConfItem = LuaConfItem(lua_global.InstallerConfigName)
20+
21+
func GetLuaConfItemString(L *lua.LState, item LuaConfItem) (result string) {
1822
v := L.GetGlobal(string(item))
1923
if v == nil {
2024
return
@@ -35,12 +39,7 @@ func GetConfItemFromLua(L *lua.LState, item LuaConfItem) (result string) {
3539
return
3640
}
3741

38-
const (
39-
InstallerConfig LuaConfItem = "ic"
40-
Crawler LuaConfItem = "crawl"
41-
)
42-
43-
func DoLuaItemExist(L *lua.LState, item LuaConfItem) bool {
42+
func DoesLuaItemExist(L *lua.LState, item LuaConfItem) bool {
4443
v := L.GetGlobal(string(item))
4544
return v.String() != "nil"
4645
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package plugin
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/gvcgo/version-manager/internal/luapi/lua_global"
8+
"github.com/stretchr/testify/assert"
9+
lua "github.com/yuin/gopher-lua"
10+
)
11+
12+
var pluginScript = `
13+
sdk_name = "lua"
14+
plugin_name = "lua"
15+
plugin_version = "0.0.1"
16+
prequisite = "miniconda"
17+
homepage = "https://www.lua.org/"
18+
function crawl()
19+
print("Crawling...")
20+
end
21+
`
22+
23+
func ExecuteLuaScriptL(script string) (*lua.LState, error) {
24+
ll := lua_global.NewLua()
25+
L := ll.GetLState()
26+
err := L.DoString(script)
27+
return L, err
28+
}
29+
30+
func TestDoesLuaItemExist(t *testing.T) {
31+
if l, err := ExecuteLuaScriptL(pluginScript); err != nil {
32+
t.Error(err)
33+
} else {
34+
ok1 := DoesLuaItemExist(l, SDKName)
35+
assert.Equal(t, true, ok1, "should be true")
36+
ok2 := DoesLuaItemExist(l, LuaConfItem("none"))
37+
assert.Equal(t, false, ok2, "should be false")
38+
}
39+
}
40+
41+
func TestGetLuaConfItem(t *testing.T) {
42+
if l, err := ExecuteLuaScriptL(pluginScript); err != nil {
43+
t.Error(err)
44+
} else {
45+
sdkName := GetLuaConfItemString(l, SDKName)
46+
sdkNameShouldBe := "lua"
47+
assert.Equal(t, sdkNameShouldBe, sdkName, fmt.Sprintf("sdk_name should be: %s", sdkNameShouldBe))
48+
s := GetLuaConfItemString(l, LuaConfItem("none"))
49+
noneShouldBe := ""
50+
assert.Equal(t, noneShouldBe, s, fmt.Sprintf("none should be: %s", noneShouldBe))
51+
}
52+
}

0 commit comments

Comments
 (0)