Skip to content

nhatthm/surveyexpect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Expects for AlecAivazis/survey

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

surveyexpect is an Expect library for AlecAivazis/survey/v2

Prerequisites

  • Go >= 1.17

Install

go get go.nhat.io/surveyexpect

Usage

Supported Types

Type Supported Supported Actions
Confirm
  • Answer yes, no or a custom one
  • Interrupt (^C)
  • Ask for help
Editor There is no plan for support
Input
  • Answer
  • No answer
  • Suggestions with navigation (Arrow Up , Arrow Down , Tab , Esc , Enter )
  • Interrupt (^C)
  • Ask for help
Multiline
  • Answer
  • No answer
  • Interrupt (^C)
Multiselect
  • Type to filter, delete
  • Navigation (Move Up , Move Down , Select None , Select All , Tab , Enter )
  • Interrupt (^C)
  • Ask for help
Password
  • Answer (+ check for *)
  • No answer
  • Interrupt (^C)
  • Ask for help
Select
  • Type to filter, delete
  • Navigation (Move Up , Move Down , Tab , Enter )
  • Interrupt (^C)
  • Ask for help

Expect

There are 2 steps:

Step 1: Create an expectation.

Call surveyexpect.Expect()

s := surveyexpect.Expect(func(s *surveyexpect.Survey) {
    s.ExpectPassword("Enter a password:").
        Answer("secret")
})(t) // t is *testing.T

Step 2: Run it.

Important: Use the stdio arg and inject it into the survey.Prompt otherwise it won't work.

s.Start(func(stdio terminal.Stdio)) {
    // For example
    p := &survey.Password{Message: "Enter a password:"}
    var answer string
    err := survey.AskOne(p, &answer, surveyexpect.WithStdio(stdio))

    // Asserts.
    assert.Equal(t, "123456", answer)
    assert.NoError(t, err)
})

Examples

package mypackage_test

import (
	"testing"

	"github.com/AlecAivazis/survey/v2"
	"github.com/AlecAivazis/survey/v2/terminal"
	"github.com/stretchr/testify/assert"
	"go.nhat.io/surveyexpect"
)

func TestMyPackage(t *testing.T) {
	t.Parallel()

	testCases := []struct {
		scenario       string
		expectSurvey   surveyexpect.Expector
		expectedAnswer string
		expectedError  string
	}{
		{
			scenario: "empty answer",
			expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
				s.ExpectPassword("Enter a password:").
					Answer("")
			}),
		},
		{
			scenario: "password without help",
			expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
				s.ExpectPassword("Enter a password:").
					Answer("secret")
			}),
			expectedAnswer: "secret",
		},

		{
			scenario: "input is interrupted",
			expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
				s.ExpectPassword("Enter a password:").
					Interrupt()
			}),
			expectedError: "interrupt",
		},
	}

	for _, tc := range testCases {
		tc := tc
		t.Run(tc.scenario, func(t *testing.T) {
			t.Parallel()

			p := &survey.Password{Message: "Enter a password:"}

			// Start the survey.
			tc.expectSurvey(t).Start(func(stdio terminal.Stdio) {
				// Run your logic here.
				// For example.
				var answer string
				err := survey.AskOne(p, &answer, surveyexpect.WithStdio(stdio))

				assert.Equal(t, tc.expectedAnswer, answer)

				if tc.expectedError == "" {
					assert.NoError(t, err)
				} else {
					assert.EqualError(t, err, tc.expectedError)
				}
			})
		})
	}
}

You can find more examples in the tests of this library:

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

       or scan this