-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathpytest.go
284 lines (244 loc) · 7.21 KB
/
pytest.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pytest
import (
"bytes"
"flag"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"github.com/go-python/gpython/compile"
"github.com/go-python/gpython/py"
"github.com/google/go-cmp/cmp"
_ "github.com/go-python/gpython/stdlib"
)
var RegenTestData = flag.Bool("regen", false, "Regenerate golden files from current testdata.")
var gContext = py.NewContext(py.DefaultContextOpts())
// Compile the program in the file prog to code in the module that is returned
func compileProgram(t testing.TB, prog string) (*py.Module, *py.Code) {
f, err := os.Open(prog)
if err != nil {
t.Fatalf("%s: Open failed: %v", prog, err)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatalf("%s: Close failed: %v", prog, err)
}
}()
str, err := io.ReadAll(f)
if err != nil {
t.Fatalf("%s: ReadAll failed: %v", prog, err)
}
return CompileSrc(t, gContext, string(str), prog)
}
func CompileSrc(t testing.TB, ctx py.Context, pySrc string, prog string) (*py.Module, *py.Code) {
code, err := compile.Compile(string(pySrc), prog, py.ExecMode, 0, true)
if err != nil {
t.Fatalf("%s: Compile failed: %v", prog, err)
}
module, err := ctx.Store().NewModule(ctx, &py.ModuleImpl{
Info: py.ModuleInfo{
FileDesc: prog,
},
})
if err != nil {
t.Fatalf("%s: NewModule failed: %v", prog, err)
}
return module, code
}
// Run the code in the module
func run(t testing.TB, module *py.Module, code *py.Code) {
_, err := gContext.RunCode(code, module.Globals, module.Globals, nil)
if err != nil {
if wantErrObj, ok := module.Globals["err"]; ok {
gotExc, ok := err.(py.ExceptionInfo)
if !ok {
t.Fatalf("got err is not ExceptionInfo: %#v", err)
}
if gotExc.Value.Type() != wantErrObj.Type() {
t.Fatalf("Want exception %v got %v", wantErrObj, gotExc.Value)
}
// t.Logf("matched exception")
return
} else {
py.TracebackDump(err)
t.Fatalf("Run failed: %v at %q", err, module.Globals["doc"])
}
}
// t.Logf("%s: Return = %v", prog, res)
if doc, ok := module.Globals["doc"]; ok {
if docStr, ok := doc.(py.String); ok {
if string(docStr) != "finished" {
t.Fatalf("Didn't finish at %q", docStr)
}
} else {
t.Fatalf("Set doc variable to non string: %#v", doc)
}
} else {
t.Fatalf("Didn't set doc variable at all")
}
}
// find the python files in the directory passed in
func findFiles(t testing.TB, testDir string) (names []string) {
files, err := os.ReadDir(testDir)
if err != nil {
t.Fatalf("ReadDir failed: %v", err)
}
for _, f := range files {
name := f.Name()
if !strings.HasPrefix(name, "lib") && strings.HasSuffix(name, ".py") {
names = append(names, name)
}
}
return names
}
// RunTests runs the tests in the directory passed in
func RunTests(t *testing.T, testDir string) {
for _, name := range findFiles(t, testDir) {
t.Run(name, func(t *testing.T) {
module, code := compileProgram(t, path.Join(testDir, name))
run(t, module, code)
})
}
}
// RunBenchmarks runs the benchmarks in the directory passed in
func RunBenchmarks(b *testing.B, testDir string) {
for _, name := range findFiles(b, testDir) {
module, code := compileProgram(b, path.Join(testDir, name))
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
run(b, module, code)
}
})
}
}
// RunScript runs the provided path to a script.
// RunScript captures the stdout and stderr while executing the script
// and compares it to a golden file, blocking until completion.
//
// RunScript("./testdata/foo.py")
//
// will compare the output with "./testdata/foo_golden.txt".
func RunScript(t *testing.T, fname string) {
RunTestTasks(t, []*Task{
{
PyFile: fname,
},
})
}
// RunTestTasks runs each given task in a newly created py.Context concurrently.
// If a fatal error is encountered, the given testing.T is signaled.
func RunTestTasks(t *testing.T, tasks []*Task) {
onCompleted := make(chan *Task)
numTasks := len(tasks)
for ti := 0; ti < numTasks; ti++ {
task := tasks[ti]
go func() {
err := task.run()
task.Err = err
onCompleted <- task
}()
}
tasks = tasks[:0]
for ti := 0; ti < numTasks; ti++ {
task := <-onCompleted
if task.Err != nil {
t.Error(task.Err)
}
tasks = append(tasks, task)
}
}
var (
taskCounter int32
)
type Task struct {
num int32 // Assigned when this task is run
ID string // unique key identifying this task. If empty, autogenerated from the basename of PyFile
PyFile string // If set, this file pathname is executed in a newly created ctx
PyTask func(ctx py.Context) error // If set, a new created ctx is created and this blocks until completion
GoldFile string // Filename containing the "gold standard" stdout+stderr. If empty, autogenerated from PyFile or ID
Err error // Non-nil if a fatal error is encountered with this task
}
func (task *Task) run() error {
fileBase := ""
opts := py.DefaultContextOpts()
if task.PyFile != "" {
opts.SysArgs = []string{task.PyFile}
if task.ID == "" {
ext := filepath.Ext(task.PyFile)
fileBase = task.PyFile[0 : len(task.PyFile)-len(ext)]
}
}
task.num = atomic.AddInt32(&taskCounter, 1)
if task.ID == "" {
if fileBase == "" {
task.ID = fmt.Sprintf("task-%04d", atomic.AddInt32(&taskCounter, 1))
} else {
task.ID = strings.TrimPrefix(fileBase, "./")
}
}
if task.GoldFile == "" {
task.GoldFile = fileBase + "_golden.txt"
}
ctx := py.NewContext(opts)
defer ctx.Close()
sys := ctx.Store().MustGetModule("sys")
tmp, err := os.MkdirTemp("", "gpython-pytest-")
if err != nil {
return err
}
defer os.RemoveAll(tmp)
out, err := os.Create(filepath.Join(tmp, "combined"))
if err != nil {
return fmt.Errorf("could not create stdout+stderr output file: %w", err)
}
defer out.Close()
sys.Globals["stdout"] = &py.File{File: out, FileMode: py.FileWrite}
sys.Globals["stderr"] = &py.File{File: out, FileMode: py.FileWrite}
if task.PyFile != "" {
_, err := py.RunFile(ctx, task.PyFile, py.CompileOpts{}, nil)
if err != nil {
return fmt.Errorf("could not run target script %q: %w", task.PyFile, err)
}
}
if task.PyTask != nil {
err := task.PyTask(ctx)
if err != nil {
return fmt.Errorf("PyTask %q failed: %w", task.ID, err)
}
}
// Close the ctx explicitly as it may legitimately generate output
ctx.Close()
<-ctx.Done()
err = out.Close()
if err != nil {
return fmt.Errorf("could not close output file: %w", err)
}
got, err := os.ReadFile(out.Name())
if err != nil {
return fmt.Errorf("could not read script output file: %w", err)
}
if *RegenTestData {
err := os.WriteFile(task.GoldFile, got, 0644)
if err != nil {
return fmt.Errorf("could not write golden output %q: %w", task.GoldFile, err)
}
}
want, err := os.ReadFile(task.GoldFile)
if err != nil {
return fmt.Errorf("could not read golden output %q: %w", task.GoldFile, err)
}
diff := cmp.Diff(string(want), string(got))
if !bytes.Equal(got, want) {
out := fileBase + ".txt"
_ = os.WriteFile(out, got, 0644)
return fmt.Errorf("output differ: -- (-ref +got)\n%s", diff)
}
return nil
}