-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a47fcc
commit b292ea4
Showing
4 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package pgsgo | ||
|
||
import ( | ||
"strings" | ||
|
||
"golang.org/x/tools/imports" | ||
|
||
pgs "github.com/lyft/protoc-gen-star" | ||
) | ||
|
||
type goImports struct{} | ||
|
||
// GoImports returns a PostProcessor that run goimports on any files ending . ".go" | ||
func GoImports() pgs.PostProcessor { return goImports{} } | ||
|
||
func (g goImports) Match(a pgs.Artifact) bool { | ||
var n string | ||
|
||
switch a := a.(type) { | ||
case pgs.GeneratorFile: | ||
n = a.Name | ||
case pgs.GeneratorTemplateFile: | ||
n = a.Name | ||
case pgs.CustomFile: | ||
n = a.Name | ||
case pgs.CustomTemplateFile: | ||
n = a.Name | ||
default: | ||
return false | ||
} | ||
|
||
return strings.HasSuffix(n, ".go") | ||
} | ||
|
||
func (g goImports) Process(in []byte) ([]byte, error) { | ||
// We do not want to give a filename here, ever. | ||
return imports.Process("", in, nil) | ||
} | ||
|
||
var _ pgs.PostProcessor = goImports{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package pgsgo | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
pgs "github.com/lyft/protoc-gen-star" | ||
) | ||
|
||
func TestGoImports_Match(t *testing.T) { | ||
t.Parallel() | ||
|
||
pp := GoImports() | ||
|
||
tests := []struct { | ||
n string | ||
a pgs.Artifact | ||
m bool | ||
}{ | ||
{"GenFile", pgs.GeneratorFile{Name: "foo.go"}, true}, | ||
{"GenFileNonGo", pgs.GeneratorFile{Name: "bar.txt"}, false}, | ||
|
||
{"GenTplFile", pgs.GeneratorTemplateFile{Name: "foo.go"}, true}, | ||
{"GenTplFileNonGo", pgs.GeneratorTemplateFile{Name: "bar.txt"}, false}, | ||
|
||
{"CustomFile", pgs.CustomFile{Name: "foo.go"}, true}, | ||
{"CustomFileNonGo", pgs.CustomFile{Name: "bar.txt"}, false}, | ||
|
||
{"CustomTplFile", pgs.CustomTemplateFile{Name: "foo.go"}, true}, | ||
{"CustomTplFileNonGo", pgs.CustomTemplateFile{Name: "bar.txt"}, false}, | ||
|
||
{"NonMatch", pgs.GeneratorAppend{FileName: "foo.go"}, false}, | ||
} | ||
|
||
for _, test := range tests { | ||
tc := test | ||
t.Run(tc.n, func(t *testing.T) { | ||
t.Parallel() | ||
assert.Equal(t, tc.m, pp.Match(tc.a)) | ||
}) | ||
} | ||
} | ||
|
||
func TestGoImports_Process(t *testing.T) { | ||
t.Parallel() | ||
|
||
src := []byte("package foo\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n)\n\nfunc Hello() {\n\tfmt.Println(\"Hello\")\n}\n") | ||
exp := []byte("package foo\n\nimport (\n\t\"fmt\"\n)\n\nfunc Hello() {\n\tfmt.Println(\"Hello\")\n}\n") | ||
|
||
out, err := GoImports().Process(src) | ||
assert.NoError(t, err) | ||
assert.Equal(t, string(exp), string(out)) | ||
} |