Skip to content

Commit

Permalink
Add test for infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Apr 5, 2023
1 parent 9f470cf commit 2bf7a1f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
6 changes: 5 additions & 1 deletion pkg/outputter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"github.com/vektra/mockery/v2/pkg/logging"
)

var (
ErrInfiniteLoop = fmt.Errorf("infintie loop in template variables detected")
)

type Cleanup func() error

type OutputStreamProvider interface {
Expand Down Expand Up @@ -175,7 +179,7 @@ func parseConfigTemplates(ctx context.Context, c *config.Config, iface *Interfac
l := log.With().Str("variable-name", key).Str("variable-value", *val).Logger()
l.Error().Msg("config variable value")
}
return fmt.Errorf("infintie loop in template variables detected")
return ErrInfiniteLoop
}
// Templated variables can refer to other templated variables,
// so we need to continue parsing the templates until it can't
Expand Down
37 changes: 32 additions & 5 deletions pkg/outputter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"context"
"errors"
"reflect"
"testing"

Expand Down Expand Up @@ -79,15 +80,16 @@ func Test_parseConfigTemplates(t *testing.T) {
iface *Interface
}
tests := []struct {
name string
args args
name string
args args
disableWantCheck bool

// pkg is used to generate a mock types.Package object.
// It has to take in the testing.T object so we can
// assert expectations.
pkg func(t *testing.T) *pkgMocks.TypesPackage
want *config.Config
wantErr bool
wantErr error
}{
{
name: "standards",
Expand Down Expand Up @@ -117,15 +119,40 @@ func Test_parseConfigTemplates(t *testing.T) {
Outpkg: "packageName",
},
},
{
name: "infinite loop in template variables",
args: args{
c: &config.Config{
Dir: "{{.InterfaceDir}}/{{.PackagePath}}",
FileName: "{{.MockName}}.go",
MockName: "Mock{{.MockName}}",
Outpkg: "{{.PackageName}}",
},

iface: &Interface{
Name: "FooBar",
FileName: "path/to/foobar.go",
},
},
pkg: func(t *testing.T) *pkgMocks.TypesPackage {
m := pkgMocks.NewTypesPackage(t)
m.EXPECT().Path().Return("github.com/user/project/package")
m.EXPECT().Name().Return("packageName")
return m
},
disableWantCheck: true,
wantErr: ErrInfiniteLoop,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.iface.Pkg = tt.pkg(t)

if err := parseConfigTemplates(context.Background(), tt.args.c, tt.args.iface); (err != nil) != tt.wantErr {
err := parseConfigTemplates(context.Background(), tt.args.c, tt.args.iface)
if !errors.Is(err, tt.wantErr) {
t.Errorf("parseConfigTemplates() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(tt.args.c, tt.want) {
if !tt.disableWantCheck && !reflect.DeepEqual(tt.args.c, tt.want) {
t.Errorf("*config.Config = %v, want %v", tt.args.c, tt.want)
}
})
Expand Down

0 comments on commit 2bf7a1f

Please sign in to comment.