-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboot.go
76 lines (63 loc) · 1.35 KB
/
boot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package purr
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"path"
"strings"
lua "github.com/yuin/gopher-lua"
)
//Injection set a lua fuction into lua state
var Injection lua.LGFunction
//RunTestWithServer Running a http backend server and then load the test case script.
func RunTestWithServer(handle http.Handler, scriptPath string) {
L := lua.NewState()
defer L.Close()
SetTestCasesPath(L, scriptPath)
LoadModules(L)
ts := httptest.NewServer(handle)
SetInternalHost(ts.URL)
defer ts.Close()
if Injection != nil {
Injection(L)
}
doTest(L, scriptPath)
}
//RunTest without server
func RunTest(httpHost string, scriptPath string) {
L := lua.NewState()
defer L.Close()
SetTestCasesPath(L, scriptPath)
LoadModules(L)
SetInternalHost(httpHost)
if Injection != nil {
Injection(L)
}
doTest(L, scriptPath)
}
func doTest(L *lua.LState, scriptPath string) {
if strings.Index(scriptPath, ".lua") != -1 {
err := L.DoFile(scriptPath)
if err != nil {
panic(err.Error())
}
} else {
dir, err := os.Open(scriptPath)
if err != nil {
panic(err.Error())
}
names, err := dir.Readdirnames(0)
if err != nil {
panic(err.Error())
}
for _, name := range names {
if strings.LastIndex(name, "_test.lua") != -1 {
err = L.DoFile(path.Join(scriptPath, name))
if err != nil {
fmt.Println(err.Error())
}
}
}
}
}