Skip to content

Commit

Permalink
Fix golangci lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hoshsadiq committed Dec 31, 2022
1 parent 675c58a commit 1a4ec2c
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 147 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ jobs:
- ubuntu
- macOS
go:
- 15
- 16
- 17
- 18
- 19
name: '${{ matrix.platform }} | 1.${{ matrix.go }}.x'
runs-on: ${{ matrix.platform }}-latest
steps:
Expand Down
12 changes: 3 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ linters:
disable-all: true
enable:
#- bodyclose
- deadcode
- unused
#- depguard
#- dogsled
#- dupl
Expand All @@ -14,35 +14,29 @@ linters:
#- funlen
- gas
#- gochecknoinits
- goconst
#- goconst
#- gocritic
#- gocyclo
#- gofmt
- goimports
- golint
#- gomnd
#- goprintffuncname
#- gosec
#- gosimple
- govet
- ineffassign
- interfacer
#- lll
- maligned
- megacheck
#- misspell
#- nakedret
#- noctx
#- nolintlint
#- rowserrcheck
#- scopelint
#- staticcheck
- structcheck
- staticcheck
#- stylecheck
#- typecheck
- unconvert
#- unparam
#- unused
- varcheck
#- whitespace
fast: false
13 changes: 10 additions & 3 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -1817,12 +1816,12 @@ func TestCommandPrintRedirection(t *testing.T) {
t.Error(err)
}

gotErrBytes, err := ioutil.ReadAll(errBuff)
gotErrBytes, err := io.ReadAll(errBuff)
if err != nil {
t.Error(err)
}

gotOutBytes, err := ioutil.ReadAll(outBuff)
gotOutBytes, err := io.ReadAll(outBuff)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -2233,6 +2232,8 @@ func TestSetContext(t *testing.T) {
return nil
},
}

//nolint:staticcheck // not necessary to create separate type for this
ctx := context.WithValue(context.Background(), key, val)
root.SetContext(ctx)
err := root.Execute()
Expand All @@ -2246,6 +2247,8 @@ func TestSetContextPreRun(t *testing.T) {
root := &zulu.Command{
Use: "root",
PreRunE: func(cmd *zulu.Command, args []string) error {

//nolint:staticcheck // not necessary to create separate type for this
ctx := context.WithValue(cmd.Context(), key, val)
cmd.SetContext(ctx)
return nil
Expand Down Expand Up @@ -2281,6 +2284,8 @@ func TestSetContextPreRunOverwrite(t *testing.T) {
return nil
},
}

//nolint:staticcheck // not necessary to create separate type for this
ctx := context.WithValue(context.Background(), key, val)
root.SetContext(ctx)
err := root.ExecuteContext(context.Background())
Expand All @@ -2294,6 +2299,8 @@ func TestSetContextPersistentPreRun(t *testing.T) {
root := &zulu.Command{
Use: "root",
PersistentPreRunE: func(cmd *zulu.Command, args []string) error {

//nolint:staticcheck // not necessary to create separate type for this
ctx := context.WithValue(cmd.Context(), key, val)
cmd.SetContext(ctx)
return nil
Expand Down
47 changes: 23 additions & 24 deletions doc/adoc_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package doc_test

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -19,14 +18,14 @@ func TestGenAsciidoc(t *testing.T) {
}
output := buf.String()

checkStringContains(t, output, echoCmd.Long)
checkStringContains(t, output, echoCmd.Example)
checkStringContains(t, output, "boolone")
checkStringContains(t, output, "rootflag")
checkStringContains(t, output, rootCmd.Short)
checkStringContains(t, output, echoSubCmd.Short)
checkStringOmits(t, output, deprecatedCmd.Short)
checkStringContains(t, output, "Options inherited from parent commands")
assertContains(t, output, echoCmd.Long)
assertContains(t, output, echoCmd.Example)
assertContains(t, output, "boolone")
assertContains(t, output, "rootflag")
assertContains(t, output, rootCmd.Short)
assertContains(t, output, echoSubCmd.Short)
assertNotContains(t, output, deprecatedCmd.Short)
assertContains(t, output, "Options inherited from parent commands")
}

func TestGenAsciidocWithNoLongOrSynopsis(t *testing.T) {
Expand All @@ -37,10 +36,10 @@ func TestGenAsciidocWithNoLongOrSynopsis(t *testing.T) {
}
output := buf.String()

checkStringContains(t, output, dummyCmd.Example)
checkStringContains(t, output, dummyCmd.Short)
checkStringContains(t, output, "Options inherited from parent commands")
checkStringOmits(t, output, "### Synopsis")
assertContains(t, output, dummyCmd.Example)
assertContains(t, output, dummyCmd.Short)
assertContains(t, output, "Options inherited from parent commands")
assertNotContains(t, output, "### Synopsis")
}

func TestGenAsciidocNoHiddenParents(t *testing.T) {
Expand All @@ -56,14 +55,14 @@ func TestGenAsciidocNoHiddenParents(t *testing.T) {
}
output := buf.String()

checkStringContains(t, output, echoCmd.Long)
checkStringContains(t, output, echoCmd.Example)
checkStringContains(t, output, "boolone")
checkStringOmits(t, output, "rootflag")
checkStringContains(t, output, rootCmd.Short)
checkStringContains(t, output, echoSubCmd.Short)
checkStringOmits(t, output, deprecatedCmd.Short)
checkStringOmits(t, output, "Options inherited from parent commands")
assertContains(t, output, echoCmd.Long)
assertContains(t, output, echoCmd.Example)
assertContains(t, output, "boolone")
assertNotContains(t, output, "rootflag")
assertContains(t, output, rootCmd.Short)
assertContains(t, output, echoSubCmd.Short)
assertNotContains(t, output, deprecatedCmd.Short)
assertNotContains(t, output, "Options inherited from parent commands")
}

func TestGenAsciidocNoTag(t *testing.T) {
Expand All @@ -76,12 +75,12 @@ func TestGenAsciidocNoTag(t *testing.T) {
}
output := buf.String()

checkStringOmits(t, output, "Auto generated")
assertNotContains(t, output, "Auto generated")
}

func TestGenAsciidocTree(t *testing.T) {
c := &zulu.Command{Use: "do [OPTIONS] arg1 arg2"}
tmpdir, err := ioutil.TempDir("", "test-gen-md-tree")
tmpdir, err := os.MkdirTemp("", "test-gen-md-tree")
if err != nil {
t.Fatalf("Failed to create tmpdir: %v", err)
}
Expand All @@ -97,7 +96,7 @@ func TestGenAsciidocTree(t *testing.T) {
}

func BenchmarkGenAsciidocToFile(b *testing.B) {
file, err := ioutil.TempFile("", "")
file, err := os.CreateTemp("", "")
if err != nil {
b.Fatal(err)
}
Expand Down
32 changes: 19 additions & 13 deletions doc/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,32 @@ var dummyCmd = &zulu.Command{
Short: "Performs a dummy action",
}

func checkStringContains(t *testing.T, got, expected string) {
if !strings.Contains(got, expected) {
t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got)
}
func assertNotContains(t *testing.T, str, unexpected string) {
t.Helper()
assertNotContainsf(t, str, unexpected, "%q should not contain %q", str, unexpected)
}

func checkStringOmits(t *testing.T, got, expected string) {
if strings.Contains(got, expected) {
t.Errorf("Expected to not contain: \n %v\nGot: %v", expected, got)
func assertNotContainsf(t *testing.T, str, unexpected string, msg string, fmt ...interface{}) {
t.Helper()
if strings.Contains(str, unexpected) {
t.Errorf(msg, fmt...)
}
}

func checkStringMatch(t *testing.T, got, pattern string) {
if ok, _ := regexp.MatchString(pattern, got); !ok {
t.Errorf("Expected to match: \n%v\nGot:\n %v\n", pattern, got)
func assertContains(t *testing.T, str, substr string) {
t.Helper()
assertContainsf(t, str, substr, "%q does not contain %q", str, substr)
}

func assertContainsf(t *testing.T, str, expected string, msg string, fmt ...interface{}) {
t.Helper()
if !strings.Contains(str, expected) {
t.Errorf(msg, fmt...)
}
}

func checkStringDontMatch(t *testing.T, got, pattern string) {
if ok, _ := regexp.MatchString(pattern, got); ok {
t.Errorf("Expected not to match: \n%v\nGot:\n %v\n", pattern, got)
func assertMatch(t *testing.T, str, pattern string) {
if ok, _ := regexp.MatchString(pattern, str); !ok {
t.Errorf("Expected to match: \n%v\nGot:\n %v\n", pattern, str)
}
}

0 comments on commit 1a4ec2c

Please sign in to comment.