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

chore: Lint tests #5709

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ec654b2
use gofmput to format code
faddat Aug 6, 2023
00c5444
use gci to format imports
faddat Aug 6, 2023
b6a44e8
lint tests, too
faddat Aug 6, 2023
74cc70f
naked return checks
faddat Aug 6, 2023
6d529e3
continue linting tests
faddat Aug 6, 2023
84504d3
reconfigure gci
faddat Aug 6, 2023
4b47929
linter autofixes
faddat Aug 6, 2023
c4a08bd
rearrange imports a little
faddat Aug 6, 2023
8cd1134
all changes other than unused-parameter
faddat Aug 6, 2023
c416fb8
use revive linter
faddat Aug 6, 2023
db5de9b
enable thelper
faddat Aug 6, 2023
4b370b9
Merge branch 'revive' into lint-tests
faddat Aug 6, 2023
ba34aa4
autofixes (golangci-lint run ./... --fix)
faddat Aug 6, 2023
968dbc1
further clean tests
faddat Aug 6, 2023
2358852
continue linting tests
faddat Aug 6, 2023
3105a69
Merge remote-tracking branch 'origin/master' into lint-tests
faddat Aug 14, 2023
273455c
update test lints
faddat Aug 14, 2023
2bf86fa
unused-recievers
faddat Aug 14, 2023
c41a8cd
lint tests
faddat Aug 14, 2023
6790811
remove a _
faddat Aug 14, 2023
e7d92a1
reverse some unused-reciever changes
faddat Aug 14, 2023
3fbbfa3
_ -> ctx
faddat Aug 14, 2023
b71f517
revert changes to dependabot
faddat Aug 14, 2023
4b22b11
restore an "unused parameter"
faddat Aug 14, 2023
54fcb8e
add nolintlint
faddat Aug 14, 2023
eb5741f
nolintlint
faddat Aug 14, 2023
8944632
return the error cleanly (handler.go)
faddat Aug 14, 2023
813b684
revert addition of blanks
faddat Aug 14, 2023
cd0e19e
revert addition of blanks
faddat Aug 14, 2023
b113c3a
revert addition of blanks
faddat Aug 14, 2023
756ae11
revert addition of blanks
faddat Aug 14, 2023
14b54e9
revert addition of blanks
faddat Aug 14, 2023
24ab9f4
revert additon of blanks
faddat Aug 14, 2023
c386c0a
Merge branch 'master' into lint-tests
faddat Aug 25, 2023
30e99a2
Merge remote-tracking branch 'origin/master' into lint-tests
faddat Sep 15, 2023
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
76 changes: 75 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,74 @@
linters-settings:
revive:
enable-all-rules: true
# Do NOT whine about the following, full explanation found in:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not convinced anybody "whines".

Suggested change
# Do NOT whine about the following, full explanation found in:
# Had to disable these out of the 77 rules. Full explanation of all rules found in:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I think they're actually referring to the linter complaining when we don't really care about these things or they aren't relevant in our project. :) Not a person. But yeah, maybe the proposed change here is better.

# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#description-of-available-rules
rules:
- name: use-any
disabled: true
- name: if-return
disabled: true
- name: max-public-structs
disabled: true
- name: cognitive-complexity
disabled: true
- name: argument-limit
disabled: true
- name: cyclomatic
disabled: true
- name: file-header
disabled: true
- name: function-length
disabled: true
- name: function-result-limit
disabled: true
- name: line-length-limit
disabled: true
- name: flag-parameter
disabled: true
- name: add-constant
disabled: true
- name: empty-lines
disabled: true
- name: banned-characters
disabled: true
- name: deep-exit
disabled: true
- name: confusing-results
disabled: true
- name: unused-parameter
disabled: true
- name: modifies-value-receiver
disabled: true
- name: early-return
disabled: true
- name: unnecessary-stmt
disabled: true
- name: import-shadowing
disabled: true
- name: defer
disabled: true
- name: confusing-naming
disabled: true
- name: nested-structs
disabled: true
- name: unhandled-error
disabled: true
# Arguments added below do not trigger the rule.
# arguments =["os\.(Create|WriteFile|Chmod)", "fmt\.Print", "myFunction", "net\..*", "bytes\.Buffer\.Write"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# arguments =["os\.(Create|WriteFile|Chmod)", "fmt\.Print", "myFunction", "net\..*", "bytes\.Buffer\.Write"]

arguments:
- 'fmt\..*'
- 'c.Conn.Close'
- 'server.Serve'
- 'http.ListenAndServe'
- 'conn.Close'
- 'os.Stderr.Write'
- 'buf\.(WriteString|Write)'
- 'csc.Close'
- 'cl.Close'
- 'out.WriteRune'
- 'sb\.(WriteRune|WriteString)'
- 'reconn.Conn.Close'
errcheck:
ignore: fmt:.*,go.uber.org/zap/zapcore:^Add.*
ignoretests: true
Expand Down Expand Up @@ -27,9 +97,12 @@ linters:
- gosimple
- govet
- ineffassign
- nolintlint
- misspell
- prealloc
- revive
- staticcheck
- thelper
- typecheck
- unconvert
- unused
Expand All @@ -51,7 +124,6 @@ linters:
# - godot
# - godox
# - goerr113
# - gofumpt
# - goheader
# - golint
# - gomnd
Expand Down Expand Up @@ -88,6 +160,8 @@ output:
print-linter-name: true

issues:
max-issues-per-linter: 0
max-same-issues: 0
exclude-rules:
# we aren't calling unknown URL
- text: 'G107' # G107: Url provided to HTTP request as taint input
Expand Down
6 changes: 4 additions & 2 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ func (h adminHandler) checkOrigin(r *http.Request) (string, error) {
return origin.String(), nil
}

func (h adminHandler) getOrigin(r *http.Request) (string, *url.URL) {
func (adminHandler) getOrigin(r *http.Request) (string, *url.URL) {
origin := r.Header.Get("Origin")
if origin == "" {
origin = r.Header.Get("Referer")
Expand Down Expand Up @@ -961,9 +961,11 @@ func handleConfig(w http.ResponseWriter, r *http.Request) error {
// Set the ETag as a trailer header.
// The alternative is to write the config to a buffer, and
// then hash that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the extra line here?
The comment is related to the next line.

Suggested change

w.Header().Set("Trailer", "ETag")

hash := etagHasher()

configWriter := io.MultiWriter(w, hash)
err := readConfig(r.URL.Path, configWriter)
if err != nil {
Expand Down Expand Up @@ -1022,7 +1024,7 @@ func handleConfig(w http.ResponseWriter, r *http.Request) error {
return nil
}

func handleConfigID(w http.ResponseWriter, r *http.Request) error {
func handleConfigID(_ http.ResponseWriter, r *http.Request) error {
idPath := r.URL.Path

parts := strings.Split(idPath, "/")
Expand Down
5 changes: 4 additions & 1 deletion admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ func TestETags(t *testing.T) {

func BenchmarkLoad(b *testing.B) {
for i := 0; i < b.N; i++ {
Load(testCfg, true)
err := Load(testCfg, true)
if err != nil {
b.Fatal(err)
}
}
}
6 changes: 3 additions & 3 deletions caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,11 +884,11 @@ func Version() (simple, full string) {
if CustomVersion != "" {
full = CustomVersion
simple = CustomVersion
return
return full, simple
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is permuting both strings, no?

Suggested change
return full, simple
return simple, full

Not that it matters here, but better be consistent.

}
full = "unknown"
simple = "unknown"
return
return full, simple
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is permuting both strings, no?

Suggested change
return full, simple
return simple, full

Not that it matters here, but better be consistent.

}
// find the Caddy module in the dependency list
for _, dep := range bi.Deps {
Expand Down Expand Up @@ -968,7 +968,7 @@ func Version() (simple, full string) {
}
}

return
return simple, full
}

// ActiveContext returns the currently-active context.
Expand Down
4 changes: 2 additions & 2 deletions caddyconfig/caddyfile/dispenser.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ func (d *Dispenser) ScalarVal() any {
if num, err := strconv.ParseFloat(text, 64); err == nil {
return num
}
if bool, err := strconv.ParseBool(text); err == nil {
return bool
if boolean, err := strconv.ParseBool(text); err == nil {
return boolean
}
return text
}
Expand Down
2 changes: 1 addition & 1 deletion caddyconfig/caddyfile/dispenser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func TestDispenser_ArgErr_Err(t *testing.T) {
t.Errorf("Expected error message with custom message in it ('foobar'); got '%v'", err)
}

var ErrBarIsFull = errors.New("bar is full")
ErrBarIsFull := errors.New("bar is full")
bookingError := d.Errf("unable to reserve: %w", ErrBarIsFull)
if !errors.Is(bookingError, ErrBarIsFull) {
t.Errorf("Errf(): should be able to unwrap the error chain")
Expand Down
10 changes: 5 additions & 5 deletions caddyconfig/caddyfile/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ func Format(input []byte) []byte {
space = true
nextLine()
continue
} else {
write(ch)
continue
}
write(ch)
continue

}

if !escaped && ch == '\\' {
Expand Down Expand Up @@ -125,13 +125,13 @@ func Format(input []byte) []byte {
spacePrior := space
space = false

//////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////
// I find it helpful to think of the formatting loop in two
// main sections; by the time we reach this point, we
// know we are in a "regular" part of the file: we know
// the character is not a space, not in a literal segment
// like a comment or quoted, it's not escaped, etc.
//////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////

if ch == '#' {
comment = true
Expand Down
2 changes: 1 addition & 1 deletion caddyconfig/caddyfile/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ block {

if string(actual) != tc.expect {
t.Errorf("\n[TEST %d: %s]\n====== EXPECTED ======\n%s\n====== ACTUAL ======\n%s^^^^^^^^^^^^^^^^^^^^^",
i, tc.description, string(tc.expect), string(actual))
i, tc.description, tc.expect, string(actual))
}
}
}
1 change: 1 addition & 0 deletions caddyconfig/caddyfile/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ EOF same-line-arg
}

func lexerCompare(t *testing.T, n int, expected, actual []Token) {
t.Helper()
if len(expected) != len(actual) {
t.Fatalf("Test case %d: expected %d token(s) but got %d", n, len(expected), len(actual))
}
Expand Down
8 changes: 4 additions & 4 deletions caddyconfig/caddyfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func (p *parser) doImport(nesting int) error {

var (
maybeSnippet bool
maybeSnippetId bool
maybeSnippetID bool
index int
)

Expand All @@ -472,16 +472,16 @@ func (p *parser) doImport(nesting int) error {
}

if index == 0 && len(token.Text) >= 3 && strings.HasPrefix(token.Text, "(") && strings.HasSuffix(token.Text, ")") {
maybeSnippetId = true
maybeSnippetID = true
}
}

switch token.Text {
case "{":
nesting++
if index == 1 && maybeSnippetId && nesting == 1 {
if index == 1 && maybeSnippetID && nesting == 1 {
maybeSnippet = true
maybeSnippetId = false
maybeSnippetID = false
}
case "}":
nesting--
Expand Down
19 changes: 10 additions & 9 deletions caddyconfig/caddyfile/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func TestParseVariadic(t *testing.T) {
var args = make([]string, 10)
args := make([]string, 10)
for i, tc := range []struct {
input string
result bool
Expand Down Expand Up @@ -107,7 +107,6 @@ func TestAllTokens(t *testing.T) {
input := []byte("a b c\nd e")
expected := []string{"a", "b", "c", "d", "e"}
tokens, err := allTokens("TestAllTokens", input)

if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
Expand Down Expand Up @@ -145,10 +144,11 @@ func TestParseOneAndImport(t *testing.T) {
"localhost",
}, []int{1}},

{`localhost:1234
{
`localhost:1234
dir1 foo bar`, false, []string{
"localhost:1234",
}, []int{3},
"localhost:1234",
}, []int{3},
},

{`localhost {
Expand Down Expand Up @@ -403,13 +403,13 @@ func TestRecursiveImport(t *testing.T) {
err = os.WriteFile(recursiveFile1, []byte(
`localhost
dir1
import recursive_import_test2`), 0644)
import recursive_import_test2`), 0o600)
if err != nil {
t.Fatal(err)
}
defer os.Remove(recursiveFile1)

err = os.WriteFile(recursiveFile2, []byte("dir2 1"), 0644)
err = os.WriteFile(recursiveFile2, []byte("dir2 1"), 0o600)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -437,7 +437,7 @@ func TestRecursiveImport(t *testing.T) {
err = os.WriteFile(recursiveFile1, []byte(
`localhost
dir1
import `+recursiveFile2), 0644)
import `+recursiveFile2), 0o600)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -491,7 +491,7 @@ func TestDirectiveImport(t *testing.T) {
}

err = os.WriteFile(directiveFile, []byte(`prop1 1
prop2 2`), 0644)
prop2 2`), 0o600)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -780,6 +780,7 @@ func TestSnippets(t *testing.T) {
}

func writeStringToTempFileOrDie(t *testing.T, str string) (pathToFile string) {
t.Helper()
file, err := os.CreateTemp("", t.Name())
if err != nil {
panic(err) // get a stack trace so we know where this was called from.
Expand Down
4 changes: 2 additions & 2 deletions caddyconfig/httpcaddyfile/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (st *ServerType) mapAddressToServerBlocks(originalServerBlocks []serverBloc
// entries are deleted from the addrToServerBlocks map. Essentially, each pairing (each
// association from multiple addresses to multiple server blocks; i.e. each element of
// the returned slice) becomes a server definition in the output JSON.
func (st *ServerType) consolidateAddrMappings(addrToServerBlocks map[string][]serverBlock) []sbAddrAssociation {
func (*ServerType) consolidateAddrMappings(addrToServerBlocks map[string][]serverBlock) []sbAddrAssociation {
sbaddrs := make([]sbAddrAssociation, 0, len(addrToServerBlocks))
for addr, sblocks := range addrToServerBlocks {
// we start with knowing that at least this address
Expand Down Expand Up @@ -188,7 +188,7 @@ func (st *ServerType) consolidateAddrMappings(addrToServerBlocks map[string][]se

// listenerAddrsForServerBlockKey essentially converts the Caddyfile
// site addresses to Caddy listener addresses for each server block.
func (st *ServerType) listenerAddrsForServerBlockKey(sblock serverBlock, key string,
func (*ServerType) listenerAddrsForServerBlockKey(sblock serverBlock, key string,
options map[string]any,
) ([]string, error) {
addr, err := ParseAddress(key)
Expand Down
2 changes: 1 addition & 1 deletion caddyconfig/httpcaddyfile/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (h Helper) GroupRoutes(vals []ConfigValue) {

// NewBindAddresses returns config values relevant to adding
// listener bind addresses to the config.
func (h Helper) NewBindAddresses(addrs []string) []ConfigValue {
func (Helper) NewBindAddresses(addrs []string) []ConfigValue {
return []ConfigValue{{Class: "bind", Value: addrs}}
}

Expand Down
9 changes: 6 additions & 3 deletions caddyconfig/httpcaddyfile/directives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ func TestHostsFromKeys(t *testing.T) {
[]Address{
{Original: ":2015", Port: "2015"},
},
[]string{}, []string{},
[]string{},
[]string{},
},
{
[]Address{
{Original: ":443", Port: "443"},
},
[]string{}, []string{},
[]string{},
[]string{},
},
{
[]Address{
{Original: "foo", Host: "foo"},
{Original: ":2015", Port: "2015"},
},
[]string{}, []string{"foo"},
[]string{},
[]string{"foo"},
},
{
[]Address{
Expand Down
Loading