diff --git a/pkg/cmd/cmd_pull.go b/pkg/cmd/cmd_pull.go index 0106ff7b..94ab3c93 100644 --- a/pkg/cmd/cmd_pull.go +++ b/pkg/cmd/cmd_pull.go @@ -4,7 +4,6 @@ package cmd import ( "fmt" - "net/url" "os" "path/filepath" @@ -65,17 +64,14 @@ func KpmPull(c *cli.Context) error { } ociOpt, event := opt.ParseOciOptionFromOciUrl(ociUrlOrPkgName, tag) - + var err error if event != nil && (event.Type() == reporter.IsNotUrl || event.Type() == reporter.UrlSchemeNotOci) { settings := settings.GetSettings() if settings.ErrorEvent != nil { return settings.ErrorEvent } - urlpath, err := url.JoinPath(settings.DefaultOciRepo(), ociUrlOrPkgName) - if err != nil { - return reporter.NewErrorEvent(reporter.Bug, err) - } + urlpath := utils.JoinPath(settings.DefaultOciRepo(), ociUrlOrPkgName) ociOpt, err = opt.ParseOciRef(urlpath) if err != nil { diff --git a/pkg/cmd/cmd_push.go b/pkg/cmd/cmd_push.go index 4826aff3..e94b12e6 100644 --- a/pkg/cmd/cmd_push.go +++ b/pkg/cmd/cmd_push.go @@ -60,10 +60,7 @@ func genDefaultOciUrlForKclPkg(pkg *pkg.KclPkg) (string, error) { return "", settings.ErrorEvent } - urlPath, err := url.JoinPath(settings.DefaultOciRepo(), pkg.GetPkgName()) - if err != nil { - return "", err - } + urlPath := utils.JoinPath(settings.DefaultOciRepo(), pkg.GetPkgName()) u := &url.URL{ Scheme: oci.OCI_SCHEME, diff --git a/pkg/mod/modfile.go b/pkg/mod/modfile.go index 898e5d60..12293c1c 100644 --- a/pkg/mod/modfile.go +++ b/pkg/mod/modfile.go @@ -3,7 +3,6 @@ package modfile import ( "fmt" - "net/url" "os" "path/filepath" @@ -110,10 +109,7 @@ func (dep *Dependency) FillDepInfo() error { return settings.ErrorEvent } dep.Source.Oci.Reg = settings.DefaultOciRegistry() - urlpath, err := url.JoinPath(settings.DefaultOciRepo(), dep.Name) - if err != nil { - return err - } + urlpath := utils.JoinPath(settings.DefaultOciRepo(), dep.Name) dep.Source.Oci.Repo = urlpath } return nil @@ -377,11 +373,7 @@ func ParseOpt(opt *opt.RegistryOptions) *Dependency { } } if opt.Oci != nil { - repoPath, err := url.JoinPath(opt.Oci.Repo, opt.Oci.PkgName) - if err != nil { - reporter.Report("kpm: failed to parse oci rul") - return nil - } + repoPath := utils.JoinPath(opt.Oci.Repo, opt.Oci.PkgName) ociSource := Oci{ Reg: opt.Oci.Reg, Repo: repoPath, diff --git a/pkg/mod/modfile_test.go b/pkg/mod/modfile_test.go index c7812105..c89760e1 100644 --- a/pkg/mod/modfile_test.go +++ b/pkg/mod/modfile_test.go @@ -1,7 +1,6 @@ package modfile import ( - "net/url" "os" "path/filepath" "testing" @@ -181,8 +180,7 @@ func TestDownloadOci(t *testing.T) { err := os.MkdirAll(testPath, 0755) assert.Equal(t, err, nil) - urlpath, err := url.JoinPath(settings.DEFAULT_REPO, "k8s") - assert.Equal(t, err, nil) + urlpath := utils.JoinPath(settings.DEFAULT_REPO, "k8s") depFromOci := Dependency{ Name: "k8s", @@ -221,8 +219,7 @@ func TestDownloadLatestOci(t *testing.T) { err := os.MkdirAll(testPath, 0755) assert.Equal(t, err, nil) - urlpath, err := url.JoinPath(settings.DEFAULT_REPO, "k8s") - assert.Equal(t, err, nil) + urlpath := utils.JoinPath(settings.DEFAULT_REPO, "k8s") depFromOci := Dependency{ Name: "k8s", diff --git a/pkg/package/package_test.go b/pkg/package/package_test.go index 43b3bacf..6c1e9b97 100644 --- a/pkg/package/package_test.go +++ b/pkg/package/package_test.go @@ -2,6 +2,7 @@ package pkg import ( "encoding/json" + "fmt" "os" "path/filepath" "testing" @@ -137,8 +138,16 @@ func TestUpdateKclModAndLock(t *testing.T) { assert.Equal(t, len(kclPkg.modFile.Deps), 2) expectKclMod, _ := os.ReadFile(filepath.Join(expectDir, "kcl.mod")) expectKclModReverse, _ := os.ReadFile(filepath.Join(expectDir, "kcl.reverse.mod")) + + gotKclModStr := utils.RmNewline(string(gotKclMod)) + fmt.Printf("gotKclModStr: '%v'\n", gotKclModStr) + expectKclModStr := utils.RmNewline(string(expectKclMod)) + fmt.Printf("expectKclModStr: '%v'\n", expectKclModStr) + expectKclModReverseStr := utils.RmNewline(string(expectKclModReverse)) + fmt.Printf("expectKclModReverseStr: '%v'\n", expectKclModReverseStr) + assert.Equal(t, - (utils.RmNewline(string(gotKclMod)) == utils.RmNewline(string(expectKclMod))) || (utils.RmNewline(string(gotKclMod)) == utils.RmNewline(string(expectKclModReverse))), + (gotKclModStr == expectKclModStr || gotKclModStr == expectKclModReverseStr), true, ) } @@ -150,8 +159,16 @@ func TestUpdateKclModAndLock(t *testing.T) { assert.Equal(t, len(kclPkg.modFile.Deps), 2) expectKclModLock, _ := os.ReadFile(filepath.Join(expectDir, "kcl.mod.lock")) expectKclModLockReverse, _ := os.ReadFile(filepath.Join(expectDir, "kcl.mod.reverse.lock")) + + gotKclModLockStr := utils.RmNewline(string(gotKclModLock)) + fmt.Printf("gotKclModLockStr: '%v'\n", gotKclModLockStr) + expectKclModLockStr := utils.RmNewline(string(expectKclModLock)) + fmt.Printf("expectKclModLockStr: '%v'\n", expectKclModLockStr) + expectKclModLockReverseStr := utils.RmNewline(string(expectKclModLockReverse)) + fmt.Printf("expectKclModLockReverseStr: '%v'\n", expectKclModLockReverseStr) + assert.Equal(t, - (utils.RmNewline(string(gotKclModLock)) == utils.RmNewline(string(expectKclModLock))) || (utils.RmNewline(string(gotKclModLock)) == utils.RmNewline(string(expectKclModLockReverse))), + (gotKclModLockStr == expectKclModLockStr) || (gotKclModLockStr == expectKclModLockReverseStr), true, ) } diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index af5025b6..1ee145b4 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -120,3 +120,13 @@ func TestDefaultKpmHome(t *testing.T) { assert.Equal(t, kpmHome, filePath) assert.Equal(t, DirExists(kpmHome), true) } + +func TestJoinPath(t *testing.T) { + assert.Equal(t, JoinPath("base", "elem"), "base/elem") + assert.Equal(t, JoinPath("base/", "elem"), "base/elem") + assert.Equal(t, JoinPath("base", "/elem"), "base/elem") + assert.Equal(t, JoinPath("", "/elem"), "/elem") + assert.Equal(t, JoinPath("", "elem"), "/elem") + assert.Equal(t, JoinPath("base/", ""), "base/") + assert.Equal(t, JoinPath("base", ""), "base/") +}