Skip to content

Commit

Permalink
Allow non capturing groups (#9)
Browse files Browse the repository at this point in the history
Fix the flags parsing to accept regexes with non capturing groups

Signed-off-by: Tom <[email protected]>
  • Loading branch information
tomfeigin authored Jan 21, 2022
1 parent 27e0a5d commit e921838
Show file tree
Hide file tree
Showing 9 changed files with 1,276 additions and 23 deletions.
51 changes: 36 additions & 15 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ import (
"golang.org/x/tools/go/analysis/passes/inspect"
)

func makeAnalyzer() *analysis.Analyzer {
cnf := Config{
RequiredAlias: make(map[string]string),
}
return &analysis.Analyzer{
Flags: flags(&cnf),
Run: func(pass *analysis.Pass) (interface{}, error) {
return runWithConfig(&cnf, pass)
},
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
}

func TestIncorrectFlags(t *testing.T) {
assertWrongAliasErr := func(msg string, err error) {
if err == nil || err.Error() != errWrongAlias.Error() {
t.Errorf("Wrong error for invalid usage[%q]: %v", msg, err)
}
}
a := makeAnalyzer()
flg := a.Flags.Lookup("alias")
assertWrongAliasErr("empty flag", flg.Value.Set(""))
assertWrongAliasErr("white space only", flg.Value.Set(" "))
assertWrongAliasErr("no colons", flg.Value.Set("no colons"))
}

func TestAnalyzer(t *testing.T) {
testdata := analysistest.TestData()

Expand Down Expand Up @@ -70,6 +96,13 @@ func TestAnalyzer(t *testing.T) {
pkg: "f",
disallowExtraAliases: true,
},
{
desc: "regexp with non capturing groups",
pkg: "g",
aliases: stringMap{
"knative.dev/serving/pkg/(?:apis/)?(\\w+)(?:/v[\\w\\d]+)?": "k$1",
},
},
}

for _, test := range testCases {
Expand All @@ -91,19 +124,7 @@ func TestAnalyzer(t *testing.T) {
t.Fatal(err, string(output))
}
}

cnf := Config{
RequiredAlias: make(map[string]string),
}
flgs := flags(&cnf)
a := &analysis.Analyzer{
Flags: flgs,
Run: func(pass *analysis.Pass) (interface{}, error) {
return runWithConfig(&cnf, pass)
},
Requires: []*analysis.Analyzer{inspect.Analyzer},
}

a := makeAnalyzer()
flg := a.Flags.Lookup("alias")
for k, v := range test.aliases {
err := flg.Value.Set(fmt.Sprintf("%s:%s", k, v))
Expand All @@ -117,8 +138,8 @@ func TestAnalyzer(t *testing.T) {
t.Fatal(err)
}

noExtraAlisesFlg := a.Flags.Lookup("no-extra-aliases")
if err := noExtraAlisesFlg.Value.Set(strconv.FormatBool(test.disallowExtraAliases)); err != nil {
noExtraAliasesFlg := a.Flags.Lookup("no-extra-aliases")
if err := noExtraAliasesFlg.Value.Set(strconv.FormatBool(test.disallowExtraAliases)); err != nil {
t.Fatal(err)
}

Expand Down
11 changes: 6 additions & 5 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
)

var errWrongAlias = errors.New("import flag must be of form path:alias")

func flags(config *Config) flag.FlagSet {
fs := flag.FlagSet{}
fs.Var(stringMap(config.RequiredAlias), "alias", "required import alias in form path:alias")
Expand All @@ -18,12 +20,11 @@ func flags(config *Config) flag.FlagSet {
type stringMap map[string]string

func (v stringMap) Set(val string) error {
spl := strings.SplitN(val, ":", 2)
if len(spl) != 2 {
return errors.New("import flag must be of form path:alias")
lastColon := strings.LastIndex(val, ":")
if lastColon <= 1 {
return errWrongAlias
}

v[spl[0]] = spl[1]
v[val[:lastColon]] = val[lastColon+1:]
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion testdata/src/d/d.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package c
package d

import (
v1alpha1 "knative.dev/serving/pkg/apis/autoscaling/v1alpha1" // want `import "knative.dev/serving/pkg/apis/autoscaling/v1alpha1" imported as "v1alpha1" but must be "autoscalingv1alpha1" according to config`
Expand Down
2 changes: 1 addition & 1 deletion testdata/src/d/d.go.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package c
package d

import (
autoscalingv1alpha1 "knative.dev/serving/pkg/apis/autoscaling/v1alpha1" // want `import "knative.dev/serving/pkg/apis/autoscaling/v1alpha1" imported as "v1alpha1" but must be "autoscalingv1alpha1" according to config`
Expand Down
2 changes: 1 addition & 1 deletion testdata/src/d/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/julz/importas/testdata/src/c
module github.com/julz/importas/testdata/src/d

go 1.15

Expand Down
10 changes: 10 additions & 0 deletions testdata/src/g/g.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package g

import (
v1 "knative.dev/serving/pkg/apis/serving/v1" // want `import "knative.dev/serving/pkg/apis/serving/v1" imported as "v1" but must be "kserving" according to config`
knative1 "knative.dev/serving/pkg/queue" // want `import "knative.dev/serving/pkg/queue" imported as "knative1" but must be "kqueue" according to config`
)

func foo() {
v1.Resource(knative1.Name)
}
10 changes: 10 additions & 0 deletions testdata/src/g/g.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package g

import (
kserving "knative.dev/serving/pkg/apis/serving/v1" // want `import "knative.dev/serving/pkg/apis/serving/v1" imported as "v1" but must be "kserving" according to config`
kqueue "knative.dev/serving/pkg/queue" // want `import "knative.dev/serving/pkg/queue" imported as "knative1" but must be "kqueue" according to config`
)

func foo() {
kserving.Resource(kqueue.Name)
}
5 changes: 5 additions & 0 deletions testdata/src/g/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/julz/importas/testdata/src/g

go 1.15

require knative.dev/serving v0.21.0
Loading

0 comments on commit e921838

Please sign in to comment.