Skip to content

Commit

Permalink
fix panic when analyzing declarations only
Browse files Browse the repository at this point in the history
  • Loading branch information
engelmi committed Nov 16, 2022
1 parent 61fa1b0 commit ae40751
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
16 changes: 15 additions & 1 deletion pkg/domain/goModuleAnalyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/fs"
"path/filepath"
"regexp"
"runtime/debug"
"strings"

"github.com/GoTestTools/limgo/pkg/model/gosrc"
Expand All @@ -28,7 +29,15 @@ func NewModuleAnalyzer(excludes []string) ModuleAnalyzer {
return nil, err
}

currentSrcFile := ""
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Errorf("analyzing '%s' failed: %s\n%s", currentSrcFile, r, string(debug.Stack())))
}
}()

for _, goSrcFile := range srcFiles {
currentSrcFile = goSrcFile.Path
functions, err := ExploreFunctions(goSrcFile.Path)
if err != nil {
return nil, errors.New(fmt.Errorf("failed opening '%s': %w", goSrcFile, err))
Expand Down Expand Up @@ -74,8 +83,13 @@ func exploreGeneralDeclaration(genDecl *ast.GenDecl, fs *token.FileSet) []gosrc.
case *ast.ValueSpec:

for i := range specType.Names {
var value ast.Expr
// check if it is only declared and not assigned
if len(specType.Values) <= i {
continue
}
value = specType.Values[i]
name := specType.Names[i].Name
value := specType.Values[i]

// only add and explore if it is a function
if _, ok := value.(*ast.FuncLit); !ok {
Expand Down
7 changes: 6 additions & 1 deletion pkg/domain/goModuleAnalyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func TestExploreFunctions(t *testing.T) {
expectErr: false,
},
{
name: "someother",
name: "Successfully analyzes example with multiple variable declaration, incl. func assigment",
file: "func_decl_go",
expectFuns: []gosrc.Function{
{
Expand Down Expand Up @@ -485,6 +485,11 @@ func TestExploreFunctions(t *testing.T) {
},
},
},
{
name: "Skips variable declarations with no assignment on file-level",
file: "only_declaration_go",
expectFuns: nil,
},
}

for _, testcase := range testcases {
Expand Down
3 changes: 3 additions & 0 deletions pkg/domain/testdata/goModuleAnalyzer/func_decl_go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ var DoSth, DoSthElse = func() {
}, func() {
fmt.Println("This is Hello World from DoSthElse")
}

var Push bool
var Build bool
4 changes: 4 additions & 0 deletions pkg/domain/testdata/goModuleAnalyzer/only_declaration_go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package testdata

var Push bool
var Build bool

0 comments on commit ae40751

Please sign in to comment.