diff --git a/core/adapter/cocafile/file_analysis_helper.go b/core/adapter/cocafile/file_analysis_helper.go index 6b866ddc8aa6824f0eb806f0933407db5ff2daa1..f1bc20ceb6a9ca7b02d69c55c4c30bf49a158ae8 100644 --- a/core/adapter/cocafile/file_analysis_helper.go +++ b/core/adapter/cocafile/file_analysis_helper.go @@ -10,44 +10,26 @@ import ( "strings" ) -func GetJavaFiles(codeDir string) []string { - files := make([]string, 0) - gitIgnore, err := ignore.CompileIgnoreFile(".gitignore") - if err != nil { - //fmt.Println(err) - } - - fi, err := os.Stat(codeDir) - if err != nil { - fmt.Println(err) - return nil - } +var javaCodeFileFilter = func(path string) bool { + return strings.HasSuffix(path, ".java") && !strings.Contains(path, "Test.java") && !strings.Contains(path, "Tests.java") +} - if fi.Mode().IsRegular() { - files = append(files, codeDir) - return files +var javaTestFileFilter = func(path string) bool { + if strings.Contains(path, "Test.java") || strings.Contains(path, "Tests.java") { + return true } + return false +} - _ = filepath.Walk(codeDir, func(path string, fi os.FileInfo, err error) error { - if gitIgnore != nil { - if gitIgnore.MatchesPath(path) { - return nil - } - } - - if strings.HasSuffix(path, ".java") && !strings.Contains(path, "Test.java") && !strings.Contains(path, "Tests.java") { - files = append(files, path) - } - return nil - }) - return files +func GetJavaFiles(codeDir string) []string { + return GetFilesWithFilter(codeDir, javaCodeFileFilter) } -func GetJavaTestFiles(codeDir string) []string { +func GetFilesWithFilter(codeDir string, filter func(path string) bool) []string { files := make([]string, 0) gitIgnore, err := ignore.CompileIgnoreFile(".gitignore") if err != nil { - fmt.Println(err) + //fmt.Println(err) } fi, err := os.Stat(codeDir) @@ -68,7 +50,7 @@ func GetJavaTestFiles(codeDir string) []string { } } - if strings.Contains(path, "Test.java") || strings.Contains(path, "Tests.java") { + if filter(path) { files = append(files, path) } return nil @@ -76,6 +58,10 @@ func GetJavaTestFiles(codeDir string) []string { return files } +func GetJavaTestFiles(codeDir string) []string { + return GetFilesWithFilter(codeDir, javaTestFileFilter) +} + func ProcessFile(path string) *JavaParser { is, _ := antlr.NewFileStream(path) lexer := NewJavaLexer(is) diff --git a/core/context/deps/dep_app.go b/core/context/deps/dep_app.go index d14359765bc9f4ed6bf0cb4cf7412338a51dfaba..8a5a3f50461873e8eeaa9ae3569bc8b76b3e27f1 100644 --- a/core/context/deps/dep_app.go +++ b/core/context/deps/dep_app.go @@ -21,4 +21,3 @@ func (d *DepApp) BuildImportMap(deps []domain.JClassNode) map[string]domain.JImp return impMap } - diff --git a/core/context/deps/dep_app_test.go b/core/context/deps/dep_app_test.go index 4605c3f550a7b57e66b565817ca2d8928c151dd5..f4d0d2216626ae9a5f992f3fe067d7cd88c0ac08 100644 --- a/core/context/deps/dep_app_test.go +++ b/core/context/deps/dep_app_test.go @@ -78,3 +78,16 @@ func Test_ShouldCountDeps_WhenHadClassNodes(t *testing.T) { g.Expect(len(importMap)).To(Equal(25)) } + +//func Test_ListUnusedImportForOneGradleFile(t *testing.T) { +// g := NewGomegaWithT(t) +// +// codePath := "../../../_fixtures/deps/maven_sample/" +// classNodes, _, _ := cocatest.BuildAnalysisDeps(codePath) +// +// depApp := NewDepApp() +// importMap := depApp.BuildImportMap(classNodes) +// +// fmt.Println(importMap) +// g.Expect(len(importMap)).To(Equal(2)) +//}