Categorygo.nhat.io/surveyexpect
modulepackage
0.8.0
Repository: https://github.com/nhatthm/surveyexpect.git
Documentation: pkg.go.dev

# README

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

TypeSupportedSupported Actions
Confirm
  • Answer yes, no or a custom one
  • Interrupt (^C)
  • Ask for help
EditorThere 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

# Packages

No description provided by the author
Package options provide options for configuring survey options.

# Functions

Expect creates an expected survey with expectations and assures that ExpectationsWereMet() is called.
IsIgnoredError checks whether the error is ignored.
IsInterrupted checks if the error is terminal.InterruptErr or not.
IsNothingTodo checks if the error is ErrNothingToDo or not.
New creates a new expected survey.
NewSignal creates a new Signal.
WaitForReaction creates a small delay to simulate human reaction.

# Variables

ErrNotFinished indicates that the step is not finished.
ErrNothingToDo indicates that there is nothing to do.
ErrSequenceClosed indicates that the step is closed and does not take more action.
ReactionTime is to create a small delay to simulate human reaction.

# Structs

Action sends an action.
Buffer is a goroutine safe bytes.Buffer.
ConfirmAnswer is an answer for confirm question.
ConfirmPrompt is an expectation of survey.Confirm.
HelpAction sends a ? to show the help.
HelpAnswer sends a ? to show the help.
InlineSteps is for internal steps and they are part of an expectation.
InputAnswer is an answer for password question.
InputPrompt is an expectation of survey.Input.
InputSuggestionSteps is a sequence of steps when user is in suggestion mode.
InterruptAnswer sends an interrupt sequence to terminate the survey.
MultilineAnswer is an answer for password question.
MultilinePrompt is an expectation of survey.Multiline.
MultiSelectPrompt is an expectation of survey.Select.
NoAnswer sends an empty line to answer the question.
PasswordAnswer is an answer for password question.
PasswordPrompt is an expectation of survey.Password.
SelectPrompt is an expectation of survey.Select.
Signal is a safe chan to notify the others.
Steps is a chain of Step.
Survey is a expectations container and responsible for testing the prompts.
TypeAnswer types an answer.

# Interfaces

Answer is an expectation for answering a question.
Console is an interface wrapper around *expect.Console.
Prompt is a prompt expectation for a survey.
Step is an execution step for a survey.
StringWriter is a wrapper for bytes.Buffer.
TestingT is an interface wrapper around *testing.T.

# Type aliases

ExpectOption is option for the survey.
Expector exp survey.
MultiSelectExpect expects a multiselect list from console.
SelectExpect expects a select list from console.
StringExpect expects a string from console.