Skip to content
This repository was archived by the owner on Aug 17, 2023. It is now read-only.

Commit 31fdc70

Browse files
authored
fix error when config file is from local relative directory (#350)
* fix local dir issue * add one unit test case
1 parent 7be0541 commit 31fdc70

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

pkg/kfconfig/types.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,23 @@ func (c *KfConfig) SyncCache() error {
504504
// Manifests are local dir
505505
if fi, err := os.Stat(r.URI); err == nil && fi.Mode().IsDir() {
506506
// check whether the cache directory is a sub directory of manifests
507-
if relDir, err := filepath.Rel(r.URI, cacheDir); err != nil {
507+
absCacheDir, err := filepath.Abs(cacheDir)
508+
if err != nil {
508509
return errors.WithStack(err)
509-
} else {
510-
if !strings.HasPrefix(relDir, ".."+string(filepath.Separator)) {
511-
return errors.WithStack(errors.New("SyncCache: could not sync cache when the cache path " + cacheDir + " is sub directory of manifests " + r.URI))
512-
}
510+
}
511+
512+
absURI, err := filepath.Abs(r.URI)
513+
if err != nil {
514+
return errors.WithStack(err)
515+
}
516+
517+
relDir, err := filepath.Rel(absURI, absCacheDir)
518+
if err != nil {
519+
return errors.WithStack(err)
520+
}
521+
522+
if !strings.HasPrefix(relDir, ".."+string(filepath.Separator)) {
523+
return errors.WithStack(errors.New("SyncCache: could not sync cache when the cache path " + cacheDir + " is sub directory of manifests " + r.URI))
513524
}
514525

515526
if err := copy.Copy(r.URI, cacheDir); err != nil {

pkg/kfconfig/types_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,25 @@ func TestSyncCache(t *testing.T) {
102102
expected: nil,
103103
expectedErr: errors.New("SyncCache: could not sync cache when the cache path " + path.Join(srcDir, "app1", ".cache", repoName) + " is sub directory of manifests " + srcDir),
104104
},
105+
{
106+
input: &KfConfig{
107+
Spec: KfConfigSpec{
108+
AppDir: ".",
109+
Repos: []Repo{{
110+
Name: repoName,
111+
URI: srcDir,
112+
},
113+
},
114+
},
115+
},
116+
expected: []Cache{
117+
{
118+
Name: repoName,
119+
LocalPath: path.Join(".cache", repoName),
120+
},
121+
},
122+
expectedErr: nil,
123+
},
105124
{
106125
input: &KfConfig{
107126
Spec: KfConfigSpec{
@@ -162,8 +181,13 @@ func TestSyncCache(t *testing.T) {
162181
for _, c := range testCases {
163182
err = c.input.SyncCache()
164183

184+
// remove the local path for the test case whose AppDir is "."
185+
if c.input.Spec.AppDir == "." {
186+
os.RemoveAll(path.Join(".cache", repoName))
187+
}
188+
165189
if err != nil {
166-
if err.Error() != c.expectedErr.Error() {
190+
if c.expectedErr == nil || err.Error() != c.expectedErr.Error() {
167191
t.Fatalf("Could not sync cache; %v", err)
168192
}
169193
}

0 commit comments

Comments
 (0)