This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 139
/
python_test.go
151 lines (136 loc) · 2.88 KB
/
python_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
package python
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"testing"
)
func TestGoPython(t *testing.T) {
cmd := exec.Command("go-python", "-c", "print 1+1")
err := cmd.Run()
if err != nil {
t.Fatalf("go-python failed: %v", err)
}
}
type pkg struct {
path string
want []byte
}
func testPkg(t *testing.T, table pkg) {
workdir, err := ioutil.TempDir("", "go-python-")
if err != nil {
t.Fatalf("[%s]: could not create workdir: %v\n", table.path, err)
}
err = os.MkdirAll(workdir, 0644)
if err != nil {
t.Fatalf("[%s]: could not create workdir: %v\n", table.path, err)
}
defer os.RemoveAll(workdir)
pypath := "." + string(os.PathListSeparator) + os.Getenv("PYTHONPATH")
os.Setenv("PYTHONPATH", pypath)
buf := new(bytes.Buffer)
cmd := exec.Command("go", "run", "main.go")
cmd.Stdin = os.Stdin
cmd.Stdout = buf
cmd.Stderr = buf
cmd.Dir = table.path
err = cmd.Run()
if err != nil {
t.Fatalf(
"[%s]: error running go-python test: %v\n%v\n",
table.path,
err,
string(buf.Bytes()),
)
}
if !reflect.DeepEqual(string(buf.Bytes()), string(table.want)) {
diffTxt := ""
diffBin, diffErr := exec.LookPath("diff")
if diffErr == nil {
wantFile, wantErr := os.Create(filepath.Join(workdir, "want.txt"))
if wantErr == nil {
wantFile.Write(table.want)
wantFile.Close()
}
gotFile, gotErr := os.Create(filepath.Join(workdir, "got.txt"))
if gotErr == nil {
gotFile.Write(buf.Bytes())
gotFile.Close()
}
if gotErr == nil && wantErr == nil {
cmd = exec.Command(diffBin, "-urN",
wantFile.Name(),
gotFile.Name(),
)
diff, _ := cmd.CombinedOutput()
diffTxt = string(diff) + "\n"
}
}
t.Fatalf("[%s]: error running go-python test:\nwant:\n%s\n\ngot:\n%s\n%s",
table.path,
string(table.want), string(buf.Bytes()),
diffTxt,
)
}
}
func TestKwArgs(t *testing.T) {
t.Parallel()
testPkg(t, pkg{
path: "tests/kw-args",
want: []byte(`importing kwargs...
args=() kwds={}
args=() kwds={'a': 3}
`),
})
}
func TestCPickle(t *testing.T) {
t.Parallel()
testPkg(t, pkg{
path: "tests/cpickle",
want: []byte(`hello [ foo ]
cPickle.dumps(foo) = "S'foo'\np1\n."
cPickle.loads("S'foo'\np1\n.") = "foo"
`),
})
}
func TestErrFetch(t *testing.T) {
t.Parallel()
testPkg(t, pkg{
path: "tests/errfetch",
want: []byte("exc=<NULL>\nval=<NULL>\ntb=<NULL>\n"),
})
}
func TestModifyValues(t *testing.T) {
t.Parallel()
testPkg(t, pkg{
path: "tests/modify-values",
want: []byte(`values.__name__: "values"
values.sval: "42"
values.ival: 666
sval='42'
ival=666
sval='42 is the answer'
ival=1666
`),
})
}
func TestIssue61(t *testing.T) {
t.Parallel()
testPkg(t, pkg{
path: "tests/issue61",
want: []byte(`['i want this gone']
[]
`),
})
}
func TestCheckNone(t *testing.T) {
t.Parallel()
testPkg(t, pkg{
path: "tests/none-check",
want: []byte(`type=<type 'NoneType'>, str=None, eq_none=true
`),
})
}