Skip to content

Commit 1a3aa5f

Browse files
committed
wip
1 parent bf5c9d9 commit 1a3aa5f

File tree

7 files changed

+59
-24
lines changed

7 files changed

+59
-24
lines changed

api/internal/loader/fileloader.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,9 @@ func newLoaderAtGitClone(
211211
// check for this after cloning repo.
212212
if !root.HasPrefix(repoSpec.CloneDir()) {
213213
_ = cleaner()
214-
return nil, fmt.Errorf("%q refers to directory outside of repo %q", repoSpec.AbsPath(),
215-
repoSpec.CloneDir())
214+
return nil, fmt.Errorf("%q refers to directory outside of repo %q",
215+
filepath.ToSlash(repoSpec.AbsPath()),
216+
filepath.ToSlash(repoSpec.CloneDir().String()))
216217
}
217218
return &FileLoader{
218219
// Clones never allowed to escape root.

api/internal/loader/fileloader_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func TestLoaderLocalScheme(t *testing.T) {
208208
t.Run("file", func(t *testing.T) {
209209
fSys, dir := setupOnDisk(t)
210210
parts := []string{
211-
"ssh:",
211+
"ssh-scheme",
212212
"resource.yaml",
213213
}
214214
require.NoError(t, fSys.Mkdir(dir.Join(parts[0])))
@@ -226,8 +226,9 @@ func TestLoaderLocalScheme(t *testing.T) {
226226
})
227227
t.Run("directory", func(t *testing.T) {
228228
fSys, dir := setupOnDisk(t)
229+
// Use scheme-like name without colon for Windows compatibility
229230
parts := []string{
230-
"https:",
231+
"https-scheme",
231232
"root",
232233
}
233234
require.NoError(t, fSys.MkdirAll(dir.Join(filepath.Join(parts...))))
@@ -379,6 +380,7 @@ func TestNewLoaderAtGitClone(t *testing.T) {
379380
rootURL := "github.com/someOrg/someRepo"
380381
pathInRepo := "foo/base"
381382
url := rootURL + "/" + pathInRepo
383+
// Use absolute path starting with / for in-memory filesystem
382384
coRoot := "/tmp"
383385
fSys := filesys.MakeFsInMemory()
384386
fSys.MkdirAll(coRoot)
@@ -422,6 +424,7 @@ func TestLoaderDisallowsLocalBaseFromRemoteOverlay(t *testing.T) {
422424
require := require.New(t)
423425

424426
// Define an overlay-base structure in the file system.
427+
// Use absolute paths for in-memory filesystem
425428
topDir := "/whatever"
426429
cloneRoot := topDir + "/someClone"
427430
fSys := filesys.MakeFsInMemory()
@@ -494,12 +497,14 @@ func TestLoaderDisallowsRemoteBaseExitRepo(t *testing.T) {
494497

495498
_, err = newLoaderAtGitClone(repoSpec, fSys, nil, git.DoNothingCloner(filesys.ConfirmedDir(repo)))
496499
require.Error(t, err)
497-
require.Contains(t, err.Error(), fmt.Sprintf("%q refers to directory outside of repo %q", base, repo))
500+
// Use filepath.ToSlash to normalize paths for cross-platform comparison
501+
require.Contains(t, err.Error(), fmt.Sprintf("%q refers to directory outside of repo %q", filepath.ToSlash(base), filepath.ToSlash(repo)))
498502
}
499503

500504
func TestLocalLoaderReferencingGitBase(t *testing.T) {
501505
require := require.New(t)
502506

507+
// Use absolute paths for in-memory filesystem
503508
topDir := "/whatever"
504509
cloneRoot := topDir + "/someClone"
505510
fSys := filesys.MakeFsInMemory()
@@ -521,6 +526,7 @@ func TestLocalLoaderReferencingGitBase(t *testing.T) {
521526
func TestRepoDirectCycleDetection(t *testing.T) {
522527
require := require.New(t)
523528

529+
// Use absolute paths for in-memory filesystem
524530
topDir := "/cycles"
525531
cloneRoot := topDir + "/someClone"
526532
fSys := filesys.MakeFsInMemory()
@@ -543,6 +549,7 @@ func TestRepoDirectCycleDetection(t *testing.T) {
543549
func TestRepoIndirectCycleDetection(t *testing.T) {
544550
require := require.New(t)
545551

552+
// Use absolute paths for in-memory filesystem
546553
topDir := "/cycles"
547554
cloneRoot := topDir + "/someClone"
548555
fSys := filesys.MakeFsInMemory()

api/internal/loader/loadrestrictions_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package loader
55

66
import (
7+
"fmt"
78
"path/filepath"
89
"strings"
910
"testing"
@@ -59,9 +60,11 @@ func TestRestrictionRootOnly(t *testing.T) {
5960
if err == nil {
6061
t.Fatal("should have an error")
6162
}
62-
if !strings.Contains(
63-
err.Error(),
64-
"file '/tmp/illegal' is not in or below '/tmp/foo'") {
63+
// Normalize paths to forward slashes for cross-platform comparison
64+
expectedErr := fmt.Sprintf("file '%s' is not in or below '%s'",
65+
filepath.ToSlash(filepath.Join(filesys.Separator+"tmp", "illegal")),
66+
filepath.ToSlash(filepath.Join(filesys.Separator+"tmp", "foo")))
67+
if !strings.Contains(err.Error(), expectedErr) {
6568
t.Fatalf("unexpected err: %s", err)
6669
}
6770
}

api/internal/localizer/util.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ func locFilePath(fileURL string) string {
152152
// Raw github urls are the only type of file urls kustomize officially accepts.
153153
// In this case, the path already consists of org, repo, version, and path in repo, in order,
154154
// so we can use it as is.
155-
return filepath.Join(LocalizeDir, u.Hostname(), path)
155+
hostname := u.Hostname()
156+
// On Windows, colons are invalid in file paths. Replace them with hyphens
157+
// to make IPv6 addresses filesystem-safe.
158+
hostname = strings.ReplaceAll(hostname, ":", "-")
159+
return filepath.Join(LocalizeDir, hostname, path)
156160
}
157161

158162
// locRootPath returns the relative localized path of the validated root url rootURL, where the local copy of its repo

api/internal/localizer/util_test.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"sigs.k8s.io/kustomize/api/ifc"
1515
"sigs.k8s.io/kustomize/api/internal/git"
1616
"sigs.k8s.io/kustomize/kyaml/filesys"
17+
"sigs.k8s.io/kustomize/kyaml/testutil"
1718
)
1819

1920
func TestDefaultNewDirRepo(t *testing.T) {
@@ -120,7 +121,8 @@ func TestLocFilePathColon(t *testing.T) {
120121

121122
// The colon is special because it was once used as the unix file separator.
122123
const url = "https://[2001:4860:4860::8888]/file.yaml"
123-
const host = "2001:4860:4860::8888"
124+
// On Windows, colons are replaced with hyphens in IPv6 addresses
125+
const host = "2001-4860-4860--8888"
124126
const file = "file.yaml"
125127
req.Equal(simpleJoin(t, LocalizeDir, host, file), locFilePath(url))
126128

@@ -139,6 +141,9 @@ func TestLocFilePathColon(t *testing.T) {
139141
}
140142

141143
func TestLocFilePath_SpecialChar(t *testing.T) {
144+
// Skip on Windows as asterisk is invalid in Windows paths
145+
testutil.SkipWindows(t)
146+
142147
req := require.New(t)
143148

144149
// The wild card character is one of the legal uri characters with more meaning
@@ -163,19 +168,26 @@ func TestLocFilePath_SpecialFiles(t *testing.T) {
163168
for name, tFSys := range map[string]struct {
164169
urlPath string
165170
pathDir, pathFile string
171+
skipWindows bool // Skip this test case on Windows
166172
}{
167173
"windows_reserved_name": {
168174
urlPath: "/aux/file",
169175
pathDir: "aux",
170176
pathFile: "file",
171177
},
172178
"hidden_files": {
173-
urlPath: "/.../.file",
174-
pathDir: "...",
175-
pathFile: ".file",
179+
urlPath: "/.../.file",
180+
pathDir: "...",
181+
pathFile: ".file",
182+
skipWindows: true, // Windows treats "..." specially
176183
},
177184
} {
178185
t.Run(name, func(t *testing.T) {
186+
// Skip Windows-incompatible tests
187+
if tFSys.skipWindows {
188+
testutil.SkipWindows(t)
189+
}
190+
179191
req := require.New(t)
180192

181193
expectedPath := simpleJoin(t, LocalizeDir, "host", tFSys.pathDir, tFSys.pathFile)
@@ -304,23 +316,27 @@ func TestLocRootPath_SymlinkPath(t *testing.T) {
304316

305317
func TestCleanedRelativePath(t *testing.T) {
306318
fSys := filesys.MakeFsInMemory()
307-
require.NoError(t, fSys.MkdirAll("/root/test"))
308-
require.NoError(t, fSys.WriteFile("/root/test/file.yaml", []byte("")))
309-
require.NoError(t, fSys.WriteFile("/root/filetwo.yaml", []byte("")))
319+
// Use platform-appropriate root path
320+
rootPath := string(filepath.Separator) + "root"
321+
testPath := rootPath + string(filepath.Separator) + "test"
322+
323+
require.NoError(t, fSys.MkdirAll(testPath))
324+
require.NoError(t, fSys.WriteFile(testPath+string(filepath.Separator)+"file.yaml", []byte("")))
325+
require.NoError(t, fSys.WriteFile(rootPath+string(filepath.Separator)+"filetwo.yaml", []byte("")))
310326

311327
// Absolute path is cleaned to relative path
312-
cleanedPath := cleanedRelativePath(fSys, "/root/", "/root/test/file.yaml")
313-
require.Equal(t, "test/file.yaml", cleanedPath)
328+
cleanedPath := cleanedRelativePath(fSys, filesys.ConfirmedDir(rootPath+string(filepath.Separator)), testPath+string(filepath.Separator)+"file.yaml")
329+
require.Equal(t, filepath.Join("test", "file.yaml"), cleanedPath)
314330

315331
// Winding absolute path is cleaned to relative path
316-
cleanedPath = cleanedRelativePath(fSys, "/root/", "/root/test/../filetwo.yaml")
332+
cleanedPath = cleanedRelativePath(fSys, filesys.ConfirmedDir(rootPath+string(filepath.Separator)), rootPath+string(filepath.Separator)+"test"+string(filepath.Separator)+".."+string(filepath.Separator)+"filetwo.yaml")
317333
require.Equal(t, "filetwo.yaml", cleanedPath)
318334

319335
// Already clean relative path stays the same
320-
cleanedPath = cleanedRelativePath(fSys, "/root/", "test/file.yaml")
321-
require.Equal(t, "test/file.yaml", cleanedPath)
336+
cleanedPath = cleanedRelativePath(fSys, filesys.ConfirmedDir(rootPath+string(filepath.Separator)), "test"+string(filepath.Separator)+"file.yaml")
337+
require.Equal(t, filepath.Join("test", "file.yaml"), cleanedPath)
322338

323339
// Winding relative path is cleaned
324-
cleanedPath = cleanedRelativePath(fSys, "/root/", "test/../filetwo.yaml")
340+
cleanedPath = cleanedRelativePath(fSys, filesys.ConfirmedDir(rootPath+string(filepath.Separator)), "test"+string(filepath.Separator)+".."+string(filepath.Separator)+"filetwo.yaml")
325341
require.Equal(t, "filetwo.yaml", cleanedPath)
326342
}

api/internal/target/kusttarget_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,13 @@ commonLabels:
122122
},
123123
}
124124

125+
// Use platform-appropriate root directory
126+
rootDir := string(filepath.Separator)
125127
kt := makeKustTargetWithRf(
126-
t, th.GetFSys(), "/", provider.NewDefaultDepProvider())
128+
t, th.GetFSys(), rootDir, provider.NewDefaultDepProvider())
127129
for tn, tc := range testCases {
128130
t.Run(tn, func(t *testing.T) {
129-
th.WriteK("/", tc.content)
131+
th.WriteK(rootDir, tc.content)
130132
err := kt.Load()
131133
if tc.errContains != "" {
132134
require.NotNilf(t, err, "expected error containing: `%s`", tc.errContains)

api/resource/origin.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func (origin *Origin) Append(inputPath string) *Origin {
5454
if err == nil {
5555
originCopy.Repo = repoSpec.CloneSpec()
5656
absPath := repoSpec.AbsPath()
57+
// Normalize to forward slashes for cross-platform compatibility
58+
absPath = strings.ReplaceAll(absPath, "\\", "/")
5759
inputPath = absPath[strings.Index(absPath[1:], "/")+1:][1:]
5860
originCopy.Path = ""
5961
originCopy.Ref = repoSpec.Ref

0 commit comments

Comments
 (0)