Skip to content

examples/{embedding,multi-context}: add LICENSE blurb, cosmetics #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 12, 2022
Merged
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
4 changes: 3 additions & 1 deletion examples/embedding/lib/REPL-startup.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 4 additions & 2 deletions examples/embedding/lib/mylib.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -47,5 +51,3 @@ def PrintItinerary(self):
i += 1

print("### Made with %s " % self._libVers)


27 changes: 16 additions & 11 deletions examples/embedding/main_test.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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) {
Expand All @@ -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)
}
}
4 changes: 4 additions & 0 deletions examples/embedding/mylib-demo.py
Original file line number Diff line number Diff line change
@@ -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!''')
Expand Down
1 change: 0 additions & 1 deletion examples/multi-context/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func main() {
// Give each trial a fresh start
runtime.GC()
}

}

var jobScript = `
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -77,5 +79,4 @@ func main() {
log.Fatal(err)
}
}

}
61 changes: 61 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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"})
}
1 change: 1 addition & 0 deletions testdata/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("hello, world!")
1 change: 1 addition & 0 deletions testdata/hello_golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello, world!