Skip to content

Commit

Permalink
chore: integrate golangci yaml config file and run all linters
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed Sep 4, 2024
1 parent 1a00d0e commit 16438bc
Show file tree
Hide file tree
Showing 29 changed files with 177 additions and 132 deletions.
44 changes: 44 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
linters:
enable:
- errorlint
- gci
- gocritic
- gofumpt
- misspell
- nonamedreturns
- testifylint
- errcheck

linters-settings:
errorlint:
# Check whether fmt.Errorf uses the %w verb for formatting errors.
# See the https://github.com/polyfloyd/go-errorlint for caveats.
errorf: true
# Permit more than 1 %w verb, valid per Go 1.20 (Requires errorf:true)
errorf-multi: true
# Check for plain type assertions and type switches.
asserts: true
# Check for plain error comparisons.
comparison: true
gci:
sections:
- standard
- default
- prefix(github.com/go-sprout)
testifylint:
disable:
- float-compare
- go-require
enable:
- bool-compare
- compares
- empty
- error-is-as
- error-nil
- expected-actual
- len
- require-error
- suite-dont-use-pkg
- suite-extra-assert-call
run:
timeout: 5m
4 changes: 1 addition & 3 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package sprout

import (
"errors"
"testing"

"log/slog"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -159,7 +158,6 @@ func TestDefaultHandler_AddRegistry_Error_RegisteriesNotices(t *testing.T) {

mockRegistry.AssertCalled(t, "RegisterFunctions", dh.cachedFuncsMap)
mockRegistry.AssertCalled(t, "RegisterNotices", &dh.notices)

}

// TestDefaultHandler_AddRegistry tests the AddRegistry method of DefaultHandler.
Expand Down
1 change: 0 additions & 1 deletion internal/helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func TestEmpty(t *testing.T) {
assert.False(t, Empty(true))
assert.False(t, Empty(uint(1)))
assert.False(t, Empty(uint8(1)))

}

func TestUntilStep(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pesticide/rand_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package pesticide
import (
"testing"

"github.com/go-sprout/sprout"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/go-sprout/sprout"
)

type RegexpTestCase struct {
Expand Down
5 changes: 3 additions & 2 deletions pesticide/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
"testing"
"text/template"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/go-sprout/sprout"
"github.com/go-sprout/sprout/registry/maps"
"github.com/go-sprout/sprout/registry/reflect"
"github.com/go-sprout/sprout/registry/slices"
"github.com/go-sprout/sprout/registry/strings"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type TestCase struct {
Expand Down
7 changes: 3 additions & 4 deletions registry/backward/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (bcr *BackwardCompatibilityRegistry) UrlParse(v string) (map[string]any, er
dict := map[string]any{}
parsedURL, err := url.Parse(v)
if err != nil {
return dict, fmt.Errorf("unable to parse url: %s", err)
return dict, fmt.Errorf("unable to parse url: %w", err)
}
dict["scheme"] = parsedURL.Scheme
dict["host"] = parsedURL.Host
Expand Down Expand Up @@ -85,7 +85,6 @@ func (bcr *BackwardCompatibilityRegistry) UrlParse(v string) (map[string]any, er
//
// {{ dict scheme="https" host="example.com" path="/path" query="query=1" opaque="opaque" fragment="fragment" | urlJoin }} // Output: "https://example.com/path?query=1#fragment"
func (bcr *BackwardCompatibilityRegistry) UrlJoin(d map[string]any) (string, error) {

resURL := url.URL{
Scheme: bcr.get(d, "scheme").(string),
Host: bcr.get(d, "host").(string),
Expand All @@ -99,7 +98,7 @@ func (bcr *BackwardCompatibilityRegistry) UrlJoin(d map[string]any) (string, err
if userinfo != "" {
tempURL, err := url.Parse(fmt.Sprintf("proto://%s@host", userinfo))
if err != nil {
return "", fmt.Errorf("unable to parse userinfo in dict: %s", err)
return "", fmt.Errorf("unable to parse userinfo in dict: %w", err)

Check warning on line 101 in registry/backward/functions.go

View check run for this annotation

Codecov / codecov/patch

registry/backward/functions.go#L101

Added line #L101 was not covered by tests
}
user = tempURL.User
}
Expand Down Expand Up @@ -128,7 +127,7 @@ func (bcr *BackwardCompatibilityRegistry) UrlJoin(d map[string]any) (string, err
func (bcr *BackwardCompatibilityRegistry) GetHostByName(name string) (string, error) {
addrs, err := net.LookupHost(name)
if err != nil {
return "", fmt.Errorf("unable to resolve hostname: %s", err)
return "", fmt.Errorf("unable to resolve hostname: %w", err)
}

Check warning on line 131 in registry/backward/functions.go

View check run for this annotation

Codecov / codecov/patch

registry/backward/functions.go#L130-L131

Added lines #L130 - L131 were not covered by tests
return addrs[rand.Intn(len(addrs))], nil
}
8 changes: 3 additions & 5 deletions registry/backward/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ import (
)

func TestFail(t *testing.T) {

var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Input: `{{fail "This is an error"}}`, ExpectedErr: "This is an error"},
}

pesticide.RunTestCases(t, backward.NewRegistry(), tc)
}

func TestUrlParse(t *testing.T) {

var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Input: `{{ urlParse "https://example.com" | urlJoin }}`, ExpectedOutput: "https://example.com"},
{Input: `{{ urlParse "https://example.com/path" | urlJoin }}`, ExpectedOutput: "https://example.com/path"},
{Input: `{{ urlParse "https://user:pass@example.com/path?query=1" | urlJoin }}`, ExpectedOutput: "https://user:pass@example.com/path?query=1"},
Expand All @@ -34,7 +32,7 @@ func TestGetHostByName(t *testing.T) {

ipAddressRegexp := ipv4 + `|` + ipv6

var tc = []pesticide.RegexpTestCase{
tc := []pesticide.RegexpTestCase{
{Template: `{{ getHostByName "example.com" }}`, Regexp: ipAddressRegexp, Length: -1},
{Template: `{{ getHostByName "github.com" }}`, Regexp: ipAddressRegexp, Length: -1},
{Template: `{{ getHostByName "127.0.0.1" }}`, Regexp: ipAddressRegexp, Length: -1},
Expand Down
6 changes: 4 additions & 2 deletions registry/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ var masterPasswordSeed = "com.lyndir.masterpassword"

var passwordTypeTemplates = map[string][][]byte{
"maximum": {[]byte("anoxxxxxxxxxxxxxxxxx"), []byte("axxxxxxxxxxxxxxxxxno")},
"long": {[]byte("CvcvnoCvcvCvcv"), []byte("CvcvCvcvnoCvcv"), []byte("CvcvCvcvCvcvno"), []byte("CvccnoCvcvCvcv"), []byte("CvccCvcvnoCvcv"),
"long": {
[]byte("CvcvnoCvcvCvcv"), []byte("CvcvCvcvnoCvcv"), []byte("CvcvCvcvCvcvno"), []byte("CvccnoCvcvCvcv"), []byte("CvccCvcvnoCvcv"),
[]byte("CvccCvcvCvcvno"), []byte("CvcvnoCvccCvcv"), []byte("CvcvCvccnoCvcv"), []byte("CvcvCvccCvcvno"), []byte("CvcvnoCvcvCvcc"),
[]byte("CvcvCvcvnoCvcc"), []byte("CvcvCvcvCvccno"), []byte("CvccnoCvccCvcv"), []byte("CvccCvccnoCvcv"), []byte("CvccCvccCvcvno"),
[]byte("CvcvnoCvccCvcc"), []byte("CvcvCvccnoCvcc"), []byte("CvcvCvccCvccno"), []byte("CvccnoCvcvCvcc"), []byte("CvccCvcvnoCvcc"),
[]byte("CvccCvcvCvccno")},
[]byte("CvccCvcvCvccno"),
},
"medium": {[]byte("CvcnoCvc"), []byte("CvcCvcno")},
"short": {[]byte("Cvcn")},
"basic": {[]byte("aaanaaan"), []byte("aannaaan"), []byte("aaannaaa")},
Expand Down
8 changes: 4 additions & 4 deletions registry/crypto/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (ch *CryptoRegistry) Htpasswd(username string, password string) (string, er
//
// {{ derivePassword 0 "bcrypt" "password" "user" "site" }} // Output: "$2a$12$C1qL8XVjIuGKzQXwC6g6tO"
func (ch *CryptoRegistry) DerivePassword(counter uint32, passwordType, password, user, site string) (string, error) {
var templates = passwordTypeTemplates[passwordType]
templates := passwordTypeTemplates[passwordType]
if templates == nil {
return "", fmt.Errorf("cannot find password template %s", passwordType)
}
Expand All @@ -96,10 +96,10 @@ func (ch *CryptoRegistry) DerivePassword(counter uint32, passwordType, password,
buffer.WriteString(site)
_ = binary.Write(&buffer, binary.BigEndian, counter)

var hmacv = hmac.New(sha256.New, key)
hmacv := hmac.New(sha256.New, key)
hmacv.Write(buffer.Bytes())
var seed = hmacv.Sum(nil)
var temp = templates[int(seed[0])%len(templates)]
seed := hmacv.Sum(nil)
temp := templates[int(seed[0])%len(templates)]

buffer.Truncate(0)
for i, element := range temp {
Expand Down
19 changes: 10 additions & 9 deletions registry/encoding/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"fmt"
"strings"

"github.com/go-sprout/sprout"
"gopkg.in/yaml.v3"

"github.com/go-sprout/sprout"
)

// Base64Encode encodes a string into its Base64 representation.
Expand Down Expand Up @@ -47,7 +48,7 @@ func (er *EncodingRegistry) Base64Encode(str string) string {
func (er *EncodingRegistry) Base64Decode(str string) (string, error) {
bytes, err := base64.StdEncoding.DecodeString(str)
if err != nil {
return "", fmt.Errorf("base64 decode error: %v", err)
return "", fmt.Errorf("base64 decode error: %w", err)
}
return string(bytes), nil
}
Expand Down Expand Up @@ -87,7 +88,7 @@ func (er *EncodingRegistry) Base32Encode(str string) string {
func (er *EncodingRegistry) Base32Decode(str string) (string, error) {
bytes, err := base32.StdEncoding.DecodeString(str)
if err != nil {
return "", fmt.Errorf("base32 decode error: %v", err)
return "", fmt.Errorf("base32 decode error: %w", err)
}
return string(bytes), nil
}
Expand All @@ -111,7 +112,7 @@ func (er *EncodingRegistry) FromJson(v string) (any, error) {
var output any
err := json.Unmarshal([]byte(v), &output)
if err != nil {
return nil, fmt.Errorf("json decode error: %v", err)
return nil, fmt.Errorf("json decode error: %w", err)
}
return output, err
}
Expand All @@ -134,7 +135,7 @@ func (er *EncodingRegistry) FromJson(v string) (any, error) {
func (er *EncodingRegistry) ToJson(v any) (string, error) {
output, err := json.Marshal(v)
if err != nil {
return "", fmt.Errorf("json encode error: %v", err)
return "", fmt.Errorf("json encode error: %w", err)
}
return string(output), nil
}
Expand All @@ -157,7 +158,7 @@ func (er *EncodingRegistry) ToJson(v any) (string, error) {
func (er *EncodingRegistry) ToPrettyJson(v any) (string, error) {
output, err := json.MarshalIndent(v, "", " ")
if err != nil {
return "", fmt.Errorf("json encode error: %v", err)
return "", fmt.Errorf("json encode error: %w", err)
}
return string(output), nil
}
Expand All @@ -183,7 +184,7 @@ func (er *EncodingRegistry) ToRawJson(v any) (string, error) {
enc.SetEscapeHTML(false)
err := enc.Encode(&v)
if err != nil {
return "", fmt.Errorf("json encode error: %v", err)
return "", fmt.Errorf("json encode error: %w", err)
}
return strings.TrimSuffix(buf.String(), "\n"), nil
}
Expand All @@ -206,7 +207,7 @@ func (er *EncodingRegistry) FromYAML(str string) (any, error) {
m := make(map[string]any)

if err := yaml.Unmarshal([]byte(str), &m); err != nil {
return nil, fmt.Errorf("yaml decode error: %v", err)
return nil, fmt.Errorf("yaml decode error: %w", err)
}

return m, nil
Expand Down Expand Up @@ -237,7 +238,7 @@ func (er *EncodingRegistry) ToYAML(v any) (out string, err error) {
// code unreachable because yaml.Marshal always panic on error and never
// returns an error, but we still need to handle the error for the sake of
// consistency. The error message is set by ErrRecoverPanic.
return "", fmt.Errorf("yaml encode error: %v", err)
return "", fmt.Errorf("yaml encode error: %w", err)

Check warning on line 241 in registry/encoding/functions.go

View check run for this annotation

Codecov / codecov/patch

registry/encoding/functions.go#L238-L241

Added lines #L238 - L241 were not covered by tests
}

return strings.TrimSuffix(string(data), "\n"), nil
Expand Down
4 changes: 2 additions & 2 deletions registry/env/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestEnv(t *testing.T) {
os.Setenv("__SPROUT_TEST_ENV_KEY", "sprout will grow!")
var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Name: "TestEmpty", Input: `{{ env "" }}`, ExpectedOutput: ""},
{Name: "TestNonExistent", Input: `{{ env "NON_EXISTENT_ENV_VAR" }}`, ExpectedOutput: ""},
{Name: "TestExisting", Input: `{{ env "__SPROUT_TEST_ENV_KEY" }}`, ExpectedOutput: "sprout will grow!"},
Expand All @@ -23,7 +23,7 @@ func TestEnv(t *testing.T) {

func TestExpandEnv(t *testing.T) {
os.Setenv("__SPROUT_TEST_ENV_KEY", "sprout will grow!")
var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Name: "TestEmpty", Input: `{{ expandEnv "" }}`, ExpectedOutput: ""},
{Name: "TestNonExistent", Input: `{{ expandEnv "Hey" }}`, ExpectedOutput: "Hey"},
{Name: "TestNonExistent", Input: `{{ expandEnv "$NON_EXISTENT_ENV_VAR" }}`, ExpectedOutput: ""},
Expand Down
10 changes: 5 additions & 5 deletions registry/filesystem/filesystem_functions_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestOsBase(t *testing.T) {
var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Name: "TestEmptyPath", Input: `{{ osBase "" }}`, ExpectedOutput: "."},
{Name: "TestRootPath", Input: `{{ osBase "/" }}`, ExpectedOutput: "/"},
{Name: "TestWithoutExtension", Input: `{{ osBase "/path/to/file" }}`, ExpectedOutput: "file"},
Expand All @@ -21,7 +21,7 @@ func TestOsBase(t *testing.T) {
}

func TestOsDir(t *testing.T) {
var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Name: "TestEmptyPath", Input: `{{ osDir "" }}`, ExpectedOutput: "."},
{Name: "TestRootPath", Input: `{{ osDir "/" }}`, ExpectedOutput: "/"},
{Name: "TestWithoutExtension", Input: `{{ osDir "/path/to/file" }}`, ExpectedOutput: "/path/to"},
Expand All @@ -34,7 +34,7 @@ func TestOsDir(t *testing.T) {
}

func TestOsExt(t *testing.T) {
var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Name: "TestEmptyPath", Input: `{{ osExt "" }}`, ExpectedOutput: ""},
{Name: "TestRootPath", Input: `{{ osExt "/" }}`, ExpectedOutput: ""},
{Name: "TestWithoutExtension", Input: `{{ osExt "/path/to/file" }}`, ExpectedOutput: ""},
Expand All @@ -47,7 +47,7 @@ func TestOsExt(t *testing.T) {
}

func TestOsClean(t *testing.T) {
var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Name: "TestEmptyPath", Input: `{{ osClean "" }}`, ExpectedOutput: "."},
{Name: "TestRootPath", Input: `{{ osClean "/" }}`, ExpectedOutput: "/"},
{Name: "TestWithoutExtension", Input: `{{ osClean "/path///to/file" }}`, ExpectedOutput: "/path/to/file"},
Expand All @@ -57,7 +57,7 @@ func TestOsClean(t *testing.T) {
}

func TestOsIsAbs(t *testing.T) {
var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Name: "TestEmptyPath", Input: `{{ osIsAbs "" }}`, ExpectedOutput: "false"},
{Name: "TestRootPath", Input: `{{ osIsAbs "/" }}`, ExpectedOutput: "true"},
{Name: "TestRelativePath", Input: `{{ osIsAbs "path/to/file" }}`, ExpectedOutput: "false"},
Expand Down
10 changes: 5 additions & 5 deletions registry/filesystem/filesystem_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestPathBase(t *testing.T) {
var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Name: "TestEmptyPath", Input: `{{ pathBase "" }}`, ExpectedOutput: "."},
{Name: "TestRootPath", Input: `{{ pathBase "/" }}`, ExpectedOutput: "/"},
{Name: "TestWithoutExtension", Input: `{{ pathBase "/path/to/file" }}`, ExpectedOutput: "file"},
Expand All @@ -21,7 +21,7 @@ func TestPathBase(t *testing.T) {
}

func TestPathDir(t *testing.T) {
var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Name: "TestEmptyPath", Input: `{{ pathDir "" }}`, ExpectedOutput: "."},
{Name: "TestRootPath", Input: `{{ pathDir "/" }}`, ExpectedOutput: "/"},
{Name: "TestWithoutExtension", Input: `{{ pathDir "/path/to/file" }}`, ExpectedOutput: "/path/to"},
Expand All @@ -34,7 +34,7 @@ func TestPathDir(t *testing.T) {
}

func TestPathExt(t *testing.T) {
var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Name: "TestEmptyPath", Input: `{{ pathExt "" }}`, ExpectedOutput: ""},
{Name: "TestRootPath", Input: `{{ pathExt "/" }}`, ExpectedOutput: ""},
{Name: "TestWithoutExtension", Input: `{{ pathExt "/path/to/file" }}`, ExpectedOutput: ""},
Expand All @@ -47,7 +47,7 @@ func TestPathExt(t *testing.T) {
}

func TestPathClean(t *testing.T) {
var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Name: "TestEmptyPath", Input: `{{ pathClean "" }}`, ExpectedOutput: "."},
{Name: "TestRootPath", Input: `{{ pathClean "/" }}`, ExpectedOutput: "/"},
{Name: "TestWithoutExtension", Input: `{{ pathClean "/path/to/file" }}`, ExpectedOutput: "/path/to/file"},
Expand All @@ -63,7 +63,7 @@ func TestPathClean(t *testing.T) {
}

func TestPathIsAbs(t *testing.T) {
var tc = []pesticide.TestCase{
tc := []pesticide.TestCase{
{Name: "TestEmptyPath", Input: `{{ pathIsAbs "" }}`, ExpectedOutput: "false"},
{Name: "TestRootPath", Input: `{{ pathIsAbs "/" }}`, ExpectedOutput: "true"},
{Name: "TestRelativePath", Input: `{{ pathIsAbs "path/to/file" }}`, ExpectedOutput: "false"},
Expand Down
Loading

0 comments on commit 16438bc

Please sign in to comment.