Skip to content

Commit

Permalink
fix: add more test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
zong-zhe committed Jul 20, 2023
1 parent 5a4aad7 commit 046b191
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 27 deletions.
8 changes: 2 additions & 6 deletions pkg/cmd/cmd_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package cmd

import (
"fmt"
"net/url"
"os"
"path/filepath"

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions pkg/cmd/cmd_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 2 additions & 10 deletions pkg/mod/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package modfile

import (
"fmt"
"net/url"
"os"
"path/filepath"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions pkg/mod/modfile_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package modfile

import (
"net/url"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
21 changes: 19 additions & 2 deletions pkg/package/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -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,
)
}
Expand All @@ -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,
)
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
}

0 comments on commit 046b191

Please sign in to comment.