Skip to content
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

Lint fixes #1557

Merged
merged 7 commits into from
Dec 11, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ jobs:
echo "::set-output name=Version::$(head -n 1 "${GITHUB_WORKSPACE}/.golangci.yml" | tr -d '# ')"
id: version
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
uses: golangci/golangci-lint-action@349d20632dbaed38f0a492cc991152e3d351e854 # latest commit at the time that uses node20
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: ${{ steps.version.outputs.Version }}
only-new-issues: true
skip-cache: true
128 changes: 73 additions & 55 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,75 @@
# v1.55.2
# Please don't remove the first line. It's used in CI to determine the
# golangci-lint version. See the lint step in .github/workflows/all.yaml
# v1.62.2
# Please don't remove the first line. It uses in CI to determine the golangci version
run:
timeout: 5m

issues:
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-issues-per-linter: 0

# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
max-same-issues: 0

# Disable default exclude rules, see `golangci-lint run --help`.
# When true it disables revive's comment warnings, and we want them enabled.
# We want to try and improve the comments in the k6 codebase, so individual
# non-golint items from the default exclusion list will gradually be added
# to the exclude-rules below
exclude-use-default: false

exclude-rules:
- path: (.+)_test.go
linters:
- wrapcheck
- funlen
- cyclop
- path: k6ext/k6test
linters:
- wrapcheck
- funlen
- cyclop
- path: tests
linters:
- wrapcheck
- funlen
- cyclop
# Exclude duplicate code and function length and complexity checking in test
# files (due to common repeats and long functions in test code)
- path: _(test|gen)\.go
linters:
- canonicalheader
- cyclop
- dupl
- gocognit
- funlen
- lll
- forcetypeassert
- path: js\/modules\/k6\/html\/.*\.go
text: "exported: exported "
linters:
- revive
- path: js\/modules\/k6\/http\/.*_test\.go
linters:
# k6/http module's tests are quite complex because they often have several nested levels.
# The module is in maintenance mode, so we don't intend to port the tests to a parallel version.
- paralleltest
- tparallel
- linters:
- forbidigo
text: 'use of `os\.(SyscallError|Signal|Interrupt)` forbidden'

linters-settings:
exhaustive:
default-signifies-exhaustive: true
cyclop:
max-complexity: 25
dupl:
threshold: 150
goconst:
min-len: 10
min-occurrences: 4
funlen:
lines: 80
statements: 60
forbidigo:
forbid:
- '^(fmt\\.Print(|f|ln)|print|println)$'
# Forbid everything in os, except os.Signal and os.SyscalError
- '^os\.(.*)$(# Using anything except Signal and SyscallError from the os package is forbidden )?'
# Forbid everything in syscall except the uppercase constants
- '^syscall\.[^A-Z_]+$(# Using anything except constants from the syscall package is forbidden )?'
- '^logrus\.Logger$'

linters:
disable-all: true
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- canonicalheader
- cyclop
- dogsled
- dupl
Expand All @@ -45,69 +80,52 @@ linters:
- errorlint
- exhaustive
- exportloopref
- fatcontext
- forbidigo
- forcetypeassert
- funlen
- gci
- gocheckcompilerdirectives
- gochecknoglobals
- gocognit
- goconst
- gocritic
- godot
- gofmt
- gofumpt
- goimports
- gomoddirectives
- goprintffuncname
- gosec
- gosimple
- govet
- importas
- ineffassign
- interfacebloat
- lll
- makezero
- misspell
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- predeclared
- nosprintfhostport
- paralleltest
- prealloc
- revive
- predeclared
- promlinter
- reassign
- rowserrcheck
- sqlclosecheck
- staticcheck
- tagliatelle
- thelper
- stylecheck
- tenv
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- wastedassign
- whitespace
- wrapcheck

linters-settings:
gci:
sections:
- standard
- prefix(github.com/grafana/xk6-browser)
- prefix(go.k6.io)
- default
dupl:
threshold: 400
gofmt:
simplify: true
goimports:
local-prefixes: github.com/grafana/xk6-browser
importas:
alias:
- pkg: go.k6.io/k6/.*/(\w+)
alias: k6$1
nlreturn:
block-size: 10
lll:
maxlength: 120
gosec:
config:
G301: '0755'
G306: '0644'
fast: false
2 changes: 1 addition & 1 deletion browser/browser_context_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// mapBrowserContext to the JS module.
func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolint:funlen,gocognit,cyclop
func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolint:funlen,gocognit
rt := vu.Runtime()
return mapping{
"addCookies": func(cookies []*common.Cookie) *sobek.Promise {
Expand Down
4 changes: 4 additions & 0 deletions browser/browser_context_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

func TestBrowserContextOptionsPermissions(t *testing.T) {
t.Parallel()
vu := k6test.NewVU(t)

opts, err := parseBrowserContextOptions(vu.Runtime(), vu.ToSobekValue((struct {
Expand All @@ -24,6 +25,7 @@ func TestBrowserContextOptionsPermissions(t *testing.T) {
}

func TestBrowserContextSetGeolocation(t *testing.T) {
t.Parallel()
vu := k6test.NewVU(t)

opts, err := parseBrowserContextOptions(vu.Runtime(), vu.ToSobekValue((struct {
Expand All @@ -43,6 +45,7 @@ func TestBrowserContextSetGeolocation(t *testing.T) {
}

func TestBrowserContextDefaultOptions(t *testing.T) {
t.Parallel()
vu := k6test.NewVU(t)

defaults := common.DefaultBrowserContextOptions()
Expand All @@ -64,6 +67,7 @@ func TestBrowserContextDefaultOptions(t *testing.T) {
}

func TestBrowserContextAllOptions(t *testing.T) {
t.Parallel()
vu := k6test.NewVU(t)
opts, err := vu.Runtime().RunString(`const opts = {
acceptDownloads: true,
Expand Down
2 changes: 1 addition & 1 deletion browser/browser_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// mapBrowser to the JS module.
func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop,gocognit
func mapBrowser(vu moduleVU) mapping { //nolint:funlen,gocognit
return mapping{
"context": func() (mapping, error) {
b, err := vu.browser()
Expand Down
2 changes: 1 addition & 1 deletion browser/element_handle_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// mapElementHandle to the JS module.
func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint:gocognit,cyclop,funlen
func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint:gocognit,funlen
rt := vu.Runtime()
maps := mapping{
"boundingBox": func() *sobek.Promise {
Expand Down
2 changes: 1 addition & 1 deletion browser/file_persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newScreenshotPersister(envLookup env.LookupFunc) (filePersister, error) {
// url=https://127.0.0.1/,basePath=/screenshots,header.1=a,header.2=b
// and return them.
//
//nolint:cyclop

func parsePresignedURLEnvVar(envVarValue string) (presignedURLConfig, error) {
ss := strings.Split(envVarValue, ",")

Expand Down
4 changes: 2 additions & 2 deletions browser/frame_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

// mapFrame to the JS module.
//
//nolint:funlen
func mapFrame(vu moduleVU, f *common.Frame) mapping { //nolint:gocognit,cyclop
//nolint:funlen,gocognit
func mapFrame(vu moduleVU, f *common.Frame) mapping {
maps := mapping{
"check": func(selector string, opts sobek.Value) *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
Expand Down
1 change: 1 addition & 0 deletions browser/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func TestSobekEmptyString(t *testing.T) {
t.Parallel()
// SobekEmpty string should return true if the argument
// is an empty string or not defined in the Sobek runtime.
rt := sobek.New()
Expand Down
14 changes: 7 additions & 7 deletions browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ type browserAPI interface {
}

// browserContextAPI is the public interface of a CDP browser context.
type browserContextAPI interface {
type browserContextAPI interface { //nolint:interfacebloat
AddCookies(cookies []*common.Cookie) error
AddInitScript(script sobek.Value, arg sobek.Value) error
Browser() *common.Browser
Expand All @@ -302,7 +302,7 @@ type browserContextAPI interface {
}

// pageAPI is the interface of a single browser tab.
type pageAPI interface {
type pageAPI interface { //nolint:interfacebloat
BringToFront() error
Check(selector string, opts sobek.Value) error
Click(selector string, opts sobek.Value) error
Expand Down Expand Up @@ -382,7 +382,7 @@ type metricEventAPI interface {
}

// frameAPI is the interface of a CDP target frame.
type frameAPI interface {
type frameAPI interface { //nolint:interfacebloat
Check(selector string, opts sobek.Value) error
ChildFrames() []*common.Frame
Click(selector string, opts sobek.Value) error
Expand Down Expand Up @@ -436,7 +436,7 @@ type frameAPI interface {
}

// elementHandleAPI is the interface of an in-page DOM element.
type elementHandleAPI interface {
type elementHandleAPI interface { //nolint:interfacebloat
common.JSHandleAPI

BoundingBox() (*common.Rect, error)
Expand Down Expand Up @@ -477,7 +477,7 @@ type elementHandleAPI interface {
}

// requestAPI is the interface of an HTTP request.
type requestAPI interface {
type requestAPI interface { //nolint:interfacebloat
AllHeaders() map[string]string
Frame() *common.Frame
HeaderValue(string) sobek.Value
Expand All @@ -495,7 +495,7 @@ type requestAPI interface {
}

// responseAPI is the interface of an HTTP response.
type responseAPI interface {
type responseAPI interface { //nolint:interfacebloat
AllHeaders() map[string]string
Body() ([]byte, error)
Frame() *common.Frame
Expand All @@ -516,7 +516,7 @@ type responseAPI interface {
}

// locatorAPI represents a way to find element(s) on a page at any moment.
type locatorAPI interface {
type locatorAPI interface { //nolint:interfacebloat
Clear(opts *common.FrameFillOptions) error
Click(opts sobek.Value) error
Dblclick(opts sobek.Value) error
Expand Down
6 changes: 3 additions & 3 deletions browser/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func newBrowserRegistry(
return r
}

func (r *browserRegistry) handleIterEvents( //nolint:funlen
func (r *browserRegistry) handleIterEvents(
ctx context.Context, eventsCh <-chan *k6event.Event, unsubscribeFn func(),
) {
var (
Expand Down Expand Up @@ -291,7 +291,7 @@ func (r *browserRegistry) handleIterEvents( //nolint:funlen
continue
}

switch e.Type { //nolint:exhaustive
switch e.Type {
case k6event.IterStart:
// Because VU.State is nil when browser registry is initialized,
// we have to initialize traces registry on the first VU iteration
Expand Down Expand Up @@ -454,7 +454,7 @@ func (r *tracesRegistry) startIterationTrace(ctx context.Context, data k6event.I

spanCtx, span := r.tracer.Start(ctx, "iteration", oteltrace.WithAttributes(
attribute.Int64("test.iteration.number", data.Iteration),
attribute.Int64("test.vu", int64(data.VUID)),
attribute.Int64("test.vu", int64(data.VUID)), //nolint:gosec
attribute.String("test.scenario", data.ScenarioName),
))

Expand Down
4 changes: 3 additions & 1 deletion browser/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func TestBrowserRegistry(t *testing.T) {

vu := k6test.NewVU(t)
var cancel context.CancelFunc
vu.CtxField, cancel = context.WithCancel(vu.CtxField)
vu.CtxField, cancel = context.WithCancel(vu.CtxField) //nolint:fatcontext
browserRegistry := newBrowserRegistry(context.Background(), vu, remoteRegistry, &pidRegistry{}, nil)

vu.ActivateVU()
Expand Down Expand Up @@ -320,6 +320,7 @@ func TestBrowserRegistry(t *testing.T) {
}

func TestParseTracesMetadata(t *testing.T) {
mstoykov marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()
testCases := []struct {
name string
env map[string]string
Expand Down Expand Up @@ -388,6 +389,7 @@ func TestParseTracesMetadata(t *testing.T) {
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
lookup := func(key string) (string, bool) {
v, ok := tc.env[key]
return v, ok
Expand Down
Loading
Loading