testutil

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2022 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertSuccess

func AssertSuccess(t *testing.T, err error, args ...any) bool

AssertSuccess checks that err is nil and returns true. If it's not, it will print all the args, then write the Error string, fail the test, and return false

func Close

func Close(server *MockHttpServer, qa *AskMocker)

it's super common to Close both the mock server and asker at the same time

func GoBegin

func GoBegin[TResult any](action func() TResult) chan TResult

func GoBegin2

func GoBegin2[TResult1 any, TResult2 any](action func() (TResult1, TResult2)) chan Pair[TResult1, TResult2]

func GoBegin3

func GoBegin3[TResult1 any, TResult2 any, TResult3 any](action func() (TResult1, TResult2, TResult3)) chan Triple[TResult1, TResult2, TResult3]

func NewMockHttpClient

func NewMockHttpClient() *http.Client

NewMockHttpClient returns an Http Client which returns 200 OK with no response body for everything

func NewMockHttpClientWithTransport

func NewMockHttpClientWithTransport(transport http.RoundTripper) *http.Client

func NewMockServerAndAsker

func NewMockServerAndAsker() (*MockHttpServer, *AskMocker)

it's super common to New both the mock server and asker at the same time

func NewRootResource

func NewRootResource() *octopusApiClient.RootResource

func ParseJsonStrict

func ParseJsonStrict[T any](input io.Reader) (T, error)

ParseJsonStrict parses the incoming byte buffer into objects of type T, failing if any unexpected fields are present

func ReadJson

func ReadJson[T any](body io.ReadCloser) (T, error)

NOTE max length of 8k

func ReceivePair

func ReceivePair[T1 any, T2 any](receiver chan Pair[T1, T2]) (T1, T2)

func ReceiveTriple

func ReceiveTriple[T1 any, T2 any, T3 any](receiver chan Triple[T1, T2, T3]) (T1, T2, T3)

func RequireSuccess

func RequireSuccess(t *testing.T, err error, args ...any) bool

Types

type AskMocker

type AskMocker struct {
	// when the client asks a question, we receive it here
	Question chan questionWithOptions
	// when we want to answer the question, we send the response here
	Answer chan answerOrError

	Closed bool

	// when we run validators against a question, if there is an error it will be
	// sent down this channel. If you aren't hooked up to receive the validation error, the test will deadlock
	LastValidationError chan error
}

func NewAskMocker

func NewAskMocker() *AskMocker

func (*AskMocker) AsAsker

func (m *AskMocker) AsAsker() func(p survey.Prompt, response interface{}, opts ...survey.AskOpt) error

func (*AskMocker) Close

func (m *AskMocker) Close()

func (*AskMocker) ExpectQuestion

func (m *AskMocker) ExpectQuestion(t *testing.T, question survey.Prompt) *QuestionWrapper

ExpectQuestion calls ReceiveQuestion and asserts that the received survey prompt matches `question`

func (*AskMocker) GetLastValidationError

func (m *AskMocker) GetLastValidationError() error

func (*AskMocker) ReceiveQuestion

func (m *AskMocker) ReceiveQuestion() *QuestionWrapper

ReceiveQuestion gets the next question and options from the channel and returns them in a wrapper struct. If the channel is closed, will still return a valid wrapper, but the wrapped contents will be nil

type CheckRemaining added in v0.5.0

type CheckRemaining func()

func NewMockAsker added in v0.5.0

func NewMockAsker(t *testing.T, pa []*PA) (question.Asker, CheckRemaining)

type FakeSpinner

type FakeSpinner struct{}

func (*FakeSpinner) Start

func (f *FakeSpinner) Start()

func (*FakeSpinner) Stop

func (f *FakeSpinner) Stop()

type MockFactory

type MockFactory struct {
	SystemClient      *octopusApiClient.Client // nil; lazily created like with the real factory
	SpaceScopedClient *octopusApiClient.Client // nil; lazily created like with the real factory
	CurrentSpace      *spaces.Space
	RawSpinner        factory.Spinner
	AskProvider       question.AskProvider
	// contains filtered or unexported fields
}

func NewMockFactory

func NewMockFactory(api *MockHttpServer) *MockFactory

func NewMockFactoryWithSpace

func NewMockFactoryWithSpace(api *MockHttpServer, space *spaces.Space) *MockFactory

func NewMockFactoryWithSpaceAndPrompt

func NewMockFactoryWithSpaceAndPrompt(api *MockHttpServer, space *spaces.Space, askProvider question.AskProvider) *MockFactory

func (*MockFactory) Ask

func (f *MockFactory) Ask(p survey.Prompt, response interface{}, opts ...survey.AskOpt) error

func (*MockFactory) BuildVersion added in v0.3.0

func (f *MockFactory) BuildVersion() string

func (*MockFactory) GetCurrentHost

func (f *MockFactory) GetCurrentHost() string

func (*MockFactory) GetCurrentSpace

func (f *MockFactory) GetCurrentSpace() *spaces.Space

func (*MockFactory) GetSpacedClient

func (f *MockFactory) GetSpacedClient(requester apiclient.Requester) (*octopusApiClient.Client, error)

func (*MockFactory) GetSystemClient

func (f *MockFactory) GetSystemClient(requester apiclient.Requester) (*octopusApiClient.Client, error)

func (*MockFactory) IsPromptEnabled

func (f *MockFactory) IsPromptEnabled() bool

func (*MockFactory) Spinner

func (f *MockFactory) Spinner() factory.Spinner

type MockHttpServer

type MockHttpServer struct {
	// when the client issues a request, we receive it here
	Request chan *http.Request
	// when we want to respond back to the client, we send it here
	Response chan responseOrError

	Closed bool
	// contains filtered or unexported fields
}

func NewMockHttpServer

func NewMockHttpServer() *MockHttpServer

func (*MockHttpServer) Close

func (m *MockHttpServer) Close()

func (*MockHttpServer) ExpectRequest

func (m *MockHttpServer) ExpectRequest(t *testing.T, method string, pathAndQuery string) *RequestWrapper

func (*MockHttpServer) GetPendingMessageCount

func (m *MockHttpServer) GetPendingMessageCount() int

func (*MockHttpServer) ReceiveRequest

func (m *MockHttpServer) ReceiveRequest() (*http.Request, bool)

func (*MockHttpServer) Respond

func (m *MockHttpServer) Respond(response *http.Response, err error)

func (*MockHttpServer) RoundTrip

func (m *MockHttpServer) RoundTrip(r *http.Request) (*http.Response, error)

type PA added in v0.5.0

type PA struct {
	Prompt               survey.Prompt
	Answer               any
	ShouldSkipValidation bool
}

func NewConfirmPrompt added in v0.5.0

func NewConfirmPrompt(prompt string, help string, response any) *PA

func NewConfirmPromptWithDefault added in v0.6.0

func NewConfirmPromptWithDefault(prompt string, help string, response any, defaultResponse bool) *PA

func NewInputPrompt added in v0.5.0

func NewInputPrompt(prompt string, help string, response string) *PA

func NewInputPromptWithDefault added in v0.6.0

func NewInputPromptWithDefault(prompt string, help string, def string, response string) *PA

func NewMultiSelectPrompt added in v0.5.0

func NewMultiSelectPrompt(prompt string, help string, options []string, responses []string) *PA

func NewMultiSelectWithAddPrompt added in v0.6.0

func NewMultiSelectWithAddPrompt(prompt string, help string, options []string, responses []string) *PA

func NewPasswordPrompt added in v0.5.0

func NewPasswordPrompt(prompt string, help string, response string) *PA

func NewSelectPrompt added in v0.5.0

func NewSelectPrompt(prompt string, help string, options []string, response string) *PA

type Pair

type Pair[T1 any, T2 any] struct {
	Item1 T1
	Item2 T2
}

type QuestionWrapper

type QuestionWrapper struct {
	// in case you need it
	Question survey.Prompt
	Options  *survey.AskOptions
	Asker    *AskMocker
}

func (*QuestionWrapper) AnswerWith

func (q *QuestionWrapper) AnswerWith(answer any) error

AnswerWith runs any validators associated with the question. If they all pass, it sends the answer down the channel. If any fail, the validation error is returned from here, and the answer is NOT sent. This mimics the behaviour of real survey, which will keep asking you in a loop until the validators pass.

If you want to test validators specifically, then do this:

q := mockSurvey.ExpectQuestion(t, &survey.Prompt{ Message: "Please input a number between 1 and 10" })
err := q.AnswerWith("9999")
assert.EqualError(t, err, "Number was not within range 1 to 10")
err := q.AnswerWith("-1")
assert.EqualError(t, err, "Number was not within range 1 to 10")
err := q.AnswerWith("5")
assert.EqualError(t, err, nil)
test sequence should proceed now

func (*QuestionWrapper) AnswerWithError

func (q *QuestionWrapper) AnswerWithError(err error)

type RequestWrapper

type RequestWrapper struct {
	// in case you need it
	Request *http.Request
	Server  *MockHttpServer
}

func (*RequestWrapper) RespondWith

func (r *RequestWrapper) RespondWith(responseObject any)

func (*RequestWrapper) RespondWithError

func (r *RequestWrapper) RespondWithError(err error)

func (*RequestWrapper) RespondWithStatus

func (r *RequestWrapper) RespondWithStatus(statusCode int, statusString string, responseObject any)

type RoundTripper

type RoundTripper func(r *http.Request) (*http.Response, error)

func (RoundTripper) RoundTrip

func (s RoundTripper) RoundTrip(r *http.Request) (*http.Response, error)

type Triple

type Triple[T1 any, T2 any, T3 any] struct {
	Item1 T1
	Item2 T2
	Item3 T3
}

Jump to

Keyboard shortcuts

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