Skip to content

Commit

Permalink
Fix mapping pom files to class directories
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked authored and ProgHaj committed Jun 13, 2023
1 parent b52bdff commit 3cf48a6
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 47 deletions.
54 changes: 25 additions & 29 deletions internal/io/finder/refiner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package finder

import (
"fmt"
"os"
"path/filepath"
"strings"
)
Expand Down Expand Up @@ -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 {
Expand Down
77 changes: 59 additions & 18 deletions internal/io/finder/refiner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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)
}
Expand Down

0 comments on commit 3cf48a6

Please sign in to comment.