Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 65 additions & 48 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1542,68 +1542,85 @@ func TestAddLocalPath(t *testing.T) {
}

func testAddDefaultRegistryDep(t *testing.T) {
pkgPath := getTestDir("add_with_default_dep")

pkgWithSumCheckPathModBak := filepath.Join(pkgPath, "kcl.mod.bak")
pkgWithSumCheckPathMod := filepath.Join(pkgPath, "kcl.mod")
pkgWithSumCheckPathModExpect := filepath.Join(pkgPath, "kcl.mod.expect")
type testCase struct {
tag string
pkgPath string
modBak string
mod string
modExpect string
modLockBak string
modLock string
modLockExpect string
}

pkgWithSumCheckPathModLockBak := filepath.Join(pkgPath, "kcl.mod.lock.bak")
pkgWithSumCheckPathModLock := filepath.Join(pkgPath, "kcl.mod.lock")
pkgWithSumCheckPathModLockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect")
rootTestPath := getTestDir("add_with_default_dep")
testCases := []testCase{
{
tag: "",
pkgPath: filepath.Join(rootTestPath, "no_tag"),
},
{
tag: "0.1.2",
pkgPath: filepath.Join(rootTestPath, "with_tag"),
},
}

err := copy.Copy(pkgWithSumCheckPathModBak, pkgWithSumCheckPathMod)
assert.Equal(t, err, nil)
err = copy.Copy(pkgWithSumCheckPathModLockBak, pkgWithSumCheckPathModLock)
assert.Equal(t, err, nil)
for _, tc := range testCases {
tc.modBak = filepath.Join(tc.pkgPath, "kcl.mod.bak")
tc.mod = filepath.Join(tc.pkgPath, "kcl.mod")
tc.modExpect = filepath.Join(tc.pkgPath, "kcl.mod.expect")
tc.modLockBak = filepath.Join(tc.pkgPath, "kcl.mod.lock.bak")
tc.modLock = filepath.Join(tc.pkgPath, "kcl.mod.lock")
tc.modLockExpect = filepath.Join(tc.pkgPath, "kcl.mod.lock.expect")

err := copy.Copy(tc.modBak, tc.mod)
assert.Equal(t, err, nil)
err = copy.Copy(tc.modLockBak, tc.modLock)
assert.Equal(t, err, nil)

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)

kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath)
assert.Equal(t, err, nil)
kclPkg, err := kpmcli.LoadPkgFromPath(tc.pkgPath)
assert.Equal(t, err, nil)

opts := opt.AddOptions{
LocalPath: pkgPath,
RegistryOpts: opt.RegistryOptions{
Registry: &opt.OciOptions{
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
PkgName: "helloworld",
Tag: "0.1.2",
opts := opt.AddOptions{
LocalPath: tc.pkgPath,
RegistryOpts: opt.RegistryOptions{
Registry: &opt.OciOptions{
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
PkgName: "helloworld",
Tag: tc.tag,
},
},
},
}
}

_, err = kpmcli.AddDepWithOpts(kclPkg, &opts)
assert.Equal(t, err, nil)
_, err = kpmcli.AddDepWithOpts(kclPkg, &opts)
assert.Equal(t, err, nil)

modContent, err := os.ReadFile(pkgWithSumCheckPathMod)
modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "")
modContentStr = strings.ReplaceAll(modContentStr, "\n", "")
assert.Equal(t, err, nil)
verifyFileContent(t, tc.mod, tc.modExpect)
verifyFileContent(t, tc.modLock, tc.modLockExpect)

modExpectContent, err := os.ReadFile(pkgWithSumCheckPathModExpect)
modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "")
modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "")
defer func() {
_ = os.Remove(tc.mod)
_ = os.Remove(tc.modLock)
}()
}
}

func verifyFileContent(t *testing.T, filePath, expectPath string) {
content, err := os.ReadFile(filePath)
assert.Equal(t, err, nil)
assert.Equal(t, modContentStr, modExpectContentStr)
contentStr := strings.ReplaceAll(string(content), "\r\n", "")
contentStr = strings.ReplaceAll(contentStr, "\n", "")

modLockContent, err := os.ReadFile(pkgWithSumCheckPathModLock)
modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "")
modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "")
assert.Equal(t, err, nil)
modLockExpectContent, err := os.ReadFile(pkgWithSumCheckPathModLockExpect)
modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "")
modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "")
expectContent, err := os.ReadFile(expectPath)
assert.Equal(t, err, nil)
assert.Equal(t, modLockContentStr, modLockExpectContentStr)
expectContentStr := strings.ReplaceAll(string(expectContent), "\r\n", "")
expectContentStr = strings.ReplaceAll(expectContentStr, "\n", "")

defer func() {
_ = os.Remove(pkgWithSumCheckPathMod)
_ = os.Remove(pkgWithSumCheckPathModLock)
}()
assert.Equal(t, contentStr, expectContentStr)
}

func testUpdateDefaultRegistryDep(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "add_with_default"
edition = "v0.9.0"
version = "0.0.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "add_with_default"
edition = "v0.9.0"
version = "0.0.1"

[dependencies]
helloworld = "0.1.2"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.2"
version = "0.1.2"
11 changes: 10 additions & 1 deletion pkg/downloader/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ func (source *Source) MarshalTOML() string {

func (registry *Registry) MarshalTOML() string {
var sb strings.Builder
sb.WriteString(registry.Version)
if len(registry.Version) != 0 {
sb.WriteString(registry.Version)
return sb.String()
}

if len(registry.Oci.Tag) != 0 {
sb.WriteString(registry.Oci.Tag)
return sb.String()
}

return sb.String()
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"path/filepath"
"strings"

orderedmap "github.com/elliotchance/orderedmap/v2"
"github.com/BurntSushi/toml"
orderedmap "github.com/elliotchance/orderedmap/v2"

"kcl-lang.io/kcl-go/pkg/kcl"

Expand Down