From 42c509debd1b167e32d79b5738067ed9a3b4492d Mon Sep 17 00:00:00 2001 From: Adam Connelly Date: Tue, 28 Nov 2023 10:44:29 -0800 Subject: [PATCH] feat: remove package flag Removing the package flag for now. It's not needed when just generating mocks based on source files. It may be needed later for generating mocks for external packages, but if so we can add it back. --- README.md | 2 +- cmd/kelpie/main.go | 12 +++--------- examples/argument_matching_test.go | 2 +- examples/result_test.go | 2 +- parser/parser.go | 6 +++--- parser/parser_test.go | 14 +++++++------- 6 files changed, 16 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 73bc551..a28298d 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ go install github.com/adamconnelly/kelpie/cmd/kelpie@latest Add a `go:generate` marker to the interface you want to mock: ```go -//go:generate kelpie generate --source-file .go --package --interfaces EmailService +//go:generate kelpie generate --source-file .go --interfaces EmailService type EmailService interface { Send(sender, recipient, body string) (cost float64, err error) } diff --git a/cmd/kelpie/main.go b/cmd/kelpie/main.go index 13cceba..c44c858 100644 --- a/cmd/kelpie/main.go +++ b/cmd/kelpie/main.go @@ -11,17 +11,13 @@ import ( "github.com/pkg/errors" "github.com/adamconnelly/kelpie/parser" - "github.com/adamconnelly/kelpie/slices" ) //go:embed "mock.go.tmpl" var mockTemplate string type GenerateCmd struct { - SourceFile string `short:"s" required:"" help:"The Go source file containing the interface to mock."` - - // TODO: maybe we don't need the package name? - Package string `short:"p" required:"" help:"The package containing the interface to mock."` + SourceFile string `short:"s" required:"" help:"The Go source file containing the interface to mock."` Interfaces []string `short:"i" required:"" help:"The names of the interfaces to mock."` OutputDir string `short:"o" required:"" default:"mocks" help:"The directory to write the mock out to."` } @@ -33,12 +29,10 @@ func (g *GenerateCmd) Run() error { } filter := parser.IncludingInterfaceFilter{ - InterfacesToInclude: slices.Map( - g.Interfaces, - func(i string) string { return fmt.Sprintf("%s.%s", g.Package, i) }), + InterfacesToInclude: g.Interfaces, } - mockedInterfaces, err := parser.Parse(file, g.Package, &filter) + mockedInterfaces, err := parser.Parse(file, &filter) if err != nil { return errors.Wrap(err, "could not parse file") } diff --git a/examples/argument_matching_test.go b/examples/argument_matching_test.go index f27d0ef..9216755 100644 --- a/examples/argument_matching_test.go +++ b/examples/argument_matching_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/suite" ) -//go:generate go run ../cmd/kelpie generate --source-file argument_matching_test.go --package github.com/adamconnelly/kelpie/examples --interfaces Maths +//go:generate go run ../cmd/kelpie generate --source-file argument_matching_test.go --interfaces Maths type Maths interface { Add(a, b int) int ParseInt(input string) (int, error) diff --git a/examples/result_test.go b/examples/result_test.go index f1543a0..1b8acdc 100644 --- a/examples/result_test.go +++ b/examples/result_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/suite" ) -//go:generate go run ../cmd/kelpie generate --source-file result_test.go --package github.com/adamconnelly/kelpie/examples --interfaces AccountService +//go:generate go run ../cmd/kelpie generate --source-file result_test.go --interfaces AccountService type AccountService interface { SendActivationEmail(emailAddress string) bool DisableAccount(id uint) diff --git a/parser/parser.go b/parser/parser.go index 599177c..8a65cfe 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -32,7 +32,7 @@ type ResultDefinition struct { Type string } -//go:generate go run ../cmd/kelpie generate --source-file parser.go --package github.com/adamconnelly/kelpie/parser --interfaces InterfaceFilter +//go:generate go run ../cmd/kelpie generate --source-file parser.go --interfaces InterfaceFilter type InterfaceFilter interface { // Include indicates that the specified interface should be included in the set of interfaces // to generate. @@ -49,7 +49,7 @@ func (f *IncludingInterfaceFilter) Include(name string) bool { }) } -func Parse(reader io.Reader, packageName string, filter InterfaceFilter) ([]MockedInterface, error) { +func Parse(reader io.Reader, filter InterfaceFilter) ([]MockedInterface, error) { var interfaces []MockedInterface fileSet := token.NewFileSet() @@ -60,7 +60,7 @@ func Parse(reader io.Reader, packageName string, filter InterfaceFilter) ([]Mock if t, ok := n.(*ast.TypeSpec); ok { if t.Name.IsExported() { if typeSpecType, ok := t.Type.(*ast.InterfaceType); ok { - if filter.Include(packageName + "." + t.Name.Name) { + if filter.Include(t.Name.Name) { mockedInterface := MockedInterface{ Name: t.Name.Name, PackageName: strings.ToLower(t.Name.Name), diff --git a/parser/parser_test.go b/parser/parser_test.go index 7e1fd8e..5ca8b45 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -35,7 +35,7 @@ type UserService interface { }` // Act - result, err := parser.Parse(strings.NewReader(input), "github.com/adamconnelly/kelpie/tests", t.interfaceFilter.Instance()) + result, err := parser.Parse(strings.NewReader(input), t.interfaceFilter.Instance()) // Assert t.NoError(err) @@ -56,10 +56,10 @@ type UserService interface { CreateUser(username string) (User, error) }` - t.interfaceFilter.Setup(interfacefilter.Include("github.com/adamconnelly/kelpie/tests.UserService").Return(false)) + t.interfaceFilter.Setup(interfacefilter.Include("UserService").Return(false)) // Act - result, err := parser.Parse(strings.NewReader(input), "github.com/adamconnelly/kelpie/tests", t.interfaceFilter.Instance()) + result, err := parser.Parse(strings.NewReader(input), t.interfaceFilter.Instance()) // Assert t.NoError(err) @@ -76,10 +76,10 @@ type NotificationService interface { BroadcastNotification(message string) (recipients int, err error) }` - t.interfaceFilter.Setup(interfacefilter.Include("github.com/adamconnelly/kelpie/tests.UserService").Return(false)) + t.interfaceFilter.Setup(interfacefilter.Include("UserService").Return(false)) // Act - result, err := parser.Parse(strings.NewReader(input), "github.com/adamconnelly/kelpie/tests", t.interfaceFilter.Instance()) + result, err := parser.Parse(strings.NewReader(input), t.interfaceFilter.Instance()) // Assert t.NoError(err) @@ -128,10 +128,10 @@ type NotificationService interface { Block(recipient string) }` - t.interfaceFilter.Setup(interfacefilter.Include("github.com/adamconnelly/kelpie/tests.UserService").Return(false)) + t.interfaceFilter.Setup(interfacefilter.Include("UserService").Return(false)) // Act - result, err := parser.Parse(strings.NewReader(input), "github.com/adamconnelly/kelpie/tests", t.interfaceFilter.Instance()) + result, err := parser.Parse(strings.NewReader(input), t.interfaceFilter.Instance()) // Assert t.NoError(err)