diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..6d6a12c --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,140 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +run: + timeout: 5m +linters: + # This set of linters are enabled by default: deadcode, errcheck, gosimple, govet, ineffasign, staticcheck, struccheck, typecheck, unused, varcheck + enable: + # List of all linters: https://golangci-lint.run/usage/linters/ + - whitespace #https://github.com/ultraware/whitespace + - noctx #https://github.com/sonatard/noctx + - nilerr #https://github.com/gostaticanalysis/nilerr + - nestif #https://github.com/nakabonne/nestif + - exportloopref #https://github.com/kyoh86/exportloopref + - bodyclose #https://github.com/timakin/bodyclose + # - goconst #https://github.com/jgautheron/goconst + - stylecheck #https://github.com/dominikh/go-tools/tree/master/stylecheck + - revive #golint is deprecated and golangci-lint recommends to use revive instead https://github.com/mgechev/revive + #other deprecated lint libraries: maligned, scopelint, interfacer + - gocritic #https://github.com/go-critic/go-critic + - unparam #https://github.com/mvdan/unparam + - misspell #https://github.com/client9/misspell + - errorlint #https://github.com/polyfloyd/go-errorlint + - gosec #https://github.com/securego/gosec + - predeclared #https://github.com/nishanths/predeclared + - unconvert #https://github.com/mdempsky/unconvert + +issues: + exclude-rules: + - path: _test\.go + linters: + - unused + - deadcode + # Exclude stylecheck underscore warnings in test files since most test files already follow this pattern + - linters: + - stylecheck + path: _test\.go + text: "should not use underscores in Go names" + # Exclude gocritic paramTypeCombine errors since it could make code less readable + - linters: + - gocritic + text: "paramTypeCombine" + # TO-DO: handle the following issues on separate tickets. + # Although errcheck is important for detecting bugs, it is disabled since fixing requires lots of logic testing. + - linters: + - errcheck + path: \.go + - linters: + - gocritic + path: resource_tfe_policy_set\.go + text: "badRegexp" + - linters: + - gocritic + path: data_source_ip_ranges_test\.go + text: "regexpSimplify: can re-write" + - linters: + - gosec + path: (_test|workspace_run_helpers)\.go + - linters: + - nestif + path: + (resource_tfe_team_access|resource_tfe_workspace|credentials|resource_tfe_policy_set| + resource_tfe_policy_set|resource_tfe_team_members|resource_tfe_notification_configuration|resource_tfe_team)\.go + - linters: + - unparam + path: _test\.go + text: "always receives" # ignore unparam warning for all test files when a function param receives same mock data for it's test examples + - linters: + - errorlint + path: + (data_source_organization|data_source_variable_set|data_source_workspace|resource_tfe_organization_token| + -|resource_tfe_team_token|plugin_provider_test|resource_tfe_agent_pool|resource_tfe_agent_token|resource_tfe_organization_module_sharing| + -|resource_tfe_policy_set|resource_tfe_policy_set_parameter|resource_tfe_team_members_test|resource_tfe_team_organization_member_test| + -|resource_tfe_notification_configuration|resource_tfe_oauth_client|resource_tfe_organization|resource_tfe_organization_membership|resource_tfe_team_members| + -|resource_tfe_registry_module|resource_tfe_run_trigger|resource_tfe_sentinel_policy|resource_tfe_ssh_key|resource_tfe_team|resource_tfe_team_access| + -|resource_tfe_team_member|resource_tfe_team_organization_member|resource_tfe_terraform_version|resource_tfe_variable_set|resource_tfe_variable| + -|resource_tfe_workspace|workspace_helpers)\.go + text: "comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error" + - linters: + - errorlint + path: provider\.go + text: "type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors" + - linters: + - gosimple + path: resource_tfe_workspace\.go + text: "S1002: should omit comparison to bool constant" + - linters: + - gosimple + path: data_source_ip_ranges_test\.go + text: "S1039: unnecessary use of fmt.Sprintf" + - linters: + - gosimple + path: resource_tfe_team_members_test\.go + text: "S1019: should use make" + - linters: + - staticcheck + path: (resource_tfe_team_access_migrate_test|workspace_helpers_test)\.go + text: "SA1012: do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use" + - linters: + - staticcheck + path: + (data_source_outputs|resource_tfe_agent_pool|resource_tfe_notification_configuration|resource_tfe_organization|resource_tfe_registry_module| + -|resource_tfe_run_trigger|resource_tfe_team_access|resource_tfe_organization_membership|resource_tfe_policy_set|resource_tfe_team_member| + -|resource_tfe_workspace|resource_tfe_team_organization_member|resource_tfe_variable_set|resource_tfe_team_project_access)\.go + text: "SA1019" + - linters: + - staticcheck + path: resource_tfe_workspace_run_task\.go + text: "SA1019: v.Stage is deprecated: Use Stages property instead" +linters-settings: + # errcheck: + # # https://github.com/kisielk/errcheck#excluding-functions + # check-type-assertions: true + # check-blank: true + goconst: + min-len: 20 + min-occurrences: 5 + ignore-calls: false + ignore-tests: true + gocritic: + enabled-tags: + - diagnostic + - opinionated + - performance + disabled-checks: + - unnamedResult + - hugeParam + - singleCaseSwitch + - ifElseChain + - nestingReduce + - sloppyTestFuncName + revive: + # see https://github.com/mgechev/revive#available-rules for details. + ignore-generated-header: false #recommended in their configuration + severity: warning + rules: + - name: indent-error-flow #Prevents redundant else statements + severity: warning + - name: useless-break + severity: warning diff --git a/GNUmakefile b/GNUmakefile index 0b2fdb4..9199428 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -10,12 +10,6 @@ build: fmtcheck terraform-svchost: fmtcheck @go build -o terraform-svchost -# Run unit tests -test: fmtcheck - go test -v $(TEST) || exit 1 - echo $(TEST) | \ - xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4 - vet: @echo "go vet ." @go vet $$(go list ./... | grep -v vendor/) ; if [ $$? -eq 1 ]; then \ @@ -41,13 +35,5 @@ fmtcheck: errcheck: @sh -c "'$(CURDIR)/scripts/errcheck.sh'" -test-compile: - @if [ "$(TEST)" = "./..." ]; then \ - echo "ERROR: Set TEST to a specific package. For example,"; \ - echo " make test-compile TEST=./$(PKG_NAME)"; \ - exit 1; \ - fi - go test -c $(TEST) $(TESTARGS) - .PHONY: build test testacc vet fmt fmtcheck errcheck test-compile sweep diff --git a/scripts/errcheck.sh b/scripts/errcheck.sh new file mode 100644 index 0000000..2589dc8 --- /dev/null +++ b/scripts/errcheck.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + + +# Check gofmt +echo "==> Checking for unchecked errors..." + +if ! which errcheck > /dev/null; then + echo "==> Installing errcheck..." + go get -u github.com/kisielk/errcheck +fi + +err_files=$(errcheck -ignoretests \ + -ignore 'github.com/hashicorp/terraform/helper/schema:Set' \ + -ignore 'bytes:.*' \ + -ignore 'io:Close|Write' \ + $(go list ./...| grep -v /vendor/)) + +if [[ -n ${err_files} ]]; then + echo 'Unchecked errors found in the following places:' + echo "${err_files}" + echo "Please handle returned errors. You can check directly with \`make errcheck\`" + exit 1 +fi + +exit 0