Skip to content

Commit b440d85

Browse files
committed
chore: update golangci-lint configuration and improve test formatting
This commit modifies the .golangci.yml file to update the version and adjust the enabled linters, while also refining the exclusion rules for test files. Additionally, it enhances the formatting of test files by removing unnecessary blank lines and ensuring consistent comment formatting across various test cases. The GitHub Actions workflow is updated to use the latest Go version and golangci-lint action, improving the CI process.
1 parent c97ad88 commit b440d85

12 files changed

Lines changed: 132 additions & 104 deletions

.github/workflows/tests.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@ jobs:
1010
test:
1111
runs-on: ubuntu-latest
1212
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
1316
- name: Set up Go
14-
uses: actions/setup-go@v2
17+
uses: actions/setup-go@v5
1518
with:
16-
go-version: ^1.20
19+
go-version: ^1.21
20+
cache-dependency-path: go.sum
1721
id: go
1822

19-
- name: Checkout code
20-
uses: actions/checkout@v2
21-
2223
- name: Set up dependencies
2324
run: go mod download
2425

2526
- name: Run golangci-lint
26-
uses: golangci/golangci-lint-action@v2
27+
uses: golangci/golangci-lint-action@v9
2728
with:
28-
version: v1.52
29+
version: v2.6.1
2930

3031
- name: Run tests
31-
run: go test -v $(go list ./... | grep -v vendor)
32+
run: go test -race -v ./...

.golangci.yml

Lines changed: 73 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
run:
2-
skip-dirs:
3-
- var
1+
version: "2"
42
linters:
53
enable:
64
- asciicheck
@@ -11,7 +9,6 @@ linters:
119
- dogsled
1210
- dupl
1311
- durationcheck
14-
- exportloopref
1512
- forbidigo
1613
- funlen
1714
- gocognit
@@ -20,68 +17,98 @@ linters:
2017
- gocyclo
2118
- godot
2219
- godox
23-
- gofmt
24-
- gofumpt
25-
- goimports
2620
- gomodguard
2721
- goprintffuncname
2822
- gosec
29-
- gosimple
30-
- govet
3123
- importas
32-
- ineffassign
3324
- lll
3425
- makezero
3526
- misspell
3627
- nakedret
3728
- nestif
3829
- nilerr
3930
- noctx
40-
- noctx
4131
- nolintlint
4232
- prealloc
4333
- predeclared
4434
- promlinter
4535
- revive
46-
- stylecheck
47-
- tenv
36+
- staticcheck
4837
- testpackage
4938
- thelper
5039
- tparallel
51-
- typecheck
5240
- unconvert
5341
- unparam
54-
- unused
5542
- whitespace
56-
57-
issues:
58-
exclude-rules:
59-
# Exclude some linters from running on tests files.
60-
- path: _test\.go
61-
linters:
62-
- contextcheck
63-
- cyclop
64-
- dupl
65-
- errcheck
66-
- exportloopref
67-
- funlen
68-
- gochecknoglobals
69-
- goconst
70-
- gocritic
71-
- gocyclo
72-
- gosec
73-
- lll
74-
- path: errors\.go
75-
linters:
76-
- errcheck
77-
- path: stack\.go
78-
linters:
79-
- errcheck
80-
- goconst
81-
- gocritic
82-
83-
linters-settings:
84-
revive:
43+
settings:
44+
depguard:
45+
rules:
46+
main:
47+
files:
48+
- $all
49+
- '!$test'
50+
- '!**/test/**/*'
51+
allow:
52+
- $gostd
53+
- github.com
54+
test:
55+
files:
56+
- $test
57+
allow:
58+
- $gostd
59+
- github.com
60+
revive:
61+
rules:
62+
- name: var-naming
63+
disabled: true
64+
exclusions:
65+
generated: lax
66+
presets:
67+
- comments
68+
- common-false-positives
69+
- legacy
70+
- std-error-handling
8571
rules:
86-
- name: var-naming
87-
disabled: true
72+
- linters:
73+
- contextcheck
74+
- cyclop
75+
- dupl
76+
- errcheck
77+
- exportloopref
78+
- funlen
79+
- gochecknoglobals
80+
- gocognit
81+
- goconst
82+
- gocritic
83+
- gocyclo
84+
- gosec
85+
- lll
86+
- nestif
87+
path: _test\.go
88+
- linters:
89+
- errcheck
90+
path: errors\.go
91+
- linters:
92+
- nestif
93+
path: errorstest
94+
- linters:
95+
- errcheck
96+
- goconst
97+
- gocritic
98+
path: stack\.go
99+
paths:
100+
- var
101+
- third_party$
102+
- builtin$
103+
- examples$
104+
formatters:
105+
enable:
106+
- gofmt
107+
- gofumpt
108+
- goimports
109+
exclusions:
110+
generated: lax
111+
paths:
112+
- third_party$
113+
- builtin$
114+
- examples$

assertions_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ func assertFormatRegexp(t *testing.T, arg interface{}, format, want string) {
3434
t.Helper()
3535

3636
got := fmt.Sprintf(format, arg)
37-
gotLines := strings.SplitN(got, "\n", -1)
38-
wantLines := strings.SplitN(want, "\n", -1)
37+
gotLines := strings.Split(got, "\n")
38+
wantLines := strings.Split(want, "\n")
3939

4040
if len(wantLines) > len(gotLines) {
4141
t.Errorf("wantLines(%d) > gotLines(%d):\n got: %q\nwant: %q", len(wantLines), len(gotLines), got, want)

errors.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,16 @@ func (e *stacked) Format(s fmt.State, verb rune) {
250250
switch verb {
251251
case 'v':
252252
if s.Flag('+') {
253-
io.WriteString(s, e.wrapped.Error())
254-
writeAttrs(s, e.wrapped.Attrs(), "")
253+
io.WriteString(s, e.Error())
254+
writeAttrs(s, e.Attrs(), "")
255255
e.stack.Format(s, verb)
256256
return
257257
}
258258
fallthrough
259259
case 's':
260-
io.WriteString(s, e.wrapped.Error())
260+
io.WriteString(s, e.Error())
261261
case 'q':
262-
fmt.Fprintf(s, "%q", e.wrapped.Error())
262+
fmt.Fprintf(s, "%q", e.Error())
263263
}
264264
}
265265

@@ -294,7 +294,7 @@ func splitArgsAndOptions(argsAndOptions []interface{}) ([]interface{}, []Option)
294294
return args, options
295295
}
296296

297-
// isOptionOrAttr checks if a value is either an Option or slog.Attr
297+
// isOptionOrAttr checks if a value is either an Option or slog.Attr.
298298
func isOptionOrAttr(v interface{}) bool {
299299
if _, ok := v.(Option); ok {
300300
return true
@@ -305,7 +305,7 @@ func isOptionOrAttr(v interface{}) bool {
305305
return false
306306
}
307307

308-
// convertToOptions converts a slice of Option and/or slog.Attr to []Option
308+
// convertToOptions converts a slice of Option and/or slog.Attr to []Option.
309309
func convertToOptions(items []interface{}) []Option {
310310
options := make([]Option, 0, len(items))
311311
for _, item := range items {
@@ -396,7 +396,7 @@ func writeAttrs(w io.Writer, attrs []slog.Attr, prefix string) {
396396
}
397397
}
398398

399-
// writeAttr writes a single attribute value to an io.Writer
399+
// writeAttr writes a single attribute value to an io.Writer.
400400
func writeAttr(w io.Writer, key string, value slog.Value) {
401401
io.WriteString(w, "\n"+key+": ")
402402

@@ -433,7 +433,7 @@ func writeAttr(w io.Writer, key string, value slog.Value) {
433433
}
434434
case json.RawMessage:
435435
// Format JSON as string
436-
io.WriteString(w, string(typed))
436+
w.Write(typed)
437437
default:
438438
// Default formatting
439439
io.WriteString(w, fmt.Sprintf("%v", v))

errors_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func TestFields(t *testing.T) {
316316
if len(attrs) == 0 {
317317
t.Fatalf("expected %#v to have attributes", test.err)
318318
}
319-
319+
320320
// Find the "key" attribute
321321
var found bool
322322
var value interface{}
@@ -327,11 +327,11 @@ func TestFields(t *testing.T) {
327327
break
328328
}
329329
}
330-
330+
331331
if !found {
332332
t.Fatalf("expected %#v to have attribute with key 'key'", test.err)
333333
}
334-
334+
335335
if !reflect.DeepEqual(value, test.expected) {
336336
t.Errorf("want value %v (%T), got %v (%T)", test.expected, test.expected, value, value)
337337
}

errorstest/mock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (m *Logger) AssertField(t *testing.T, key string, expected interface{}) {
5151
}
5252
}
5353

54-
// findAttr recursively searches for an attribute by key, handling groups with dot notation
54+
// findAttr recursively searches for an attribute by key, handling groups with dot notation.
5555
func (m *Logger) findAttr(key string, attrs []slog.Attr, prefix string) (interface{}, bool) {
5656
for _, attr := range attrs {
5757
if attr.Value.Kind() == slog.KindGroup {
@@ -157,7 +157,7 @@ func (m *Logger) AssertStackTrace(t *testing.T, want StackTrace) {
157157
}
158158
}
159159

160-
// attrsEqual compares two slog.Attr values for equality
160+
// attrsEqual compares two slog.Attr values for equality.
161161
func attrsEqual(a, b slog.Attr) bool {
162162
if a.Key != b.Key {
163163
return false

example_log_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func ExampleLog_typicalErrorHandling() {
125125

126126
// Get attributes from error
127127
attrs := errors.Attrs(notFoundError)
128-
128+
129129
// Get stack trace
130130
var stackTrace errors.StackTrace
131131
for e := notFoundError; e != nil; e = errors.Unwrap(e) {
@@ -134,7 +134,7 @@ func ExampleLog_typicalErrorHandling() {
134134
break
135135
}
136136
}
137-
137+
138138
fmt.Println(`log repository error, attrs count:`, len(attrs))
139139
fmt.Printf(
140140
"log repository error, first line of stack trace: %s %s:%d\n",
@@ -165,13 +165,13 @@ func ExampleLog_typicalErrorHandling() {
165165

166166
// Get attributes from error
167167
attrs := errors.Attrs(sqlError)
168-
168+
169169
// Create a map for display
170170
fields := make(map[string]interface{})
171171
for _, attr := range attrs {
172172
fields[attr.Key] = attr.Value.Any()
173173
}
174-
174+
175175
// Get stack trace
176176
var stackTrace errors.StackTrace
177177
for e := sqlError; e != nil; e = errors.Unwrap(e) {
@@ -180,7 +180,7 @@ func ExampleLog_typicalErrorHandling() {
180180
break
181181
}
182182
}
183-
183+
184184
fmt.Println(`log repository error, fields:`, fields)
185185
fmt.Printf(
186186
"log repository error, first line of stack trace: %s %s:%d\n",

example_loggable_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ func ExampleLog_loggableError() {
4040

4141
// Get attributes from error
4242
attrs := errors.Attrs(err)
43-
43+
4444
// Create a map for display
4545
fields := make(map[string]interface{})
4646
for _, attr := range attrs {
4747
fields[attr.Key] = attr.Value.Any()
4848
}
49-
49+
5050
// Get stack trace
5151
var stackTrace errors.StackTrace
5252
for e := err; e != nil; e = errors.Unwrap(e) {
@@ -55,7 +55,7 @@ func ExampleLog_loggableError() {
5555
break
5656
}
5757
}
58-
58+
5959
fmt.Println(`error message:`, err.Error())
6060
fmt.Println(`error fields:`, fields)
6161
fmt.Printf(

0 commit comments

Comments
 (0)