Skip to content

Commit 7b5cb02

Browse files
committed
fix: add changes for linter
1 parent f0d33ae commit 7b5cb02

File tree

11 files changed

+41
-47
lines changed

11 files changed

+41
-47
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
linters:
55
enable-all: true
66
disable:
7+
- cyclop
8+
- depguard
9+
- varnamelen
710
- goimports # handled by `make format`
811
- gofmt # handled by `make format`
912
- wsl # hyper specific to a the creator and not configurable enough to be useful - https://github.com/bombsimon/wsl

analyze.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ const ngxAnyConf = ngxMainConf | ngxEventConf | ngxMailMainConf | ngxMailSrvConf
6161
ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPUpsConf
6262

6363
// map for getting bitmasks from certain context tuples
64-
// nolint:gochecknoglobals
64+
//
65+
//nolint:gochecknoglobals
6566
var contexts = map[string]uint{
6667
blockCtx{}.key(): ngxMainConf,
6768
blockCtx{"events"}.key(): ngxEventConf,
@@ -88,7 +89,7 @@ func enterBlockCtx(stmt *Directive, ctx blockCtx) blockCtx {
8889
return append(ctx, stmt.Directive)
8990
}
9091

91-
// nolint:gocyclo,funlen,gocognit
92+
//nolint:gocyclo,funlen,gocognit
9293
func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options *ParseOptions) error {
9394
masks, knownDirective := directives[stmt.Directive]
9495
currCtx, knownContext := contexts[ctx.key()]
@@ -159,7 +160,7 @@ func analyze(fname string, stmt *Directive, term string, ctx blockCtx, options *
159160
}
160161

161162
// use mask to check the directive's arguments
162-
// nolint:gocritic
163+
//nolint:gocritic
163164
if ((mask>>len(stmt.Args)&1) != 0 && len(stmt.Args) <= 7) || // NOARGS to TAKE7
164165
((mask&ngxConfFlag) != 0 && len(stmt.Args) == 1 && validFlag(stmt.Args[0])) ||
165166
((mask & ngxConfAny) != 0) ||

analyze_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func TestAnalyze_auth_jwt_require(t *testing.T) {
172172
}
173173
}
174174

175-
//nolint:exhaustruct
175+
//nolint:exhaustruct,funlen
176176
func TestAnalyze_njs(t *testing.T) {
177177
t.Parallel()
178178
testcases := map[string]struct {

build.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type BuildOptions struct {
2525

2626
const MaxIndent = 100
2727

28-
// nolint:gochecknoglobals
28+
//nolint:gochecknoglobals
2929
var (
3030
marginSpaces = strings.Repeat(" ", MaxIndent)
3131
marginTabs = strings.Repeat("\t", MaxIndent)
@@ -108,7 +108,6 @@ func Build(w io.Writer, config Config, options *BuildOptions) error {
108108
return err
109109
}
110110

111-
//nolint:gocognit
112111
func buildBlock(sb io.StringWriter, parent *Directive, block Directives, depth int, lastLine int, options *BuildOptions) {
113112
for i, stmt := range block {
114113
// if the this statement is a comment on the same line as the preview, do not emit EOL for this stmt
@@ -185,7 +184,7 @@ func Enquote(arg string) string {
185184
return strings.ReplaceAll(repr(arg), `\\`, `\`)
186185
}
187186

188-
// nolint:gocyclo,gocognit
187+
//nolint:gocyclo,gocognit
189188
func needsQuote(s string) bool {
190189
if s == "" {
191190
return true
@@ -201,7 +200,7 @@ func needsQuote(s string) bool {
201200
}
202201

203202
// get first rune
204-
char, off := utf8.DecodeRune([]byte(chars))
203+
char, off := utf8.DecodeRuneInString(chars)
205204

206205
// arguments can't start with variable expansion syntax
207206
if unicode.IsSpace(char) || strings.ContainsRune("{};\"'", char) || strings.HasPrefix(chars, "${") {
@@ -211,7 +210,7 @@ func needsQuote(s string) bool {
211210
chars = chars[off:]
212211

213212
expanding := false
214-
var prev rune = 0
213+
var prev rune
215214
for _, c := range chars {
216215
char = c
217216

build_test.go

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package crossplane
1010
import (
1111
"bytes"
1212
"encoding/json"
13-
"io/ioutil"
1413
"os"
1514
"path/filepath"
1615
"strings"
@@ -36,7 +35,7 @@ type compareFixture struct {
3635
options ParseOptions
3736
}
3837

39-
// nolint:gochecknoglobals
38+
//nolint:gochecknoglobals
4039
var buildFixtures = []buildFixture{
4140
{
4241
name: "nested-and-multiple-args",
@@ -279,7 +278,7 @@ func TestBuild(t *testing.T) {
279278
}
280279
}
281280

282-
// nolint:gochecknoglobals
281+
//nolint:gochecknoglobals
283282
var buildFilesFixtures = []buildFilesFixture{
284283
{
285284
name: "with-missing-status-and-errors",
@@ -332,17 +331,14 @@ func TestBuildFiles(t *testing.T) {
332331
t.Run(fixture.name, func(t *testing.T) {
333332
t.Parallel()
334333
fixture := fixture
335-
tmpdir, err := ioutil.TempDir("", "TestBuildFiles-")
336-
if err != nil {
337-
t.Fatal(err)
338-
}
334+
tmpdir := t.TempDir()
339335
defer os.RemoveAll(tmpdir)
340336

341-
if err = BuildFiles(fixture.payload, tmpdir, &fixture.options); err != nil {
337+
if err := BuildFiles(fixture.payload, tmpdir, &fixture.options); err != nil {
342338
t.Fatal(err)
343339
}
344340

345-
content, err := ioutil.ReadFile(filepath.Join(tmpdir, "nginx.conf"))
341+
content, err := os.ReadFile(filepath.Join(tmpdir, "nginx.conf"))
346342
if err != nil {
347343
t.Fatal(err)
348344
}
@@ -355,7 +351,7 @@ func TestBuildFiles(t *testing.T) {
355351
}
356352
}
357353

358-
// nolint:gochecknoglobals
354+
//nolint:gochecknoglobals
359355
var compareFixtures = []compareFixture{
360356
{"simple", ParseOptions{}},
361357
{"messy", ParseOptions{}},
@@ -367,17 +363,14 @@ var compareFixtures = []compareFixture{
367363
{"empty-config", ParseOptions{}},
368364
}
369365

370-
//nolint:gocognit,funlen
366+
//nolint:gocognit
371367
func TestCompareParsedAndBuilt(t *testing.T) {
372368
t.Parallel()
373369
for _, fixture := range compareFixtures {
374370
fixture := fixture
375371
t.Run(fixture.name, func(t *testing.T) {
376372
t.Parallel()
377-
tmpdir, err2 := ioutil.TempDir("", "TestCompareParsedAndBuilt-")
378-
if err2 != nil {
379-
t.Fatal(err2)
380-
}
373+
tmpdir := t.TempDir()
381374
defer os.RemoveAll(tmpdir)
382375

383376
origPayload, err2 := Parse(getTestConfigPath(fixture.name, "nginx.conf"), &fixture.options)
@@ -391,15 +384,15 @@ func TestCompareParsedAndBuilt(t *testing.T) {
391384
}
392385
build1File := filepath.Join(tmpdir, "build1.conf")
393386
build1Config := build1Buffer.Bytes()
394-
if err := ioutil.WriteFile(build1File, build1Config, os.ModePerm); err != nil {
387+
if err := os.WriteFile(build1File, build1Config, os.ModePerm); err != nil {
395388
t.Fatal(err)
396389
}
397390
build1Payload, err2 := Parse(build1File, &fixture.options)
398391
if err2 != nil {
399392
t.Fatal(err2)
400393
}
401394

402-
if !equalPayloads(*origPayload, *build1Payload) {
395+
if !equalPayloads(t, *origPayload, *build1Payload) {
403396
b1, _ := json.Marshal(origPayload)
404397
b2, _ := json.Marshal(build1Payload)
405398
if string(b1) != string(b2) {
@@ -413,15 +406,15 @@ func TestCompareParsedAndBuilt(t *testing.T) {
413406
}
414407
build2File := filepath.Join(tmpdir, "build2.conf")
415408
build2Config := build2Buffer.Bytes()
416-
if err := ioutil.WriteFile(build2File, build2Config, os.ModePerm); err != nil {
409+
if err := os.WriteFile(build2File, build2Config, os.ModePerm); err != nil {
417410
t.Fatal(err)
418411
}
419412
build2Payload, err2 := Parse(build2File, &fixture.options)
420413
if err2 != nil {
421414
t.Fatal(err2)
422415
}
423416

424-
if !equalPayloads(*build1Payload, *build2Payload) {
417+
if !equalPayloads(t, *build1Payload, *build2Payload) {
425418
b1, _ := json.Marshal(build1Payload)
426419
b2, _ := json.Marshal(build2Payload)
427420
if string(b1) != string(b2) {
@@ -432,13 +425,13 @@ func TestCompareParsedAndBuilt(t *testing.T) {
432425
}
433426
}
434427

435-
func equalPayloads(p1, p2 Payload) bool {
428+
func equalPayloads(t *testing.T, p1, p2 Payload) bool {
436429
return p1.Status == p2.Status &&
437-
equalPayloadErrors(p1.Errors, p2.Errors) &&
430+
equalPayloadErrors(t, p1.Errors, p2.Errors) &&
438431
equalPayloadConfigs(p1.Config, p2.Config)
439432
}
440433

441-
func equalPayloadErrors(e1, e2 []PayloadError) bool {
434+
func equalPayloadErrors(t *testing.T, e1, e2 []PayloadError) bool {
442435
if len(e1) != len(e2) {
443436
return false
444437
}
@@ -447,8 +440,8 @@ func equalPayloadErrors(e1, e2 []PayloadError) bool {
447440
(e1[i].Error != nil && e2[i].Error != nil && e1[i].Error.Error() != e2[i].Error.Error()) ||
448441
(e1[i].Line == nil) != (e2[i].Line == nil) ||
449442
(e1[i].Line != nil && *e1[i].Line != *e2[i].Line) {
450-
println(e1[i].Error.Error())
451-
println(e2[i].Error.Error())
443+
t.Log(e1[i].Error.Error())
444+
t.Log(e2[i].Error.Error())
452445
return false
453446
}
454447
}

lex.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ const (
3333

3434
const TokenChanCap = 2048
3535

36-
// nolint:gochecknoglobals
36+
//nolint:gochecknoglobals
3737
var lexerFile = "lexer" // pseudo file name for use by parse errors
3838

39-
// nolint:gochecknoglobals
39+
//nolint:gochecknoglobals
4040
var tokChanCap = TokenChanCap // capacity of lexer token channel
4141

4242
// note: this is only used during tests, should not be changed
@@ -49,7 +49,7 @@ func Lex(reader io.Reader) chan NgxToken {
4949
return tc
5050
}
5151

52-
// nolint:gocyclo,funlen,gocognit
52+
//nolint:gocyclo,funlen,gocognit
5353
func tokenize(reader io.Reader, tokenCh chan NgxToken) {
5454
token := strings.Builder{}
5555
tokenLine := 1

lex_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type lexFixture struct {
2323
tokens []tokenLine
2424
}
2525

26-
// nolint:gochecknoglobals
26+
//nolint:gochecknoglobals
2727
var lexFixtures = []lexFixture{
2828
{"simple", []tokenLine{
2929
{"events", 1},

parse.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"strings"
1919
)
2020

21-
// nolint:gochecknoglobals
21+
//nolint:gochecknoglobals
2222
var (
2323
hasMagic = regexp.MustCompile(`[*?[]`)
2424
osOpen = func(path string) (io.Reader, error) { return os.Open(path) }
@@ -189,7 +189,8 @@ func (p *parser) openFile(path string) (io.Reader, error) {
189189
}
190190

191191
// parse Recursively parses directives from an nginx config context.
192-
// nolint:gocyclo,funlen,gocognit
192+
//
193+
//nolint:gocyclo,funlen,gocognit,maintidx,nonamedreturns
193194
func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, consume bool) (parsed Directives, err error) {
194195
var tokenOk bool
195196
// parse recursively by pulling from a flat stream of tokens

parse_bench_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"compress/bzip2"
1313
"flag"
1414
"io"
15-
"io/ioutil"
1615
"os"
1716
"path/filepath"
1817
"sync"
@@ -21,7 +20,7 @@ import (
2120
"github.com/stretchr/testify/require"
2221
)
2322

24-
// nolint:gochecknoglobals
23+
//nolint:gochecknoglobals
2524
var (
2625
runBenchLocally = flag.Bool("local-parse-bench", false, "perform local parse benchmark test")
2726

@@ -45,10 +44,7 @@ func getLargeConfig(b *testing.B) (string, func()) {
4544
defer f.Close()
4645

4746
// Open output file
48-
tmpdir, e := ioutil.TempDir("", "alog")
49-
if e != nil {
50-
b.Skip("cannot create output dir")
51-
}
47+
tmpdir := b.TempDir()
5248
remove := func() {
5349
b.Logf("removing temporary dir %s", tmpdir)
5450
_ = os.RemoveAll(tmpdir)

parse_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ func TestParse(t *testing.T) {
12901290
if err != nil {
12911291
t.Fatal(err)
12921292
}
1293-
if !equalPayloads(*payload, fixture.expected) {
1293+
if !equalPayloads(t, *payload, fixture.expected) {
12941294
b1, _ := json.Marshal(fixture.expected)
12951295
b2, _ := json.Marshal(payload)
12961296
t.Fatalf("expected: %s\nbut got: %s", b1, b2)
@@ -1299,6 +1299,7 @@ func TestParse(t *testing.T) {
12991299
}
13001300
}
13011301

1302+
//nolint:errchkjson
13021303
func TestParseVarArgs(t *testing.T) {
13031304
t.Parallel()
13041305
tcs := map[string]struct {

0 commit comments

Comments
 (0)