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 15 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
10 changes: 7 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
---
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
- package-ecosystem: 'github-actions'
directory: '/'
schedule:
interval: "monthly"
interval: 'weekly'
- package-ecosystem: 'gomod'
directory: '/'
schedule:
interval: 'weekly'
faddat marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 26 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,37 @@ linters-settings:
errcheck:
ignore: fmt:.*,go.uber.org/zap/zapcore:^Add.*
ignoretests: true
gci:
sections:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
- prefix(github.com/caddyserver/caddy/v2/cmd) # ensure that this is always at the top and always has a line break.
- prefix(github.com/caddyserver/caddy) # Custom section: groups all imports with the specified Prefix.
# Skip generated files.
# Default: true
skip-generated: true
# Enable custom order of sections.
# If `true`, make the section order the same as the order of `sections`.
# Default: false
custom-order: true

linters:
disable-all: true
enable:
- bodyclose
- errcheck
- gofmt
- goimports
- gci
- gofumpt
- gosec
- gosimple
- govet
- ineffassign
- nakedret
- misspell
- prealloc
- revive
- staticcheck
- thelper
- typecheck
- unconvert
- unused
Expand All @@ -38,7 +54,6 @@ linters:
# - godot
# - godox
# - goerr113
# - gofumpt
# - goheader
# - golint
# - gomnd
Expand Down Expand Up @@ -66,7 +81,7 @@ run:
# concurrency: 4 # explicitly omit this value to fully utilize available resources.
deadline: 5m
issues-exit-code: 1
tests: false
tests: true

# output configuration options
output:
Expand All @@ -75,25 +90,27 @@ output:
print-linter-name: true

issues:
max-issues-per-linter: 10000
max-same-issues: 10000
exclude-rules:
# we aren't calling unknown URL
- text: "G107" # G107: Url provided to HTTP request as taint input
- text: 'G107' # G107: Url provided to HTTP request as taint input
linters:
- gosec
# as a web server that's expected to handle any template, this is totally in the hands of the user.
- text: "G203" # G203: Use of unescaped data in HTML templates
- text: 'G203' # G203: Use of unescaped data in HTML templates
linters:
- gosec
# we're shelling out to known commands, not relying on user-defined input.
- text: "G204" # G204: Audit use of command execution
- text: 'G204' # G204: Audit use of command execution
linters:
- gosec
# the choice of weakrand is deliberate, hence the named import "weakrand"
- path: modules/caddyhttp/reverseproxy/selectionpolicies.go
text: "G404" # G404: Insecure random number source (rand)
text: 'G404' # G404: Insecure random number source (rand)
linters:
- gosec
- path: modules/caddyhttp/reverseproxy/streaming.go
text: "G404" # G404: Insecure random number source (rand)
text: 'G404' # G404: Insecure random number source (rand)
linters:
- gosec
6 changes: 3 additions & 3 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,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 Expand Up @@ -1058,7 +1058,7 @@ func handleConfigID(w http.ResponseWriter, r *http.Request) error {
return errInternalRedir
}

func handleStop(w http.ResponseWriter, r *http.Request) error {
func handleStop(_ http.ResponseWriter, r *http.Request) error {
if r.Method != http.MethodPost {
return APIError{
HTTPStatus: http.StatusMethodNotAllowed,
Expand Down Expand Up @@ -1346,7 +1346,7 @@ var (
// will get deleted before the process gracefully exits.
func PIDFile(filename string) error {
pid := []byte(strconv.Itoa(os.Getpid()) + "\n")
err := os.WriteFile(filename, pid, 0600)
err := os.WriteFile(filename, pid, 0o600)
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestUnsyncedConfigAccess(t *testing.T) {

// TestLoadConcurrent exercises Load under concurrent conditions
// and is most useful under test with `-race` enabled.
func TestLoadConcurrent(t *testing.T) {
func TestLoadConcurrent(_ *testing.T) {
var wg sync.WaitGroup

for i := 0; i < 100; i++ {
Expand Down 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)
}
}
}
15 changes: 8 additions & 7 deletions caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ import (
"sync/atomic"
"time"

"github.com/caddyserver/caddy/v2/notify"
"github.com/caddyserver/certmagic"
"github.com/google/uuid"
"go.uber.org/zap"

"github.com/caddyserver/caddy/v2/notify"
)

// Config is the top (or beginning) of the Caddy configuration structure.
Expand Down Expand Up @@ -356,13 +357,13 @@ func unsyncedDecodeAndRun(cfgJSON []byte, allowPersist bool) error {
newCfg.Admin.Config.Persist == nil ||
*newCfg.Admin.Config.Persist) {
dir := filepath.Dir(ConfigAutosavePath)
err := os.MkdirAll(dir, 0700)
err := os.MkdirAll(dir, 0o700)
if err != nil {
Log().Error("unable to create folder for config autosave",
zap.String("dir", dir),
zap.Error(err))
} else {
err := os.WriteFile(ConfigAutosavePath, cfgJSON, 0600)
err := os.WriteFile(ConfigAutosavePath, cfgJSON, 0o600)
if err == nil {
Log().Info("autosaved config (load with --resume flag)", zap.String("file", ConfigAutosavePath))
} else {
Expand Down Expand Up @@ -831,7 +832,7 @@ func InstanceID() (uuid.UUID, error) {
if err != nil {
return uuid, err
}
err = os.WriteFile(uuidFilePath, []byte(uuid.String()), 0600)
err = os.WriteFile(uuidFilePath, []byte(uuid.String()), 0o600)
return uuid, err
} else if err != nil {
return [16]byte{}, err
Expand Down Expand Up @@ -883,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 @@ -967,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
6 changes: 3 additions & 3 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
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))
}
}
}
3 changes: 2 additions & 1 deletion caddyconfig/caddyfile/importargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
"strconv"
"strings"

"github.com/caddyserver/caddy/v2"
"go.uber.org/zap"

"github.com/caddyserver/caddy/v2"
)

// parseVariadic determines if the token is a variadic placeholder,
Expand Down
3 changes: 3 additions & 0 deletions caddyconfig/caddyfile/importgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (i *importGraph) addNode(name string) {
}
i.nodes[name] = true
}

func (i *importGraph) addNodes(names []string) {
for _, name := range names {
i.addNode(name)
Expand All @@ -43,6 +44,7 @@ func (i *importGraph) addNodes(names []string) {
func (i *importGraph) removeNode(name string) {
delete(i.nodes, name)
}

func (i *importGraph) removeNodes(names []string) {
for _, name := range names {
i.removeNode(name)
Expand Down Expand Up @@ -73,6 +75,7 @@ func (i *importGraph) addEdge(from, to string) error {
i.edges[from] = append(i.edges[from], to)
return nil
}

func (i *importGraph) addEdges(from string, tos []string) error {
for _, to := range tos {
err := i.addEdge(from, to)
Expand Down
1 change: 1 addition & 0 deletions caddyconfig/caddyfile/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,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
12 changes: 6 additions & 6 deletions caddyconfig/caddyfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
"path/filepath"
"strings"

"github.com/caddyserver/caddy/v2"
"go.uber.org/zap"

"github.com/caddyserver/caddy/v2"
)

// Parse parses the input just enough to group tokens, in
Expand Down Expand Up @@ -445,7 +446,7 @@ func (p *parser) doImport(nesting int) error {

var (
maybeSnippet bool
maybeSnippetId bool
maybeSnippetID bool
index int
)

Expand All @@ -471,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 Expand Up @@ -565,7 +566,6 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) {
// are loaded into the current server block for later use
// by directive setup functions.
func (p *parser) directive() error {

// a segment is a list of tokens associated with this directive
var segment Segment

Expand Down
Loading