Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed wrong root identification in module detection #179

13 changes: 12 additions & 1 deletion utils/techutils/techutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,24 @@ func getExistingRootDir(path string, workingDirectoryToIndicators map[string][]s
for wd := range workingDirectoryToIndicators {
parentWd := filepath.Dir(wd)
parentRoot := filepath.Dir(root)
if parentRoot != parentWd && strings.HasPrefix(root, wd) {
if parentRoot != parentWd && hasCompletePathPrefix(root, wd) {
root = wd
}
}
return
}

func hasCompletePathPrefix(root, wd string) bool {
if strings.HasPrefix(root, wd) {
amountOfSkippedChars := len(wd)
partialRoot := root[amountOfSkippedChars:]
if partialRoot == "" || partialRoot[0] == filepath.Separator {
return true
}
}
eranturgeman marked this conversation as resolved.
Show resolved Hide resolved
return false
}

// DetectedTechnologiesToSlice returns a string slice that includes all the names of the detected technologies.
func DetectedTechnologiesToSlice(detected map[Technology]map[string][]string) []string {
keys := make([]string, 0, len(detected))
Expand Down
11 changes: 11 additions & 0 deletions utils/techutils/techutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ func TestGetExistingRootDir(t *testing.T) {
},
expected: "directory",
},
{
// This test case checks that we capture the correct root when sub is a prefix to root, but not an actual path prefix
// Example: root = "dir1/dir2", sub = "dir" -> root indeed starts with 'dir' but 'dir' is not a path prefix to the root
name: "match root when root's letters start with sub's letters",
path: filepath.Join("dir3", "dir"),
workingDirectoryToIndicators: map[string][]string{
filepath.Join("dir3", "dir"): {filepath.Join("dir3", "dir", "package.json")},
"dir": {filepath.Join("dir", "package.json")},
},
expected: filepath.Join("dir3", "dir"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
Loading