From 377e000eddfc2d566bec1667cbf2a822e9609e97 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Sun, 15 Sep 2024 15:39:16 +0300 Subject: [PATCH 1/7] fixed wrong root identification when sub is a prefix to root but not a path prefix --- utils/techutils/techutils.go | 13 ++++++++++++- utils/techutils/techutils_test.go | 11 +++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/utils/techutils/techutils.go b/utils/techutils/techutils.go index cb537e60..1231af79 100644 --- a/utils/techutils/techutils.go +++ b/utils/techutils/techutils.go @@ -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 + } + } + 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)) diff --git a/utils/techutils/techutils_test.go b/utils/techutils/techutils_test.go index 18be1100..4ee5994b 100644 --- a/utils/techutils/techutils_test.go +++ b/utils/techutils/techutils_test.go @@ -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) { From 101d7b6f144ebbbadb9f1c7e1d83bf0c3abba555 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Sun, 15 Sep 2024 17:32:28 +0300 Subject: [PATCH 2/7] simplified and explained new logic --- utils/techutils/techutils.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/utils/techutils/techutils.go b/utils/techutils/techutils.go index 1231af79..f35a440c 100644 --- a/utils/techutils/techutils.go +++ b/utils/techutils/techutils.go @@ -492,15 +492,17 @@ func getExistingRootDir(path string, workingDirectoryToIndicators map[string][]s return } +// This functions checks if wd is a PATH prefix for root. Examples: +// root = dir1/dir2/dir3 | wd = dir1/dir --> false (wd is prefix to root, but is not actually a valid part of its path) +// root = dir1/dir2/dir3 | wd = dir1/dir2 --> true 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 - } + if !strings.HasPrefix(root, wd) { + return false } - return false + rootParts := strings.Split(root, string(filepath.Separator)) + wdParts := strings.Split(wd, string(filepath.Separator)) + idxToCheck := len(wdParts) - 1 + return rootParts[idxToCheck] == wdParts[idxToCheck] } // DetectedTechnologiesToSlice returns a string slice that includes all the names of the detected technologies. From cceca23a400c2e92480f8ec69b5303700b41f67b Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Sun, 15 Sep 2024 18:28:02 +0300 Subject: [PATCH 3/7] added test --- utils/techutils/techutils_test.go | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/utils/techutils/techutils_test.go b/utils/techutils/techutils_test.go index 4ee5994b..f81a0e8a 100644 --- a/utils/techutils/techutils_test.go +++ b/utils/techutils/techutils_test.go @@ -296,6 +296,39 @@ func TestGetExistingRootDir(t *testing.T) { } } +func TestHasCompletePathPrefix(t *testing.T) { + tests := []struct { + name string + root string + wd string + expected bool + }{ + { + name: "no prefix", + root: "dir1/dir2", + wd: "some/other/project", + expected: false, + }, + { + name: "prefix but not a path prefix", + root: "dir1/dir2", + wd: "dir", + expected: false, + }, + { + name: "path prefix", + root: "dir1/dir2", + wd: "dir1", + expected: true, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.Equal(t, test.expected, hasCompletePathPrefix(test.root, test.wd)) + }) + } +} + func TestCleanSubDirectories(t *testing.T) { tests := []struct { name string From 7d8f3414d02ecc43f1b9abfc07724a05d943d230 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Sun, 15 Sep 2024 21:20:59 +0300 Subject: [PATCH 4/7] comment --- utils/techutils/techutils_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/techutils/techutils_test.go b/utils/techutils/techutils_test.go index f81a0e8a..7f192167 100644 --- a/utils/techutils/techutils_test.go +++ b/utils/techutils/techutils_test.go @@ -536,6 +536,7 @@ func TestGetTechInformationFromWorkingDir(t *testing.T) { requestedDescriptors: map[Technology][]string{}, expected: map[string][]string{"dir": {filepath.Join("dir", "project.sln"), filepath.Join("dir", "sub1", "project.csproj")}}, }, + // todo paste here the code section from the bug } for _, test := range tests { From 7e359be041f113867a3c9bdf7d91abd87b0311a7 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Mon, 16 Sep 2024 11:07:35 +0300 Subject: [PATCH 5/7] added test case required from previous PR --- utils/techutils/techutils_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/utils/techutils/techutils_test.go b/utils/techutils/techutils_test.go index 7b1861be..8f4973a2 100644 --- a/utils/techutils/techutils_test.go +++ b/utils/techutils/techutils_test.go @@ -575,6 +575,17 @@ func TestGetTechInformationFromWorkingDir(t *testing.T) { "dir4": {filepath.Join("dir4", "package.json")}, }, }, + { + name: "pnpmTestWithProvidedTechFromUser", + tech: Pnpm, + requestedDescriptors: make(map[Technology][]string), + techProvidedByUser: true, + expected: map[string][]string{ + filepath.Join("dir3", "dir"): {filepath.Join("dir3", "dir", "package.json")}, + "dir": {filepath.Join("dir", "package.json")}, + "dir4": {filepath.Join("dir4", "package.json")}, + }, + }, } for _, test := range tests { From 1870be4d737f512929c607734f0ea4d3caf7387c Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Mon, 16 Sep 2024 11:08:20 +0300 Subject: [PATCH 6/7] added prints to check error in Windows --- utils/techutils/techutils.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/utils/techutils/techutils.go b/utils/techutils/techutils.go index 4710aa76..b0a19572 100644 --- a/utils/techutils/techutils.go +++ b/utils/techutils/techutils.go @@ -510,9 +510,14 @@ func hasCompletePathPrefix(root, wd string) bool { if !strings.HasPrefix(root, wd) { return false } + log.Info(fmt.Sprintf("ERAN TEST - root path: %s", root)) + log.Info(fmt.Sprintf("ERAN TEST - wd path: %s", root)) rootParts := strings.Split(root, string(filepath.Separator)) wdParts := strings.Split(wd, string(filepath.Separator)) + log.Info(fmt.Sprintf("ERAN TEST - root slice: %s", rootParts)) + log.Info(fmt.Sprintf("ERAN TEST - wd parts: %s", wdParts)) idxToCheck := len(wdParts) - 1 + log.Info(fmt.Sprintf("ERAN TEST - idx to check: %d", idxToCheck)) return rootParts[idxToCheck] == wdParts[idxToCheck] } From 2f6bf5ed62cb81177a56d4bf0b23e9f22b00c42a Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Mon, 16 Sep 2024 12:25:52 +0300 Subject: [PATCH 7/7] deleted prints and fixed the bug in windows --- utils/techutils/techutils.go | 5 ----- utils/techutils/techutils_test.go | 8 ++++---- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/utils/techutils/techutils.go b/utils/techutils/techutils.go index b0a19572..4710aa76 100644 --- a/utils/techutils/techutils.go +++ b/utils/techutils/techutils.go @@ -510,14 +510,9 @@ func hasCompletePathPrefix(root, wd string) bool { if !strings.HasPrefix(root, wd) { return false } - log.Info(fmt.Sprintf("ERAN TEST - root path: %s", root)) - log.Info(fmt.Sprintf("ERAN TEST - wd path: %s", root)) rootParts := strings.Split(root, string(filepath.Separator)) wdParts := strings.Split(wd, string(filepath.Separator)) - log.Info(fmt.Sprintf("ERAN TEST - root slice: %s", rootParts)) - log.Info(fmt.Sprintf("ERAN TEST - wd parts: %s", wdParts)) idxToCheck := len(wdParts) - 1 - log.Info(fmt.Sprintf("ERAN TEST - idx to check: %d", idxToCheck)) return rootParts[idxToCheck] == wdParts[idxToCheck] } diff --git a/utils/techutils/techutils_test.go b/utils/techutils/techutils_test.go index 8f4973a2..796e4d57 100644 --- a/utils/techutils/techutils_test.go +++ b/utils/techutils/techutils_test.go @@ -318,19 +318,19 @@ func TestHasCompletePathPrefix(t *testing.T) { }{ { name: "no prefix", - root: "dir1/dir2", - wd: "some/other/project", + root: filepath.Join("dir1", "dir2"), + wd: filepath.Join("some", "other", "project"), expected: false, }, { name: "prefix but not a path prefix", - root: "dir1/dir2", + root: filepath.Join("dir1", "dir2"), wd: "dir", expected: false, }, { name: "path prefix", - root: "dir1/dir2", + root: filepath.Join("dir1", "dir2"), wd: "dir1", expected: true, },