Skip to content

Commit f5b703a

Browse files
committed
tests: wire up tests with go-test
1 parent 7074fff commit f5b703a

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

python_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package python
22

33
import (
4+
"bytes"
5+
"io/ioutil"
6+
"os"
47
"os/exec"
8+
"path/filepath"
9+
"reflect"
510
"testing"
611
)
712

@@ -13,4 +18,94 @@ func TestGoPython(t *testing.T) {
1318
}
1419
}
1520

21+
type pkg struct {
22+
path string
23+
want []byte
24+
}
25+
26+
func testPkg(t *testing.T, table pkg) {
27+
workdir, err := ioutil.TempDir("", "go-python-")
28+
if err != nil {
29+
t.Fatalf("[%s]: could not create workdir: %v\n", table.path, err)
30+
}
31+
err = os.MkdirAll(workdir, 0644)
32+
if err != nil {
33+
t.Fatalf("[%s]: could not create workdir: %v\n", table.path, err)
34+
}
35+
defer os.RemoveAll(workdir)
36+
37+
pypath := "." + string(os.PathListSeparator) + os.Getenv("PYTHONPATH")
38+
os.Setenv("PYTHONPATH", pypath)
39+
40+
buf := new(bytes.Buffer)
41+
cmd := exec.Command("go", "run", "main.go")
42+
cmd.Stdin = os.Stdin
43+
cmd.Stdout = buf
44+
cmd.Stderr = buf
45+
cmd.Dir = table.path
46+
err = cmd.Run()
47+
if err != nil {
48+
t.Fatalf(
49+
"[%s]: error running go-python test: %v\n%v\n",
50+
table.path,
51+
err,
52+
string(buf.Bytes()),
53+
)
54+
}
55+
56+
if !reflect.DeepEqual(string(buf.Bytes()), string(table.want)) {
57+
diffTxt := ""
58+
diffBin, diffErr := exec.LookPath("diff")
59+
if diffErr == nil {
60+
wantFile, wantErr := os.Create(filepath.Join(workdir, "want.txt"))
61+
if wantErr == nil {
62+
wantFile.Write(table.want)
63+
wantFile.Close()
64+
}
65+
gotFile, gotErr := os.Create(filepath.Join(workdir, "got.txt"))
66+
if gotErr == nil {
67+
gotFile.Write(buf.Bytes())
68+
gotFile.Close()
69+
}
70+
if gotErr == nil && wantErr == nil {
71+
cmd = exec.Command(diffBin, "-urN",
72+
wantFile.Name(),
73+
gotFile.Name(),
74+
)
75+
diff, _ := cmd.CombinedOutput()
76+
diffTxt = string(diff) + "\n"
77+
}
78+
}
79+
80+
t.Fatalf("[%s]: error running go-python test:\nwant:\n%s\n\ngot:\n%s\n%s",
81+
table.path,
82+
string(table.want), string(buf.Bytes()),
83+
diffTxt,
84+
)
85+
}
86+
87+
}
88+
89+
func TestKwArgs(t *testing.T) {
90+
t.Parallel()
91+
testPkg(t, pkg{
92+
path: "tests/kw-args",
93+
want: []byte(`importing kwargs...
94+
args=() kwds={}
95+
args=() kwds={'a': 3}
96+
`),
97+
})
98+
}
99+
100+
func TestCPickle(t *testing.T) {
101+
t.Parallel()
102+
testPkg(t, pkg{
103+
path: "tests/cpickle",
104+
want: []byte(`hello [ foo ]
105+
cPickle.dumps(foo) = "S'foo'\np1\n."
106+
cPickle.loads("S'foo'\np1\n.") = "foo"
107+
`),
108+
})
109+
}
110+
16111
// EOF
File renamed without changes.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy