diff --git a/.travis.yml b/.travis.yml index 678d6f3..56585f9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/Makefile b/Makefile deleted file mode 100644 index cb18d84..0000000 --- a/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright 2009 The Go Authors. All rights reserved. -# Use of this source code is governed by a BSD-style -# license that can be found in the LICENSE file. - -.PHONY: all install test - -# default to gc, but allow caller to override on command line -GO_COMPILER:=$(GC) -ifeq ($(GO_COMPILER),) - GO_COMPILER:="gc" -endif - -GO_VERBOSE := $(VERBOSE) -ifneq ($(GO_VERBOSE),) - GO_VERBOSE:= -v -x -endif - -install_cwd = go get $(GO_VERBOSE) -compiler=$(GO_COMPILER) . -test_cwd = go test $(GO_VERBOSE) -compiler=$(GO_COMPILER) . - -all: install test - -install: - $(install_cwd) - (cd ./cmd/go-python && $(install_cwd)) - -test: install - $(test_cwd) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..430d4b0 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..640315c --- /dev/null +++ b/go.sum @@ -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= diff --git a/python_test.go b/python_test.go index 564b64e..732d61b 100644 --- a/python_test.go +++ b/python_test.go @@ -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=\nval=\ntb=\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=, str=None, eq_none=true -`), - }) } diff --git a/tests/cpickle/main.go b/testdata/cpickle/main.go similarity index 100% rename from tests/cpickle/main.go rename to testdata/cpickle/main.go diff --git a/testdata/cpickle/want.txt b/testdata/cpickle/want.txt new file mode 100644 index 0000000..6df8594 --- /dev/null +++ b/testdata/cpickle/want.txt @@ -0,0 +1,3 @@ +hello [ foo ] +cPickle.dumps(foo) = "S'foo'\np1\n." +cPickle.loads("S'foo'\np1\n.") = "foo" diff --git a/tests/errfetch/main.go b/testdata/errfetch/main.go similarity index 100% rename from tests/errfetch/main.go rename to testdata/errfetch/main.go diff --git a/testdata/errfetch/want.txt b/testdata/errfetch/want.txt new file mode 100644 index 0000000..9ec3b1e --- /dev/null +++ b/testdata/errfetch/want.txt @@ -0,0 +1,3 @@ +exc= +val= +tb= diff --git a/tests/issue61/main.go b/testdata/issue61/main.go similarity index 100% rename from tests/issue61/main.go rename to testdata/issue61/main.go diff --git a/testdata/issue61/want.txt b/testdata/issue61/want.txt new file mode 100644 index 0000000..845899e --- /dev/null +++ b/testdata/issue61/want.txt @@ -0,0 +1,2 @@ +['i want this gone'] +[] diff --git a/tests/kw-args/kwargs.py b/testdata/kw-args/kwargs.py similarity index 100% rename from tests/kw-args/kwargs.py rename to testdata/kw-args/kwargs.py diff --git a/tests/kw-args/main.go b/testdata/kw-args/main.go similarity index 100% rename from tests/kw-args/main.go rename to testdata/kw-args/main.go diff --git a/testdata/kw-args/want.txt b/testdata/kw-args/want.txt new file mode 100644 index 0000000..029f732 --- /dev/null +++ b/testdata/kw-args/want.txt @@ -0,0 +1,3 @@ +importing kwargs... +args=() kwds={} +args=() kwds={'a': 3} diff --git a/tests/modify-values/main.go b/testdata/modify-values/main.go similarity index 100% rename from tests/modify-values/main.go rename to testdata/modify-values/main.go diff --git a/tests/modify-values/values.py b/testdata/modify-values/values.py similarity index 100% rename from tests/modify-values/values.py rename to testdata/modify-values/values.py diff --git a/testdata/modify-values/want.txt b/testdata/modify-values/want.txt new file mode 100644 index 0000000..94f3e44 --- /dev/null +++ b/testdata/modify-values/want.txt @@ -0,0 +1,7 @@ +values.__name__: "values" +values.sval: "42" +values.ival: 666 +sval='42' +ival=666 +sval='42 is the answer' +ival=1666 diff --git a/tests/none-check/get_none.py b/testdata/none-check/get_none.py similarity index 100% rename from tests/none-check/get_none.py rename to testdata/none-check/get_none.py diff --git a/tests/none-check/main.go b/testdata/none-check/main.go similarity index 100% rename from tests/none-check/main.go rename to testdata/none-check/main.go diff --git a/testdata/none-check/want.txt b/testdata/none-check/want.txt new file mode 100644 index 0000000..7a6ecf6 --- /dev/null +++ b/testdata/none-check/want.txt @@ -0,0 +1 @@ +type=, str=None, eq_none=true