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) ```
72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package kong
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/alecthomas/assert/v2"
|
|
)
|
|
|
|
func TestConfigFlag(t *testing.T) {
|
|
var cli struct {
|
|
Config ConfigFlag
|
|
Flag string
|
|
}
|
|
|
|
w, err := ioutil.TempFile("", "")
|
|
assert.NoError(t, err)
|
|
defer os.Remove(w.Name())
|
|
w.WriteString(`{"flag": "hello world"}`) //nolint: errcheck
|
|
w.Close()
|
|
|
|
p := Must(&cli, Configuration(JSON))
|
|
_, err = p.Parse([]string{"--config", w.Name()})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "hello world", cli.Flag)
|
|
}
|
|
|
|
func TestVersionFlag(t *testing.T) {
|
|
var cli struct {
|
|
Version VersionFlag
|
|
}
|
|
w := &strings.Builder{}
|
|
p := Must(&cli, Vars{"version": "0.1.1"})
|
|
p.Stdout = w
|
|
called := 1
|
|
p.Exit = func(s int) { called = s }
|
|
|
|
_, err := p.Parse([]string{"--version"})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "0.1.1", strings.TrimSpace(w.String()))
|
|
assert.Equal(t, 0, called)
|
|
}
|
|
|
|
func TestChangeDirFlag(t *testing.T) {
|
|
cwd, err := os.Getwd()
|
|
assert.NoError(t, err)
|
|
defer os.Chdir(cwd) //nolint: errcheck
|
|
|
|
dir := t.TempDir()
|
|
file := filepath.Join(dir, "out.txt")
|
|
err = os.WriteFile(file, []byte("foobar"), 0o600)
|
|
assert.NoError(t, err)
|
|
|
|
var cli struct {
|
|
ChangeDir ChangeDirFlag `short:"C"`
|
|
Path string `arg:"" type:"existingfile"`
|
|
}
|
|
|
|
p := Must(&cli)
|
|
_, err = p.Parse([]string{"-C", dir, "out.txt"})
|
|
assert.NoError(t, err)
|
|
if runtime.GOOS != "windows" {
|
|
file, err = filepath.EvalSymlinks(file) // Needed because OSX uses a symlinked tmp dir.
|
|
assert.NoError(t, err)
|
|
}
|
|
assert.Equal(t, file, cli.Path)
|
|
}
|