Skip to content

Commit

Permalink
Run staticcheck explicitly in make lint
Browse files Browse the repository at this point in the history
It is currently part of golangci-lint, but seems to have configuration
issues. If we run is directly, we can control it better, including using
the recommended `//lint:ignore` directives.

This also fixes or ignores staticcheck issues in the code
  • Loading branch information
Kieron Browne authored and kieron-dev committed Jul 6, 2023
1 parent d0f67a2 commit 46b1ad2
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ linters:
- dupl
- ginkgolinter

disable:
- staticcheck # we run this separately

issues:
exclude-rules:
- path: _test\.go
Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@ fmt: install-gofumpt install-shfmt
vet: ## Run go vet against code.
go vet ./...

lint: fmt vet gosec
lint: fmt vet gosec staticcheck
golangci-lint run -v

gosec: install-gosec
$(GOSEC) --exclude=G101,G304,G401,G404,G505 --exclude-dir=tests ./...

staticcheck: install-staticcheck
$(STATICCHECK) ./...

test: lint
@for comp in $(COMPONENTS); do make -C $$comp test; done
make test-tools
make test-e2e


test-tools:
./scripts/run-tests.sh tools

Expand All @@ -79,5 +81,9 @@ GOSEC = $(shell go env GOPATH)/bin/gosec
install-gosec:
go install github.com/securego/gosec/v2/cmd/gosec@latest

STATICCHECK = $(shell go env GOPATH)/bin/staticcheck
install-staticcheck:
go install honnef.co/go/tools/cmd/staticcheck@latest

vendir-update-dependencies: install-vendir
$(VENDIR) sync --chdir tests
10 changes: 6 additions & 4 deletions api/actions/manifest/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@ func procValIfSet[T any](appVal, procVal *T) *T {

func fixDeprecatedFields(appInfo *payloads.ManifestApplication) {
if appInfo.DiskQuota == nil {
//nolint:staticcheck
//lint:ignore SA1019 we have to deal with this deprecation
appInfo.DiskQuota = appInfo.AltDiskQuota
}

for i := range appInfo.Processes {
if appInfo.Processes[i].DiskQuota == nil {
//nolint:staticcheck
//lint:ignore SA1019 we have to deal with this deprecation
appInfo.Processes[i].DiskQuota = appInfo.Processes[i].AltDiskQuota
}
}

if hasBuildpackSet(appInfo.Buildpack) { // nolint: staticcheck
appInfo.Buildpacks = append(appInfo.Buildpacks, appInfo.Buildpack) // nolint: staticcheck
//lint:ignore SA1019 we have to deal with this deprecation
if hasBuildpackSet(appInfo.Buildpack) {
//lint:ignore SA1019 we have to deal with this deprecation
appInfo.Buildpacks = append(appInfo.Buildpacks, appInfo.Buildpack)
}
}

Expand Down
11 changes: 7 additions & 4 deletions api/actions/manifest/normalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ var _ = Describe("Normalizer", func() {

When("deprecated 'buildpack' is specified", func() {
BeforeEach(func() {
appInfo.Buildpack = "deprecated-buildpack" // nolint: staticcheck
//lint:ignore SA1019 we have to deal with this deprecation
appInfo.Buildpack = "deprecated-buildpack"
})

It("adds it to the buildpacks list", func() {
Expand All @@ -91,7 +92,8 @@ var _ = Describe("Normalizer", func() {

When("set to 'default'", func() {
BeforeEach(func() {
appInfo.Buildpack = "default" // nolint: staticcheck
//lint:ignore SA1019 we have to deal with this deprecation
appInfo.Buildpack = "default"
})

It("ignores it", func() {
Expand All @@ -103,7 +105,8 @@ var _ = Describe("Normalizer", func() {

When("set to 'null'", func() {
BeforeEach(func() {
appInfo.Buildpack = "null" // nolint: staticcheck
//lint:ignore SA1019 we have to deal with this deprecation
appInfo.Buildpack = "null"
})

It("ignores it", func() {
Expand Down Expand Up @@ -370,7 +373,7 @@ var _ = Describe("Normalizer", func() {

When("disk-quota is set on app", func() {
BeforeEach(func() {
//nolint:staticcheck
//lint:ignore SA1019 we have to deal with this deprecation
appInfo.AltDiskQuota = tools.PtrTo("123M")
})

Expand Down
2 changes: 1 addition & 1 deletion api/authorization/testhelpers/client_cert.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package testhelpers

import (
. "github.com/onsi/gomega"
. "github.com/onsi/gomega" //lint:ignore ST1001 this is a test file
"sigs.k8s.io/controller-runtime/pkg/envtest"
)

Expand Down
2 changes: 1 addition & 1 deletion api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (c *APIConfig) validate() error {

if c.UserCertificateExpirationWarningDuration != "" {
if _, err := time.ParseDuration(c.UserCertificateExpirationWarningDuration); err != nil {
return errors.New(`Invalid duration format for userCertificateExpirationWarningDuration. Use a format like "48h"`)
return errors.New(`invalid duration format for userCertificateExpirationWarningDuration. Use a format like "48h"`)
}
}

Expand Down
2 changes: 1 addition & 1 deletion api/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ var _ = Describe("Config", func() {
})

It("returns an error", func() {
Expect(loadErr).To(MatchError(ContainSubstring("Invalid duration format")))
Expect(loadErr).To(MatchError(ContainSubstring("invalid duration format")))
})
})

Expand Down
6 changes: 3 additions & 3 deletions api/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,12 @@ type RollingDeployNotSupportedError struct {
apiError
}

func NewRollingDeployNotSupportedError(cause error) RollingDeployNotSupportedError {
func NewRollingDeployNotSupportedError(runnerName string) RollingDeployNotSupportedError {
detail := fmt.Sprintf("The configured runner '%s' does not support rolling deploys", runnerName)
return RollingDeployNotSupportedError{
apiError: apiError{
cause: cause,
title: "CF-RollingDeployNotSupported",
detail: cause.Error(),
detail: detail,
code: 42000,
httpStatus: http.StatusBadRequest,
},
Expand Down
7 changes: 2 additions & 5 deletions api/handlers/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package handlers
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"

Expand Down Expand Up @@ -74,15 +73,13 @@ func (h *Deployment) create(r *http.Request) (*routing.Response, error) {
var notFoundErr apierrors.NotFoundError
if errors.As(err, &notFoundErr) {
logger.Info("Could not find RunnerInfo", "runner", h.runnerName, "error", err)
ret := fmt.Errorf("The configured runner '%s' does not support rolling deploys", h.runnerName)
return nil, apierrors.NewRollingDeployNotSupportedError(ret)
return nil, apierrors.NewRollingDeployNotSupportedError(h.runnerName)
}
return nil, apierrors.LogAndReturn(logger, apierrors.ForbiddenAsNotFound(err), "Error getting runner info in repository")
}

if !runnerInfo.Capabilities.RollingDeploy {
err = fmt.Errorf("The configured runner '%s' does not support rolling deploys", h.runnerName)
return nil, apierrors.LogAndReturn(logger, apierrors.NewRollingDeployNotSupportedError(err), "runner does not support rolling deploys", "name", h.runnerName)
return nil, apierrors.LogAndReturn(logger, apierrors.NewRollingDeployNotSupportedError(h.runnerName), "runner does not support rolling deploys", "name", h.runnerName)
}

deploymentCreateMessage := payload.ToMessage()
Expand Down
5 changes: 3 additions & 2 deletions scripts/helmdoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"

"golang.org/x/exp/maps"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)

func printDocForSchema(schema map[string]any, indentLevel int) {
Expand All @@ -34,8 +36,7 @@ func printDocForSchema(schema map[string]any, indentLevel int) {
if typeStr == "object" {
typeStr = ""
} else {
// nolint:staticcheck
typeStr = " (_" + strings.Title(typeStr) + "_)"
typeStr = " (_" + cases.Title(language.AmericanEnglish).String(typeStr) + "_)"
}

fmt.Printf("%s- `%s`%s:%s\n", indentStr, name, typeStr, desc)
Expand Down
4 changes: 2 additions & 2 deletions tests/helpers/cache_syncing_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package helpers
import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/ginkgo/v2" //lint:ignore ST1001 this is a test file
. "github.com/onsi/gomega" //lint:ignore ST1001 this is a test file
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/cert_auth_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"math/big"
"time"

. "github.com/onsi/gomega"
. "github.com/onsi/gomega" //lint:ignore ST1001 this is a test file
)

func CreateCertificatePEM() []byte {
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/eventually_should_hold.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package helpers

import (
. "github.com/onsi/gomega"
. "github.com/onsi/gomega" //lint:ignore ST1001 this is a test file
)

func EventuallyShouldHold(condition func(g Gomega)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/matchers/wraps_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"

. "github.com/onsi/gomega"
. "github.com/onsi/gomega" //lint:ignore ST1001 this is a test file
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
)
Expand Down

0 comments on commit 46b1ad2

Please sign in to comment.