Skip to content

Commit b6d1809

Browse files
authored
Merge pull request #665 from wolfchkov/fix-skip-dep-download
fix: prevent skipping dependency download when cache folder is empty
2 parents d2b887d + 62411b7 commit b6d1809

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

pkg/downloader/downloader.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func (d *DepDownloader) Download(opts *DownloadOptions) error {
293293
localPath := opts.LocalPath
294294
cacheFullPath := opts.CachePath
295295
if ok, err := features.Enabled(features.SupportNewStorage); err == nil && !ok && opts.EnableCache {
296-
if utils.DirExists(cacheFullPath) &&
296+
if dependencyPackageExists(cacheFullPath) &&
297297
// If the version in modspec is empty, meanings the latest version is needed.
298298
// The latest version should be requested first and the cache should be updated.
299299
((opts.Source.ModSpec != nil && opts.Source.ModSpec.Version != "") || opts.Source.ModSpec == nil) {
@@ -315,8 +315,7 @@ func (d *DepDownloader) Download(opts *DownloadOptions) error {
315315

316316
// If the dependency package is already exist,
317317
// Skip the download process.
318-
if utils.DirExists(localPath) &&
319-
utils.DirExists(filepath.Join(localPath, constants.KCL_MOD)) {
318+
if dependencyPackageExists(localPath) {
320319
return nil
321320
} else {
322321
opts.LocalPath = tmpDir
@@ -370,6 +369,11 @@ func (d *DepDownloader) Download(opts *DownloadOptions) error {
370369
return nil
371370
}
372371

372+
func dependencyPackageExists(path string) bool {
373+
return utils.DirExists(path) &&
374+
utils.DirExists(filepath.Join(path, constants.KCL_MOD))
375+
}
376+
373377
// Platform option struct.
374378
type Platform struct {
375379
PlatformSpec string

pkg/downloader/downloader_test.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ func getTestDir(subDir string) string {
2323
return testDir
2424
}
2525

26-
func testOciDownloader(t *testing.T) {
27-
path_oci := getTestDir("test_oci")
28-
if err := os.MkdirAll(path_oci, os.ModePerm); err != nil {
26+
func makeDir(t *testing.T, path string) {
27+
if err := os.MkdirAll(path, os.ModePerm); err != nil {
2928
t.Fatal(err)
3029
}
30+
}
31+
32+
func testOciDownloader(t *testing.T) {
33+
path_oci := getTestDir("test_oci")
34+
makeDir(t, path_oci)
3135

3236
defer func() {
3337
_ = os.RemoveAll(path_oci)
@@ -54,9 +58,7 @@ func testOciDownloader(t *testing.T) {
5458
func testGitDownloader(t *testing.T) {
5559
features.Enable(features.SupportNewStorage)
5660
path_git := getTestDir("test_git_bare_repo")
57-
if err := os.MkdirAll(path_git, os.ModePerm); err != nil {
58-
t.Fatal(err)
59-
}
61+
makeDir(t, path_git)
6062

6163
defer func() {
6264
_ = os.RemoveAll(path_git)
@@ -86,7 +88,46 @@ func testGitDownloader(t *testing.T) {
8688
assert.Equal(t, utils.DirExists(filepath.Join(path_git, "git", "src", gitHash, "kcl.mod")), true)
8789
}
8890

91+
func testDepDownloaderWhenPackageCacheFolderExistsButEmpty(t *testing.T) {
92+
path_tmp := getTestDir("test_dep_downloader")
93+
makeDir(t, path_tmp)
94+
defer func() {
95+
_ = os.RemoveAll(path_tmp)
96+
}()
97+
98+
path_local := filepath.Join(path_tmp, "package_local")
99+
makeDir(t, path_local)
100+
path_cache := filepath.Join(path_tmp, "package_cache")
101+
makeDir(t, path_cache)
102+
103+
depDownloader := DepDownloader{
104+
OciDownloader: &OciDownloader{
105+
Platform: "linux/amd64",
106+
},
107+
}
108+
109+
err := depDownloader.Download(NewDownloadOptions(
110+
WithSource(Source{
111+
Oci: &Oci{
112+
Reg: "ghcr.io",
113+
Repo: "zong-zhe/helloworld",
114+
Tag: "0.0.3",
115+
},
116+
}),
117+
WithLocalPath(path_local),
118+
WithCachePath(path_cache),
119+
WithEnableCache(true),
120+
))
121+
122+
assert.Equal(t, err, nil)
123+
existFile, err := utils.Exists(path_local + "/kcl.mod")
124+
assert.NilError(t, err)
125+
assert.Check(t, existFile)
126+
}
127+
89128
func TestWithGlobalLock(t *testing.T) {
90129
test.RunTestWithGlobalLock(t, "TestOciDownloader", testOciDownloader)
91130
test.RunTestWithGlobalLock(t, "TestGitDownloader", testGitDownloader)
131+
test.RunTestWithGlobalLock(t, "TestDepDownloaderWhenPackageCacheFolderExistsButEmpty",
132+
testDepDownloaderWhenPackageCacheFolderExistsButEmpty)
92133
}

0 commit comments

Comments
 (0)