Skip to content

Commit 8e22314

Browse files
committed
pytest: introduce RunScript
1 parent e8acd73 commit 8e22314

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/go-python/gpython
33
go 1.17
44

55
require (
6+
github.com/google/go-cmp v0.5.7
67
github.com/gopherjs/gopherwasm v1.1.0
78
github.com/peterh/liner v1.2.2
89
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
2+
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
13
github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c h1:16eHWuMGvCjSfgRJKqIzapE78onvvTbdi1rMkU00lZw=
24
github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
35
github.com/gopherjs/gopherwasm v1.1.0 h1:fA2uLoctU5+T3OhOn2vYP0DVT6pxc7xhTlBB1paATqQ=
@@ -12,3 +14,5 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
1214
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1315
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs=
1416
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
17+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
18+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pytest/pytest.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
package pytest
66

77
import (
8+
"bytes"
89
"io"
910
"os"
1011
"path"
12+
"path/filepath"
1113
"strings"
1214
"testing"
1315

1416
"github.com/go-python/gpython/compile"
1517
"github.com/go-python/gpython/py"
18+
"github.com/google/go-cmp/cmp"
1619

1720
_ "github.com/go-python/gpython/stdlib"
1821
)
@@ -126,3 +129,57 @@ func RunBenchmarks(b *testing.B, testDir string) {
126129
})
127130
}
128131
}
132+
133+
// RunScript runs the provided path to a script.
134+
// RunScript captures the stdout and stderr while executing the script
135+
// and compares it to a golden file:
136+
// RunScript("./testdata/foo.py")
137+
// will compare the output with "./testdata/foo_golden.txt".
138+
func RunScript(t *testing.T, fname string) {
139+
opts := py.DefaultContextOpts()
140+
opts.SysArgs = []string{fname}
141+
ctx := py.NewContext(opts)
142+
defer ctx.Close()
143+
144+
sys := ctx.Store().MustGetModule("sys")
145+
tmp, err := os.MkdirTemp("", "gpython-pytest-")
146+
if err != nil {
147+
t.Fatal(err)
148+
}
149+
defer os.RemoveAll(tmp)
150+
151+
out, err := os.Create(filepath.Join(tmp, "combined"))
152+
if err != nil {
153+
t.Fatalf("could not create stdout/stderr: %+v", err)
154+
}
155+
defer out.Close()
156+
157+
sys.Globals["stdout"] = &py.File{File: out, FileMode: py.FileWrite}
158+
sys.Globals["stderr"] = &py.File{File: out, FileMode: py.FileWrite}
159+
160+
_, err = py.RunFile(ctx, fname, py.CompileOpts{}, nil)
161+
if err != nil {
162+
t.Fatalf("could not run script %q: %+v", fname, err)
163+
}
164+
165+
err = out.Close()
166+
if err != nil {
167+
t.Fatalf("could not close stdout/stderr: %+v", err)
168+
}
169+
170+
got, err := os.ReadFile(out.Name())
171+
if err != nil {
172+
t.Fatalf("could not read script output: %+v", err)
173+
}
174+
175+
ref := fname[:len(fname)-len(".py")] + "_golden.txt"
176+
want, err := os.ReadFile(ref)
177+
if err != nil {
178+
t.Fatalf("could not read golden output %q: %+v", ref, err)
179+
}
180+
181+
if !bytes.Equal(got, want) {
182+
diff := cmp.Diff(string(want), string(got))
183+
t.Fatalf("output differ: -- (-ref +got)\n%s", diff)
184+
}
185+
}

0 commit comments

Comments
 (0)