From 24b3ef1d37af71b48553a27631aae8bc3ca51597 Mon Sep 17 00:00:00 2001 From: rappleyard_depoel Date: Tue, 2 Apr 2019 14:32:02 +0100 Subject: [PATCH] Issue #217: handling of _test.go files If a directory is passed to the command line tool, the directory includes test files, and those test files specify types that the tool is interested in (e.g. struct types) then it will trigger a compiler error, fail to generate, and leave some detritus in the target directory. We should filter out test files from consideration to prevent this error from occuring. Future development could include a command line option to process test files, if anyone wants that (unlikely). --- parser/parser.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/parser/parser.go b/parser/parser.go index 3639ed0..6f7cc22 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -5,6 +5,7 @@ import ( "go/ast" "go/parser" "go/token" + "os" "os/exec" "strings" ) @@ -73,7 +74,7 @@ func (p *Parser) Parse(fname string, isDir bool) error { fset := token.NewFileSet() if isDir { - packages, err := parser.ParseDir(fset, fname, nil, parser.ParseComments) + packages, err := parser.ParseDir(fset, fname, excludeTestFiles, parser.ParseComments) if err != nil { return err } @@ -96,3 +97,7 @@ func getDefaultGoPath() (string, error) { output, err := exec.Command("go", "env", "GOPATH").Output() return string(bytes.TrimSpace(output)), err } + +func excludeTestFiles(fi os.FileInfo) bool { + return !strings.HasSuffix(fi.Name(), "_test.go") +}