Skip to content

Commit

Permalink
feat: Update PathMatcher adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
khalifaa55 committed Sep 5, 2024
1 parent 50c5caa commit f4f2f9a
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions internal/monitoring/utils/pathMatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,35 @@ limitations under the License.
*/
package utils

import "path/filepath"
import (
"strings"
)

// PathMatcher is a custom matcher for filepath comparison that implements the gomock.Matcher interface
type PathMatcher struct {
Expected string
}

func (m PathMatcher) Matches(x interface{}) bool {
path, ok := x.(string)
actual, ok := x.(string)
if !ok {
return false
}
// Remove drive letter if present

// Normalize paths
expectedPath := normalizePath(m.Expected)
actualPath := normalizePath(actual)

return expectedPath == actualPath
}

func normalizePath(path string) string {
// Remove drive letter
path = removeDriveLetter(path)
expected := removeDriveLetter(m.Expected)
return filepath.Clean(path) == filepath.Clean(expected)
// Replace backslashes with forward slashes
path = strings.ReplaceAll(path, "\\", "/")
// Remove leading slash if present
return strings.TrimPrefix(path, "/")
}

func (m PathMatcher) String() string {
Expand Down

0 comments on commit f4f2f9a

Please sign in to comment.