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

Add golangci-lint #58

Merged
merged 8 commits into from
May 19, 2023
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
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: 2
jobs:
build-go1.19:
build-go1.20:
docker:
- image: golang:1.19
- image: golang:1.20
working_directory: /go/src/github.com/kolide/kit
steps: &steps
- checkout
Expand All @@ -13,4 +13,4 @@ workflows:
version: 2
build:
jobs:
- build-go1.19
- build-go1.20
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# set line endings for go files. Mostly needed for golang-ci
*.go text eol=lf

33 changes: 33 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: golangci-lint

on:
push:
branches: [main, master]
pull_request:
branches: '**'

jobs:
golangci:
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
name: lint
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.20.4'

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
skip-pkg-cache: true

# Run again as a workaround for https://github.com/golangci/golangci-lint-action/issues/362
- name: golangci-lint
if: ${{ always() }}
run: golangci-lint run
39 changes: 39 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
run:
skip-dirs:
- test-cmds
timeout: 5m

linters:
enable:
- bodyclose
- unused
- gofmt
- govet
- misspell
- nakedret
- unconvert
- paralleltest
disable:
- errcheck
- gosec
- gosimple
- ineffassign
- interfacer
- maligned
- noctx
- staticcheck
- structcheck
- varcheck

linters-settings:
errcheck:
ignore: github.com/go-kit/kit/log:Log
gofmt:
simplify: false

issues:
exclude-rules:
# False positive: https://github.com/kunwardeep/paralleltest/issues/8.
- linters:
- paralleltest
text: "does not use range value in test Run"
10 changes: 6 additions & 4 deletions entrypoint/entrypoint.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/*
Package entrypoint replaces the shell version of
exec $@
which is often use when creating docker entrypoint scripts to wrap a
binary with some initial setup.
Package entrypoint replaces the shell version of

exec $@

which is often use when creating docker entrypoint scripts to wrap a
binary with some initial setup.
*/
package entrypoint

Expand Down
16 changes: 8 additions & 8 deletions env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"
)

func TestDuration(t *testing.T) {
func TestDuration(t *testing.T) { //nolint:paralleltest
var tests = []struct {
value time.Duration
}{
Expand All @@ -16,7 +16,7 @@ func TestDuration(t *testing.T) {
{value: 1 * time.Hour},
}

for _, tt := range tests {
for _, tt := range tests { //nolint:paralleltest
t.Run(tt.value.String(), func(t *testing.T) {
key := strings.ToUpper(tt.value.String())
if err := os.Setenv(key, tt.value.String()); err != nil {
Expand All @@ -37,7 +37,7 @@ func TestDuration(t *testing.T) {
}
}

func TestString(t *testing.T) {
func TestString(t *testing.T) { //nolint:paralleltest
var tests = []struct {
value string
}{
Expand All @@ -46,7 +46,7 @@ func TestString(t *testing.T) {
{value: "baz"},
}

for _, tt := range tests {
for _, tt := range tests { //nolint:paralleltest
t.Run(tt.value, func(t *testing.T) {
key := strings.ToUpper(tt.value)
if err := os.Setenv(key, tt.value); err != nil {
Expand All @@ -67,7 +67,7 @@ func TestString(t *testing.T) {
}
}

func TestBool(t *testing.T) {
func TestBool(t *testing.T) { //nolint:paralleltest
var tests = []struct {
env string
value bool
Expand All @@ -81,7 +81,7 @@ func TestBool(t *testing.T) {
{env: "0", value: false},
}

for _, tt := range tests {
for _, tt := range tests { //nolint:paralleltest
t.Run(tt.env, func(t *testing.T) {
key := "TEST_BOOL"
if err := os.Setenv(key, tt.env); err != nil {
Expand All @@ -106,7 +106,7 @@ func TestBool(t *testing.T) {
}
}

func TestInt(t *testing.T) {
func TestInt(t *testing.T) { //nolint:paralleltest
var tests = []struct {
env string
value int
Expand All @@ -117,7 +117,7 @@ func TestInt(t *testing.T) {
{env: "0", value: 0},
}

for _, tt := range tests {
for _, tt := range tests { //nolint:paralleltest
t.Run(tt.env, func(t *testing.T) {
key := "TEST_INT"
if err := os.Setenv(key, tt.env); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions fsutil/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

func TestSanitizeExtractPath(t *testing.T) {
t.Parallel()

var tests = []struct {
filepath string
destination string
Expand Down
7 changes: 7 additions & 0 deletions health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
)

func TestCheckHealth(t *testing.T) {
t.Parallel()

checkers := map[string]Checker{
"fail": fail{},
"pass": Nop(),
Expand All @@ -34,6 +36,8 @@ func (c fail) HealthCheck() error {
}

func TestHealthzHandler(t *testing.T) {
t.Parallel()

logger := log.NewNopLogger()
failing := Handler(logger, map[string]Checker{
"mock": healthcheckFunc(func() error {
Expand All @@ -53,7 +57,10 @@ func TestHealthzHandler(t *testing.T) {
{500, failing},
}
for _, tt := range httpTests {
tt := tt
t.Run("", func(t *testing.T) {
t.Parallel()

rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/healthz", nil)
tt.handler.ServeHTTP(rr, req)
Expand Down
4 changes: 3 additions & 1 deletion httputil/middleware_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ func ExampleChain() {
srv := httptest.NewServer(h)
defer srv.Close()

if _, err := http.Get(srv.URL); err != nil {
resp, err := http.Get(srv.URL)
if err != nil {
panic(err)
}
defer resp.Body.Close()

// Output:
// annotate: one
Expand Down
1 change: 1 addition & 0 deletions logutil/swap_signal.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package logutil
Expand Down
1 change: 1 addition & 0 deletions logutil/swap_signal_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package logutil
Expand Down
14 changes: 14 additions & 0 deletions munemo/munemo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,30 @@ func TestMunemoOriginal(t *testing.T) {

func testMunemo(t *testing.T, mg *munemoGenerator, tests []testCase) {
for _, tt := range tests {
tt := tt
if tt.e == "" {
// If we lack an error, this is a legit conversion. Try both ways
if !tt.skipInt {
t.Run(fmt.Sprintf("string/%d", tt.i), func(t *testing.T) {
t.Parallel()

ret := mg.String(tt.i)
require.Equal(t, tt.s, ret)
})
}

t.Run(fmt.Sprintf("int/%s", tt.s), func(t *testing.T) {
t.Parallel()

ret, err := mg.Int(tt.s)
assert.Equal(t, tt.i, ret)
assert.NoError(t, err)
})
} else {
// Having an error, means we're looking for an error
t.Run(fmt.Sprintf("interr/%s", tt.s), func(t *testing.T) {
t.Parallel()

ret, err := mg.Int(tt.s)
require.Equal(t, tt.i, ret)
require.EqualError(t, err, tt.e)
Expand All @@ -98,23 +105,30 @@ func TestLegacyInterfaces(t *testing.T) {
t.Parallel()

for _, tt := range originalTests {
tt := tt
if tt.e == "" {
// If we lack an error, this is a legit conversion. Try both ways
if !tt.skipInt {
t.Run(fmt.Sprintf("Munemo/%d", tt.i), func(t *testing.T) {
t.Parallel()

ret := Munemo(tt.i)
require.Equal(t, tt.s, ret)
})
}

t.Run(fmt.Sprintf("UnMunemo/%s", tt.s), func(t *testing.T) {
t.Parallel()

ret, err := UnMunemo(tt.s)
assert.Equal(t, tt.i, ret)
assert.NoError(t, err)
})
} else {
// Having an error, means we're looking for an error
t.Run(fmt.Sprintf("UnMunemo/%s", tt.s), func(t *testing.T) {
t.Parallel()

ret, err := UnMunemo(tt.s)
require.Equal(t, tt.i, ret)
require.EqualError(t, err, tt.e)
Expand Down
3 changes: 3 additions & 0 deletions pgutil/pgutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func TestConversion(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run("", func(t *testing.T) {
t.Parallel()

c, err := NewFromURL(tt.in, tt.opts...)
if tt.err {
require.Error(t, err)
Expand Down
16 changes: 16 additions & 0 deletions testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

func TestErrorAfterSuccess(t *testing.T) {
t.Parallel()

var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand All @@ -18,6 +20,8 @@ func TestErrorAfterSuccess(t *testing.T) {
}

func TestErrorAfterError(t *testing.T) {
t.Parallel()

var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand All @@ -30,6 +34,8 @@ func TestErrorAfterError(t *testing.T) {
}

func TestErrorAfterFuncSuccess(t *testing.T) {
t.Parallel()

err := ErrorAfterFunc(100*time.Millisecond, func() {
time.Sleep(1 * time.Millisecond)
})
Expand All @@ -39,6 +45,8 @@ func TestErrorAfterFuncSuccess(t *testing.T) {
}

func TestErrorAfterFuncError(t *testing.T) {
t.Parallel()

err := ErrorAfterFunc(1*time.Millisecond, func() {
time.Sleep(100 * time.Millisecond)
})
Expand Down Expand Up @@ -69,6 +77,8 @@ func (m *mockFatal) Fatalf(string, ...interface{}) {
}

func TestFatalAfterFuncSuccess(t *testing.T) {
t.Parallel()

var m mockFatal

// Should not fatal
Expand All @@ -84,6 +94,8 @@ func TestFatalAfterFuncSuccess(t *testing.T) {
}

func TestFatalAfterFuncFatal(t *testing.T) {
t.Parallel()

var m mockFatal

// Should fatal
Expand Down Expand Up @@ -115,6 +127,8 @@ func ExampleFatalAfterFunc_fatal() {
}

func TestFatalAfterSuccess(t *testing.T) {
t.Parallel()

var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand All @@ -132,6 +146,8 @@ func TestFatalAfterSuccess(t *testing.T) {
}

func TestFatalAfterFatal(t *testing.T) {
t.Parallel()

var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand Down
2 changes: 2 additions & 0 deletions tlsutil/tlsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
)

func TestNewConfig(t *testing.T) {
t.Parallel()

// default, should have Modern compatibility.
cfg := NewConfig()
if have, want := cfg.MinVersion, uint16(tls.VersionTLS12); have != want {
Expand Down
Loading
Loading