diff --git a/internal/io/finder/refiner.go b/internal/io/finder/refiner.go index dbd2d1e9..11147a48 100644 --- a/internal/io/finder/refiner.go +++ b/internal/io/finder/refiner.go @@ -2,6 +2,7 @@ package finder import ( "fmt" + "os" "path/filepath" "strings" ) @@ -34,57 +35,52 @@ func ConvertPathsToAbsPaths(paths []string) ([]string, error) { return absPaths, nil } -func MapFilesToDir(dirs []string, files []string) map[string][]string { - dirToFilesMap := make(map[string][]string) +// Matches class directories to closest root pom file and creates a map +// with each root pom file pointing at a list of its related class directories +func MapFilesToDir(rootPomFiles []string, classDirs []string) map[string][]string { + pomFileToClassDirsMap := make(map[string][]string) - if len(dirs) == 0 { - return dirToFilesMap + if len(rootPomFiles) == 0 { + return pomFileToClassDirsMap } - for _, file := range files { - matchingDir, err := findLongestDirMatch(file, dirs) + for _, classDir := range classDirs { + matchingPomFile, err := findPomFileMatch(classDir, rootPomFiles) if err != nil { continue } - if _, ok := dirToFilesMap[matchingDir]; !ok { - dirToFilesMap[matchingDir] = []string{} + if _, ok := pomFileToClassDirsMap[matchingPomFile]; !ok { + pomFileToClassDirsMap[matchingPomFile] = []string{} } - dirToFilesMap[matchingDir] = append(dirToFilesMap[matchingDir], file) + pomFileToClassDirsMap[matchingPomFile] = append(pomFileToClassDirsMap[matchingPomFile], classDir) } - return dirToFilesMap + return pomFileToClassDirsMap } -func findLongestDirMatch(file string, dirs []string) (string, error) { - var matchingDir string - longestMatchLength := 0 +func findPomFileMatch(classDir string, pomFiles []string) (string, error) { + var matchingPomFile string + longestSeperatorMatch := 0 matched := false - for _, dir := range dirs { - matchLength := 0 - longestSeperatorMatch := 0 - for i := 0; i < len(file) && i < len(dir); i++ { - if file[i] != dir[i] { - break - } - matchLength++ - if filepath.Separator == file[i] { - longestSeperatorMatch = matchLength + for _, pomFile := range pomFiles { + pomFilePath := strings.TrimSuffix(pomFile, "pom.xml") + if strings.Contains(classDir, pomFilePath) { + numberSeparators := strings.Count(pomFilePath, string(os.PathSeparator)) + if numberSeparators > longestSeperatorMatch { + matched = true + longestSeperatorMatch = numberSeparators + matchingPomFile = pomFile } } - if longestSeperatorMatch > longestMatchLength { - longestMatchLength = longestSeperatorMatch - matchingDir = dir - matched = true - } } if !matched { return "", fmt.Errorf("No part of the path matches") } - return matchingDir, nil + return matchingPomFile, nil } func GCDPath(paths []string) string { diff --git a/internal/io/finder/refiner_test.go b/internal/io/finder/refiner_test.go index ce1ed7ed..36e8e648 100644 --- a/internal/io/finder/refiner_test.go +++ b/internal/io/finder/refiner_test.go @@ -18,45 +18,75 @@ func TestConvertPathsToAbsPaths(t *testing.T) { } func TestMapFilesToDir(t *testing.T) { - dirs := []string{ - "test" + string(os.PathSeparator), - "test2" + string(os.PathSeparator), + filepath.Join("foo/bar/tree/target/classfiles") + string(os.PathSeparator), + filepath.Join("foo/bar/tree/asd/hej/target/classfiles") + string(os.PathSeparator), + filepath.Join("foo/bar/tree/asd/fast/target/classfiles") + string(os.PathSeparator), } files := []string{ - filepath.Join("test/asd"), - filepath.Join("test2/basd/qwe"), - filepath.Join("test2/test/asd"), - filepath.Join("test3/tes"), + filepath.Join("foo/bar/tree/pom.xml"), + filepath.Join("foo/bar/tree/asd/fast/pom.xml"), } - mapFiles := MapFilesToDir(dirs, files) + mapFiles := MapFilesToDir(files, dirs) fmt.Println(dirs) fmt.Println(files) + fmt.Println(mapFiles) - assert.Len(t, mapFiles["test"+string(os.PathSeparator)], 1) - assert.Len(t, mapFiles["test2"+string(os.PathSeparator)], 2) + assert.Len(t, mapFiles[filepath.Join("foo/bar/tree/pom.xml")], 2) + assert.Len(t, mapFiles[filepath.Join("foo/bar/tree/asd/fast/pom.xml")], 1) } -func TestFindLongestDirMatch(t *testing.T) { +func TestFindPomFileMatch(t *testing.T) { - dirs := []string{"test" + string(os.PathSeparator), "test2" + string(os.PathSeparator)} - file := filepath.Join("test", "asd") - dirMatch, err := findLongestDirMatch(file, dirs) + classDir := filepath.Join("foo/bar/tree/asd/hej/target/classfiles") + string(os.PathSeparator) + pomFiles := []string{ + filepath.Join("foo/bar/tree/pom.xml"), + filepath.Join("foo/bar/tree/asd/fast/pom.xml"), + } + pomFileMatch, err := findPomFileMatch(classDir, pomFiles) + + assert.Equal(t, filepath.Join("foo/bar/tree/pom.xml"), pomFileMatch) + assert.Nil(t, err) +} + +func TestFindPomFileMatchDifficult(t *testing.T) { + + classDir := filepath.Join("foo/bar/tree/asd/hej/target/classfiles") + string(os.PathSeparator) + pomFiles := []string{ + filepath.Join("foo/bar/tree/pom.xml"), + filepath.Join("foo/bar/tree/asd/fast/pom.xml"), + filepath.Join("foo/bar/tree/asd/hej/pom.xml"), + } + pomFileMatch, err := findPomFileMatch(classDir, pomFiles) - assert.Equal(t, "test"+string(os.PathSeparator), dirMatch) + assert.Equal(t, filepath.Join("foo/bar/tree/asd/hej/pom.xml"), pomFileMatch) assert.Nil(t, err) } -func TestFindLongestDirMatchErr(t *testing.T) { +func TestFindPomFileMatchErr(t *testing.T) { dirs := []string{"test" + string(os.PathSeparator), "test2" + string(os.PathSeparator)} file := filepath.Join("gest", "asd") - dirMatch, err := findLongestDirMatch(file, dirs) + dirMatch, err := findPomFileMatch(file, dirs) assert.Equal(t, "", dirMatch) assert.NotNil(t, err) } +func TestMapFilesToDirNoFiles(t *testing.T) { + + dirs := []string{ + filepath.Join("test/asd"), + filepath.Join("test2/basd/qwe"), + filepath.Join("test2/test/asd"), + filepath.Join("test3/tes"), + } + files := []string{} + mapFiles := MapFilesToDir(files, dirs) + + assert.Empty(t, mapFiles) +} + func TestMapFilesToEmptyDir(t *testing.T) { dirs := []string{} @@ -66,7 +96,18 @@ func TestMapFilesToEmptyDir(t *testing.T) { filepath.Join("test2/test/asd"), filepath.Join("test3/tes"), } - mapFiles := MapFilesToDir(dirs, files) + mapFiles := MapFilesToDir(files, dirs) + + assert.Empty(t, mapFiles) +} + +func TestMapFilesToDirNoMatches(t *testing.T) { + + dirs := []string{"tset/pom.xml"} + files := []string{ + filepath.Join("test/target/classes"), + } + mapFiles := MapFilesToDir(files, dirs) assert.Empty(t, mapFiles) }