Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

test: use gotest.tools/golden #89

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
language: go

env:
GO111MODULE=on

go:
- 1.9.x
- 1.10.x
- 1.12.x

go_import_path: github.com/sbinet/go-python

before_install:
- sudo apt-get update -qq
- sudo apt-get install python-dev -qq
- export PATH=$HOME/gopath/bin:$PATH

notifications:
email:
Expand Down
28 changes: 0 additions & 28 deletions Makefile

This file was deleted.

9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/sbinet/go-python

go 1.12

require (
github.com/google/go-cmp v0.3.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
gotest.tools v2.2.0+incompatible
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
165 changes: 27 additions & 138 deletions python_test.go
Original file line number Diff line number Diff line change
@@ -1,151 +1,40 @@
package python

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"testing"

"gotest.tools/assert"
"gotest.tools/golden"
)

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)
cmd := exec.Command("go", "run", "main.go", "-c", "print 1+1")
cmd.Dir = "./cmd/go-python/"
output, err := cmd.CombinedOutput()
assert.NilError(t, err, string(output))
}

func TestCases(t *testing.T) {
cases := []string{
"cpickle",
"errfetch",
"modify-values",
"issue61",
"none-check",
}
}

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)
os.Setenv("PYTHONPATH", os.ExpandEnv(".:$PYTHONPATH"))
for _, dir := range cases {
t.Run(golden.Path(dir), func(t *testing.T) {
t.Parallel()
cmd := exec.Command("go", "run", "main.go")
cmd.Env = os.Environ()
cmd.Dir = golden.Path(dir)
got, err := cmd.CombinedOutput()
assert.NilError(t, err, string(got))
golden.Assert(t, string(got), filepath.Join(dir, "want.txt"))
})
}
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
`),
})
}
File renamed without changes.
3 changes: 3 additions & 0 deletions testdata/cpickle/want.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hello [ foo ]
cPickle.dumps(foo) = "S'foo'\np1\n."
cPickle.loads("S'foo'\np1\n.") = "foo"
File renamed without changes.
3 changes: 3 additions & 0 deletions testdata/errfetch/want.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exc=<NULL>
val=<NULL>
tb=<NULL>
File renamed without changes.
2 changes: 2 additions & 0 deletions testdata/issue61/want.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
['i want this gone']
[]
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions testdata/kw-args/want.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
importing kwargs...
args=() kwds={}
args=() kwds={'a': 3}
7 changes: 7 additions & 0 deletions testdata/modify-values/want.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
values.__name__: "values"
values.sval: "42"
values.ival: 666
sval='42'
ival=666
sval='42 is the answer'
ival=1666
File renamed without changes.
1 change: 1 addition & 0 deletions testdata/none-check/want.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type=<type 'NoneType'>, str=None, eq_none=true