Skip to content

Commit

Permalink
fix: fill the default source info when pull with only ModSpec
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Nov 12, 2024
1 parent 8c15131 commit 35c1b84
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
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 35c1b84

Please sign in to comment.