Closed Prnyself closed 4 years ago
We are facing the same problem, seems like when trying to read runes something unexpected happened. @Prnyself this problem was solved with a turnaround implementation?
We are facing the same problem, seems like when trying to read runes something unexpected happened. @Prnyself this problem was solved with a turnaround implementation?
Still not figure out. sigh
@Prnyself i figured it out. The following snippet may help you:
func terminalRunner() {
bufOut := new(bytes.Buffer)
c, err := expect.NewConsole(expect.WithStdout(bufOut))
if err != nil {
fmt.Println(err)
}
defer c.Close()
cmd := exec.Command("rit", "scaffold", "generate", "coffee-go")
cmd.Stdin = c.Tty()
cmd.Stdout = c.Tty()
go func() {
c.ExpectEOF()
}()
testCases := []testCase{
{input: "test", expectOut: ".*Type your name.*"},
{input: "es", expectOut: ".*Pick your coffee.*"},
{input: "", expectOut: ".*Delivery.*"},
}
go func() {
for i := 0; i < len(testCases); i++ {
for {
matched, _ := regexp.MatchString(testCases[i].expectOut, bufOut.String())
if matched {
c.SendLine(testCases[i].input)
break
}
time.Sleep(1 * time.Second)
}
}
}()
cmd.Start()
cmd.Wait()
fmt.Println(bufOut.String())
}
cool, that looks similar with input manually.
By the way, the package expect
indicates go-expect ?
Yes, it is. Ops, I missed the import section, here it goes:
import (
"bytes"
"fmt"
"github.com/Netflix/go-expect"
"os/exec"
"regexp"
"time"
)
Thank you for your help! Let's just leave this open, and see whether the maintainer would raise any native-project features for this.
I want to test my code and inject the input from another file for mocking input in
os.Stdin
, and I tried to replaceos.Stdin
with a tempfile or usesurvey.WithStdio
to inject the tempfile, but neither worked. Looks like this buffer turned into all-zero-bytes-slice, so thatrr.ReadRune()
cannot read rune fromstdio.In
and just return anEOF
error. So how can I get input from another file?