Documentation ¶
Index ¶
- func AssertSuccess(t *testing.T, err error, args ...any) bool
- func Close(server *MockHttpServer, qa *AskMocker)
- func GoBegin[TResult any](action func() TResult) chan TResult
- func GoBegin2[TResult1 any, TResult2 any](action func() (TResult1, TResult2)) chan Pair[TResult1, TResult2]
- func GoBegin3[TResult1 any, TResult2 any, TResult3 any](action func() (TResult1, TResult2, TResult3)) chan Triple[TResult1, TResult2, TResult3]
- func NewMockHttpClient() *http.Client
- func NewMockHttpClientWithTransport(transport http.RoundTripper) *http.Client
- func NewMockServerAndAsker() (*MockHttpServer, *AskMocker)
- func NewRootResource() *octopusApiClient.RootResource
- func ParseJsonStrict[T any](input io.Reader) (T, error)
- func ReadJson[T any](body io.ReadCloser) (T, error)
- func ReceivePair[T1 any, T2 any](receiver chan Pair[T1, T2]) (T1, T2)
- func ReceiveTriple[T1 any, T2 any, T3 any](receiver chan Triple[T1, T2, T3]) (T1, T2, T3)
- func RequireSuccess(t *testing.T, err error, args ...any) bool
- type AskMocker
- func (m *AskMocker) AsAsker() func(p survey.Prompt, response interface{}, opts ...survey.AskOpt) error
- func (m *AskMocker) Close()
- func (m *AskMocker) ExpectQuestion(t *testing.T, question survey.Prompt) *QuestionWrapper
- func (m *AskMocker) GetLastValidationError() error
- func (m *AskMocker) ReceiveQuestion() *QuestionWrapper
- type CheckRemaining
- type FakeSpinner
- type MockFactory
- func (f *MockFactory) Ask(p survey.Prompt, response interface{}, opts ...survey.AskOpt) error
- func (f *MockFactory) BuildVersion() string
- func (f *MockFactory) GetCurrentHost() string
- func (f *MockFactory) GetCurrentSpace() *spaces.Space
- func (f *MockFactory) GetSpacedClient(requester apiclient.Requester) (*octopusApiClient.Client, error)
- func (f *MockFactory) GetSystemClient(requester apiclient.Requester) (*octopusApiClient.Client, error)
- func (f *MockFactory) IsPromptEnabled() bool
- func (f *MockFactory) Spinner() factory.Spinner
- type MockHttpServer
- func (m *MockHttpServer) Close()
- func (m *MockHttpServer) ExpectRequest(t *testing.T, method string, pathAndQuery string) *RequestWrapper
- func (m *MockHttpServer) GetPendingMessageCount() int
- func (m *MockHttpServer) ReceiveRequest() (*http.Request, bool)
- func (m *MockHttpServer) Respond(response *http.Response, err error)
- func (m *MockHttpServer) RoundTrip(r *http.Request) (*http.Response, error)
- type PA
- func NewConfirmPrompt(prompt string, help string, response any) *PA
- func NewConfirmPromptWithDefault(prompt string, help string, response any, defaultResponse bool) *PA
- func NewInputPrompt(prompt string, help string, response string) *PA
- func NewInputPromptWithDefault(prompt string, help string, def string, response string) *PA
- func NewMultiSelectPrompt(prompt string, help string, options []string, responses []string) *PA
- func NewMultiSelectWithAddPrompt(prompt string, help string, options []string, responses []string) *PA
- func NewPasswordPrompt(prompt string, help string, response string) *PA
- func NewSelectPrompt(prompt string, help string, options []string, response string) *PA
- type Pair
- type QuestionWrapper
- type RequestWrapper
- type RoundTripper
- type Triple
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertSuccess ¶
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 NewMockHttpClient ¶
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 ¶
ParseJsonStrict parses the incoming byte buffer into objects of type T, failing if any unexpected fields are present
func ReceivePair ¶
func ReceiveTriple ¶
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) 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 (*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
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)
type PA ¶ added in v0.5.0
func NewConfirmPrompt ¶ added in v0.5.0
func NewConfirmPromptWithDefault ¶ added in v0.6.0
func NewInputPrompt ¶ added in v0.5.0
func NewInputPromptWithDefault ¶ added in v0.6.0
func NewMultiSelectPrompt ¶ added in v0.5.0
func NewMultiSelectWithAddPrompt ¶ added in v0.6.0
func NewPasswordPrompt ¶ added in v0.5.0
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)