AlecAivazis / survey

A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
MIT License
4.08k stars 350 forks source link

Issue with executing test suggested in readme #168

Closed singhrasster closed 5 years ago

singhrasster commented 5 years ago

Following the readme, I was trying to play with the library and write a test simulating the command line input and output.

My main.go looks like:

package main
import (
    "fmt"
    "gopkg.in/AlecAivazis/survey.v1"
)
var qs = []*survey.Question{
    {
        Name:     "name",
        Prompt:   &survey.Input{Message: "What is your name?"},
        Validate: survey.Required,
        Transform: survey.Title,
    },
    {
        Name: "color",
        Prompt: &survey.Select{
            Message: "Choose a color:",
            Options: []string{"red", "blue", "green"},
            Default: "red",
        },
    },
    {
        Name: "age",
        Prompt:   &survey.Input{Message: "How old are you?"},
    },
}
func main() {
    answers := struct {
        Name          string                  
        FavoriteColor string `survey:"color"` 
        Age           int                    
    }{}
    err := survey.Ask(qs, &answers)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    fmt.Printf("%s chose %s.", answers.Name, answers.FavoriteColor)
}

I did go build main.go to create a Go executible

I wrote a test as:

package main
import (
    "testing"
    "github.com/Netflix/go-expect"
    "os"
    "os/exec"
    "fmt"
    "log"
)
func TestCLI(t *testing.T) {
    c, err:= expect.NewConsole(expect.WithStdout(os.Stdout))
    if err != nil {
        log.Fatal("console failed: " + err.Error() + ". Exiting.")
    }
    defer c.Close()
    donec := make(chan struct{})
    go func() {
        defer close(donec)
            c.ExpectString("What is your name?")
            c.SendLine("Johnny")
            c.ExpectEOF()
                c.ExpectString("Choose a color:")
            c.SendLine("red")
            c.ExpectEOF()
        c.ExpectString("How old are you?")
            c.SendLine("10")
            c.ExpectEOF()
    }()
    cmd := exec.Command("./main")
    cmd.Stdin = c.Tty()
    cmd.Stdout = c.Tty()
    cmd.Stderr = c.Tty()
    if err := cmd.Run(); err != nil {
        log.Fatal("CV HSM Client Installation failed: " + err.Error() + ". Exiting.")
    }
    c.Tty().Close()
    <-donec
    // Dump the terminal's screen.
    //t.Log(expect.StripTrailingEmptyLines(state.String()))
}

When I run this test as 'go test'

I get the following output:

? What is your name? 
^[[24;80R

Not sure why it prints "^[[24;80R".

Shouldn't it be printing the three questions above followed by answers specified above? Am I missing something or my understanding is wrong on how it should work?

AlecAivazis commented 5 years ago

Hey @singhrasster that doesn't sound right to me either. Can you tell me some more info about your system? I'm mostly interested in which operating system and terminal you are using

AlecAivazis commented 5 years ago

I'm going to close this since it's gone stale

aleksandrzhiliaev commented 3 years ago

Hey @AlecAivazis , I am getting same issue:

package main

import (
    "fmt"
    "gopkg.in/AlecAivazis/survey.v1"
)

// the questions to ask
var qs = []*survey.Question{
    {
        Name:      "name",
        Prompt:    &survey.Input{Message: "What is your name?"},
        Validate:  survey.Required,
        Transform: survey.Title,
    },
    {
        Name: "color",
        Prompt: &survey.Select{
            Message: "Choose a color:",
            Options: []string{"red", "blue", "green"},
            Default: "red",
        },
    },
    {
        Name:   "age",
        Prompt: &survey.Input{Message: "How old are you?"},
    },
}

func main() {
    // the answers will be written to this struct
    answers := struct {
        Name          string // survey will match the question and field names
        FavoriteColor string `survey:"color"` // or you can tag fields to match a specific name
        Age           int    // if the types don't match, survey will convert it
    }{}

    // perform the questions
    err := survey.Ask(qs, &answers)
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Printf("%s chose %s.", answers.Name, answers.FavoriteColor)
}

Trying to run with go-expect:

package main

import (
    "github.com/Netflix/go-expect"
    "log"
    "os"
    "os/exec"
)

func main() {
    c, err := expect.NewConsole(expect.WithStdout(os.Stdout))
    if err != nil {
        log.Fatal(err)
    }
    defer c.Close()

    cmd := exec.Command("/example")
    cmd.Stdin = c.Tty()
    cmd.Stdout = c.Tty()
    cmd.Stderr = c.Tty()

    err = cmd.Start()
    if err != nil {
        log.Fatal(err)
    }

    c.ExpectString("What is your name?")
    c.SendLine("A")
    c.ExpectString("Choose a color:")
    c.SendLine("red")
    c.ExpectString("How old are you?")
    c.SendLine("10")
    c.ExpectEOF()

    err = cmd.Wait()
    if err != nil {
        log.Fatal(err)
    }
}

Getting:

? What is your name?

^[[47;101R

Any clue how to make it work?