Skip to content

Commit a4dc80b

Browse files
authored
Merge pull request #63 from nginxinc/main
fix: make linter happy
2 parents f0d33ae + 170711f commit a4dc80b

File tree

11 files changed

+41
-49
lines changed

11 files changed

+41
-49
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
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 & 26 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,13 @@ 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-
}
339-
defer os.RemoveAll(tmpdir)
334+
tmpdir := t.TempDir()
340335

341-
if err = BuildFiles(fixture.payload, tmpdir, &fixture.options); err != nil {
336+
if err := BuildFiles(fixture.payload, tmpdir, &fixture.options); err != nil {
342337
t.Fatal(err)
343338
}
344339

345-
content, err := ioutil.ReadFile(filepath.Join(tmpdir, "nginx.conf"))
340+
content, err := os.ReadFile(filepath.Join(tmpdir, "nginx.conf"))
346341
if err != nil {
347342
t.Fatal(err)
348343
}
@@ -355,7 +350,7 @@ func TestBuildFiles(t *testing.T) {
355350
}
356351
}
357352

358-
// nolint:gochecknoglobals
353+
//nolint:gochecknoglobals
359354
var compareFixtures = []compareFixture{
360355
{"simple", ParseOptions{}},
361356
{"messy", ParseOptions{}},
@@ -367,18 +362,14 @@ var compareFixtures = []compareFixture{
367362
{"empty-config", ParseOptions{}},
368363
}
369364

370-
//nolint:gocognit,funlen
365+
//nolint:gocognit
371366
func TestCompareParsedAndBuilt(t *testing.T) {
372367
t.Parallel()
373368
for _, fixture := range compareFixtures {
374369
fixture := fixture
375370
t.Run(fixture.name, func(t *testing.T) {
376371
t.Parallel()
377-
tmpdir, err2 := ioutil.TempDir("", "TestCompareParsedAndBuilt-")
378-
if err2 != nil {
379-
t.Fatal(err2)
380-
}
381-
defer os.RemoveAll(tmpdir)
372+
tmpdir := t.TempDir()
382373

383374
origPayload, err2 := Parse(getTestConfigPath(fixture.name, "nginx.conf"), &fixture.options)
384375
if err2 != nil {
@@ -391,15 +382,15 @@ func TestCompareParsedAndBuilt(t *testing.T) {
391382
}
392383
build1File := filepath.Join(tmpdir, "build1.conf")
393384
build1Config := build1Buffer.Bytes()
394-
if err := ioutil.WriteFile(build1File, build1Config, os.ModePerm); err != nil {
385+
if err := os.WriteFile(build1File, build1Config, os.ModePerm); err != nil {
395386
t.Fatal(err)
396387
}
397388
build1Payload, err2 := Parse(build1File, &fixture.options)
398389
if err2 != nil {
399390
t.Fatal(err2)
400391
}
401392

402-
if !equalPayloads(*origPayload, *build1Payload) {
393+
if !equalPayloads(t, *origPayload, *build1Payload) {
403394
b1, _ := json.Marshal(origPayload)
404395
b2, _ := json.Marshal(build1Payload)
405396
if string(b1) != string(b2) {
@@ -413,15 +404,15 @@ func TestCompareParsedAndBuilt(t *testing.T) {
413404
}
414405
build2File := filepath.Join(tmpdir, "build2.conf")
415406
build2Config := build2Buffer.Bytes()
416-
if err := ioutil.WriteFile(build2File, build2Config, os.ModePerm); err != nil {
407+
if err := os.WriteFile(build2File, build2Config, os.ModePerm); err != nil {
417408
t.Fatal(err)
418409
}
419410
build2Payload, err2 := Parse(build2File, &fixture.options)
420411
if err2 != nil {
421412
t.Fatal(err2)
422413
}
423414

424-
if !equalPayloads(*build1Payload, *build2Payload) {
415+
if !equalPayloads(t, *build1Payload, *build2Payload) {
425416
b1, _ := json.Marshal(build1Payload)
426417
b2, _ := json.Marshal(build2Payload)
427418
if string(b1) != string(b2) {
@@ -432,13 +423,13 @@ func TestCompareParsedAndBuilt(t *testing.T) {
432423
}
433424
}
434425

435-
func equalPayloads(p1, p2 Payload) bool {
426+
func equalPayloads(t *testing.T, p1, p2 Payload) bool {
436427
return p1.Status == p2.Status &&
437-
equalPayloadErrors(p1.Errors, p2.Errors) &&
428+
equalPayloadErrors(t, p1.Errors, p2.Errors) &&
438429
equalPayloadConfigs(p1.Config, p2.Config)
439430
}
440431

441-
func equalPayloadErrors(e1, e2 []PayloadError) bool {
432+
func equalPayloadErrors(t *testing.T, e1, e2 []PayloadError) bool {
442433
if len(e1) != len(e2) {
443434
return false
444435
}
@@ -447,8 +438,8 @@ func equalPayloadErrors(e1, e2 []PayloadError) bool {
447438
(e1[i].Error != nil && e2[i].Error != nil && e1[i].Error.Error() != e2[i].Error.Error()) ||
448439
(e1[i].Line == nil) != (e2[i].Line == nil) ||
449440
(e1[i].Line != nil && *e1[i].Line != *e2[i].Line) {
450-
println(e1[i].Error.Error())
451-
println(e2[i].Error.Error())
441+
t.Log(e1[i].Error.Error())
442+
t.Log(e2[i].Error.Error())
452443
return false
453444
}
454445
}

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)