diff --git a/examples/embedding/lib/REPL-startup.py b/examples/embedding/lib/REPL-startup.py index 6b27c415..4c84fb03 100644 --- a/examples/embedding/lib/REPL-startup.py +++ b/examples/embedding/lib/REPL-startup.py @@ -1,4 +1,6 @@ - +# Copyright 2022 The go-python Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. # This file is called from main.go when in REPL mode diff --git a/examples/embedding/lib/mylib.py b/examples/embedding/lib/mylib.py index dd5af499..b0105e7d 100644 --- a/examples/embedding/lib/mylib.py +++ b/examples/embedding/lib/mylib.py @@ -1,3 +1,7 @@ +# Copyright 2022 The go-python Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + import mylib_go as _go PY_VERSION = _go.PY_VERSION @@ -47,5 +51,3 @@ def PrintItinerary(self): i += 1 print("### Made with %s " % self._libVers) - - \ No newline at end of file diff --git a/examples/embedding/main_test.go b/examples/embedding/main_test.go index e5287be8..642867ec 100644 --- a/examples/embedding/main_test.go +++ b/examples/embedding/main_test.go @@ -1,3 +1,7 @@ +// Copyright 2022 The go-python Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package main import ( @@ -9,8 +13,6 @@ import ( "testing" ) -const embeddingTestOutput = "testdata/embedding_out_golden.txt" - var regen = flag.Bool("regen", false, "regenerate golden files") func TestEmbeddedExample(t *testing.T) { @@ -25,33 +27,36 @@ func TestEmbeddedExample(t *testing.T) { cmd := exec.Command("go", "build", "-o", exe, ".") err = cmd.Run() if err != nil { - t.Fatalf("failed to compile embedding example: %v", err) + t.Fatalf("failed to compile embedding example: %+v", err) } out := new(bytes.Buffer) cmd = exec.Command(exe, "mylib-demo.py") cmd.Stdout = out + cmd.Stderr = out err = cmd.Run() if err != nil { - t.Fatalf("failed to run embedding binary: %v", err) + t.Fatalf("failed to run embedding binary: %+v", err) } - testOutput := out.Bytes() + const fname = "testdata/embedding_out_golden.txt" + + got := out.Bytes() flag.Parse() if *regen { - err = os.WriteFile(embeddingTestOutput, testOutput, 0644) + err = os.WriteFile(fname, got, 0644) if err != nil { - t.Fatalf("failed to write test output: %v", err) + t.Fatalf("could not write golden file: %+v", err) } } - mustMatch, err := os.ReadFile(embeddingTestOutput) + want, err := os.ReadFile(fname) if err != nil { - t.Fatalf("failed read %q", embeddingTestOutput) + t.Fatalf("could not read golden file: %+v", err) } - if !bytes.Equal(testOutput, mustMatch) { - t.Fatalf("embedded test output did not match accepted output from %q", embeddingTestOutput) + if !bytes.Equal(got, want) { + t.Fatalf("stdout differ:\ngot:\n%s\nwant:\n%s\n", got, want) } } diff --git a/examples/embedding/mylib-demo.py b/examples/embedding/mylib-demo.py index 4a461023..1785c307 100644 --- a/examples/embedding/mylib-demo.py +++ b/examples/embedding/mylib-demo.py @@ -1,3 +1,7 @@ +# Copyright 2022 The go-python Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + print(''' Welcome to a gpython embedded example, where your wildest Go-based python dreams come true!''') diff --git a/examples/multi-context/main.go b/examples/multi-context/main.go index e9f72212..7559d60e 100644 --- a/examples/multi-context/main.go +++ b/examples/multi-context/main.go @@ -36,7 +36,6 @@ func main() { // Give each trial a fresh start runtime.GC() } - } var jobScript = ` diff --git a/main.go b/main.go index 5aca0dc1..77fe4927 100644 --- a/main.go +++ b/main.go @@ -41,10 +41,12 @@ Full options: func main() { flag.Usage = syntaxError flag.Parse() - args := flag.Args() + xmain(flag.Args()) +} +func xmain(args []string) { opts := py.DefaultContextOpts() - opts.SysArgs = flag.Args() + opts.SysArgs = args ctx := py.NewContext(opts) if *cpuprofile != "" { @@ -77,5 +79,4 @@ func main() { log.Fatal(err) } } - } diff --git a/main_test.go b/main_test.go new file mode 100644 index 00000000..e41ed869 --- /dev/null +++ b/main_test.go @@ -0,0 +1,61 @@ +// Copyright 2022 The go-python Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "bytes" + "flag" + "os" + "os/exec" + "path/filepath" + "testing" +) + +var regen = flag.Bool("regen", false, "regenerate golden files") + +func TestGPython(t *testing.T) { + + tmp, err := os.MkdirTemp("", "go-python-") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmp) + + exe := filepath.Join(tmp, "out.exe") + cmd := exec.Command("go", "build", "-o", exe, ".") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err = cmd.Run() + if err != nil { + t.Fatalf("failed to compile embedding example: %+v", err) + } + + got, err := exec.Command(exe, "testdata/hello.py").CombinedOutput() + if err != nil { + t.Fatalf("could not run gpython:\n%s\nerr: %+v", got, err) + } + + const fname = "testdata/hello_golden.txt" + + flag.Parse() + if *regen { + err = os.WriteFile(fname, got, 0644) + if err != nil { + t.Fatalf("could not write golden file: %+v", err) + } + } + + want, err := os.ReadFile(fname) + if err != nil { + t.Fatalf("could not read golden file: %+v", err) + } + if !bytes.Equal(got, want) { + t.Fatalf("stdout differ:\ngot:\n%s\nwant:\n%s\n", got, want) + } +} + +func TestRunFile(t *testing.T) { + xmain([]string{"./testdata/hello.py"}) +} diff --git a/testdata/hello.py b/testdata/hello.py new file mode 100644 index 00000000..e94acca7 --- /dev/null +++ b/testdata/hello.py @@ -0,0 +1 @@ +print("hello, world!") diff --git a/testdata/hello_golden.txt b/testdata/hello_golden.txt new file mode 100644 index 00000000..270c611e --- /dev/null +++ b/testdata/hello_golden.txt @@ -0,0 +1 @@ +hello, world! 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