Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove package flag #5

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ vet: ## Run go vet against code.

.PHONY: test
test: fmt vet ## Run tests.
go test -coverprofile cover.out
go test -coverprofile cover.out ./...

.PHONY: lint
lint: ## Run golangci-lint
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <filename>.go --package <package-name> --interfaces EmailService
//go:generate kelpie generate --source-file <filename>.go --interfaces EmailService
type EmailService interface {
Send(sender, recipient, body string) (cost float64, err error)
}
Expand Down
12 changes: 3 additions & 9 deletions cmd/kelpie/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."`
}
Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion examples/argument_matching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
Expand All @@ -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),
Expand Down
14 changes: 7 additions & 7 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading