Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Jan 20, 2025
1 parent 52a82ea commit 30cc8c1
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tasks:
sources:
- "**/*.go"
cmds:
- go fmt ./...
- gofumpt -l -w .

mocks:
desc: generate new mocks from scratch
Expand Down
6 changes: 2 additions & 4 deletions internal/cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import (
"golang.org/x/tools/go/packages"
)

var (
ErrCfgFileNotFound = errors.New("config file not found")
)
var ErrCfgFileNotFound = errors.New("config file not found")

func NewRootCmd() (*cobra.Command, error) {
var pFlags *pflag.FlagSet
Expand Down Expand Up @@ -82,7 +80,7 @@ func Execute() {
if err != nil {
os.Exit(1)
}
if cmd.Execute(); err != nil {
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
Expand Down
5 changes: 4 additions & 1 deletion internal/cmd/showconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func NewShowConfigCmd() *cobra.Command {
}

k := koanf.New("|")
k.Load(structs.Provider(conf, "koanf"), nil)
if err := k.Load(structs.Provider(conf, "koanf"), nil); err != nil {
log.Err(err).Msg("failed to load config")
return err
}
b, _ := k.Marshal(koanfYAML.Parser())
fmt.Println(string(b))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func Test(t *testing.T) {
}
res := s.ExecDoB()
assert.Equal(t, mockInterfaceB, res)

})
t.Run("ExecDoB0", func(t *testing.T) {
mockInterfaceB0 := NewMockinterfaceB0(t)
Expand All @@ -49,7 +48,6 @@ func Test(t *testing.T) {
}
res := s.ExecDoB0()
assert.Equal(t, mockInterfaceB0, res)

})
t.Run("ExecDoB0v2", func(t *testing.T) {
mockInterfaceB0 := NewMockinterfaceB0(t)
Expand All @@ -61,6 +59,5 @@ func Test(t *testing.T) {
}
res := s.ExecDoB0v2()
assert.Equal(t, mockInterfaceB0, res)

})
}
9 changes: 5 additions & 4 deletions internal/fixtures/requester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestRequesterMock(t *testing.T) {
m.EXPECT().Get("foo").Return("bar", nil).Once()
retString, err := m.Get("foo")
assert.NoError(t, err)
assert.Equal(t, retString, "bar")
assert.Equal(t, "bar", retString)
}

func TestRequesterMockRunAndReturn(t *testing.T) {
Expand All @@ -23,7 +23,7 @@ func TestRequesterMockRunAndReturn(t *testing.T) {
})
retString, err := m.Get("hello")
assert.NoError(t, err)
assert.Equal(t, retString, "hello world")
assert.Equal(t, "hello world", retString)
}

func TestRequesterMockRun(t *testing.T) {
Expand All @@ -34,15 +34,16 @@ func TestRequesterMockRun(t *testing.T) {
})
retString, err := m.Get("hello")
assert.NoError(t, err)
assert.Equal(t, retString, "")
assert.Equal(t, "", retString)
}

//nolint:errcheck
func TestRequesterMockTestifyEmbed(t *testing.T) {
m := NewMockRequester(t)
m.EXPECT().Get(mock.Anything).Return("", nil).Twice()
m.Get("hello")
m.Get("world")
assert.Equal(t, len(m.Mock.Calls), 2)
assert.Len(t, m.Mock.Calls, 2)
}

func TestRequesterMoq(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions internal/template_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
type Formatter string

const (
FORMAT_GOFMT Formatter = "gofmt"
FORMAT_GOIMPORRTS Formatter = "goimports"
FORMAT_NOOP Formatter = "noop"
FormatGofmt Formatter = "gofmt"
FormatGoImports Formatter = "goimports"
FormatNoop Formatter = "noop"
)

var (
Expand Down Expand Up @@ -166,11 +166,11 @@ func NewTemplateGenerator(

func (g *TemplateGenerator) format(src []byte) ([]byte, error) {
switch g.formatter {
case FORMAT_GOIMPORRTS:
case FormatGoImports:
return goimports(src)
case FORMAT_GOFMT:
case FormatGofmt:
return gofmt(src)
case FORMAT_NOOP:
case FormatNoop:
return src, nil
}

Expand Down
26 changes: 18 additions & 8 deletions template/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,19 @@ func NewRootConfig(
}
k := koanf.New("|")
rootConfig.koanf = k
k.Set("dir", "{{.InterfaceDir}}")
k.Set("filename", "mocks_test.go")
k.Set("formatter", "goimports")
k.Set("mockname", "Mock{{.InterfaceName}}")
k.Set("pkgname", "{{.SrcPackageName}}")
k.Set("log-level", "info")
for key, val := range map[string]string{
"dir": "{{.InterfaceDir}}",
"filename": "mocks_test.go",
"formatter": "goimports",
"mockname": "Mock{{.InterfaceName}}",
"pkgname": "{{.SrcPackageName}}",
"log-level": "info",
} {
if err := k.Set(key, val); err != nil {
log.Err(err).Msg("failed to set default value")
return nil, nil, stackerr.NewStackErr(err)
}
}

configFileFromEnv := os.Getenv("MOCKERY_CONFIG")
if configFileFromEnv != "" {
Expand All @@ -111,15 +118,18 @@ func NewRootConfig(
log.Debug().Str("config-file", configFile.String()).Msg("config file found")
}
rootConfig.configFile = configFile
k.Load(
if err := k.Load(
env.Provider(
"MOCKERY_",
".",
func(s string) string {
return strings.Replace(strings.ToLower(strings.TrimPrefix(s, "MOCKERY_")), "_", "-", -1)
}),
nil,
)
); err != nil {
log.Err(err).Msg("failed to load environment provider")
return nil, nil, stackerr.NewStackErr(err)
}

if err := k.Load(file.Provider(configFile.String()), koanfYAML.Parser()); err != nil {
return nil, k, fmt.Errorf("loading config file: %w", err)
Expand Down
1 change: 1 addition & 0 deletions template/method_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (m *MethodScope) AddVar(ctx context.Context, vr *types.Var, prefix string,
moqPkgPath: m.moqPkgPath,
}
} else {
//nolint:contextcheck
imports = m.populateImports(context.Background(), vr.Type())
v = Var{
vr: vr,
Expand Down
7 changes: 0 additions & 7 deletions template/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,6 @@ func (p Package) uniqueName(lvl int) string {
return name
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

func reverse(a []string) {
for i := len(a)/2 - 1; i >= 0; i-- {
opp := len(a) - 1 - i
Expand Down
1 change: 1 addition & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ var TemplateMockFuncs = template.FuncMap{
},
}

//nolint:predeclared
var StringManipulationFuncs = template.FuncMap{
// String inspection and manipulation. Note that the first argument is replaced
// as the last argument in some functions in order to support chained
Expand Down
1 change: 1 addition & 0 deletions tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import (
_ "github.com/go-task/task/v3/cmd/task"
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
_ "gotest.tools/gotestsum"
_ "mvdan.cc/gofumpt"
)

0 comments on commit 30cc8c1

Please sign in to comment.