-
Notifications
You must be signed in to change notification settings - Fork 33
/
php_test.go
215 lines (191 loc) · 4.46 KB
/
php_test.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main_test
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"regexp"
"runtime/debug"
"strings"
"testing"
"github.com/MagicalTux/goro/core/compiler"
"github.com/MagicalTux/goro/core/phpctx"
"github.com/MagicalTux/goro/core/phpv"
"github.com/MagicalTux/goro/core/tokenizer"
"github.com/andreyvit/diff"
)
// Currently focusing on lang tests, change variable to run other tests
const TestsPath = "test"
type phptest struct {
f *os.File
reader *bufio.Reader
output *bytes.Buffer
name string
path string
req *http.Request
p *phpctx.Process
t *testing.T
}
type skipError struct{}
func (s skipError) Error() string {
return "test skipped"
}
var skipTest skipError
func (p *phptest) handlePart(part string, b *bytes.Buffer) error {
switch part {
case "TEST":
testName := strings.TrimSpace(b.String())
p.name += ": " + testName
return nil
case "CREDITS":
// is there something we should do with this?
return nil
case "GET":
p.req.URL.RawQuery = strings.TrimRight(b.String(), "\r\n")
return nil
case "POST":
// we need a new request with the post data
p.req = httptest.NewRequest("POST", "/"+path.Base(p.path), bytes.NewBuffer(bytes.TrimRight(b.Bytes(), "\r\n")))
p.req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return nil
case "FILE":
// pass data to the engine
g := phpctx.NewGlobalReq(p.req, p.p)
g.SetOutput(p.output)
g.Chdir(phpv.ZString(path.Dir(p.path))) // chdir execution to path
t := tokenizer.NewLexer(b, p.path)
c, err := compiler.Compile(g, t)
if err != nil {
return err
}
_, err = c.Run(g)
g.Close()
return phpv.FilterError(err)
case "EXPECT":
// compare p.output with b
out := bytes.TrimSpace(p.output.Bytes())
exp := bytes.TrimSpace(b.Bytes())
if bytes.Compare(out, exp) != 0 {
return fmt.Errorf("output not as expected!\n%s", diff.LineDiff(string(exp), string(out)))
}
return nil
case "SKIPIF":
t := tokenizer.NewLexer(b, p.path)
g := phpctx.NewGlobal(context.Background(), p.p)
output := &bytes.Buffer{}
g.SetOutput(output)
c, err := compiler.Compile(g, t)
if err != nil {
return err
}
_, err = c.Run(g)
err = phpv.FilterError(err)
if err != nil {
return err
}
if bytes.HasPrefix(output.Bytes(), []byte("skip ")) {
return skipTest
}
return nil
case "INI", "EXPECTF", "EXTENSIONS":
// TODO
return skipTest
case "XFAIL":
// TODO but safe to ignore
return nil
default:
return fmt.Errorf("unhandled part type %s for test", part)
}
}
func runTest(t *testing.T, fpath string) (p *phptest, err error) {
p = &phptest{t: t, output: &bytes.Buffer{}, name: fpath, path: fpath}
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("failed to run: %s\n%s", r, debug.Stack())
}
}()
// read & parse test file
p.f, err = os.Open(fpath)
if err != nil {
return
}
defer p.f.Close()
p.reader = bufio.NewReader(p.f)
var b *bytes.Buffer
var part string
// prepare env
p.p = phpctx.NewProcess("test")
p.req = httptest.NewRequest("GET", "/"+path.Base(fpath), nil)
r := regexp.MustCompile("^--([A-Z]+)--$")
for {
lin, err := p.reader.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
return p, err
}
if strings.HasPrefix(lin, "--") {
lin_trimmed := strings.TrimRight(lin, "\r\n")
if sub := r.FindSubmatch([]byte(lin_trimmed)); sub != nil {
thing := string(sub[1])
// start of a new thing?
if b != nil {
err := p.handlePart(part, b)
if err != nil {
return p, err
}
}
b = &bytes.Buffer{}
part = thing
continue
}
}
if b == nil {
return p, fmt.Errorf("malformed test file %s", fpath)
}
b.Write([]byte(lin))
}
if b != nil {
err := p.handlePart(part, b)
if err != nil {
return p, err
}
}
return p, nil
}
func TestPhp(t *testing.T) {
// run all tests in "test"
count := 0
pass := 0
skip := 0
fail := 0
filepath.Walk(TestsPath, func(path string, info os.FileInfo, err error) error {
if !info.Mode().IsRegular() {
return err
}
if !strings.HasSuffix(path, ".phpt") {
return err
}
count += 1
p, err := runTest(t, path)
if err != nil {
if err == skipTest {
skip += 1
return nil
}
fail += 1
t.Errorf("Error in %s: %s", p.name, err.Error())
} else {
pass += 1
}
return nil
})
t.Logf("Total of %d tests, %d passed (%01.2f%% success), %d skipped and %d failed", count, pass, float64(pass)*100/float64(count-skip), skip, fail)
}