fix windows glob issue (#1572)

This commit is contained in:
yxxhero 2024-06-26 13:03:09 +08:00 committed by GitHub
parent 0a4894f7ed
commit 59ee3ce862
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 81 additions and 13 deletions

View File

@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
@ -3230,7 +3231,7 @@ func (st *HelmState) setFlags(setValues []SetValue) ([]string, error) {
}
flags = append(flags, "--set", fmt.Sprintf("%s=%s", escape(set.Name), escape(renderedValue[0])))
} else if set.File != "" {
flags = append(flags, "--set-file", fmt.Sprintf("%s=%s", escape(set.Name), st.storage().normalizePath(set.File)))
flags = append(flags, "--set-file", fmt.Sprintf("%s=%s", escape(set.Name), st.storage().normalizeSetFilePath(set.File, runtime.GOOS)))
} else if len(set.Values) > 0 {
renderedValues, err := renderValsSecrets(st.valsRuntime, set.Values...)
if err != nil {

View File

@ -4,7 +4,6 @@ import (
"fmt"
"net/url"
"path/filepath"
"runtime"
"sort"
"strings"
@ -136,7 +135,7 @@ func (st *Storage) normalizePath(path string) string {
if u != nil && (u.Scheme != "" || filepath.IsAbs(path)) {
return path
} else {
return st.JoinBase(path, runtime.GOOS)
return st.JoinBase(path)
}
}
@ -144,10 +143,15 @@ func (st *Storage) normalizePath(path string) string {
// Helm's setFiles command does not support unescaped filepath separators (\) on Windows.
// Instead, it requires double backslashes (\\) as filepath separators.
// See https://github.com/helm/helm/issues/9537
func (st *Storage) JoinBase(relPath, GOOS string) string {
func (st *Storage) JoinBase(relPath string) string {
path := filepath.Join(st.basePath, relPath)
if GOOS == "windows" {
return strings.ReplaceAll(path, "\\", "\\\\")
}
return path
}
func (st *Storage) normalizeSetFilePath(path, goos string) string {
normalizedPath := st.normalizePath(path)
if goos == "windows" {
return strings.ReplaceAll(normalizedPath, "\\", "\\\\")
}
return normalizedPath
}

View File

@ -3,6 +3,7 @@ package state
import (
"fmt"
"io"
"path/filepath"
"reflect"
"testing"
@ -176,39 +177,101 @@ func TestJoinBase(t *testing.T) {
tests := []struct {
name string
base string
goos string
path string
want string
}{
{
name: "joinBase with non-root base",
base: "/root",
goos: "linux",
path: "local/timespan-application.yml",
want: "/local/timespan-application.yml",
},
{
name: "joinBase with root path",
base: "/",
goos: "linux",
path: "data/timespan-application.yml",
want: "/data/timespan-application.yml",
},
{
name: "windows joinBase",
base: "",
goos: "windows",
path: "data\\timespan-application.yml",
want: "data\\\\timespan-application.yml",
want: "data\\timespan-application.yml",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
storageIns := NewStorage(tt.base, helmexec.NewLogger(io.Discard, "debug"), filesystem.DefaultFileSystem())
if got := storageIns.JoinBase(tt.path, tt.goos); got != tt.want {
if got := storageIns.JoinBase(tt.path); got != tt.want {
t.Errorf("JoinBase() = %v, want %v", got, tt.want)
}
})
}
}
func TestNormalizeSetFilePath(t *testing.T) {
st := &Storage{
basePath: "/base/path",
}
tests := []struct {
name string
path string
expected string
osGOOS string
}{
{
name: "Unix path on Unix",
path: "relative/path",
expected: "/base/path/relative/path",
osGOOS: "linux",
},
{
name: "Windows path on Windows",
path: "relative\\path",
expected: "/base/path/relative\\\\path",
osGOOS: "windows",
},
{
name: "Unix path on Windows",
path: "relative/path",
expected: "/base/path/relative/path",
osGOOS: "windows",
},
{
name: "Absolute path on Unix",
path: "/absolute/path",
expected: "/absolute/path",
osGOOS: "linux",
},
{
name: "Absolute path on Windows",
path: "C:\\absolute\\path",
expected: "C:\\\\absolute\\\\path",
osGOOS: "windows",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := st.normalizeSetFilePath(tt.path, tt.osGOOS)
if tt.osGOOS == "windows" {
if result != tt.expected {
t.Errorf("normalizeSetFilePath() = %v, want %v", result, tt.expected)
}
} else {
expectedPath := filepath.Join(st.basePath, tt.path)
if !filepath.IsAbs(tt.path) {
if result != expectedPath {
t.Errorf("normalizeSetFilePath() = %v, want %v", result, expectedPath)
}
} else {
if result != tt.path {
t.Errorf("normalizeSetFilePath() = %v, want %v", result, tt.path)
}
}
}
})
}
}