Skip to content

Commit cf9017e

Browse files
sbinetDrew O'Meara
andcommitted
examples/embedding: use new pytest testing scaffolding
Co-authored-by: Drew O'Meara <drew@plan-systems.org> Signed-off-by: Sebastien Binet <binet@cern.ch>
1 parent 89a6ebc commit cf9017e

File tree

5 files changed

+12
-59
lines changed

5 files changed

+12
-59
lines changed

examples/embedding/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ modules that are only available in CPython.
3535

3636
### Packing List
3737

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

4646

4747
### Invoking a Python Script
4848

4949
```bash
5050
$ cd examples/embedding/
5151
$ go build .
52-
$ ./embedding mylib-demo.py
52+
$ ./embedding ./testdata/mylib-demo.py
5353
```
5454
```
5555
Welcome to a gpython embedded example,

examples/embedding/main_test.go

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,11 @@
55
package main
66

77
import (
8-
"bytes"
9-
"flag"
10-
"os"
11-
"os/exec"
12-
"path/filepath"
138
"testing"
14-
)
159

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

1813
func TestEmbeddedExample(t *testing.T) {
19-
20-
tmp, err := os.MkdirTemp("", "go-python-embedding-")
21-
if err != nil {
22-
t.Fatal(err)
23-
}
24-
defer os.RemoveAll(tmp)
25-
26-
exe := filepath.Join(tmp, "out.exe")
27-
cmd := exec.Command("go", "build", "-o", exe, ".")
28-
err = cmd.Run()
29-
if err != nil {
30-
t.Fatalf("failed to compile embedding example: %+v", err)
31-
}
32-
33-
out := new(bytes.Buffer)
34-
cmd = exec.Command(exe, "mylib-demo.py")
35-
cmd.Stdout = out
36-
cmd.Stderr = out
37-
38-
err = cmd.Run()
39-
if err != nil {
40-
t.Fatalf("failed to run embedding binary: %+v", err)
41-
}
42-
43-
const fname = "testdata/embedding_out_golden.txt"
44-
45-
got := out.Bytes()
46-
47-
flag.Parse()
48-
if *regen {
49-
err = os.WriteFile(fname, got, 0644)
50-
if err != nil {
51-
t.Fatalf("could not write golden file: %+v", err)
52-
}
53-
}
54-
55-
want, err := os.ReadFile(fname)
56-
if err != nil {
57-
t.Fatalf("could not read golden file: %+v", err)
58-
}
59-
if !bytes.Equal(got, want) {
60-
t.Fatalf("stdout differ:\ngot:\n%s\nwant:\n%s\n", got, want)
61-
}
14+
pytest.RunScript(t, "./testdata/mylib-demo.py")
6215
}

examples/embedding/mylib.module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func init() {
4848
"MYLIB_VERS": py.String("Vacation 1.0 by Fletch F. Fletcher"),
4949
},
5050
OnContextClosed: func(instance *py.Module) {
51-
fmt.Print("<<< host py.Context of py.Module instance closing >>>\n+++\n")
51+
py.Println(instance, "<<< host py.Context of py.Module instance closing >>>\n+++")
5252
},
5353
})
5454
}

0 commit comments

Comments
 (0)
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