surveyexpect

package module
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2022 License: MIT Imports: 16 Imported by: 1

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
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

Documentation

Overview

Package surveyexpect provides expects for github.com/AlecAivazis/survey.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNothingToDo indicates that there is nothing to do.
	ErrNothingToDo = errors.New("nothing to do")
	// ErrNotFinished indicates that the step is not finished.
	ErrNotFinished = errors.New("step is not finished")
	// ErrSequenceClosed indicates that the step is closed and does not take more action.
	ErrSequenceClosed = errors.New("sequence is closed")
)
View Source
var ReactionTime = 10 * time.Millisecond

ReactionTime is to create a small delay to simulate human reaction.

Functions

func IsIgnoredError

func IsIgnoredError(err error) bool

IsIgnoredError checks whether the error is ignored.

func IsInterrupted

func IsInterrupted(err error) bool

IsInterrupted checks if the error is terminal.InterruptErr or not.

func IsNothingTodo

func IsNothingTodo(err error) bool

IsNothingTodo checks if the error is ErrNothingToDo or not.

func WaitForReaction

func WaitForReaction() <-chan time.Time

WaitForReaction creates a small delay to simulate human reaction.

Types

type Action

type Action struct {
	// contains filtered or unexported fields
}

Action sends an action.

func (*Action) Do

func (a *Action) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*Action) String

func (a *Action) String() string

String represents the answer as a string.

type Answer

type Answer interface {
	Step
}

Answer is an expectation for answering a question.

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer is a goroutine safe bytes.Buffer.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset resets the buffer to be empty, but it retains the underlying storage for use by future writes. Reset is the same as Truncate(0).

func (*Buffer) String

func (b *Buffer) String() string

String returns the contents of the unread portion of the buffer as a string. If the Buffer is a nil pointer, it returns "<nil>".

To build strings more efficiently, see the strings.Builder type.

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (int, error)

Write appends the contents of p to the buffer, growing the buffer as needed. The return value n is the length of p; err is always nil. If the buffer becomes too large, Write will panic with ErrTooLarge.

type ConfirmAnswer

type ConfirmAnswer struct {
	// contains filtered or unexported fields
}

ConfirmAnswer is an answer for confirm question.

func (*ConfirmAnswer) Do

func (a *ConfirmAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*ConfirmAnswer) Interrupted

func (a *ConfirmAnswer) Interrupted()

Interrupted expects the answer will be interrupted.

func (*ConfirmAnswer) String

func (a *ConfirmAnswer) String() string

String represents the answer as a string.

type ConfirmPrompt

type ConfirmPrompt struct {
	// contains filtered or unexported fields
}

ConfirmPrompt is an expectation of survey.Confirm.

func (*ConfirmPrompt) Answer

func (c *ConfirmPrompt) Answer(answer string) *ConfirmAnswer

Answer sets a custom answer to the prompt.

If the answer is not not empty, the survey expects to have a feedback from the prompt:

`Sorry, your reply was invalid: "hello world!" is not a valid answer, please try again.`

Survey.ExpectConfirm("Are you sure to delete this file?").
	Answer("hello world!").

func (*ConfirmPrompt) Do

func (c *ConfirmPrompt) Do(console Console) error

Do runs the step.

func (*ConfirmPrompt) Interrupt

func (c *ConfirmPrompt) Interrupt()

Interrupt marks the answer is interrupted.

Survey.ExpectConfirm("Are you sure to delete this file?").
	Interrupt().

func (*ConfirmPrompt) No

func (c *ConfirmPrompt) No()

No sets "no" as the answer to the prompt.

Survey.ExpectConfirm("Are you sure to delete this file?").
	No().

func (*ConfirmPrompt) ShowHelp

func (c *ConfirmPrompt) ShowHelp(help string, options ...string)

ShowHelp sets help for the expectation.

Survey.ExpectConfirm("Are you sure to delete this file?").
	ShowHelp("The file will be permanently deleted").

func (*ConfirmPrompt) String

func (c *ConfirmPrompt) String() string

String represents the expectation as a string.

func (*ConfirmPrompt) Yes

func (c *ConfirmPrompt) Yes()

Yes sets "yes" as the answer to the prompt.

Survey.ExpectConfirm("Are you sure to delete this file?").
	Yes().

type Console

type Console interface {
	// terminal device.
	Tty() *os.File

	// pty.
	Fd() uintptr

	// Close closes Console's tty. Calling Close will unblock Expect and ExpectEOF.
	Close() error

	// Send writes string s to Console's tty.
	Send(s string) (int, error)

	// SendLine writes string s to Console's tty with a trailing newline.
	SendLine(s string) (int, error)

	// Expectf reads from the Console's tty until the provided formatted string
	// is read or an error occurs, and returns the buffer read by Console.
	Expectf(format string, args ...interface{}) (string, error)

	// ExpectString reads from Console's tty until the provided string is read or
	// an error occurs, and returns the buffer read by Console.
	ExpectString(s string) (string, error)

	// ExpectEOF reads from Console's tty until EOF or an error occurs, and returns
	// the buffer read by Console.  We also treat the PTSClosed error as an EOF.
	ExpectEOF() (string, error)

	// Expect reads from Console's tty until a condition specified from opts is
	// encountered or an error occurs, and returns the buffer read by console.
	// No extra bytes are read once a condition is met, so if a program isn't
	// expecting input yet, it will be blocked. Sends are queued up in tty's
	// internal buffer so that the next Expect will read the remaining bytes (i.e.
	// rest of prompt) as well as its conditions.
	Expect(opts ...expect.ExpectOpt) (string, error)
}

Console is an interface wrapper around *expect.Console.

type ExpectOption

type ExpectOption func(s *Survey)

ExpectOption is option for the survey.

type Expector

type Expector func(t TestingT) *Survey

Expector exp survey.

func Expect

func Expect(options ...ExpectOption) Expector

Expect creates an expected survey with expectations and assures that ExpectationsWereMet() is called.

type HelpAction

type HelpAction struct {
	// contains filtered or unexported fields
}

HelpAction sends a ? to show the help.

func (*HelpAction) Do

func (a *HelpAction) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*HelpAction) String

func (a *HelpAction) String() string

String represents the answer as a string.

type HelpAnswer

type HelpAnswer struct {
	// contains filtered or unexported fields
}

HelpAnswer sends a ? to show the help.

func (*HelpAnswer) Do

func (a *HelpAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*HelpAnswer) String

func (a *HelpAnswer) String() string

String represents the answer as a string.

type InlineSteps

type InlineSteps struct {
	*Steps
}

InlineSteps is for internal steps and they are part of an expectation.

func (*InlineSteps) String

func (s *InlineSteps) String() string

String represents the answer as a string.

type InputAnswer

type InputAnswer struct {
	// contains filtered or unexported fields
}

InputAnswer is an answer for password question.

func (*InputAnswer) Do

func (a *InputAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*InputAnswer) Interrupted

func (a *InputAnswer) Interrupted()

Interrupted expects the answer will be interrupted.

func (*InputAnswer) String

func (a *InputAnswer) String() string

String represents the answer as a string.

type InputPrompt

type InputPrompt struct {
	// contains filtered or unexported fields
}

InputPrompt is an expectation of survey.Input.

func (*InputPrompt) Answer

func (p *InputPrompt) Answer(answer string) *InputAnswer

Answer sets the answer to the input prompt.

Survey.ExpectInput("Enter your name:").
	Answer("johnny")

func (*InputPrompt) Do

func (p *InputPrompt) Do(c Console) error

Do runs the step.

func (*InputPrompt) Interrupt

func (p *InputPrompt) Interrupt()

Interrupt marks the answer is interrupted.

Survey.ExpectInput("Enter your name:").
	Interrupt()

func (*InputPrompt) Once

func (p *InputPrompt) Once() *InputPrompt

Once indicates that the message should only be asked once.

Survey.ExpectInput("Enter your name:").
	Answer("johnny").
	Once()

func (*InputPrompt) ShowHelp

func (p *InputPrompt) ShowHelp(help string, options ...string)

ShowHelp sets help for the expectation.

Survey.ExpectInput("Enter your name:").
	ShowHelp("It's your full name")

func (*InputPrompt) String

func (p *InputPrompt) String() string

String represents the expectation as a string.

func (*InputPrompt) Tab

func (p *InputPrompt) Tab(times ...int) *InputSuggestionSteps

Tab starts a sequence of steps to interact with suggestion mode. Default is 1 when omitted.

Survey.ExpectInput("Enter your name:").
	Tab()

func (*InputPrompt) Times

func (p *InputPrompt) Times(i int) *InputPrompt

Times indicates that the message should only be asked the indicated number of times.

Survey.ExpectInput("Enter your name:").
	Answer("johnny").
	Times(5)

func (*InputPrompt) Twice

func (p *InputPrompt) Twice() *InputPrompt

Twice indicates that the message should only be asked twice.

Survey.ExpectInput("Enter your name:").
	Answer("johnny").
	Twice()

func (*InputPrompt) Type

Type starts a sequence of steps to interact with suggestion mode.

Survey.ExpectInput("Enter your name:").
	Type("johnny")

type InputSuggestionSteps

type InputSuggestionSteps struct {
	// contains filtered or unexported fields
}

InputSuggestionSteps is a sequence of steps when user is in suggestion mode.

func (*InputSuggestionSteps) Delete

func (a *InputSuggestionSteps) Delete(times ...int) *InputSuggestionSteps

Delete sends the DELETE key the indicated times. Default is 1 when omitted.

Survey.ExpectInput("Enter your name:").
	Type("johnny").
	Delete(5)

func (*InputSuggestionSteps) Do

Do runs the step.

func (*InputSuggestionSteps) Enter

func (a *InputSuggestionSteps) Enter()

Enter sends the ENTER key and ends the sequence.

Survey.ExpectInput("Enter your name:").
	Type("hello").
	Enter()

func (*InputSuggestionSteps) Esc

Esc sends the ESC key.

Survey.ExpectInput("Enter your name:").
	Type("hello").
	Esc()

func (*InputSuggestionSteps) ExpectSuggestions

func (a *InputSuggestionSteps) ExpectSuggestions(suggestions ...string) *InputSuggestionSteps

ExpectSuggestions expects a list of suggestions.

func (*InputSuggestionSteps) Interrupt

Interrupt sends ^C and closes the suggestions.

Survey.ExpectInput("Enter your name:").
	Type("johnny").
	Interrupt()

func (*InputSuggestionSteps) MoveDown

func (a *InputSuggestionSteps) MoveDown(times ...int) *InputSuggestionSteps

MoveDown sends the ARROW DOWN key the indicated times. Default is 1 when omitted.

Survey.ExpectInput("Enter your name:").
	Tab().
	MoveDown(5)

func (*InputSuggestionSteps) MoveUp

func (a *InputSuggestionSteps) MoveUp(times ...int) *InputSuggestionSteps

MoveUp sends the ARROW UP key the indicated times. Default is 1 when omitted.

Survey.ExpectInput("Enter your name:").
	Tab().
	MoveUp(5)

func (*InputSuggestionSteps) String

func (a *InputSuggestionSteps) String() string

String represents the answer as a string.

func (*InputSuggestionSteps) Tab

func (a *InputSuggestionSteps) Tab(times ...int) *InputSuggestionSteps

Tab sends the TAB key the indicated times. Default is 1 when omitted.

Survey.ExpectInput("Enter your name:").
	Type("hello").
	Tab(5)

func (*InputSuggestionSteps) Type

Type sends a string without enter.

Survey.ExpectInput("Enter your name:").
	Type("johnny").
	Tab().
	Type(".c").
	Enter()

type InterruptAnswer

type InterruptAnswer struct{}

InterruptAnswer sends an interrupt sequence to terminate the survey.

func (*InterruptAnswer) Do

func (a *InterruptAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*InterruptAnswer) String

func (a *InterruptAnswer) String() string

String represents the answer as a string.

type MultiSelectExpect

type MultiSelectExpect []string

MultiSelectExpect expects a multiselect list from console.

func (*MultiSelectExpect) Do

func (e *MultiSelectExpect) Do(c Console) error

Do runs the step.

func (*MultiSelectExpect) String

func (e *MultiSelectExpect) String() string

String represents the answer as a string.

type MultiSelectPrompt

type MultiSelectPrompt struct {
	// contains filtered or unexported fields
}

MultiSelectPrompt is an expectation of survey.Select.

func (*MultiSelectPrompt) Delete

func (p *MultiSelectPrompt) Delete(times ...int) *MultiSelectPrompt

Delete sends the DELETE key the indicated times. Default is 1 when omitted.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		Delete(3)

func (*MultiSelectPrompt) Do

func (p *MultiSelectPrompt) Do(c Console) error

Do runs the step.

func (*MultiSelectPrompt) Enter

func (p *MultiSelectPrompt) Enter()

Enter sends the ENTER key and ends the sequence.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		Enter()

func (*MultiSelectPrompt) ExpectOptions

func (p *MultiSelectPrompt) ExpectOptions(options ...string) *MultiSelectPrompt

ExpectOptions expects a list of options.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		ExpectOptions("English")

func (*MultiSelectPrompt) Interrupt

func (p *MultiSelectPrompt) Interrupt()

Interrupt sends ^C and ends the sequence.

   Survey.ExpectMultiSelect("Select a language:").
		Interrupt()

func (*MultiSelectPrompt) MoveDown

func (p *MultiSelectPrompt) MoveDown(times ...int) *MultiSelectPrompt

MoveDown sends the ARROW DOWN key the indicated times. Default is 1 when omitted.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		MoveDown()

func (*MultiSelectPrompt) MoveUp

func (p *MultiSelectPrompt) MoveUp(times ...int) *MultiSelectPrompt

MoveUp sends the ARROW UP key the indicated times. Default is 1 when omitted.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		MoveUp()

func (*MultiSelectPrompt) Select

func (p *MultiSelectPrompt) Select() *MultiSelectPrompt

Select selects an option. If the option is selected, it will be deselected.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		Select()

func (*MultiSelectPrompt) SelectAll

func (p *MultiSelectPrompt) SelectAll() *MultiSelectPrompt

SelectAll selects all filtered options.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		SelectAll()

func (*MultiSelectPrompt) SelectNone

func (p *MultiSelectPrompt) SelectNone() *MultiSelectPrompt

SelectNone deselects all filtered options.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		SelectNone()

func (*MultiSelectPrompt) ShowHelp

func (p *MultiSelectPrompt) ShowHelp(help string, options ...string) *MultiSelectPrompt

ShowHelp asks for help and asserts the help text.

Survey.ExpectMultiSelect("Select a language:").
	ShowHelp("Your preferred language")

func (*MultiSelectPrompt) String

func (p *MultiSelectPrompt) String() string

String represents the expectation as a string.

func (*MultiSelectPrompt) Tab

func (p *MultiSelectPrompt) Tab(times ...int) *MultiSelectPrompt

Tab sends the TAB key the indicated times. Default is 1 when omitted.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		Tab()

func (*MultiSelectPrompt) Type

Type sends some text to filter the options.

Survey.ExpectMultiSelect("Select a language:").
	Type("Eng")

type MultilineAnswer

type MultilineAnswer struct {
	// contains filtered or unexported fields
}

MultilineAnswer is an answer for password question.

func (*MultilineAnswer) Do

func (a *MultilineAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*MultilineAnswer) Interrupted

func (a *MultilineAnswer) Interrupted()

Interrupted expects the answer will be interrupted.

func (*MultilineAnswer) String

func (a *MultilineAnswer) String() string

String represents the answer as a string.

type MultilinePrompt

type MultilinePrompt struct {
	// contains filtered or unexported fields
}

MultilinePrompt is an expectation of survey.Multiline.

func (*MultilinePrompt) Answer

func (p *MultilinePrompt) Answer(answer string) *MultilineAnswer

Answer sets the answer to the input prompt.

Survey.ExpectMultiline("Enter your message:").
	Answer("hello world")

func (*MultilinePrompt) Do

func (p *MultilinePrompt) Do(c Console) error

Do runs the step.

func (*MultilinePrompt) Interrupt

func (p *MultilinePrompt) Interrupt()

Interrupt marks the answer is interrupted.

Survey.ExpectMultiline("Enter your message:").
	Interrupt()

func (*MultilinePrompt) Once

func (p *MultilinePrompt) Once() *MultilinePrompt

Once indicates that the message should only be asked once.

Survey.ExpectMultiline("Enter your message:").
	Answer("hello world").
	Once()

func (*MultilinePrompt) String

func (p *MultilinePrompt) String() string

String represents the expectation as a string.

func (*MultilinePrompt) Times

func (p *MultilinePrompt) Times(i int) *MultilinePrompt

Times indicates that the message should only be asked the indicated number of times.

Survey.ExpectMultiline("Enter your message:").
	Answer("hello world").
	Times(5)

func (*MultilinePrompt) Twice

func (p *MultilinePrompt) Twice() *MultilinePrompt

Twice indicates that the message should only be asked twice.

Survey.ExpectMultiline("Enter your message:").
	Answer("hello world").
	Twice()

type NoAnswer

type NoAnswer struct{}

NoAnswer sends an empty line to answer the question.

func (*NoAnswer) Do

func (a *NoAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*NoAnswer) String

func (a *NoAnswer) String() string

String represents the answer as a string.

type PasswordAnswer

type PasswordAnswer struct {
	// contains filtered or unexported fields
}

PasswordAnswer is an answer for password question.

func (*PasswordAnswer) Do

func (a *PasswordAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*PasswordAnswer) Interrupted

func (a *PasswordAnswer) Interrupted()

Interrupted expects the answer will be interrupted.

func (*PasswordAnswer) String

func (a *PasswordAnswer) String() string

String represents the answer as a string.

type PasswordPrompt

type PasswordPrompt struct {
	// contains filtered or unexported fields
}

PasswordPrompt is an expectation of survey.Password.

func (*PasswordPrompt) Answer

func (p *PasswordPrompt) Answer(answer string) *PasswordAnswer

Answer sets the answer to the password prompt.

Survey.ExpectPassword("Enter password:").
	Answer("hello world!")

func (*PasswordPrompt) Do

func (p *PasswordPrompt) Do(c Console) error

Do runs the step.

func (*PasswordPrompt) Interrupt

func (p *PasswordPrompt) Interrupt()

Interrupt marks the answer is interrupted.

Survey.ExpectPassword("Enter password:").
	Interrupt()

func (*PasswordPrompt) Once

func (p *PasswordPrompt) Once() *PasswordPrompt

Once indicates that the message should only be asked once.

Survey.ExpectPassword("Enter password:").
	Answer("hello world!").
	Once()

func (*PasswordPrompt) ShowHelp

func (p *PasswordPrompt) ShowHelp(help string, options ...string)

ShowHelp sets help for the expectation.

Survey.ExpectPassword("Enter password:").
	ShowHelp("Your shiny password")

func (*PasswordPrompt) String

func (p *PasswordPrompt) String() string

String represents the expectation as a string.

func (*PasswordPrompt) Times

func (p *PasswordPrompt) Times(i int) *PasswordPrompt

Times indicates that the message should only be asked the indicated number of times.

Survey.ExpectPassword("Enter password:").
	Answer("hello world!").
	Times(5)

func (*PasswordPrompt) Twice

func (p *PasswordPrompt) Twice() *PasswordPrompt

Twice indicates that the message should only be asked twice.

Survey.ExpectPassword("Enter password:").
	Answer("hello world!").
	Twice()

type Prompt

type Prompt interface {
	Step
}

Prompt is a prompt expectation for a survey.

type SelectExpect

type SelectExpect []string

SelectExpect expects a select list from console.

func (*SelectExpect) Do

func (e *SelectExpect) Do(c Console) error

Do runs the step.

func (*SelectExpect) String

func (e *SelectExpect) String() string

String represents the answer as a string.

type SelectPrompt

type SelectPrompt struct {
	// contains filtered or unexported fields
}

SelectPrompt is an expectation of survey.Select.

func (*SelectPrompt) Delete

func (p *SelectPrompt) Delete(times ...int) *SelectPrompt

Delete sends the DELETE key the indicated times. Default is 1 when omitted.

   Survey.ExpectSelect("Select a language:").
   	Type("Eng").
		Delete(3)

func (*SelectPrompt) Do

func (p *SelectPrompt) Do(c Console) error

Do runs the step.

func (*SelectPrompt) Enter

func (p *SelectPrompt) Enter()

Enter sends the ENTER key and ends the sequence.

   Survey.ExpectSelect("Select a language:").
   	Type("Eng").
		Enter()

func (*SelectPrompt) ExpectOptions

func (p *SelectPrompt) ExpectOptions(options ...string) *SelectPrompt

ExpectOptions expects a list of options.

   Survey.ExpectSelect("Select a language:").
   	Type("Eng").
		ExpectOptions("English")

func (*SelectPrompt) Interrupt

func (p *SelectPrompt) Interrupt()

Interrupt sends ^C and ends the sequence.

   Survey.ExpectSelect("Select a language:").
		Interrupt()

func (*SelectPrompt) MoveDown

func (p *SelectPrompt) MoveDown(times ...int) *SelectPrompt

MoveDown sends the ARROW DOWN key the indicated times. Default is 1 when omitted.

   Survey.ExpectSelect("Select a language:").
   	Type("Eng").
		MoveDown()

func (*SelectPrompt) MoveUp

func (p *SelectPrompt) MoveUp(times ...int) *SelectPrompt

MoveUp sends the ARROW UP key the indicated times. Default is 1 when omitted.

   Survey.ExpectSelect("Select a language:").
   	Type("Eng").
		MoveUp()

func (*SelectPrompt) ShowHelp

func (p *SelectPrompt) ShowHelp(help string, options ...string) *SelectPrompt

ShowHelp asks for help and asserts the help text.

Survey.ExpectSelect("Select a language:").
	ShowHelp("Your preferred language")

func (*SelectPrompt) String

func (p *SelectPrompt) String() string

String represents the expectation as a string.

func (*SelectPrompt) Tab

func (p *SelectPrompt) Tab(times ...int) *SelectPrompt

Tab sends the TAB key the indicated times. Default is 1 when omitted.

   Survey.ExpectSelect("Select a language:").
   	Type("Eng").
		Tab()

func (*SelectPrompt) Type

func (p *SelectPrompt) Type(s string) *SelectPrompt

Type sends some text to filter the options.

Survey.ExpectSelect("Select a language:").
	Type("Eng")

type Signal

type Signal struct {
	// contains filtered or unexported fields
}

Signal is a safe chan to notify the others.

func NewSignal

func NewSignal() *Signal

NewSignal creates a new Signal.

func (*Signal) Done

func (s *Signal) Done() <-chan struct{}

Done checks whether the notification arrives or not.

func (*Signal) Notify

func (s *Signal) Notify()

Notify notifies the listeners.

type Step

type Step interface {
	// Do runs the step.
	Do(c Console) error

	// String represents the step as a string.
	String() string
}

Step is an execution step for a survey.

type Steps

type Steps struct {
	// contains filtered or unexported fields
}

Steps is a chain of Step.

func (*Steps) Append

func (s *Steps) Append(more ...Step) *Steps

Append appends an expectation to the sequence. nolint: unparam

func (*Steps) Close

func (s *Steps) Close()

Close closes the steps.

func (*Steps) Do

func (s *Steps) Do(c Console) error

Do runs all the steps.

func (*Steps) DoFirst

func (s *Steps) DoFirst(c Console) error

DoFirst runs the first step.

func (*Steps) ExpectationsWereMet

func (s *Steps) ExpectationsWereMet() error

ExpectationsWereMet checks whether all queued expectations were met in order. If any of them was not met - an error is returned.

func (*Steps) HasNothingToDo

func (s *Steps) HasNothingToDo() bool

HasNothingToDo checks whether the steps are not empty.

func (*Steps) Len

func (s *Steps) Len() int

Len returns the number of steps.

func (*Steps) Reset

func (s *Steps) Reset()

Reset removes all the steps.

func (*Steps) String

func (s *Steps) String() string

String represents the steps as a string.

type StringExpect

type StringExpect string

StringExpect expects a string from console.

func (StringExpect) Do

func (e StringExpect) Do(c Console) error

Do runs the step.

func (StringExpect) String

func (e StringExpect) String() string

String represents the answer as a string.

type StringWriter

type StringWriter interface {
	io.Writer
	fmt.Stringer
}

StringWriter is a wrapper for bytes.Buffer.

type Survey

type Survey struct {
	// contains filtered or unexported fields
}

Survey is a expectations container and responsible for testing the prompts.

func New

func New(t TestingT, options ...ExpectOption) *Survey

New creates a new expected survey.

func (*Survey) Expect

func (s *Survey) Expect(c Console) error

Expect runs an expectation against a given console.

func (*Survey) ExpectConfirm

func (s *Survey) ExpectConfirm(message string) *ConfirmPrompt

ExpectConfirm expects a ConfirmPrompt.

Survey.ExpectConfirm("ConfirmPrompt?").
	Yes()

func (*Survey) ExpectInput

func (s *Survey) ExpectInput(message string) *InputPrompt

ExpectInput expects an InputPrompt.

Survey.ExpectInput("Enter password:").
	Answer("hello world!")

func (*Survey) ExpectMultiSelect

func (s *Survey) ExpectMultiSelect(message string) *MultiSelectPrompt

ExpectMultiSelect expects a MultiSelectPrompt.

Survey.ExpectMultiSelect("Enter password:").
	Enter()

func (*Survey) ExpectMultiline

func (s *Survey) ExpectMultiline(message string) *MultilinePrompt

ExpectMultiline expects a MultilinePrompt.

Survey.ExpectMultiline("Enter password:").
	Answer("hello world")

func (*Survey) ExpectPassword

func (s *Survey) ExpectPassword(message string) *PasswordPrompt

ExpectPassword expects a PasswordPrompt.

Survey.ExpectPassword("Enter password:").
	Answer("hello world!")

func (*Survey) ExpectSelect

func (s *Survey) ExpectSelect(message string) *SelectPrompt

ExpectSelect expects a SelectPrompt.

Survey.ExpectSelect("Enter password:").
	Enter()

func (*Survey) ExpectationsWereMet

func (s *Survey) ExpectationsWereMet() error

ExpectationsWereMet checks whether all queued expectations were met in order. If any of them was not met - an error is returned.

func (*Survey) ResetExpectations

func (s *Survey) ResetExpectations()

ResetExpectations resets all the expectations.

func (*Survey) Start

func (s *Survey) Start(fn func(stdio terminal.Stdio))

Start starts the survey with a default timeout.

func (*Survey) WithTimeout

func (s *Survey) WithTimeout(t time.Duration) *Survey

WithTimeout sets the timeout of a survey.

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
	FailNow()
	Cleanup(func())
	Log(args ...interface{})
	Logf(format string, args ...interface{})
}

TestingT is an interface wrapper around *testing.T.

type TypeAnswer

type TypeAnswer struct {
	// contains filtered or unexported fields
}

TypeAnswer types an answer.

func (*TypeAnswer) Do

func (a *TypeAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*TypeAnswer) String

func (a *TypeAnswer) String() string

String represents the answer as a string.

Directories

Path Synopsis
Package options provide options for configuring survey options.
Package options provide options for configuring survey options.
cobra
Package cobra provides support for stdio with cobra command.
Package cobra provides support for stdio with cobra command.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL