Skip to content

Commit

Permalink
fix: fill the default source info when pull with only ModSpec (#537)
Browse files Browse the repository at this point in the history
* fix: fill the default source info when pull with only ModSpec

Signed-off-by: zongz <[email protected]>

* fix: fix test case

Signed-off-by: zongz <[email protected]>

---------

Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Nov 12, 2024
1 parent 8c15131 commit e41835e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions pkg/client/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions pkg/client/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Empty file.

0 comments on commit e41835e

Please sign in to comment.