Skip to content

Commit b8d4a91

Browse files
committed
pytest: introduce RunScript
1 parent e8acd73 commit b8d4a91

File tree

8 files changed

+128
-0
lines changed

8 files changed

+128
-0
lines changed

go.mod

+1
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

+4
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

+57
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+
diff := cmp.Diff(string(want), string(got))
182+
if !bytes.Equal(got, want) {
183+
t.Fatalf("output differ: -- (-ref +got)\n%s", diff)
184+
}
185+
}

pytest/pytest_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2018 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package pytest
6+
7+
import (
8+
"testing"
9+
)
10+
11+
func TestCompileSrc(t *testing.T) {
12+
for _, tc := range []struct {
13+
name string
14+
code string
15+
}{
16+
{
17+
name: "hello",
18+
code: `print("hello")`,
19+
},
20+
} {
21+
t.Run(tc.name, func(t *testing.T) {
22+
_, _ = CompileSrc(t, gContext, tc.code, tc.name)
23+
})
24+
}
25+
}
26+
27+
func TestRunTests(t *testing.T) {
28+
RunTests(t, "./testdata/tests")
29+
}
30+
31+
func TestRunScript(t *testing.T) {
32+
RunScript(t, "./testdata/hello.py")
33+
}

pytest/testdata/hello.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright 2022 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
print("hello")
6+
print("world")
7+
print("bye.")

pytest/testdata/hello_golden.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello
2+
world
3+
bye.

pytest/testdata/tests/libtest.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright 2022 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
"""
6+
Simple test harness
7+
"""
8+
9+
def testFunc():
10+
return
11+

pytest/testdata/tests/module.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright 2022 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
from libtest import testFunc
6+
7+
doc="module"
8+
assert True
9+
assert not False
10+
assert testFunc() is None
11+
12+
doc="finished"

0 commit comments

Comments
 (0)