a86bda490b
The golangci-lint being used was quite dated. This change upgrades to the latest version. Adds and updates exclusions based on new failures. Note on revive: I've included an opt-out for unused parameters for revive because turning `newThing(required bool)` to `newThing(_ bool)` is a loss of useful information. The changes to the Go files are to fix the following issues: ``` camelcase.go:16: File is not `gofmt`-ed with `-s` (gofmt) config_test.go:50:18: directive `//nolint: gosec` is unused for linter "gosec" (nolintlint) defaults_test.go:28:25: G601: Implicit memory aliasing in for loop. (gosec) doc.go:5: File is not `gofmt`-ed with `-s` (gofmt) kong.go:446:18: directive `//nolint: gosec` is unused for linter "gosec" (nolintlint) kong_test.go:503:20: G601: Implicit memory aliasing in for loop. (gosec) model.go:493:10: composites: reflect.ValueError struct literal uses unkeyed fields (govet) scanner.go:112: File is not `gofmt`-ed with `-s` (gofmt) ``` And to address broken nolint directives reported as follows by golangci-lint. ``` [.. skipped .. ] tag.go:65:1: directive `// nolint:gocyclo` should be written without leading space as `//nolint:gocyclo` (nolintlint) tag.go:206:51: directive `// nolint: gocyclo` should be written without leading space as `//nolint: gocyclo` (nolintlint) util_test.go:23:43: directive `// nolint: errcheck` should be written without leading space as `//nolint: errcheck` (nolintlint) util_test.go:51:22: directive `// nolint: errcheck` should be written without leading space as `//nolint: errcheck` (nolintlint) ```
125 lines
2.5 KiB
Go
125 lines
2.5 KiB
Go
package kong
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/alecthomas/assert/v2"
|
|
)
|
|
|
|
func TestOptions(t *testing.T) {
|
|
var cli struct{}
|
|
p, err := New(&cli, Name("name"), Description("description"), Writers(nil, nil), Exit(nil))
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "name", p.Model.Name)
|
|
assert.Equal(t, "description", p.Model.Help)
|
|
assert.Zero(t, p.Stdout)
|
|
assert.Zero(t, p.Stderr)
|
|
assert.Zero(t, p.Exit)
|
|
}
|
|
|
|
type impl string
|
|
|
|
func (impl) Method() {}
|
|
|
|
func TestBindTo(t *testing.T) {
|
|
type iface interface {
|
|
Method()
|
|
}
|
|
|
|
saw := ""
|
|
method := func(i iface) error {
|
|
saw = string(i.(impl)) //nolint
|
|
return nil
|
|
}
|
|
|
|
var cli struct{}
|
|
|
|
p, err := New(&cli, BindTo(impl("foo"), (*iface)(nil)))
|
|
assert.NoError(t, err)
|
|
err = callFunction(reflect.ValueOf(method), p.bindings)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "foo", saw)
|
|
}
|
|
|
|
func TestInvalidCallback(t *testing.T) {
|
|
type iface interface {
|
|
Method()
|
|
}
|
|
|
|
saw := ""
|
|
method := func(i iface) string {
|
|
saw = string(i.(impl)) //nolint
|
|
return saw
|
|
}
|
|
|
|
var cli struct{}
|
|
|
|
p, err := New(&cli, BindTo(impl("foo"), (*iface)(nil)))
|
|
assert.NoError(t, err)
|
|
err = callFunction(reflect.ValueOf(method), p.bindings)
|
|
assert.EqualError(t, err, `return value of func(kong.iface) string must implement "error"`)
|
|
}
|
|
|
|
type zrror struct{}
|
|
|
|
func (*zrror) Error() string {
|
|
return "error"
|
|
}
|
|
|
|
func TestCallbackCustomError(t *testing.T) {
|
|
type iface interface {
|
|
Method()
|
|
}
|
|
|
|
saw := ""
|
|
method := func(i iface) *zrror {
|
|
saw = string(i.(impl)) //nolint
|
|
return nil
|
|
}
|
|
|
|
var cli struct{}
|
|
|
|
p, err := New(&cli, BindTo(impl("foo"), (*iface)(nil)))
|
|
assert.NoError(t, err)
|
|
err = callFunction(reflect.ValueOf(method), p.bindings)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "foo", saw)
|
|
}
|
|
|
|
type bindToProviderCLI struct {
|
|
Called bool
|
|
Cmd bindToProviderCmd `cmd:""`
|
|
}
|
|
|
|
type boundThing struct {
|
|
}
|
|
|
|
type bindToProviderCmd struct{}
|
|
|
|
func (*bindToProviderCmd) Run(cli *bindToProviderCLI, b *boundThing) error {
|
|
cli.Called = true
|
|
return nil
|
|
}
|
|
|
|
func TestBindToProvider(t *testing.T) {
|
|
var cli bindToProviderCLI
|
|
app, err := New(&cli, BindToProvider(func() (*boundThing, error) { return &boundThing{}, nil }))
|
|
assert.NoError(t, err)
|
|
ctx, err := app.Parse([]string{"cmd"})
|
|
assert.NoError(t, err)
|
|
err = ctx.Run()
|
|
assert.NoError(t, err)
|
|
assert.True(t, cli.Called)
|
|
}
|
|
|
|
func TestFlagNamer(t *testing.T) {
|
|
var cli struct {
|
|
SomeFlag string
|
|
}
|
|
app, err := New(&cli, FlagNamer(strings.ToUpper))
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "SOMEFLAG", app.Model.Flags[1].Name)
|
|
}
|