Skip to content

Commit fc9ccdc

Browse files
committed
lua plugins test
1 parent 1cbef82 commit fc9ccdc

File tree

9 files changed

+328
-44
lines changed

9 files changed

+328
-44
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/pelletier/go-toml/v2 v2.0.7
2121
github.com/shirou/gopsutil v3.21.11+incompatible
2222
github.com/spf13/cobra v1.8.0
23+
github.com/stretchr/testify v1.10.0
2324
github.com/yuin/gopher-lua v1.1.1
2425
golang.org/x/sys v0.31.0
2526
golang.org/x/term v0.30.0
@@ -39,6 +40,7 @@ require (
3940
github.com/charmbracelet/harmonica v0.2.0 // indirect
4041
github.com/clbanning/mxj/v2 v2.7.0 // indirect
4142
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
43+
github.com/davecgh/go-spew v1.1.1 // indirect
4244
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 // indirect
4345
github.com/fsnotify/fsnotify v1.7.0 // indirect
4446
github.com/gdamore/encoding v1.0.1 // indirect
@@ -68,6 +70,7 @@ require (
6870
github.com/nwaples/rardecode v1.1.3 // indirect
6971
github.com/nwaples/rardecode/v2 v2.1.0 // indirect
7072
github.com/pierrec/lz4/v4 v4.1.21 // indirect
73+
github.com/pmezard/go-difflib v1.0.0 // indirect
7174
github.com/rivo/uniseg v0.4.7 // indirect
7275
github.com/sorairolake/lzip-go v0.3.5 // indirect
7376
github.com/spf13/pflag v1.0.5 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
245245
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
246246
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
247247
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
248-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
249-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
248+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
249+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
250250
github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+xzw=
251251
github.com/therootcompany/xz v1.0.1/go.mod h1:3K3UH1yCKgBneZYhuQUvJ9HPD19UEXEI0BWbMn8qNMY=
252252
github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU=

internal/installer/prequisite.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"os"
55

66
"github.com/gvcgo/goutils/pkgs/gtea/gprint"
7-
"github.com/gvcgo/goutils/pkgs/gutils"
87
"github.com/gvcgo/version-manager/internal/luapi/plugin"
8+
"github.com/gvcgo/version-manager/internal/utils"
99
)
1010

1111
const (
@@ -19,16 +19,6 @@ Automatically detects and installs prerequisites for the installer.
1919
2. coursier latest
2020
*/
2121

22-
func IsMinicondaInstalled() bool {
23-
_, err := gutils.ExecuteSysCommand(true, "", "conda", "--help")
24-
return err == nil
25-
}
26-
27-
func IsCoursierInstalled() bool {
28-
_, err := gutils.ExecuteSysCommand(true, "", "cs", "--help")
29-
return err == nil
30-
}
31-
3222
func installPrequisite(pluginName string) {
3323
// add envs temporarily, so the following command will easilly find prequisites.
3424
os.Setenv(AddToPathTemporarillyEnvName, "1")
@@ -40,14 +30,14 @@ func installPrequisite(pluginName string) {
4030
}
4131

4232
func CheckAndInstallMiniconda() {
43-
if !IsMinicondaInstalled() {
33+
if !utils.IsMinicondaInstalled() {
4434
gprint.PrintInfo("Installing miniconda first: ")
4535
installPrequisite(MinicondaSDKName)
4636
}
4737
}
4838

4939
func CheckAndInstallCoursier() {
50-
if !IsCoursierInstalled() {
40+
if !utils.IsCoursierInstalled() {
5141
gprint.PrintInfo("Installing coursier first: ")
5242
installPrequisite(CoursierSDKName)
5343
}
Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,64 @@
11
package lua_global
22

33
import (
4-
"fmt"
54
"testing"
6-
)
7-
8-
var condaScript = `print("-----------------conda-------------------")
9-
10-
local vl = newVersionList()
11-
local result = searchByConda(vl, "php")
125

13-
print(result)
14-
`
6+
"github.com/gvcgo/version-manager/internal/utils"
7+
"github.com/stretchr/testify/assert"
8+
)
159

16-
func TestConda(t *testing.T) {
17-
fmt.Println("test conda")
10+
func ExecuteLuaScript(script string) error {
1811
ll := NewLua()
1912
defer ll.Close()
13+
L := ll.GetLState()
14+
return L.DoString(script)
15+
}
2016

21-
if err := ll.GetLState().DoString(condaScript); err != nil {
17+
func TestConda(t *testing.T) {
18+
if !utils.IsMinicondaInstalled() {
19+
return
20+
}
21+
22+
var condaScript = `print("-----------------conda-------------------")
23+
local vl = newVersionList()
24+
local result = vmrSearchByConda(vl, "php")
25+
print(result)
26+
`
27+
if err := ExecuteLuaScript(condaScript); err != nil {
2228
t.Error(err)
2329
}
2430
}
31+
32+
func TestParseArch(t *testing.T) {
33+
platforms := map[string]string{
34+
"linux-64": "amd64",
35+
"win-64": "amd64",
36+
"osx-64": "amd64",
37+
"linux-aarch64": "arm64",
38+
"win-arm64": "arm64",
39+
"osx-arm64": "arm64",
40+
"osx": "",
41+
}
42+
43+
for k, v := range platforms {
44+
a := ParseArch(k)
45+
assert.Equal(t, v, a, "should be equal")
46+
}
47+
}
48+
49+
func TestParseOS(t *testing.T) {
50+
platforms := map[string]string{
51+
"linux-64": "linux",
52+
"win-64": "windows",
53+
"osx-64": "darwin",
54+
"linux-aarch64": "linux",
55+
"win-arm64": "windows",
56+
"osx-arm64": "darwin",
57+
"amd64": "",
58+
}
59+
60+
for k, v := range platforms {
61+
o := ParseOS(k)
62+
assert.Equal(t, v, o, "should be equal")
63+
}
64+
}

internal/luapi/lua_global/lua.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (l *Lua) SetGlobal(name string, fn lua.LGFunction) {
3333
}
3434

3535
func (l *Lua) init() {
36-
l.SetGlobal("getResponse", GetResponse)
36+
l.SetGlobal("vmrGetResponse", GetResponse)
3737
// goquery
3838
l.SetGlobal("initSelection", InitSelection)
3939
l.SetGlobal("find", Find)
@@ -50,22 +50,22 @@ func (l *Lua) init() {
5050
l.SetGlobal("getByIndex", GetGJsonFromSliceByIndex) // for array
5151
l.SetGlobal("sliceEach", GetGJsonSliceEach)
5252
// utils
53-
l.SetGlobal("getOsArch", GetOsArch)
54-
l.SetGlobal("regexpFindString", RegExpFindString)
55-
l.SetGlobal("hasPrefix", HasPrefix)
56-
l.SetGlobal("hasSuffix", HasSuffix)
57-
l.SetGlobal("contains", Contains)
58-
l.SetGlobal("trimPrefix", TrimPrefix)
59-
l.SetGlobal("trimSuffix", TrimSuffix)
60-
l.SetGlobal("trim", Trim)
61-
l.SetGlobal("trimSpace", TrimSpace)
62-
l.SetGlobal("sprintf", Sprintf)
63-
l.SetGlobal("urlJoin", UrlJoin)
64-
l.SetGlobal("lenString", LenString)
53+
l.SetGlobal("vmrGetOsArch", GetOsArch)
54+
l.SetGlobal("vmrRegexpFindString", RegExpFindString)
55+
l.SetGlobal("vmrHasPrefix", HasPrefix)
56+
l.SetGlobal("vmrHasSuffix", HasSuffix)
57+
l.SetGlobal("vmrContains", Contains)
58+
l.SetGlobal("vmrTrimPrefix", TrimPrefix)
59+
l.SetGlobal("vmrTrimSuffix", TrimSuffix)
60+
l.SetGlobal("vmrTrim", Trim)
61+
l.SetGlobal("vmrTrimSpace", TrimSpace)
62+
l.SetGlobal("vmrSprintf", Sprintf)
63+
l.SetGlobal("vmrUrlJoin", UrlJoin)
64+
l.SetGlobal("vmrLenString", LenString)
6565
// version
66-
l.SetGlobal("newVersionList", NewVersionList)
67-
l.SetGlobal("addItem", AddItem)
68-
l.SetGlobal("mergeVersionList", MergeVersionList)
66+
l.SetGlobal("vmrNewVersionList", NewVersionList)
67+
l.SetGlobal("vmrAddItem", AddItem)
68+
l.SetGlobal("vmrMergeVersionList", MergeVersionList)
6969
// github
7070
l.SetGlobal("getGithubRelease", GetGithubRelease)
7171
// installer_config
@@ -75,7 +75,7 @@ func (l *Lua) init() {
7575
l.SetGlobal("addBinaryDirs", AddBinaryDirs)
7676
l.SetGlobal("addAdditionalEnvs", AddAdditionalEnvs)
7777
// conda
78-
l.SetGlobal("searchByConda", SearchByConda)
78+
l.SetGlobal("vmrSearchByConda", SearchByConda)
7979
}
8080

8181
func (l *Lua) GetLState() *lua.LState {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package lua_global
2+
3+
import "testing"
4+
5+
func TestGetResponse(t *testing.T) {
6+
script := `url = "https://www.baidu.com/"
7+
timeout = 10
8+
headers = { ["User-Agent"] ="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"}
9+
resp = vmrGetResponse(url, timeout, headers)
10+
print(resp)
11+
`
12+
if err := ExecuteLuaScript(script); err != nil {
13+
t.Error(err)
14+
}
15+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package lua_global
2+
3+
import "testing"
4+
5+
func TestGetOsArch(t *testing.T) {
6+
script := `os, arch = vmrGetOsArch()
7+
print(os)
8+
print(arch)
9+
`
10+
if err := ExecuteLuaScript(script); err != nil {
11+
t.Error(err)
12+
}
13+
}
14+
15+
func TestRegExpFindString(t *testing.T) {
16+
script := `s = vmrRegexpFindString("r(.+)d", "hello regexp world")
17+
print(s)
18+
`
19+
if err := ExecuteLuaScript(script); err != nil {
20+
t.Error(err)
21+
}
22+
}
23+
24+
func TestHasPrefix(t *testing.T) {
25+
script := `s = vmrHasPrefix("hello", "he")
26+
print(s)
27+
`
28+
if err := ExecuteLuaScript(script); err != nil {
29+
t.Error(err)
30+
}
31+
}
32+
33+
func TestHasSuffix(t *testing.T) {
34+
script := `s = vmrHasSuffix("hello", "lo")
35+
print(s)
36+
`
37+
if err := ExecuteLuaScript(script); err != nil {
38+
t.Error(err)
39+
}
40+
}
41+
42+
func TestContains(t *testing.T) {
43+
script := `if vmrContains("abc", "a") then
44+
print("true")
45+
end
46+
`
47+
if err := ExecuteLuaScript(script); err != nil {
48+
t.Error(err)
49+
}
50+
}
51+
52+
func TestTrimPrefix(t *testing.T) {
53+
script := `s = vmrTrimPrefix("abc", "a")
54+
print(s)
55+
`
56+
if err := ExecuteLuaScript(script); err != nil {
57+
t.Error(err)
58+
}
59+
}
60+
61+
func TestTrimSuffix(t *testing.T) {
62+
script := `s = vmrTrimSuffix("abc", "c")
63+
print(s)
64+
`
65+
if err := ExecuteLuaScript(script); err != nil {
66+
t.Error(err)
67+
}
68+
}
69+
70+
func TestTrim(t *testing.T) {
71+
script := `s = vmrTrim("dabcd", "d")
72+
print(s)
73+
`
74+
if err := ExecuteLuaScript(script); err != nil {
75+
t.Error(err)
76+
}
77+
}
78+
79+
func TestTrimSpace(t *testing.T) {
80+
script := `s = vmrTrimSpace(" abc ")
81+
print(s)
82+
`
83+
if err := ExecuteLuaScript(script); err != nil {
84+
t.Error(err)
85+
}
86+
}
87+
88+
func TestSprintf(t *testing.T) {
89+
script := `s = vmrSprintf("abc %s", {"def"})
90+
print(s)
91+
`
92+
if err := ExecuteLuaScript(script); err != nil {
93+
t.Error(err)
94+
}
95+
}
96+
97+
func TestUrlJoin(t *testing.T) {
98+
script := `s = vmrUrlJoin("https://test.com/v1", "check")
99+
print(s)
100+
`
101+
if err := ExecuteLuaScript(script); err != nil {
102+
t.Error(err)
103+
}
104+
}
105+
106+
func TestLenString(t *testing.T) {
107+
script := `s = vmrLenString("check")
108+
print(s)
109+
`
110+
if err := ExecuteLuaScript(script); err != nil {
111+
t.Error(err)
112+
}
113+
}

0 commit comments

Comments
 (0)