Skip to content

Commit 01cbebd

Browse files
committed
gpython: add a simple test for gpython command
Signed-off-by: Sebastien Binet <binet@cern.ch>
1 parent e865158 commit 01cbebd

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ Full options:
4141
func main() {
4242
flag.Usage = syntaxError
4343
flag.Parse()
44-
args := flag.Args()
44+
xmain(flag.Args())
45+
}
4546

47+
func xmain(args []string) {
4648
opts := py.DefaultContextOpts()
47-
opts.SysArgs = flag.Args()
49+
opts.SysArgs = args
4850
ctx := py.NewContext(opts)
4951

5052
if *cpuprofile != "" {
@@ -77,5 +79,4 @@ func main() {
7779
log.Fatal(err)
7880
}
7981
}
80-
8182
}

main_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2022 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"bytes"
9+
"flag"
10+
"os"
11+
"os/exec"
12+
"path/filepath"
13+
"testing"
14+
)
15+
16+
var regen = flag.Bool("regen", false, "regenerate golden files")
17+
18+
func TestGPython(t *testing.T) {
19+
20+
tmp, err := os.MkdirTemp("", "go-python-")
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+
cmd.Stdout = os.Stdout
29+
cmd.Stderr = os.Stderr
30+
err = cmd.Run()
31+
if err != nil {
32+
t.Fatalf("failed to compile embedding example: %+v", err)
33+
}
34+
35+
got, err := exec.Command(exe, "testdata/hello.py").CombinedOutput()
36+
if err != nil {
37+
t.Fatalf("could not run gpython:\n%s\nerr: %+v", got, err)
38+
}
39+
40+
const fname = "testdata/hello_golden.txt"
41+
42+
flag.Parse()
43+
if *regen {
44+
err = os.WriteFile(fname, got, 0644)
45+
if err != nil {
46+
t.Fatalf("could not write golden file: %+v", err)
47+
}
48+
}
49+
50+
want, err := os.ReadFile(fname)
51+
if err != nil {
52+
t.Fatalf("could not read golden file: %+v", err)
53+
}
54+
if !bytes.Equal(got, want) {
55+
t.Fatalf("stdout differ:\ngot:\n%s\nwant:\n%s\n", got, want)
56+
}
57+
}
58+
59+
func TestRunFile(t *testing.T) {
60+
xmain([]string{"./testdata/hello.py"})
61+
}

testdata/hello.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("hello, world!")

testdata/hello_golden.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello, world!

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