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

locspec: fix SubstitutePath when converting a Windows path to Linux #3453

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions pkg/locspec/locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,49 @@ func hasPathSeparatorPrefix(path string) bool {
return strings.HasPrefix(path, "/") || strings.HasPrefix(path, "\\")
}

func pickSeparator(to string) string {
var sep byte
for i := range to {
if to[i] == '/' || to[i] == '\\' {
if sep == 0 {
sep = to[i]
} else if sep != to[i] {
return ""
}
}
}
return string(sep)
}

func joinPath(to, rest string) string {
sep := pickSeparator(to)

switch sep {
case "/":
rest = strings.ReplaceAll(rest, "\\", sep)
case "\\":
rest = strings.ReplaceAll(rest, "/", sep)
default:
sep = "/"
}

toEndsWithSlash := hasPathSeparatorSuffix(to)
restStartsWithSlash := hasPathSeparatorPrefix(rest)

switch {
case toEndsWithSlash && restStartsWithSlash:
return to[:len(to)-1] + rest
case toEndsWithSlash && !restStartsWithSlash:
return to + rest
case !toEndsWithSlash && restStartsWithSlash:
return to + rest
case !toEndsWithSlash && !restStartsWithSlash:
fallthrough
default:
return to + sep + rest
}
}

// SubstitutePath applies the specified path substitution rules to path.
func SubstitutePath(path string, rules [][2]string) string {
// Look for evidence that we are dealing with windows somewhere, if we are use case-insensitive matching
Expand Down Expand Up @@ -559,19 +602,7 @@ func SubstitutePath(path string, rules [][2]string) string {
return rest
}

toEndsWithSlash := hasPathSeparatorSuffix(to)
restStartsWithSlash := hasPathSeparatorPrefix(rest)

switch {
case toEndsWithSlash && restStartsWithSlash:
return to[:len(to)-1] + rest
case toEndsWithSlash && !restStartsWithSlash:
return to + rest
case !toEndsWithSlash && restStartsWithSlash:
return to + rest
case !toEndsWithSlash && !restStartsWithSlash:
return to + "/" + rest
}
return joinPath(to, rest)
}
}
return path
Expand Down
6 changes: 5 additions & 1 deletion pkg/locspec/locations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,23 @@ func platformCases() []tCase {
{[]tRule{{`c:\tmp\path`, `d:\new\path2`}}, `c:\tmp\path\file.go`, `d:\new\path2\file.go`},
{[]tRule{{`c:\tmp\path\`, `d:\new\path2\`}}, `c:\tmp\path\file.go`, `d:\new\path2\file.go`},
{[]tRule{{`c:\tmp\path`, `d:\new\path2\`}}, `c:\tmp\path\file.go`, `d:\new\path2\file.go`},
{[]tRule{{`c:\tmp\path\`, `d:\new\path2`}}, `c:\tmp\path\file.go`, `d:\new\path2/file.go`},
{[]tRule{{`c:\tmp\path\`, `d:\new\path2`}}, `c:\tmp\path\file.go`, `d:\new\path2\file.go`},
// Should apply to directory prefixes
{[]tRule{{`c:\tmp\path`, `d:\new\path2`}}, `c:\tmp\path-2\file.go`, `c:\tmp\path-2\file.go`},
// Should apply to exact matches
{[]tRule{{`c:\tmp\path\file.go`, `d:\new\path2\file2.go`}}, `c:\tmp\path\file.go`, `d:\new\path2\file2.go`},
// Should be case-insensitive
{[]tRule{{`c:\tmp\path`, `d:\new\path2`}}, `C:\TmP\PaTh\file.go`, `d:\new\path2\file.go`},
}
casesCross := []tCase{
{[]tRule{{"C:\\some\\repo", "/go/src/github.com/some/repo/"}}, `c:\some\repo\folder\file.go`, "/go/src/github.com/some/repo/folder/file.go"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about "C:\some\repo" (single backslash)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\\ inside double quotes is a single backslash

}

r := append(casesUnix, casesLinux...)
r = append(r, casesFreebsd...)
r = append(r, casesDarwin...)
r = append(r, casesWindows...)
r = append(r, casesCross...)

return r
}
Expand Down