From de0b7818b67d1a38f69e54545e27dfaeeed504fb Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Mon, 11 Mar 2019 11:25:51 +0200 Subject: [PATCH] fix golangci-lint problems --- js/common/bridge.go | 2 ++ js/modules/k6/http/http_test.go | 1 + js/modules/k6/http/request.go | 6 +++++- js/modules/k6/http/request_test.go | 21 +++++++++++++-------- js/runner.go | 4 +++- js/runner_test.go | 1 + lib/netext/httpext/request.go | 9 +++++++-- lib/netext/httpext/response.go | 1 + lib/netext/httpext/tracer_test.go | 7 ++++++- lib/netext/httpext/transport.go | 12 +++++++++--- 10 files changed, 48 insertions(+), 16 deletions(-) diff --git a/js/common/bridge.go b/js/common/bridge.go index 4937385fc9f7..84f445947e8f 100644 --- a/js/common/bridge.go +++ b/js/common/bridge.go @@ -46,6 +46,7 @@ var ( // if a fieldName is the key of this map exactly than the value for the given key should be used as // the name of the field in js +//nolint: gochecknoglobals var fieldNameExceptions = map[string]string{ "OCSP": "ocsp", } @@ -77,6 +78,7 @@ func FieldName(t reflect.Type, f reflect.StructField) string { // if a methodName is the key of this map exactly than the value for the given key should be used as // the name of the method in js +//nolint: gochecknoglobals var methodNameExceptions = map[string]string{ "JSON": "json", "HTML": "html", diff --git a/js/modules/k6/http/http_test.go b/js/modules/k6/http/http_test.go index 65226af953e1..f81c9ff57143 100644 --- a/js/modules/k6/http/http_test.go +++ b/js/modules/k6/http/http_test.go @@ -43,6 +43,7 @@ func TestTagURL(t *testing.T) { `http://localhost/anything/${1+1}/${1+2}/`: {"http://localhost/anything/2/3/", "http://localhost/anything/${}/${}/"}, } for expr, data := range testdata { + expr, data := expr, data t.Run("expr="+expr, func(t *testing.T) { tag, err := httpext.NewURL(data.u, data.n) require.NoError(t, err) diff --git a/js/modules/k6/http/request.go b/js/modules/k6/http/request.go index 6c9cb2e2e4de..36fcdab0eb81 100644 --- a/js/modules/k6/http/request.go +++ b/js/modules/k6/http/request.go @@ -116,7 +116,11 @@ func (h *HTTP) Request(ctx context.Context, method string, url goja.Value, args return responseFromHttpext(resp), nil } -func (h *HTTP) parseRequest(ctx context.Context, method string, reqURL httpext.URL, body interface{}, params goja.Value) (*httpext.ParsedHTTPRequest, error) { +//TODO break this function up +//nolint: gocyclo +func (h *HTTP) parseRequest( + ctx context.Context, method string, reqURL httpext.URL, body interface{}, params goja.Value, +) (*httpext.ParsedHTTPRequest, error) { rt := common.GetRuntime(ctx) state := lib.GetState(ctx) if state == nil { diff --git a/js/modules/k6/http/request_test.go b/js/modules/k6/http/request_test.go index 76980ced4335..90edc9c65845 100644 --- a/js/modules/k6/http/request_test.go +++ b/js/modules/k6/http/request_test.go @@ -97,7 +97,9 @@ func assertRequestMetricsEmitted(t *testing.T, sampleContainers []stats.SampleCo assert.True(t, seenReceiving, "url %s didn't emit Receiving", url) } -func newRuntime(t *testing.T) (*testutils.HTTPMultiBin, *lib.State, chan stats.SampleContainer, *goja.Runtime, *context.Context) { +func newRuntime( + t *testing.T, +) (*testutils.HTTPMultiBin, *lib.State, chan stats.SampleContainer, *goja.Runtime, *context.Context) { tb := testutils.NewHTTPMultiBin(t) root, err := lib.NewGroup("", nil) @@ -456,10 +458,10 @@ func TestRequestAndBatch(t *testing.T) { }(state.Options.Throw) state.Options.Throw = null.BoolFrom(false) - tb.Mux.HandleFunc("/no-location-redirect", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + tb.Mux.HandleFunc("/no-location-redirect", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(302) })) - tb.Mux.HandleFunc("/bad-location-redirect", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + tb.Mux.HandleFunc("/bad-location-redirect", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Location", "h\t:/") // \n is forbidden w.WriteHeader(302) })) @@ -517,11 +519,13 @@ func TestRequestAndBatch(t *testing.T) { expectedScriptError: sr(`GoError: Get HTTPBIN_URL/no-location-redirect: 302 response missing Location header`), }, { - name: "Bad location redirect", - expectedErrorCode: 0, - expectedErrorMsg: "", - script: `let res = http.request("GET", "HTTPBIN_URL/bad-location-redirect");`, - expectedScriptError: sr("GoError: Get HTTPBIN_URL/bad-location-redirect: failed to parse Location header \"h\\t:/\": parse h\t:/: first path segment in URL cannot contain colon"), + name: "Bad location redirect", + expectedErrorCode: 0, + expectedErrorMsg: "", + script: `let res = http.request("GET", "HTTPBIN_URL/bad-location-redirect");`, + expectedScriptError: sr( + "GoError: Get HTTPBIN_URL/bad-location-redirect:failed to parse Location header" + + " \"h\\t:/\": parse h\t:/: first path segment in URL cannot contain colon"), }, { name: "Missing protocol", @@ -539,6 +543,7 @@ func TestRequestAndBatch(t *testing.T) { } for _, testCase := range testCases { + testCase := testCase // clear the Samples stats.GetBufferedSamples(samples) t.Run(testCase.name, func(t *testing.T) { diff --git a/js/runner.go b/js/runner.go index 766e1f58beef..d6d169e8e349 100644 --- a/js/runner.go +++ b/js/runner.go @@ -402,7 +402,9 @@ func (u *VU) RunOnce(ctx context.Context) error { return err } -func (u *VU) runFn(ctx context.Context, group *lib.Group, fn goja.Callable, args ...goja.Value) (goja.Value, *lib.State, error) { +func (u *VU) runFn( + ctx context.Context, group *lib.Group, fn goja.Callable, args ...goja.Value, +) (goja.Value, *lib.State, error) { cookieJar, err := cookiejar.New(nil) if err != nil { return goja.Undefined(), nil, err diff --git a/js/runner_test.go b/js/runner_test.go index 19956c850d4a..0ed21dbbc012 100644 --- a/js/runner_test.go +++ b/js/runner_test.go @@ -587,6 +587,7 @@ func TestVUIntegrationGroups(t *testing.T) { testdata := map[string]*Runner{"Source": r1, "Archive": r2} for name, r := range testdata { + r := r t.Run(name, func(t *testing.T) { vu, err := r.newVU(make(chan stats.SampleContainer, 100)) if !assert.NoError(t, err) { diff --git a/lib/netext/httpext/request.go b/lib/netext/httpext/request.go index 0f5a413e0dd9..0e76ccc00d1c 100644 --- a/lib/netext/httpext/request.go +++ b/lib/netext/httpext/request.go @@ -104,6 +104,8 @@ func stdCookiesToHTTPRequestCookies(cookies []*http.Cookie) map[string][]*HTTPRe } // MakeRequest makes http request for tor the provided ParsedHTTPRequest +//TODO break this function up +//nolint: gocyclo func MakeRequest(ctx context.Context, preq *ParsedHTTPRequest) (*Response, error) { state := lib.GetState(ctx) @@ -180,7 +182,9 @@ func MakeRequest(ctx context.Context, preq *ParsedHTTPRequest) (*Response, error if l > 0 { url = via[0].URL } - state.Logger.WithFields(log.Fields{"url": url.String()}).Warnf("Stopped after %d redirects and returned the redirection; pass { redirects: n } in request params or set global maxRedirects to silence this", l) + state.Logger.WithFields(log.Fields{"url": url.String()}).Warnf( + "Stopped after %d redirects and returned the redirection; pass { redirects: n }"+ + " in request params or set global maxRedirects to silence this", l) } return http.ErrUseLastResponse } @@ -189,7 +193,8 @@ func MakeRequest(ctx context.Context, preq *ParsedHTTPRequest) (*Response, error }, } - // if digest authentication option is passed, make an initial request to get the authentication params to compute the authorization header + // if digest authentication option is passed, make an initial request + // to get the authentication params to compute the authorization header if preq.Auth == "digest" { username := preq.URL.u.User.Username() password, _ := preq.URL.u.User.Password() diff --git a/lib/netext/httpext/response.go b/lib/netext/httpext/response.go index bab34bc3b7b6..0cb9a83a517b 100644 --- a/lib/netext/httpext/response.go +++ b/lib/netext/httpext/response.go @@ -36,6 +36,7 @@ import ( // ResponseType is used in the request to specify how the response body should be treated // The conversion and validation methods are auto-generated with https://github.com/alvaroloes/enumer: +//nolint: lll //go:generate enumer -type=ResponseType -transform=snake -json -text -trimprefix ResponseType -output response_type_gen.go type ResponseType uint diff --git a/lib/netext/httpext/tracer_test.go b/lib/netext/httpext/tracer_test.go index 471c52a3241d..75b3d12fbe07 100644 --- a/lib/netext/httpext/tracer_test.go +++ b/lib/netext/httpext/tracer_test.go @@ -199,7 +199,12 @@ func TestTracerError(t *testing.T) { req, err := http.NewRequest("GET", srv.URL+"/get", nil) require.NoError(t, err) - _, err = http.DefaultTransport.RoundTrip(req.WithContext(httptrace.WithClientTrace(context.Background(), tracer.Trace()))) + _, err = http.DefaultTransport.RoundTrip( + req.WithContext( + httptrace.WithClientTrace( + context.Background(), + tracer.Trace()))) + assert.Error(t, err) assert.Len(t, tracer.protoErrors, 1) diff --git a/lib/netext/httpext/transport.go b/lib/netext/httpext/transport.go index 42df897767d4..fa2532192667 100644 --- a/lib/netext/httpext/transport.go +++ b/lib/netext/httpext/transport.go @@ -50,7 +50,12 @@ var _ http.RoundTripper = &transport{} // NewTransport returns a new Transport wrapping around the provide Roundtripper and will send // samples on the provided channel adding the tags in accordance to the Options provided -func newTransport(roundTripper http.RoundTripper, samplesCh chan<- stats.SampleContainer, options *lib.Options, tags map[string]string) *transport { +func newTransport( + roundTripper http.RoundTripper, + samplesCh chan<- stats.SampleContainer, + options *lib.Options, + tags map[string]string, +) *transport { return &transport{ roundTripper: roundTripper, tags: tags, @@ -77,7 +82,7 @@ func (t *transport) TLSInfo() netext.TLSInfo { // RoundTrip is the implementation of http.RoundTripper func (t *transport) RoundTrip(req *http.Request) (res *http.Response, err error) { if t.roundTripper == nil { - return nil, errors.New("No roundtrip defined") + return nil, errors.New("no roundtrip defined") } tags := map[string]string{} @@ -134,7 +139,8 @@ func (t *transport) RoundTrip(req *http.Request) (res *http.Response, err error) } } if t.options.SystemTags["ip"] && trail.ConnRemoteAddr != nil { - if ip, _, err := net.SplitHostPort(trail.ConnRemoteAddr.String()); err == nil { + var ip string + if ip, _, err = net.SplitHostPort(trail.ConnRemoteAddr.String()); err == nil { tags["ip"] = ip } }