From e41835e686a889bad414d89b2d257f4dc8b58b5f Mon Sep 17 00:00:00 2001 From: zongz <68977949+zong-zhe@users.noreply.github.com> Date: Tue, 12 Nov 2024 10:33:06 +0800 Subject: [PATCH] fix: fill the default source info when pull with only ModSpec (#537) * fix: fill the default source info when pull with only ModSpec Signed-off-by: zongz * fix: fix test case Signed-off-by: zongz --------- Signed-off-by: zongz --- pkg/client/client_test.go | 1 + pkg/client/pull.go | 8 +++++ pkg/client/pull_test.go | 32 +++++++++++++++++++ .../test_pull_with_only_modspec/.gitkeep | 0 4 files changed, 41 insertions(+) create mode 100644 pkg/client/test_data/test_pull_with_only_modspec/.gitkeep diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index a37556b2..07d3cf6c 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -96,6 +96,7 @@ func TestWithGlobalLock(t *testing.T) { test.RunTestWithGlobalLock(t, "testAddWithOnlyModSpec", testAddWithOnlyModSpec) test.RunTestWithGlobalLock(t, "testAddRenameWithModSpec", testAddRenameWithModSpec) test.RunTestWithGlobalLock(t, "testAddRenameWithNoSpec", testAddRenameWithNoSpec) + test.RunTestWithGlobalLock(t, "testPullWithOnlySpec", testPullWithOnlySpec) features.Enable(features.SupportNewStorage) test.RunTestWithGlobalLock(t, "testAddWithModSpec", testAddWithModSpec) diff --git a/pkg/client/pull.go b/pkg/client/pull.go index f580954c..2d708624 100644 --- a/pkg/client/pull.go +++ b/pkg/client/pull.go @@ -84,6 +84,14 @@ func (c *KpmClient) Pull(options ...PullOption) (*pkg.KclPkg, error) { } } + if opts.Source.SpecOnly() { + opts.Source.Oci = &downloader.Oci{ + Reg: c.GetSettings().DefaultOciRegistry(), + Repo: utils.JoinPath(c.GetSettings().DefaultOciRepo(), opts.Source.ModSpec.Name), + Tag: opts.Source.ModSpec.Version, + } + } + sourceFilePath, err := opts.Source.ToFilePath() if err != nil { return nil, err diff --git a/pkg/client/pull_test.go b/pkg/client/pull_test.go index 479267fd..b99138f2 100644 --- a/pkg/client/pull_test.go +++ b/pkg/client/pull_test.go @@ -182,3 +182,35 @@ func testPullWithModSpec(t *testing.T) { ) assert.Equal(t, err.Error(), "version mismatch: 0.0.1 != 0.0.2, version 0.0.2 not found") } + +func testPullWithOnlySpec(t *testing.T) { + pulledPath := getTestDir("test_pull_with_only_modspec") + defer func() { + err := os.RemoveAll(filepath.Join(pulledPath, "oci")) + assert.NilError(t, err) + }() + + kpmcli, err := NewKpmClient() + assert.NilError(t, err) + + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + kPkg, err := kpmcli.Pull( + WithLocalPath(pulledPath), + WithPullSource(&downloader.Source{ + ModSpec: &downloader.ModSpec{ + Name: "helloworld", + Version: "0.1.4", + }, + }), + ) + + pkgPath := filepath.Join(pulledPath, "oci", "ghcr.io", "kcl-lang", "helloworld", "0.1.4", "helloworld", "0.1.4") + assert.NilError(t, err) + assert.Equal(t, kPkg.GetPkgName(), "helloworld") + assert.Equal(t, kPkg.GetPkgVersion(), "0.1.4") + assert.Equal(t, kPkg.HomePath, pkgPath) + err = os.RemoveAll(filepath.Join(pulledPath, "oci")) + assert.NilError(t, err) +} diff --git a/pkg/client/test_data/test_pull_with_only_modspec/.gitkeep b/pkg/client/test_data/test_pull_with_only_modspec/.gitkeep new file mode 100644 index 00000000..e69de29b