From 163c52530d8399a13fb2dc9edf69b169e8a2b755 Mon Sep 17 00:00:00 2001 From: Sascha Grunert Date: Mon, 30 May 2022 11:46:03 +0200 Subject: [PATCH] Fix CI and switch to go 1.18 Signed-off-by: Sascha Grunert --- .golangci.yml | 109 +++++++++++++++++++++++++++++------ go.mod | 14 +++-- go.sum | 9 ++- hack/verify-golangci-lint.sh | 2 +- mdtoc_test.go | 14 ++++- pkg/mdtoc/mdtoc.go | 34 ++++++----- 6 files changed, 137 insertions(+), 45 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 167e985..8ac0e6f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,66 +8,96 @@ issues: - path: fake_.*\.go linters: - gocritic - - golint - dupl linters: disable-all: true enable: - asciicheck + - bidichk - bodyclose + - containedctx + - contextcheck - deadcode + - decorder - depguard - dogsled - dupl + - durationcheck - errcheck + - errchkjson + - errname + - errorlint + - execinquery + - exhaustive + - exportloopref + - forcetypeassert + - funlen + - gci + - gocognit - goconst - gocritic - gocyclo + - godot - godox - gofmt - gofumpt - goheader - goimports - - golint + - gomoddirectives - gomodguard - goprintffuncname + - gosec - gosimple - govet + - grouper + - ifshort + - importas - ineffassign - - interfacer - - maligned + - maintidx + - makezero - misspell - nakedret + - nestif + - nilerr + - nilnil + - noctx + - nolintlint + - nosprintfhostport + - paralleltest - prealloc + - predeclared + - promlinter + - revive - rowserrcheck - sqlclosecheck - staticcheck - structcheck - stylecheck + - tagliatelle + - tenv + - thelper + - tparallel - typecheck - unconvert - unparam - unused - varcheck + - wastedassign - whitespace - # - exhaustive - # - exportloopref - # - funlen - # - gci + - wrapcheck + # - cyclop + # - forbidigo + # - exhaustruct # - gochecknoglobals # - gochecknoinits - # - gocognit - # - godot # - goerr113 # - gomnd - # - gosec + # - ireturn # - lll - # - nestif # - nlreturn - # - noctx - # - nolintlint - # - scopelint + # - nonamedreturns # - testpackage + # - varnamelen # - wsl linters-settings: godox: @@ -83,31 +113,57 @@ linters-settings: # Diagnostic - appendAssign - argOrder + - badCall - badCond + - badLock + - badRegexp + - badSorting + - builtinShadowDecl - caseOrder - codegenComment - commentedOutCode + - deferInLoop - deprecatedComment - dupArg - dupBranchBody - dupCase - dupSubExpr + - dynamicFmtString + - emptyDecl + - evalOrder - exitAfterDefer + - externalErrorReassign + - filepathJoin - flagDeref - flagName + - mapKey - nilValReturn - offBy1 + - regexpPattern + - returnAfterHttpError - sloppyReassign + - sloppyTypeAssert + - sortSlice + - sprintfQuotedString + - sqlQuery + - syncMapLoadAndDelete + - truncateCmp + - unnecessaryDefer - weakCond - - octalLiteral # Performance - appendCombine - equalFold - hugeParam - indexAlloc + - preferDecodeRune + - preferFprint + - preferStringWriter + - preferWriteByte - rangeExprCopy - rangeValCopy + - sliceClear + - stringXbytes # Style - assignOp @@ -116,27 +172,43 @@ linters-settings: - commentFormatting - commentedOutImport - defaultCaseOrder + - deferUnlambda - docStub + - dupImport - elseif - emptyFallthrough - emptyStringTest + - exposedSyncMutex - hexLiteral + - httpNoBody + - ifElseChain + - ioutilDeprecated - methodExprCall + - newDeref + - octalLiteral + - preferFilepathJoin + - redundantSprint - regexpMust + - regexpSimplify + - ruleguard - singleCaseSwitch - sloppyLen - - stringXbytes + - stringConcatSimplify + - stringsCompare - switchTrue + - timeExprSimplify + - tooManyResultsChecker - typeAssertChain + - typeDefFirst - typeSwitchVar - underef - unlabelStmt - unlambda - unslice - valSwap + - whyNoLint - wrapperFunc - yodaStyleExpr - # - ifElseChain # Opinionated - builtinShadow @@ -145,6 +217,7 @@ linters-settings: - nestingReduce - paramTypeCombine - ptrToRefParam + - todoCommentWithoutDetail - typeUnparen - unnamedResult - unnecessaryBlock diff --git a/go.mod b/go.mod index a258925..c17b6d3 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,16 @@ module sigs.k8s.io/mdtoc -go 1.15 +go 1.18 require ( - github.com/BurntSushi/toml v0.3.1 // indirect - github.com/gomarkdown/markdown v0.0.0-20210514010506-3b9f47219fe7 + github.com/gomarkdown/markdown v0.0.0-20220527210340-c82b80a9daf2 github.com/mmarkdown/mmark v2.0.40+incompatible - github.com/stretchr/testify v1.7.0 + github.com/stretchr/testify v1.7.1 +) + +require ( + github.com/BurntSushi/toml v0.3.1 // indirect + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/go.sum b/go.sum index 8f0e730..a0567a2 100644 --- a/go.sum +++ b/go.sum @@ -2,16 +2,15 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gomarkdown/markdown v0.0.0-20210514010506-3b9f47219fe7 h1:oKYOfNR7Hp6XpZ4JqolL5u642Js5Z0n7psPVl+S5heo= -github.com/gomarkdown/markdown v0.0.0-20210514010506-3b9f47219fe7/go.mod h1:aii0r/K0ZnHv7G0KF7xy1v0A7s2Ljrb5byB7MO5p6TU= +github.com/gomarkdown/markdown v0.0.0-20220527210340-c82b80a9daf2 h1:VJwys0mqRBeVxECc/DyXgveOqOqI81J9sjQFZHZwJvs= +github.com/gomarkdown/markdown v0.0.0-20220527210340-c82b80a9daf2/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= github.com/mmarkdown/mmark v2.0.40+incompatible h1:vMeUeDzBK3H+/mU0oMVfMuhSXJlIA+DE/DMPQNAj5C4= github.com/mmarkdown/mmark v2.0.40+incompatible/go.mod h1:Uvmoz7tvsWpr7bMVxIpqZPyN3FbOtzDmnsJDFp7ltJs= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/dl v0.0.0-20190829154251-82a15e2f2ead/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/hack/verify-golangci-lint.sh b/hack/verify-golangci-lint.sh index 49de49e..85e9417 100755 --- a/hack/verify-golangci-lint.sh +++ b/hack/verify-golangci-lint.sh @@ -18,7 +18,7 @@ set -o errexit set -o nounset set -o pipefail -VERSION=v1.31.0 +VERSION=v1.46.2 URL_BASE=https://raw.githubusercontent.com/golangci/golangci-lint URL=$URL_BASE/$VERSION/install.sh diff --git a/mdtoc_test.go b/mdtoc_test.go index ebc6af7..f61812f 100644 --- a/mdtoc_test.go +++ b/mdtoc_test.go @@ -83,8 +83,11 @@ func testdata(subpath string) string { } func TestDryRun(t *testing.T) { + t.Parallel() for _, test := range testcases { + test := test t.Run(test.file, func(t *testing.T) { + t.Parallel() opts := utilityOptions{ Options: mdtoc.Options{ Dryrun: true, @@ -107,9 +110,12 @@ func TestDryRun(t *testing.T) { } func TestInplace(t *testing.T) { + t.Parallel() for _, test := range testcases { + test := test t.Run(test.file, func(t *testing.T) { - original, err := ioutil.ReadFile(test.file) + t.Parallel() + original, err := os.ReadFile(test.file) require.NoError(t, err, test.file) // Create a copy of the test.file to modify. @@ -137,7 +143,7 @@ func TestInplace(t *testing.T) { assert.Error(t, err, test.file) } - updated, err := ioutil.ReadFile(tmpFile.Name()) + updated, err := os.ReadFile(tmpFile.Name()) require.NoError(t, err, test.file) if test.completeTOC || !test.validTOCTags { // Invalid tags should not modify contents. @@ -150,13 +156,17 @@ func TestInplace(t *testing.T) { } func TestOutput(t *testing.T) { + t.Parallel() for _, test := range testcases { + test := test + // Ignore the invalid cases, they're only for inplace tests. if !test.validTOCTags { continue } t.Run(test.file, func(t *testing.T) { + t.Parallel() opts := utilityOptions{ Options: mdtoc.Options{ Dryrun: false, diff --git a/pkg/mdtoc/mdtoc.go b/pkg/mdtoc/mdtoc.go index 071c9d8..64f90ce 100644 --- a/pkg/mdtoc/mdtoc.go +++ b/pkg/mdtoc/mdtoc.go @@ -19,7 +19,6 @@ package mdtoc import ( "bytes" "fmt" - "io/ioutil" "math" "os" "regexp" @@ -45,7 +44,7 @@ var ( endTOCRegex = regexp.MustCompile("(?i)" + EndTOC) ) -// Options set for the toc generator +// Options set for the toc generator. type Options struct { Dryrun bool SkipPrefix bool @@ -62,7 +61,7 @@ func parse(b []byte) ast.Node { return p.Parse(b) } -// GenerateTOC parses a document and returns its TOC +// GenerateTOC parses a document and returns its TOC. func GenerateTOC(doc []byte, opts Options) (string, error) { anchors := make(anchorGen) @@ -153,7 +152,7 @@ func asText(node ast.Node) (text string) { return text } -// Renders the heading body as HTML +// Renders the heading body as HTML. func headingBody(renderer *html.Renderer, heading *ast.Heading) string { var buf bytes.Buffer for _, child := range heading.Children { @@ -183,9 +182,9 @@ var punctuation = regexp.MustCompile(`[^\w\- ]`) // WriteTOC writes the TOC generator on file with options. // Returns the generated toc, and any error. func WriteTOC(file string, opts Options) error { - raw, err := ioutil.ReadFile(file) + raw, err := os.ReadFile(file) if err != nil { - return fmt.Errorf("unable to read %s: %v", file, err) + return fmt.Errorf("unable to read %s: %w", file, err) } start, end := findTOCTags(raw) @@ -208,7 +207,7 @@ func WriteTOC(file string, opts Options) error { } toc, err := GenerateTOC(doc, opts) if err != nil { - return fmt.Errorf("failed to generate toc: %v", err) + return fmt.Errorf("failed to generate toc: %w", err) } realStart := start + len(StartTOC) @@ -231,9 +230,9 @@ func WriteTOC(file string, opts Options) error { // GetTOC generates the TOC from a file with options. // Returns the generated toc, and any error. func GetTOC(file string, opts Options) (string, error) { - doc, err := ioutil.ReadFile(file) + doc, err := os.ReadFile(file) if err != nil { - return "", fmt.Errorf("unable to read %s: %v", file, err) + return "", fmt.Errorf("unable to read %s: %w", file, err) } start, end := findTOCTags(doc) @@ -245,7 +244,7 @@ func GetTOC(file string, opts Options) (string, error) { } toc, err := GenerateTOC(doc[startPos:], opts) if err != nil { - return toc, fmt.Errorf("failed to generate toc: %v", err) + return toc, fmt.Errorf("failed to generate toc: %w", err) } return toc, err @@ -255,9 +254,9 @@ func GetTOC(file string, opts Options) (string, error) { // A temporary file is used so no changes are made to the original in the case of an error. func atomicWrite(filePath string, chunks ...string) error { tmpPath := filePath + "_tmp" - tmp, err := os.OpenFile(tmpPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600) + tmp, err := os.OpenFile(tmpPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) if err != nil { - return fmt.Errorf("unable to open tepmorary file %s: %v", tmpPath, err) + return fmt.Errorf("unable to open tepmorary file %s: %w", tmpPath, err) } // Cleanup @@ -268,12 +267,17 @@ func atomicWrite(filePath string, chunks ...string) error { for _, chunk := range chunks { if _, err := tmp.WriteString(chunk); err != nil { - return err + return fmt.Errorf("write temp string: %w", err) } } if err := tmp.Close(); err != nil { - return err + return fmt.Errorf("close temp file: %w", err) } - return os.Rename(tmp.Name(), filePath) + + if err := os.Rename(tmp.Name(), filePath); err != nil { + return fmt.Errorf("rename temp file: %w", err) + } + + return nil }