Skip to content

Commit

Permalink
Example RedHatOfficial#7: Communication with Python interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
tisnik committed Nov 27, 2019
1 parent 8bcd4f3 commit ff3f7b4
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions testing/go-expect/07_python_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"os"
"os/exec"
"testing"
"time"

expect "github.com/Netflix/go-expect"
)

func TestPythonInterpreter(t *testing.T) {
console, err := expect.NewConsole(expect.WithStdout(os.Stdout))
if err != nil {
t.Fatal(err)
}
defer console.Close()
t.Log("Console created")

command := exec.Command("python")
command.Stdin = console.Tty()
command.Stdout = console.Tty()
command.Stderr = console.Tty()

err = command.Start()
if err != nil {
t.Fatal(err)
}

t.Log("Python interpreter started")
time.Sleep(time.Second)

str, err := console.Expect(expect.String("Python 2", "Python 3"), expect.WithTimeout(100*time.Millisecond))
if err != nil {
t.Fatal("Python not detected")
}
t.Log("Python interpreter detected: " + str)

if str == "Python 2" {
console.SendLine("print 1,2,3")
_, err = console.ExpectString("1 2 3")
if err != nil {
t.Fatal("print statement failure")
}
t.Log("print statement works as expected")
_, err = console.ExpectString(">>> ")
if err != nil {
t.Fatal("prompt is not displayed")
}
} else {
console.SendLine("print(1,2,3)")
_, err = console.ExpectString("1 2 3")
if err != nil {
t.Fatal("print function failure")
}
t.Log("print function works as expected")
_, err = console.ExpectString(">>> ")
if err != nil {
t.Fatal("prompt is not displayed")
}
}

console.SendLine("quit()")

err = command.Wait()
if err != nil {
t.Fatal(err)
}
t.Log("Done")
}

0 comments on commit ff3f7b4

Please sign in to comment.