Skip to content

Commit 748aec0

Browse files
committed
fix cache check for empty folder
Signed-off-by: liangyuanpeng <gcslyp@gmail.com>
1 parent f5c7dec commit 748aec0

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

pkg/downloader/downloader.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,10 @@ func (d *DepDownloader) Download(opts *DownloadOptions) error {
292292

293293
localPath := opts.LocalPath
294294
cacheFullPath := opts.CachePath
295-
if ok, err := features.Enabled(features.SupportNewStorage); err == nil && !ok && opts.EnableCache {
295+
if ok, err := features.Enabled(features.SupportNewStorage); err == nil &&
296+
!ok &&
297+
opts.EnableCache &&
298+
utils.DirExists(filepath.Join(localPath, constants.KCL_MOD)) {
296299
if utils.DirExists(cacheFullPath) &&
297300
// If the version in modspec is empty, meanings the latest version is needed.
298301
// The latest version should be requested first and the cache should be updated.

pkg/downloader/downloader_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,40 @@ func testGitDownloader(t *testing.T) {
8686
assert.Equal(t, utils.DirExists(filepath.Join(path_git, "git", "src", gitHash, "kcl.mod")), true)
8787
}
8888

89+
func testDepDownloader(t *testing.T) {
90+
path_git := getTestDir("test_dep_downloader")
91+
if err := os.MkdirAll(path_git, os.ModePerm); err != nil {
92+
t.Fatal(err)
93+
}
94+
95+
defer func() {
96+
_ = os.RemoveAll(path_git)
97+
}()
98+
dep := NewOciDownloader("linux/amd64")
99+
err := dep.Download(&DownloadOptions{
100+
LocalPath: path_git,
101+
CachePath: path_git,
102+
EnableCache: true,
103+
Source: Source{
104+
ModSpec: &ModSpec{
105+
Name: "k8s",
106+
Version: "1.28.1",
107+
},
108+
Oci: &Oci{
109+
Reg: "ghcr.io",
110+
Repo: "kcl-lang/k8s",
111+
},
112+
},
113+
})
114+
assert.Equal(t, err, nil)
115+
existFile, err := utils.Exists(path_git + "/kcl.mod")
116+
assert.Equal(t, err, nil)
117+
assert.Equal(t, existFile, true)
118+
}
119+
120+
// go test -timeout 30s -run ^TestWithGlobalLock$ kcl-lang.io/kpm/pkg/downloader -v
89121
func TestWithGlobalLock(t *testing.T) {
90122
test.RunTestWithGlobalLock(t, "TestOciDownloader", testOciDownloader)
91123
test.RunTestWithGlobalLock(t, "TestGitDownloader", testGitDownloader)
124+
test.RunTestWithGlobalLock(t, "TestDepDownloader", testDepDownloader)
92125
}

0 commit comments

Comments
 (0)