Skip to content

Commit

Permalink
added golangci lint configuration file with default linter as well as…
Browse files Browse the repository at this point in the history
… a few code format linters
  • Loading branch information
emilien-puget committed Feb 21, 2024
1 parent 40f2589 commit 81d1f2c
Show file tree
Hide file tree
Showing 24 changed files with 134 additions and 72 deletions.
27 changes: 27 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
run:
timeout: "10m"
linters:
disable-all: true
enable:
# Format
- "gofumpt" # Gofumpt checks whether code was gofumpt-ed. Which is a superset of gofmt that handles some cases that gofmt missed.
- "gci" # Gci controls Go package import order and makes it always deterministic.
- "asciicheck" # Checks that all code identifiers does not have non-ASCII symbols in the name.
- "bidichk" # Checks for dangerous unicode character sequences.
# Error
- "errcheck"
# Simplify
- "gosimple"
- "govet"
- "ineffassign"
- "staticcheck"
- "unused"
linters-settings:
gci:
sections:
- standard
- default
- prefix(github.com/ovechkin-dm/mockio)
- blank
- dot
- alias
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
gofumpt:
gofumpt -l -w .

import:
gci write --skip-generated -s standard -s default -s "prefix(github.com/ovechkin-dm/mockio)" -s blank -s dot -s alias .
5 changes: 3 additions & 2 deletions mock/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package mock
import (
"context"
"fmt"
"github.com/ovechkin-dm/mockio/matchers"
"github.com/ovechkin-dm/mockio/registry"
"reflect"
"strings"

"github.com/ovechkin-dm/mockio/matchers"
"github.com/ovechkin-dm/mockio/registry"
)

// SetUp initializes the mock library with the reporter.
Expand Down
7 changes: 4 additions & 3 deletions registry/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package registry

import (
"fmt"
"reflect"
"sync"

"github.com/ovechkin-dm/go-dyno/pkg/dyno"
"github.com/ovechkin-dm/go-dyno/proxy"

"github.com/ovechkin-dm/mockio/matchers"
"reflect"
"sync"
)

type invocationHandler struct {
Expand Down Expand Up @@ -139,7 +141,6 @@ func (h *invocationHandler) VerifyMethod(verifier matchers.MethodVerifier) {
}

func (h *invocationHandler) DoVerifyMethod(call *MethodCall) []reflect.Value {

matchersOk := h.validateMatchers(call)

verifyMatchersOk := true
Expand Down
3 changes: 2 additions & 1 deletion registry/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package registry

import (
"fmt"
"github.com/ovechkin-dm/mockio/matchers"
"reflect"

"github.com/ovechkin-dm/mockio/matchers"
)

func AnyMatcher[T any]() matchers.Matcher[T] {
Expand Down
21 changes: 12 additions & 9 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package registry

import (
"fmt"
"github.com/ovechkin-dm/go-dyno/pkg/dyno"
"github.com/ovechkin-dm/mockio/matchers"
"github.com/ovechkin-dm/mockio/threadlocal"

"log"
"reflect"
"sync"

"github.com/ovechkin-dm/go-dyno/pkg/dyno"

"github.com/ovechkin-dm/mockio/matchers"
"github.com/ovechkin-dm/mockio/threadlocal"
)

var instance = threadlocal.NewThreadLocal(newRegistry)
var lock sync.Mutex
var (
instance = threadlocal.NewThreadLocal(newRegistry)
lock sync.Mutex
)

type Registry struct {
mockContext *mockContext
Expand Down Expand Up @@ -62,8 +65,8 @@ func Mock[T any]() T {
func AddMatcher[T any](m matchers.Matcher[T]) {
withCheck[any](func() any {
w := &matcherWrapper{
matcher: untypedMatcher(m),
rec: nil,
matcher: untypedMatcher(m),
rec: nil,
stackTrace: NewStackTrace(),
}
getInstance().mockContext.getState().matchers = append(getInstance().mockContext.getState().matchers, w)
Expand All @@ -78,7 +81,7 @@ func AddCaptor[T any](c *captorImpl[T]) {
matcher: FunMatcher(fmt.Sprintf("Captor[%s]", tp), func(call []any, a any) bool {
return true
}),
rec: c,
rec: c,
stackTrace: NewStackTrace(),
}
getInstance().mockContext.getState().matchers = append(getInstance().mockContext.getState().matchers, w)
Expand Down
14 changes: 6 additions & 8 deletions registry/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package registry

import (
"fmt"
"github.com/ovechkin-dm/go-dyno/proxy"
"github.com/ovechkin-dm/mockio/matchers"
"reflect"
"strings"

"github.com/ovechkin-dm/go-dyno/proxy"

"github.com/ovechkin-dm/mockio/matchers"
)

type panicReporter struct {
}
type panicReporter struct{}

func (p *panicReporter) Cleanup(f func()) {

}

func (p *panicReporter) Fatalf(format string, args ...any) {
Expand Down Expand Up @@ -64,7 +64,6 @@ func (e *EnrichedReporter) ReportUnregisteredMockVerify(t any) {
Example of correct verification:
Verify(mock, Times(10)).SomeMethod()`, t)
}

}

func (e *EnrichedReporter) ReportInvalidUseOfMatchers(instanceType reflect.Type, call *MethodCall, m []*matcherWrapper) {
Expand All @@ -84,7 +83,7 @@ func (e *EnrichedReporter) ReportInvalidUseOfMatchers(instanceType reflect.Type,
numActual := len(m)
declarationLines := make([]string, 0)
for i := range m {
declarationLines = append(declarationLines, "\t\t" + m[i].stackTrace.CallerLine())
declarationLines = append(declarationLines, "\t\t"+m[i].stackTrace.CallerLine())
}
decl := strings.Join(declarationLines, "\n")
expectedStr := fmt.Sprintf("%v expected, %v recorded:\n", numExpected, numActual)
Expand Down Expand Up @@ -160,7 +159,6 @@ func (e *EnrichedReporter) ReportVerifyMethodError(
Invocations:
%v`, err, callStr, sb.String())
}

}

func (e *EnrichedReporter) ReportEmptyCaptor() {
Expand Down
6 changes: 2 additions & 4 deletions registry/returners.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ func ToReturnerDouble[A any, B any](retAll matchers.ReturnerAll) matchers.Return
}
}

type returnerDummyImpl struct {

}
type returnerDummyImpl struct{}

func (r *returnerDummyImpl) ThenReturn(values ...any) matchers.ReturnerAll {
return r
Expand Down Expand Up @@ -99,4 +97,4 @@ func NewReturnerAll(ctx *mockContext, data *methodMatch) matchers.ReturnerAll {

func NewEmptyReturner() matchers.ReturnerAll {
return &returnerDummyImpl{}
}
}
8 changes: 5 additions & 3 deletions registry/state.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package registry

import (
"github.com/ovechkin-dm/go-dyno/pkg/dyno"
"github.com/ovechkin-dm/mockio/matchers"
"github.com/ovechkin-dm/mockio/threadlocal"
"reflect"
"sync"
"sync/atomic"

"github.com/ovechkin-dm/go-dyno/pkg/dyno"

"github.com/ovechkin-dm/mockio/matchers"
"github.com/ovechkin-dm/mockio/threadlocal"
)

type fiberState struct {
Expand Down
15 changes: 9 additions & 6 deletions registry/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package registry

import (
"fmt"
"github.com/ovechkin-dm/go-dyno/proxy"
"reflect"
"runtime/debug"
"strings"

"github.com/ovechkin-dm/go-dyno/proxy"
)

const PackageName = "github.com/ovechkin-dm/mockio"
const DynoPackageName = "github.com/ovechkin-dm/go-dyno"
const TestPackageName = "github.com/ovechkin-dm/mockio/tests"
const DebugPackage = "runtime/debug.Stack()"
const GOIDPackageName = "github.com/petermattis/goid"
const (
PackageName = "github.com/ovechkin-dm/mockio"
DynoPackageName = "github.com/ovechkin-dm/go-dyno"
TestPackageName = "github.com/ovechkin-dm/mockio/tests"
DebugPackage = "runtime/debug.Stack()"
GOIDPackageName = "github.com/petermattis/goid"
)

func createDefaultReturnValues(m reflect.Method) []reflect.Value {
result := make([]reflect.Value, m.Type.NumOut())
Expand Down
6 changes: 4 additions & 2 deletions tests/captor/captor_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package captor

import (
. "github.com/ovechkin-dm/mockio/mock"
"github.com/ovechkin-dm/mockio/tests/common"
"testing"

"github.com/ovechkin-dm/mockio/tests/common"

. "github.com/ovechkin-dm/mockio/mock"
)

type iface interface {
Expand Down
11 changes: 5 additions & 6 deletions tests/check/check_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package check

import (
. "github.com/ovechkin-dm/mockio/mock"
"github.com/ovechkin-dm/mockio/tests/common"
"testing"

"github.com/ovechkin-dm/mockio/tests/common"

. "github.com/ovechkin-dm/mockio/mock"
)

type St struct {
}
type St struct{}

func TestNonInterfaceNotAllowed(t *testing.T) {
r := common.NewMockReporter(t)
SetUp(r)
_ = Mock[St]()
r.AssertError()
}


6 changes: 4 additions & 2 deletions tests/concurrent/concurrent_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package concurrent

import (
. "github.com/ovechkin-dm/mockio/mock"
"github.com/ovechkin-dm/mockio/tests/common"
"sync"
"testing"

"github.com/ovechkin-dm/mockio/tests/common"

. "github.com/ovechkin-dm/mockio/mock"
)

type myInterface interface {
Expand Down
6 changes: 4 additions & 2 deletions tests/match/match_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package match

import (
. "github.com/ovechkin-dm/mockio/mock"
"github.com/ovechkin-dm/mockio/tests/common"
"testing"

"github.com/ovechkin-dm/mockio/tests/common"

. "github.com/ovechkin-dm/mockio/mock"
)

type Iface interface {
Expand Down
6 changes: 4 additions & 2 deletions tests/mocking/mock_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package mocking

import (
. "github.com/ovechkin-dm/mockio/mock"
"github.com/ovechkin-dm/mockio/tests/common"
"testing"

"github.com/ovechkin-dm/mockio/tests/common"

. "github.com/ovechkin-dm/mockio/mock"
)

type ByteArrInterface interface {
Expand Down
8 changes: 5 additions & 3 deletions tests/reporting/error_reporting_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package reporting

import (
. "github.com/ovechkin-dm/mockio/mock"
"github.com/ovechkin-dm/mockio/tests/common"
"sync"
"testing"

"github.com/ovechkin-dm/mockio/tests/common"

. "github.com/ovechkin-dm/mockio/mock"
)

type Foo interface {
Expand Down Expand Up @@ -172,4 +174,4 @@ func TestUnexpectedMatchers(t *testing.T) {
Verify(mock, Once()).Baz(10, 10, 10)
r.AssertError()
r.PrintError()
}
}
6 changes: 4 additions & 2 deletions tests/returners/returners_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package returners

import (
. "github.com/ovechkin-dm/mockio/mock"
"github.com/ovechkin-dm/mockio/tests/common"
"testing"

"github.com/ovechkin-dm/mockio/tests/common"

. "github.com/ovechkin-dm/mockio/mock"
)

type iface interface {
Expand Down
6 changes: 4 additions & 2 deletions tests/simple/simple_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package simple

import (
. "github.com/ovechkin-dm/mockio/mock"
"github.com/ovechkin-dm/mockio/tests/common"
"testing"

"github.com/ovechkin-dm/mockio/tests/common"

. "github.com/ovechkin-dm/mockio/mock"
)

type myInterface interface {
Expand Down
8 changes: 5 additions & 3 deletions tests/variadic/variadic_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package variadic

import (
. "github.com/ovechkin-dm/mockio/mock"
"github.com/ovechkin-dm/mockio/tests/common"
"testing"

"github.com/ovechkin-dm/mockio/tests/common"

. "github.com/ovechkin-dm/mockio/mock"
)

type myInterface interface {
Expand Down Expand Up @@ -34,4 +36,4 @@ func TestCaptor(t *testing.T) {
r.AssertEqual(c1.Last(), 1)
r.AssertEqual(c2.Last(), 2)
r.AssertNoError()
}
}
Loading

0 comments on commit 81d1f2c

Please sign in to comment.