Skip to content

integration tests: Use t.Helper() #7654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integrations/html_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type HTMLDoc struct {

// NewHTMLParser parse html file
func NewHTMLParser(t testing.TB, body *bytes.Buffer) *HTMLDoc {
t.Helper()
doc, err := goquery.NewDocumentFromReader(body)
assert.NoError(t, err)
return &HTMLDoc{doc: doc}
Expand Down
17 changes: 17 additions & 0 deletions integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func initIntegrationTest() {
}

func prepareTestEnv(t testing.TB, skip ...int) {
t.Helper()
ourSkip := 2
if len(skip) > 0 {
ourSkip += skip[0]
Expand Down Expand Up @@ -201,6 +202,7 @@ func (s *TestSession) GetCookie(name string) *http.Cookie {
}

func (s *TestSession) MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.ResponseRecorder {
t.Helper()
baseURL, err := url.Parse(setting.AppURL)
assert.NoError(t, err)
for _, c := range s.jar.Cookies(baseURL) {
Expand All @@ -217,6 +219,7 @@ func (s *TestSession) MakeRequest(t testing.TB, req *http.Request, expectedStatu
}

func (s *TestSession) MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
t.Helper()
baseURL, err := url.Parse(setting.AppURL)
assert.NoError(t, err)
for _, c := range s.jar.Cookies(baseURL) {
Expand All @@ -237,13 +240,15 @@ const userPassword = "password"
var loginSessionCache = make(map[string]*TestSession, 10)

func emptyTestSession(t testing.TB) *TestSession {
t.Helper()
jar, err := cookiejar.New(nil)
assert.NoError(t, err)

return &TestSession{jar: jar}
}

func loginUser(t testing.TB, userName string) *TestSession {
t.Helper()
if session, ok := loginSessionCache[userName]; ok {
return session
}
Expand All @@ -253,6 +258,7 @@ func loginUser(t testing.TB, userName string) *TestSession {
}

func loginUserWithPassword(t testing.TB, userName, password string) *TestSession {
t.Helper()
req := NewRequest(t, "GET", "/user/login")
resp := MakeRequest(t, req, http.StatusOK)

Expand All @@ -278,6 +284,7 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession
}

func getTokenForLoggedInUser(t testing.TB, session *TestSession) string {
t.Helper()
req := NewRequest(t, "GET", "/user/settings/applications")
resp := session.MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body)
Expand All @@ -294,14 +301,17 @@ func getTokenForLoggedInUser(t testing.TB, session *TestSession) string {
}

func NewRequest(t testing.TB, method, urlStr string) *http.Request {
t.Helper()
return NewRequestWithBody(t, method, urlStr, nil)
}

func NewRequestf(t testing.TB, method, urlFormat string, args ...interface{}) *http.Request {
t.Helper()
return NewRequest(t, method, fmt.Sprintf(urlFormat, args...))
}

func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string]string) *http.Request {
t.Helper()
urlValues := url.Values{}
for key, value := range values {
urlValues[key] = []string{value}
Expand All @@ -312,6 +322,7 @@ func NewRequestWithValues(t testing.TB, method, urlStr string, values map[string
}

func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *http.Request {
t.Helper()
jsonBytes, err := json.Marshal(v)
assert.NoError(t, err)
req := NewRequestWithBody(t, method, urlStr, bytes.NewBuffer(jsonBytes))
Expand All @@ -320,6 +331,7 @@ func NewRequestWithJSON(t testing.TB, method, urlStr string, v interface{}) *htt
}

func NewRequestWithBody(t testing.TB, method, urlStr string, body io.Reader) *http.Request {
t.Helper()
request, err := http.NewRequest(method, urlStr, body)
assert.NoError(t, err)
request.RequestURI = urlStr
Expand All @@ -334,6 +346,7 @@ func AddBasicAuthHeader(request *http.Request, username string) *http.Request {
const NoExpectedStatus = -1

func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.ResponseRecorder {
t.Helper()
recorder := httptest.NewRecorder()
mac.ServeHTTP(recorder, req)
if expectedStatus != NoExpectedStatus {
Expand All @@ -346,6 +359,7 @@ func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.
}

func MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
t.Helper()
recorder := NewNilResponseRecorder()
mac.ServeHTTP(recorder, req)
if expectedStatus != NoExpectedStatus {
Expand All @@ -359,6 +373,7 @@ func MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedSta

// logUnexpectedResponse logs the contents of an unexpected response.
func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) {
t.Helper()
respBytes := recorder.Body.Bytes()
if len(respBytes) == 0 {
return
Expand All @@ -381,11 +396,13 @@ func logUnexpectedResponse(t testing.TB, recorder *httptest.ResponseRecorder) {
}

func DecodeJSON(t testing.TB, resp *httptest.ResponseRecorder, v interface{}) {
t.Helper()
decoder := json.NewDecoder(resp.Body)
assert.NoError(t, decoder.Decode(v))
}

func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
t.Helper()
req := NewRequest(t, "GET", urlStr)
resp := session.MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body)
Expand Down