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 1 commit
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
Prev Previous commit
gpython: add a simple test for gpython command
Signed-off-by: Sebastien Binet <binet@cern.ch>
  • Loading branch information
sbinet committed Feb 12, 2022
commit 01cbebd2719ccda5bb8fcfe008707617d1452f02
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!