Skip to content

Commit

Permalink
ignore go:generate directives for other tools
Browse files Browse the repository at this point in the history
- fixes #137
  • Loading branch information
joefitzgerald committed Jul 19, 2019
1 parent c570f95 commit 35e91c8
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 13 deletions.
47 changes: 34 additions & 13 deletions command/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,34 @@ func invocations(cwd string, generateMode bool) ([]Invocation, error) {
return result, nil
}

var re = regexp.MustCompile(`(?mi)^//(go:generate|counterfeiter:generate)\s*(?:go run github\.com/maxbrunsfeld/counterfeiter/v6|gobin -m -run github\.com/maxbrunsfeld/counterfeiter/v6|counterfeiter|counterfeiter.exe)?\s*(.*)?\s*$`)
var directive = regexp.MustCompile(`(?mi)^//(go:generate|counterfeiter:generate)\s*(.*)?\s*$`)
var args = regexp.MustCompile(`(?mi)^(?:go run github\.com/maxbrunsfeld/counterfeiter/v6|gobin -m -run github\.com/maxbrunsfeld/counterfeiter/v6|counterfeiter|counterfeiter.exe)\s*(.*)?\s*$`)

type match struct {
directive string
args []string
}

func matchForString(s string) *match {
m := directive.FindStringSubmatch(s)
if m == nil {
return nil
}
if m[1] == "counterfeiter:generate" {
return &match{
directive: m[1],
args: stringToArgs(m[2]),
}
}
m2 := args.FindStringSubmatch(m[2])
if m2 == nil {
return nil
}
return &match{
directive: m[1],
args: stringToArgs(m2[1]),
}
}

func open(dir string, file string, generateMode bool) ([]Invocation, error) {
str, err := ioutil.ReadFile(filepath.Join(dir, file))
Expand All @@ -101,20 +128,20 @@ func open(dir string, file string, generateMode bool) ([]Invocation, error) {
line := 0
for i := range lines {
line++
match := re.FindStringSubmatch(lines[i])
match := matchForString(lines[i])
if match == nil {
continue
}
inv, err := NewInvocation(file, line, stringToArgs(match[2]))
inv, err := NewInvocation(file, line, match.args)
if err != nil {
return nil, err
}

if generateMode && match[1] == "counterfeiter:generate" {
if generateMode && match.directive == "counterfeiter:generate" {
result = append(result, inv)
}

if !generateMode && match[1] == "go:generate" {
if !generateMode && match.directive == "go:generate" {
if len(inv.Args) == 2 && strings.EqualFold(strings.TrimSpace(inv.Args[1]), "-generate") {
continue
}
Expand All @@ -126,16 +153,10 @@ func open(dir string, file string, generateMode bool) ([]Invocation, error) {
}

func stringToArgs(s string) []string {
a := strings.Split(s, " ")
a := strings.Fields(s)
result := []string{
"counterfeiter",
}
for i := range a {
item := strings.TrimSpace(a[i])
if item == "" {
continue
}
result = append(result, item)
}
result = append(result, a...)
return result
}
68 changes: 68 additions & 0 deletions command/runner_internals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package command

import (
"log"
"testing"

. "github.com/onsi/gomega"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
)

func TestRunner(t *testing.T) {
spec.Run(t, "Regexp", testRegexp, spec.Report(report.Terminal{}))
}

type Case struct {
input string
matches bool
args []string
}

func testRegexp(t *testing.T, when spec.G, it spec.S) {
var cases []Case

it.Before(func() {
RegisterTestingT(t)
log.SetFlags(log.Llongfile)
cases = []Case{
{
input: "//go:generate counterfeiter . Intf",
matches: true,
args: []string{".", "Intf"},
},
{
input: "//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Intf",
matches: true,
args: []string{".", "Intf"},
},
{
input: "//counterfeiter:generate . Intf",
matches: true,
args: []string{".", "Intf"},
},
{
input: "//go:generate stringer -type=Enum",
matches: false,
args: []string{".", "Intf"},
},
}
})

it.Focus("splits args correctly", func() {
Expect(stringToArgs(". Intf")).To(ConsistOf([]string{"counterfeiter", ".", "Intf"}))
Expect(stringToArgs(" . Intf ")).To(ConsistOf([]string{"counterfeiter", ".", "Intf"}))
})

it("matches lines appropriately", func() {
for _, c := range cases {
result := matchForString(c.input)
if c.matches {
Expect(result).NotTo(BeNil(), c.input)
Expect(result.args).To(ConsistOf(c.args))
} else {
Expect(result).To(BeNil(), c.input)
}
}
})
}

0 comments on commit 35e91c8

Please sign in to comment.