From b870af3b637eaa3f9e1f56828110ec5e3ecbdcaf Mon Sep 17 00:00:00 2001 From: Jay Pipes Date: Thu, 20 Jun 2024 13:38:13 -0400 Subject: [PATCH] clean up any linter failures Signed-off-by: Jay Pipes --- .github/workflows/lint.yml | 8 ++++---- .golangci.yaml | 6 ++++++ assertion/json/errors.go | 3 +-- assertion/json/json.go | 14 +++++++------- assertion/json/json_test.go | 4 ++-- debug/print.go | 2 ++ fixture/generic_test.go | 2 +- fixture/json/json.go | 16 +++++++--------- plugin/exec/action.go | 11 +++++++---- plugin/exec/defaults.go | 13 ------------- plugin/exec/eval_test.go | 8 ++++---- scenario/from.go | 3 +-- scenario/run_test.go | 6 +++--- suite/from.go | 2 +- 14 files changed, 46 insertions(+), 52 deletions(-) create mode 100644 .golangci.yaml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 2ad167d..50c153f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest steps: - name: harden runner - uses: step-security/harden-runner@55d479fb1c5bcad5a4f9099a5d9f37c8857b2845 # v2.4.1 + uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1 with: egress-policy: block disable-sudo: true @@ -28,14 +28,14 @@ jobs: objects.githubusercontent.com:443 proxy.golang.org:443 - name: checkout code - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - name: setup go uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1 with: go-version: 1.21 - name: lint - uses: golangci/golangci-lint-action@639cd343e1d3b897ff35927a75193d57cfcba299 # v3.6.0 + uses: golangci/golangci-lint-action@a4f60bb28d35aeee14e6880718e0c85ff1882e64 # v6.0.1 with: - version: v1.53 + version: v1.59.1 args: --timeout=5m0s --verbose only-new-issues: true diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..1425afb --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,6 @@ +linters-settings: + staticcheck: + checks: + - all + - '-ST1000' # ignore at least one file in package should have comment + - '-EXC0003' # ignore the stuttered name error diff --git a/assertion/json/errors.go b/assertion/json/errors.go index 0f580b0..6bdb521 100644 --- a/assertion/json/errors.go +++ b/assertion/json/errors.go @@ -103,9 +103,8 @@ func JSONUnmarshalError(err error, node *yaml.Node) error { "%w: %s at line %d, column %d", ErrJSONUnmarshalError, err, node.Line, node.Column, ) - } else { - return fmt.Errorf("%w: %s", ErrJSONUnmarshalError, err) } + return fmt.Errorf("%w: %s", ErrJSONUnmarshalError, err) } // JSONPathInvalid returns an ErrParse when a JSONPath expression could not be diff --git a/assertion/json/json.go b/assertion/json/json.go index 7a60584..8a23615 100644 --- a/assertion/json/json.go +++ b/assertion/json/json.go @@ -104,7 +104,7 @@ func (e *Expect) UnmarshalYAML(node *yaml.Node) error { if err := valNode.Decode(&paths); err != nil { return err } - for path, _ := range paths { + for path := range paths { if len(path) == 0 || path[0] != '$' { return JSONPathInvalidNoRoot(path, valNode) } @@ -121,7 +121,7 @@ func (e *Expect) UnmarshalYAML(node *yaml.Node) error { if err := valNode.Decode(&pathFormats); err != nil { return err } - for pathFormat, _ := range pathFormats { + for pathFormat := range pathFormats { if len(pathFormat) == 0 || pathFormat[0] != '$' { return JSONPathInvalidNoRoot(pathFormat, valNode) } @@ -229,9 +229,9 @@ func (a *assertions) pathsOK() bool { a.Fail(JSONPathNotFound(path, err)) return false } - switch got.(type) { + switch got := got.(type) { case string: - if expVal != got.(string) { + if expVal != got { a.Fail(JSONPathNotEqual(path, expVal, got)) return false } @@ -241,7 +241,7 @@ func (a *assertions) pathsOK() bool { a.Fail(JSONPathConversionError(path, expVal, got)) return false } - if expValInt != got.(int) { + if expValInt != got { a.Fail(JSONPathNotEqual(path, expVal, got)) return false } @@ -251,7 +251,7 @@ func (a *assertions) pathsOK() bool { a.Fail(JSONPathConversionError(path, expVal, got)) return false } - if expValFloat != got.(float64) { + if expValFloat != got { a.Fail(JSONPathNotEqual(path, expVal, got)) return false } @@ -261,7 +261,7 @@ func (a *assertions) pathsOK() bool { a.Fail(JSONPathConversionError(path, expVal, got)) return false } - if expValBool != got.(bool) { + if expValBool != got { a.Fail(JSONPathNotEqual(path, expVal, got)) return false } diff --git a/assertion/json/json_test.go b/assertion/json/json_test.go index 3eed0e2..6b2fdbd 100644 --- a/assertion/json/json_test.go +++ b/assertion/json/json_test.go @@ -6,7 +6,7 @@ package json_test import ( "context" - "io/ioutil" + "os" "path/filepath" "testing" @@ -92,7 +92,7 @@ paths: } func content() []byte { - b, _ := ioutil.ReadFile(filepath.Join("testdata", "books.json")) + b, _ := os.ReadFile(filepath.Join("testdata", "books.json")) return b } diff --git a/debug/print.go b/debug/print.go index 8aa9d28..19eb949 100644 --- a/debug/print.go +++ b/debug/print.go @@ -31,6 +31,7 @@ func Printf( } msg := fmt.Sprintf(format, args...) for _, w := range writers { + //nolint:errcheck w.Write([]byte(msg)) } } @@ -57,6 +58,7 @@ func Println( } msg := fmt.Sprintf(format, args...) for _, w := range writers { + //nolint:errcheck w.Write([]byte(msg)) } } diff --git a/fixture/generic_test.go b/fixture/generic_test.go index 95e2b37..2fe567c 100644 --- a/fixture/generic_test.go +++ b/fixture/generic_test.go @@ -47,7 +47,7 @@ func TestStarter(t *testing.T) { assert.False(started) - f.Start(context.TODO()) + _ = f.Start(context.TODO()) assert.True(started) } diff --git a/fixture/json/json.go b/fixture/json/json.go index b034106..69f6b3a 100644 --- a/fixture/json/json.go +++ b/fixture/json/json.go @@ -8,7 +8,6 @@ import ( "context" "encoding/json" "io" - "io/ioutil" "strconv" "github.com/PaesslerAG/jsonpath" @@ -49,11 +48,11 @@ func (f *jsonFixture) State(path string) interface{} { if err != nil { return nil } - switch got.(type) { + switch got := got.(type) { case string: - return got.(string) + return got case float64: - return strconv.FormatFloat(got.(float64), 'f', 0, 64) + return strconv.FormatFloat(got, 'f', 0, 64) default: return nil } @@ -64,17 +63,16 @@ func (f *jsonFixture) State(path string) interface{} { func New(data interface{}) (gdttypes.Fixture, error) { var err error var b []byte - switch data.(type) { + switch data := data.(type) { case io.Reader: - r := data.(io.Reader) - b, err = ioutil.ReadAll(r) + b, err = io.ReadAll(data) if err != nil { return nil, err } case []byte: - b = data.([]byte) + b = data case string: - b = []byte(data.(string)) + b = []byte(data) } f := jsonFixture{ data: interface{}(nil), diff --git a/plugin/exec/action.go b/plugin/exec/action.go index da3f753..f1b8ef2 100644 --- a/plugin/exec/action.go +++ b/plugin/exec/action.go @@ -58,8 +58,7 @@ func (a *Action) Do( debug.Println(ctx, "exec: %s %s", target, args) - var cmd *exec.Cmd - cmd = exec.CommandContext(ctx, target, args...) + cmd := exec.CommandContext(ctx, target, args...) outpipe, err := cmd.StdoutPipe() if err != nil { @@ -78,13 +77,17 @@ func (a *Action) Do( return err } if outbuf != nil { - outbuf.ReadFrom(outpipe) + if _, err = outbuf.ReadFrom(outpipe); err != nil { + debug.Println(ctx, "exec: error reading from stdout: %s", err) + } if outbuf.Len() > 0 { debug.Println(ctx, "exec: stdout: %s", outbuf.String()) } } if errbuf != nil { - errbuf.ReadFrom(errpipe) + if _, err = errbuf.ReadFrom(errpipe); err != nil { + debug.Println(ctx, "exec: error reading from stderr: %s", err) + } if errbuf.Len() > 0 { debug.Println(ctx, "exec: stderr: %s", errbuf.String()) } diff --git a/plugin/exec/defaults.go b/plugin/exec/defaults.go index ecf6e32..5be9bd9 100644 --- a/plugin/exec/defaults.go +++ b/plugin/exec/defaults.go @@ -8,7 +8,6 @@ import ( "gopkg.in/yaml.v3" "github.com/gdt-dev/gdt/errors" - gdttypes "github.com/gdt-dev/gdt/types" ) type execDefaults struct{} @@ -47,15 +46,3 @@ func (d *Defaults) UnmarshalYAML(node *yaml.Node) error { } return nil } - -// fromBaseDefaults returns an exec plugin-specific Defaults from a Spec -func fromBaseDefaults(base *gdttypes.Defaults) *Defaults { - if base == nil { - return nil - } - d := base.For(pluginName) - if d == nil { - return nil - } - return d.(*Defaults) -} diff --git a/plugin/exec/eval_test.go b/plugin/exec/eval_test.go index b3e9bc9..62820e1 100644 --- a/plugin/exec/eval_test.go +++ b/plugin/exec/eval_test.go @@ -9,7 +9,6 @@ import ( "bytes" "context" "flag" - "fmt" "os" "os/exec" "path/filepath" @@ -196,7 +195,7 @@ func TestExecSleepTimeout(t *testing.T) { // The test should have failed... require.NotNil(err) - debugout := fmt.Sprintf("%s", outerr) + debugout := string(outerr) require.Contains(debugout, "assertion failed: timeout exceeded") } @@ -273,6 +272,7 @@ func TestFailExecTimeoutCascade(t *testing.T) { ctx := gdtcontext.New(gdtcontext.WithDebug()) err = s.Run(ctx, t) + require.Nil(err) } func TestExecTimeoutCascade(t *testing.T) { @@ -288,7 +288,7 @@ func TestExecTimeoutCascade(t *testing.T) { // The test should have failed... require.NotNil(err) - debugout := fmt.Sprintf("%s", outerr) + debugout := string(outerr) require.Contains(debugout, "using timeout of 500ms (expected: false) [scenario default]") require.Contains(debugout, "using timeout of 20ms (expected: true)") } @@ -328,7 +328,7 @@ func TestExecOnFail(t *testing.T) { // The test should have failed... require.NotNil(err) - debugout := fmt.Sprintf("%s", outerr) + debugout := string(outerr) require.Contains(debugout, "assertion failed: not equal: expected dat but got cat") require.Contains(debugout, "echo [bad kitty]") } diff --git a/scenario/from.go b/scenario/from.go index 4a71bee..44cc7bc 100644 --- a/scenario/from.go +++ b/scenario/from.go @@ -6,7 +6,6 @@ package scenario import ( "io" - "io/ioutil" "gopkg.in/yaml.v3" @@ -20,7 +19,7 @@ func FromReader( r io.Reader, mods ...ScenarioModifier, ) (*Scenario, error) { - contents, err := ioutil.ReadAll(r) + contents, err := io.ReadAll(r) if err != nil { return nil, err } diff --git a/scenario/run_test.go b/scenario/run_test.go index f2585fa..e3a1432 100644 --- a/scenario/run_test.go +++ b/scenario/run_test.go @@ -9,7 +9,6 @@ import ( "bytes" "context" "flag" - "fmt" "os" "os/exec" "path/filepath" @@ -35,7 +34,8 @@ func TestRun(t *testing.T) { require.Nil(err) require.NotNil(s) - s.Run(context.TODO(), t) + err = s.Run(context.TODO(), t) + require.Nil(err) } func TestPriorRun(t *testing.T) { @@ -172,7 +172,7 @@ func TestRetryTestOverride(t *testing.T) { // The test should have failed... require.NotNil(err) - debugout := fmt.Sprintf("%s", outerr) + debugout := string(outerr) require.Contains(debugout, "[gdt] [retry-test-override] run: exceeded max attempts 2. stopping.") } diff --git a/suite/from.go b/suite/from.go index b8b0c0c..d094452 100644 --- a/suite/from.go +++ b/suite/from.go @@ -31,7 +31,7 @@ func FromDir( if err := filepath.Walk( dirPath, - func(path string, info os.FileInfo, err error) error { + func(path string, info os.FileInfo, _ error) error { if info.IsDir() { return nil }