Manager: allow backslashes in variables

Windows machines should be able to simply use backslashes.
This commit is contained in:
Sybren A. Stüvel 2022-08-25 13:59:02 +02:00
parent f82ebea11d
commit 6b4b205c1c
3 changed files with 46 additions and 11 deletions

View File

@ -378,15 +378,7 @@ func (c *Conf) constructVariableLookupTableForVars(vars map[string]Variable) {
// Given a variable 'apps' with value '/path/to/apps',
// '/path/to/apps/blender' should be remapped to '{apps}/blender'.
if variable.IsTwoWay {
if strings.Contains(value.Value, "\\") {
log.Warn().
Str("variable", name).
Str("audience", string(value.Audience)).
Str("platform", string(value.Platform)).
Str("value", value.Value).
Msg("Backslash found in variable value. Change paths to use forward slashes instead.")
}
value.Value = strings.TrimRight(value.Value, "/")
value.Value = crosspath.TrimTrailingSep(value.Value)
}
if value.Platform != "" {

View File

@ -149,3 +149,16 @@ func pathSepForPlatform(platform string) string {
func isPathSep(r rune) bool {
return r == '/' || r == '\\'
}
// TrimTrailingSep removes any trailling path separator.
func TrimTrailingSep(path string) string {
if path == "" {
return ""
}
trimmed := strings.TrimRightFunc(path, isPathSep)
if trimmed == "" {
return string([]rune(path)[0])
}
return trimmed
}

View File

@ -1,7 +1,5 @@
package crosspath
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"path"
"path/filepath"
@ -237,3 +235,35 @@ func TestToPlatform(t *testing.T) {
})
}
}
func TestTrimTrailingSep(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
args args
want string
}{
{"empty", args{""}, ""},
{"single-slash", args{`/`}, `/`},
{"single-backslash", args{`\`}, `\`},
{"multiple-slashes", args{`///`}, `/`},
{"multiple-backslashes", args{`\\\`}, `\`},
{"multiple-mixed-slash-first", args{`/\\`}, `/`},
{"multiple-mixed-backslash-first", args{`\//`}, `\`},
{"nothing-trailing", args{`nothing/trailing\here`}, `nothing/trailing\here`},
{"trailing-space", args{`trailing/space/ `}, `trailing/space/ `},
{"trailing-slash", args{`trailing/slash/`}, `trailing/slash`},
{"trailing-slashes", args{`trailing/slashes///`}, `trailing/slashes`},
{"trailing-backslash", args{`trailing\backslash\`}, `trailing\backslash`},
{"trailing-backslashes", args{`trailing\backslashes\\\`}, `trailing\backslashes`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := TrimTrailingSep(tt.args.path); got != tt.want {
t.Errorf("TrimTrailingSep() = %v, want %v", got, tt.want)
}
})
}
}