Skip to content

Commit

Permalink
fix: fix redownload when the remote repo has no mod file
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Nov 13, 2024
1 parent 5de98e2 commit 2e5b342
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 5 deletions.
28 changes: 27 additions & 1 deletion pkg/client/add_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"bytes"
"os"
"path/filepath"
"testing"
Expand All @@ -12,46 +13,66 @@ import (
"kcl-lang.io/kpm/pkg/utils"
)

func testAddWithModSpec(t *testing.T) {
func TestAddWithModSpec(t *testing.T) {
tests := []struct {
name string
pkgSubPath string
sourceUrl string
msg string
}{
{
name: "TestAddOciWithModSpec",
pkgSubPath: "oci",
sourceUrl: "oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4&mod=subhelloworld:0.0.1",
msg: "adding dependency 'subhelloworld'add dependency 'subhelloworld:0.0.1' successfully",
},
{
name: "TestAddGitWithModSpec",
pkgSubPath: "git",
sourceUrl: "git://github.com/kcl-lang/flask-demo-kcl-manifests.git?commit=8308200&mod=cc:0.0.1",
msg: "adding dependency 'cc'add dependency 'cc:0.0.1' successfully",
},
{
name: "TestAddGitWithModSpec",
pkgSubPath: "git_mod_0",
sourceUrl: "git://github.com/kcl-lang/flask-demo-kcl-manifests.git?commit=8308200&mod=cc",
msg: "adding dependency 'cc'add dependency 'cc:0.0.1' successfully",
},
{
name: "TestAddGitWithoutModFileWithModSpec",
pkgSubPath: "git_mod_1",
sourceUrl: "git://github.com/kcl-lang/flask-demo-kcl-manifests.git?commit=5ab0fff&mod=cc",
msg: "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with commit '5ab0fff'adding dependency 'cc'add dependency 'cc:0.0.1' successfull",
},
{
name: "TestAddLocalWithModSpec",
pkgSubPath: filepath.Join("local", "pkg"),
sourceUrl: "../dep?mod=sub:0.0.1",
msg: "adding dependency 'sub'add dependency 'sub:0.0.1' successfully",
},
{
name: "TestAddOciWithEmptyVersion",
pkgSubPath: "empty_version",
sourceUrl: "oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4&mod=subhelloworld",
msg: "adding dependency 'subhelloworld'add dependency 'subhelloworld:0.0.1' successfully",
},
{
name: "TestAddOciWithNoSpec",
pkgSubPath: "no_spec",
sourceUrl: "oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4",
msg: "adding dependency 'helloworld'add dependency 'helloworld:0.1.4' successfully",
},
{
name: "TestAddOciWithNoTag",
pkgSubPath: "no_oci_ref",
sourceUrl: "oci://ghcr.io/kcl-lang/helloworld",
msg: "the lastest version '0.1.4' will be downloadedadding dependency 'helloworld'add dependency 'helloworld:0.1.4' successfully",
},
{
name: "TestAddGitWithNoTag",
pkgSubPath: "no_git_ref",
sourceUrl: "git://github.com/kcl-lang/flask-demo-kcl-manifests.git",
msg: "the lastest version 'ade147b' will be downloadedadding dependency 'flask_manifests'add dependency 'flask_manifests:0.0.1' successfully",
},
}

Expand Down Expand Up @@ -94,6 +115,9 @@ func testAddWithModSpec(t *testing.T) {
t.Fatal(err)
}

var buf bytes.Buffer
kpmcli.SetLogWriter(&buf)

kpkg, err := pkg.LoadKclPkgWithOpts(
pkg.WithPath(pkgPath),
pkg.WithSettings(kpmcli.GetSettings()),
Expand All @@ -112,6 +136,8 @@ func testAddWithModSpec(t *testing.T) {
t.Fatal(err)
}

assert.Equal(t, utils.RmNewline(tt.msg), utils.RmNewline(buf.String()))

expectedMod, err := os.ReadFile(modExpect)
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestWithGlobalLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "TestDownloadGitWithPackage", testDownloadGitWithPackage)
test.RunTestWithGlobalLock(t, "TestModandLockFilesWithGitPackageDownload", testModandLockFilesWithGitPackageDownload)
test.RunTestWithGlobalLock(t, "TestDependencyGraph", testDependencyGraph)
test.RunTestWithGlobalLock(t, "TestAddWithModSpec", testAddWithModSpec)
test.RunTestWithGlobalLock(t, "TestAddWithModSpec", TestAddWithModSpec)
test.RunTestWithGlobalLock(t, "TestRunRemoteWithArgsInvalid", testRunRemoteWithArgsInvalid)
test.RunTestWithGlobalLock(t, "TestRunRemoteWithArgs", testRunRemoteWithArgs)
test.RunTestWithGlobalLock(t, "TestRunWithNoSumCheck", testRunWithGitPackage)
Expand All @@ -97,7 +97,7 @@ func TestWithGlobalLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "testAddRenameWithModSpec", testAddRenameWithModSpec)

features.Enable(features.SupportNewStorage)
test.RunTestWithGlobalLock(t, "testAddWithModSpec", testAddWithModSpec)
test.RunTestWithGlobalLock(t, "testAddWithModSpec", TestAddWithModSpec)
test.RunTestWithGlobalLock(t, "testAddWithOnlyModSpec", testAddWithOnlyModSpec)
test.RunTestWithGlobalLock(t, "testAddRenameWithModSpec", testAddRenameWithModSpec)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/client/test_data/add_with_mod_spec/git_mod_0/kcl.mod.bk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "git"
edition = "v0.10.0"
version = "0.0.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "git"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
cc = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "8308200", version = "0.0.1" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.cc]
name = "cc"
full_name = "cc_0.0.1"
version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
commit = "8308200"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_mod_spec/git_mod_0/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
4 changes: 4 additions & 0 deletions pkg/client/test_data/add_with_mod_spec/git_mod_1/kcl.mod.bk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "git"
edition = "v0.10.0"
version = "0.0.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "git"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
cc = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "8308200", version = "0.0.1" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.cc]
name = "cc"
full_name = "cc_0.0.1"
version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
commit = "8308200"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_mod_spec/git_mod_1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
2 changes: 1 addition & 1 deletion pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (d *DepDownloader) Download(opts *DownloadOptions) error {
localPath := opts.LocalPath
cacheFullPath := opts.CachePath
if ok, err := features.Enabled(features.SupportNewStorage); err == nil && !ok && opts.EnableCache {
if utils.DirExists(cacheFullPath) && utils.DirExists(filepath.Join(cacheFullPath, constants.KCL_MOD)) &&
if utils.DirExists(cacheFullPath) &&
// If the version in modspec is empty, meanings the latest version is needed.
// The latest version should be requested first and the cache should be updated.
((opts.Source.ModSpec != nil && opts.Source.ModSpec.Version != "") || opts.Source.ModSpec == nil) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) {
Version: opt.Registry.Tag,
Name: opt.Registry.Ref,
},
Oci: &ociSource,
Oci: &ociSource,
},
Version: opt.Registry.Tag,
}, nil
Expand Down

0 comments on commit 2e5b342

Please sign in to comment.