Integrate golangci-lint linter

Integrate golangci-lint, which is a full-featured linter tool that
includes the checks from 'go vet' and many more.  Borrowed the
install_linter script from airship/airshipctl to install the script
from upstream. Address issues uncovered by the tool.

Change-Id: If30e0819f5d03243e4004bca07951f8aa5218749
This commit is contained in:
Gary Smith 2019-12-04 23:27:26 +00:00
parent 3575299373
commit 252c00c780
6 changed files with 192 additions and 16 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
# Coverage File
coverage.out
tools/bin

152
.golangci.yaml Normal file
View File

@ -0,0 +1,152 @@
# This file contains all available configuration options
# with their default values.
# options for analysis running
run:
# default concurrency is a available CPU number
concurrency: 4
# timeout for analysis, e.g. 30s, 5m, default is 1m
deadline: 5m
# exit code when at least one issue was found, default is 1
issues-exit-code: 1
# include test files or not, default is true
tests: true
# list of build tags, all linters use it. Default is empty list.
# build-tags:
# - mytag
# which dirs to skip: they won't be analyzed;
# can use regexp here: generated.*, regexp is applied on full path;
# default value is empty list, but next dirs are always skipped independently
# from this option's value:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
# skip-dirs:
# - src/external_libs
# - autogenerated_by_my_lib
# which files to skip: they will be analyzed, but issues from them
# won't be reported. Default value is empty list, but there is
# no need to include all autogenerated files, we confidently recognize
# autogenerated files. If it's not please let us know.
# skip-files:
# - ".*\\.my\\.go$"
# - lib/bad.go
# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
# If invoked with -mod=readonly, the go command is disallowed from the implicit
# automatic updating of go.mod described above. Instead, it fails when any changes
# to go.mod are needed. This setting is most useful to check that go.mod does
# not need updates, such as in a continuous integration and testing system.
# If invoked with -mod=vendor, the go command assumes that the vendor
# directory holds the correct copies of dependencies and ignores
# the dependency descriptions in go.mod.
# modules-download-mode: readonly|release|vendor
# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
format: colored-line-number
# print lines of code with issue, default is true
print-issued-lines: true
# print linter name in the end of issue text, default is true
print-linter-name: true
# all available settings of specific linters
linters-settings:
errcheck:
# report about not checking of errors in type assetions: `a := b.(MyStruct)`;
# default is false: such cases aren't reported by default.
check-type-assertions: false
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
# default is false: such cases aren't reported by default.
check-blank: false
# path to a file containing a list of functions to exclude from checking
# see https://github.com/kisielk/errcheck#excluding-functions for details
# exclude: /path/to/file.txt
govet:
# report about shadowed variables
check-shadowing: true
gofmt:
# simplify code: gofmt with `-s` option, true by default
simplify: true
goimports:
# put imports beginning with prefix after 3rd-party packages;
# it's a comma-separated list of prefixes
local-prefixes: opendev.org/airship/airshipui
gocyclo:
# minimal code complexity to report, 30 by default (but we recommend 10-20)
min-complexity: 15
dupl:
# tokens count to trigger issue, 150 by default
threshold: 100
goconst:
# minimal length of string constant, 3 by default
min-len: 3
# minimal occurrences count to trigger, 3 by default
min-occurrences: 3
misspell:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
locale: US
# ignore-words:
# - someword
lll:
# max line length, lines longer will be reported. Default is 120.
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
line-length: 120
# tab width in spaces. Default to 1.
tab-width: 1
unused:
# treat code as a program (not a library) and report unused exported identifiers; default is false.
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors:
# if it's called for subdir of a project it can't find funcs usages. All text editor integrations
# with golangci-lint call it on a directory with the changed file.
check-exported: false
unparam:
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
# if it's called for subdir of a project it can't find external interfaces. All text editor integrations
# with golangci-lint call it on a directory with the changed file.
check-exported: false
nakedret:
# make an issue if func has more lines of code than this setting and it has naked returns; default is 30
max-func-lines: 10
prealloc:
# XXX: we don't recommend using this linter before doing performance profiling.
# For most programs usage of prealloc will be a premature optimization.
# Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them.
# True by default.
simple: true
range-loops: true # Report preallocation suggestions on range loops, true by default
for-loops: false # Report preallocation suggestions on for loops, false by default
linters:
disable-all: true
enable:
- dupl # Tool for code clone detection
- goconst # Finds repeated strings that could be replaced by a constant
- gocritic # The most opinionated Go source code linter
- gocyclo # Computes and checks the cyclomatic complexity of functions
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification
- goimports # Goimports does everything that gofmt does. Additionally it checks unused imports
- gosec # Inspects source code for security problems
- interfacer # Linter that suggests narrower interface types
- lll # Reports long lines
- misspell # Finds commonly misspelled English words in comments
- nakedret # Finds naked returns in functions greater than a specified function length
- prealloc # Finds slice declarations that could potentially be preallocated
- scopelint # Scopelint checks for unpinned variables in go programs
- unconvert # Remove unnecessary type conversions
- unparam # Reports unused function parameters

View File

@ -5,6 +5,12 @@ SHELL=/bin/bash
# Obtain the version and git commit info
GIT_VERSION=$(shell git describe --match 'v*' --always)
TOOLBINDIR := tools/bin
LINTER := $(TOOLBINDIR)/golangci-lint
LINTER_CONFIG := .golangci.yaml
TESTFLAGS ?=
# Override the value of the version variable in main.go
LD_FLAGS= '-X main.version=$(GIT_VERSION)'
GO_FLAGS= -ldflags=$(LD_FLAGS)
@ -29,27 +35,28 @@ $(PLUGINS):
go build -o $(OCTANT_PLUGINSTUB_DIR)/$@-plugin $(GO_FLAGS) opendev.org/airship/airshipui/cmd/$@
.PHONY: test
test: generate
go test $(RECURSIVE_DIRS) -v -coverprofile=coverage.out
test:
go test $(RECURSIVE_DIRS) -v $(TESTFLAGS)
.PHONY: coverage-html
coverage-html: test
.PHONY: cover
cover: TESTFLAGS += -coverprofile=coverage.out
cover: test
go tool cover -html=coverage.out
.PHONY: vet
vet:
go vet $(RECURSIVE_DIRS)
.PHONY: generate
generate:
go generate -v $(RECURSIVE_DIRS)
.PHONY: clean
clean:
git clean -dx $(DIRS)
.PHONY: ci
ci: test vet
ci: test lint
# The golang-unit zuul job calls the env target, so create one
.PHONY: env
.PHONY: lint
lint: $(LINTER)
$(LINTER) run --config $(LINTER_CONFIG)
$(LINTER):
mkdir -p $(TOOLBINDIR)
./tools/install_linter

View File

@ -4,12 +4,12 @@ import (
"fmt"
"log"
"opendev.org/airship/airshipui/internal/plugin/airshipui"
plugin "opendev.org/airship/airshipui/internal/plugin/airshipui"
)
var (
pluginName = "airship-ui"
// version will be overriden by ldflags supplied in Makefile
// version will be overridden by ldflags supplied in Makefile
version = "(dev-version)"
)

View File

@ -14,7 +14,10 @@ func TestRegister(t *testing.T) {
t.Fatalf("Registering the plugin returned an error")
}
p.Validate()
err = p.Validate()
if err != nil {
t.Fatalf("Validating the plugin returned an error")
}
}
func TestRoutes(t *testing.T) {
@ -37,6 +40,7 @@ func TestRoutes(t *testing.T) {
}
for _, test := range tests {
test := test // pin the value so that the following function literal binds to it
t.Run(fmt.Sprintf("Path='%s'", test.path), func(t *testing.T) {
_, found := router.Match(test.path)
@ -66,6 +70,7 @@ func TestRouteHandles(t *testing.T) {
}
for _, test := range tests {
test := test // pin the value so that the following function literal binds to it
t.Run(fmt.Sprintf("Path='%s'", test.path), func(t *testing.T) {
handleFunc, found := router.Match(test.path)

11
tools/install_linter Executable file
View File

@ -0,0 +1,11 @@
#!/bin/bash
set -x
tools_bin_dir="${BASH_SOURCE%/*}"
download_url=https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh
version=v1.21.0
if ! curl -sfL "$download_url" | sh -s -- -b "$tools_bin_dir/bin" "$version"; then
printf "Something went wrong while installing golangci-lint\n" 1>&2
exit 1
fi