fix set-file cannot find the file on windows (#908)
This commit is contained in:
		
							parent
							
								
									c55fa0f765
								
							
						
					
					
						commit
						3384a40fd9
					
				|  | @ -4,6 +4,7 @@ import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"net/url" | 	"net/url" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
|  | 	"runtime" | ||||||
| 	"sort" | 	"sort" | ||||||
| 	"strings" | 	"strings" | ||||||
| 
 | 
 | ||||||
|  | @ -135,11 +136,18 @@ func (st *Storage) normalizePath(path string) string { | ||||||
| 	if u != nil && (u.Scheme != "" || filepath.IsAbs(path)) { | 	if u != nil && (u.Scheme != "" || filepath.IsAbs(path)) { | ||||||
| 		return path | 		return path | ||||||
| 	} else { | 	} else { | ||||||
| 		return st.JoinBase(path) | 		return st.JoinBase(path, runtime.GOOS) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // JoinBase returns an absolute path in the form basePath/relative
 | // JoinBase returns an absolute path in the form basePath/relative
 | ||||||
| func (st *Storage) JoinBase(relPath string) string { | // Helm's setFiles command does not support unescaped filepath separators (\) on Windows.
 | ||||||
| 	return filepath.Join(st.basePath, relPath) | // Instead, it requires double backslashes (\\) as filepath separators.
 | ||||||
|  | // See https://github.com/helm/helm/issues/9537
 | ||||||
|  | func (st *Storage) JoinBase(relPath, GOOS string) string { | ||||||
|  | 	path := filepath.Join(st.basePath, relPath) | ||||||
|  | 	if GOOS == "windows" { | ||||||
|  | 		return strings.ReplaceAll(path, "\\", "\\\\") | ||||||
|  | 	} | ||||||
|  | 	return path | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -176,27 +176,37 @@ func TestJoinBase(t *testing.T) { | ||||||
| 	tests := []struct { | 	tests := []struct { | ||||||
| 		name string | 		name string | ||||||
| 		base string | 		base string | ||||||
|  | 		goos string | ||||||
| 		path string | 		path string | ||||||
| 		want string | 		want string | ||||||
| 	}{ | 	}{ | ||||||
| 		{ | 		{ | ||||||
| 			name: "joinBase with non-root base", | 			name: "joinBase with non-root base", | ||||||
| 			base: "/root", | 			base: "/root", | ||||||
|  | 			goos: "linux", | ||||||
| 			path: "local/timespan-application.yml", | 			path: "local/timespan-application.yml", | ||||||
| 			want: "/local/timespan-application.yml", | 			want: "/local/timespan-application.yml", | ||||||
| 		}, | 		}, | ||||||
| 		{ | 		{ | ||||||
| 			name: "joinBase with root path", | 			name: "joinBase with root path", | ||||||
| 			base: "/", | 			base: "/", | ||||||
|  | 			goos: "linux", | ||||||
| 			path: "data/timespan-application.yml", | 			path: "data/timespan-application.yml", | ||||||
| 			want: "/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", | ||||||
|  | 		}, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	for _, tt := range tests { | 	for _, tt := range tests { | ||||||
| 		t.Run(tt.name, func(t *testing.T) { | 		t.Run(tt.name, func(t *testing.T) { | ||||||
| 			storageIns := NewStorage(tt.base, helmexec.NewLogger(io.Discard, "debug"), filesystem.DefaultFileSystem()) | 			storageIns := NewStorage(tt.base, helmexec.NewLogger(io.Discard, "debug"), filesystem.DefaultFileSystem()) | ||||||
| 			if got := storageIns.JoinBase(tt.path); got != tt.want { | 			if got := storageIns.JoinBase(tt.path, tt.goos); got != tt.want { | ||||||
| 				t.Errorf("JoinBase() = %v, want %v", got, tt.want) | 				t.Errorf("JoinBase() = %v, want %v", got, tt.want) | ||||||
| 			} | 			} | ||||||
| 		}) | 		}) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue