Skip to content

Commit

Permalink
examples/embedding: use new pytest testing scaffolding
Browse files Browse the repository at this point in the history
Co-authored-by: Drew O'Meara <[email protected]>
Signed-off-by: Sebastien Binet <[email protected]>
  • Loading branch information
sbinet and Drew O'Meara committed Aug 17, 2022
1 parent 89a6ebc commit cf9017e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 59 deletions.
16 changes: 8 additions & 8 deletions examples/embedding/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ modules that are only available in CPython.

### Packing List

| | |
|---------------------- | ------------------------------------------------------------------|
| `main.go` | if no args, runs in REPL mode, otherwise runs the given file |
| `lib/mylib.py` | models a library that your application would expose for users |
| `lib/REPL-startup.py` | invoked by `main.go` when starting REPL mode |
| `mylib-demo.py` | models a user-authored script that consumes `mylib` |
| `mylib.module.go` | Go implementation of `mylib_go` consumed by `mylib` |
| | |
|------------------------- | ------------------------------------------------------------------|
| `main.go` | if no args, runs in REPL mode, otherwise runs the given file |
| `lib/mylib.py` | models a library that your application would expose for users |
| `lib/REPL-startup.py` | invoked by `main.go` when starting REPL mode |
| `testdata/mylib-demo.py` | models a user-authored script that consumes `mylib` |
| `mylib.module.go` | Go implementation of `mylib_go` consumed by `mylib` |


### Invoking a Python Script

```bash
$ cd examples/embedding/
$ go build .
$ ./embedding mylib-demo.py
$ ./embedding ./testdata/mylib-demo.py
```
```
Welcome to a gpython embedded example,
Expand Down
53 changes: 3 additions & 50 deletions examples/embedding/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,11 @@
package main

import (
"bytes"
"flag"
"os"
"os/exec"
"path/filepath"
"testing"
)

var regen = flag.Bool("regen", false, "regenerate golden files")
"github.com/go-python/gpython/pytest"
)

func TestEmbeddedExample(t *testing.T) {

tmp, err := os.MkdirTemp("", "go-python-embedding-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)

exe := filepath.Join(tmp, "out.exe")
cmd := exec.Command("go", "build", "-o", exe, ".")
err = cmd.Run()
if err != nil {
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)
}

const fname = "testdata/embedding_out_golden.txt"

got := out.Bytes()

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)
}
pytest.RunScript(t, "./testdata/mylib-demo.py")
}
2 changes: 1 addition & 1 deletion examples/embedding/mylib.module.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func init() {
"MYLIB_VERS": py.String("Vacation 1.0 by Fletch F. Fletcher"),
},
OnContextClosed: func(instance *py.Module) {
fmt.Print("<<< host py.Context of py.Module instance closing >>>\n+++\n")
py.Println(instance, "<<< host py.Context of py.Module instance closing >>>\n+++")
},
})
}
Expand Down
File renamed without changes.

0 comments on commit cf9017e

Please sign in to comment.