From 35c1b8448f22b8ba6f4d3d2c12e14e829f7b29d9 Mon Sep 17 00:00:00 2001 From: zongz Date: Mon, 11 Nov 2024 23:35:15 +0800 Subject: [PATCH] fix: fill the default source info when pull with only ModSpec Signed-off-by: zongz --- pkg/client/pull.go | 8 +++++ pkg/client/pull_test.go | 32 +++++++++++++++++++ .../test_pull_with_only_modspec/.gitkeep | 0 3 files changed, 40 insertions(+) create mode 100644 pkg/client/test_data/test_pull_with_only_modspec/.gitkeep 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..cd7fcbc1 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