Skip to content

Commit e7f583a

Browse files
authored
Merge pull request #365 from zong-zhe/fix-oci-replace
feat: remove the replace for default registry dep
2 parents 02fd238 + 32df960 commit e7f583a

File tree

39 files changed

+377
-60
lines changed

39 files changed

+377
-60
lines changed

pkg/api/kpm_pkg.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ func NewKclTypes(name, path string, tys *gpyrpc.KclType) *KclType {
4646
//
4747
// 'kcl_pkg_path' is the path of dependencies download by kpm.
4848
func GetKclPackage(pkgPath string) (*KclPackage, error) {
49-
kclPkg, err := pkg.LoadKclPkg(pkgPath)
49+
kpmcli, err := client.NewKpmClient()
50+
if err != nil {
51+
return nil, err
52+
}
53+
kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath)
5054
if err != nil {
5155
return nil, err
5256
}

pkg/api/kpm_pkg_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ func TestApiGetDependenciesInModFile(t *testing.T) {
6868
assert.Equal(t, dep.Name, "k8s")
6969
assert.Equal(t, dep.FullName, "k8s_1.27")
7070
assert.Equal(t, dep.Version, "1.27")
71-
assert.Equal(t, dep.Source.Oci.Reg, "ghcr.io")
72-
assert.Equal(t, dep.Source.Oci.Repo, "kcl-lang/k8s")
73-
assert.Equal(t, dep.Source.Oci.Tag, "1.27")
71+
assert.Equal(t, dep.Source.Registry.Oci.Reg, "ghcr.io")
72+
assert.Equal(t, dep.Source.Registry.Oci.Repo, "kcl-lang/k8s")
73+
assert.Equal(t, dep.Source.Registry.Oci.Tag, "1.27")
7474
}
7575

7676
func TestGetAllSchemaTypesMappingNamed(t *testing.T) {

pkg/client/client.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ func (c *KpmClient) getDepStorePath(search_path string, d *pkg.Dependency, isVen
256256
} else {
257257
storePkgName = fmt.Sprintf(PKG_NAME_PATTERN, d.Name, d.Source.Git.Branch)
258258
}
259+
} else if d.Source.Registry != nil {
260+
storePkgName = fmt.Sprintf(PKG_NAME_PATTERN, d.Name, d.Source.Registry.Version)
259261
} else {
260262
storePkgName = fmt.Sprintf(PKG_NAME_PATTERN, d.Name, d.Version)
261263
}
@@ -891,6 +893,18 @@ func (c *KpmClient) FillDepInfo(dep *pkg.Dependency, homepath string) error {
891893
dep.Source.Oci.Repo = urlpath
892894
}
893895
}
896+
if dep.Source.Registry != nil {
897+
if len(dep.Source.Registry.Reg) == 0 {
898+
dep.Source.Registry.Reg = c.GetSettings().DefaultOciRegistry()
899+
}
900+
901+
if len(dep.Source.Registry.Repo) == 0 {
902+
urlpath := utils.JoinPath(c.GetSettings().DefaultOciRepo(), dep.Name)
903+
dep.Source.Registry.Repo = urlpath
904+
}
905+
906+
dep.Version = dep.Source.Registry.Version
907+
}
894908
return nil
895909
}
896910

@@ -946,14 +960,24 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
946960
dep.Version = modFile.Pkg.Version
947961
}
948962

949-
if dep.Source.Oci != nil {
963+
if dep.Source.Oci != nil || dep.Source.Registry != nil {
964+
var ociSource *pkg.Oci
965+
if dep.Source.Oci != nil {
966+
ociSource = dep.Source.Oci
967+
} else if dep.Source.Registry != nil {
968+
ociSource = dep.Source.Registry.Oci
969+
}
950970
// Select the latest tag, if the tag, the user inputed, is empty.
951-
if dep.Source.Oci.Tag == "" || dep.Source.Oci.Tag == constants.LATEST {
952-
latestTag, err := c.AcquireTheLatestOciVersion(*dep.Source.Oci)
971+
if ociSource.Tag == "" || ociSource.Tag == constants.LATEST {
972+
latestTag, err := c.AcquireTheLatestOciVersion(*ociSource)
953973
if err != nil {
954974
return nil, err
955975
}
956-
dep.Source.Oci.Tag = latestTag
976+
ociSource.Tag = latestTag
977+
978+
if dep.Source.Registry != nil {
979+
dep.Source.Registry.Tag = latestTag
980+
}
957981

958982
// Complete some information that the local three dependencies depend on.
959983
// The invalid path such as '$HOME/.kcl/kpm/k8s_' is placed because the version field is missing.

pkg/client/client_test.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,9 @@ func TestOciDownloader(t *testing.T) {
14441444
// make test case running in order to test the log output
14451445
testRunWithOciDownloader(t)
14461446
testAddWithOciDownloader(t)
1447+
testAddDefaultRegistryDep(t)
1448+
testUpdateDefaultRegistryDep(t)
1449+
testRunDefaultRegistryDep(t)
14471450
}
14481451

14491452
func testAddWithOciDownloader(t *testing.T) {
@@ -1540,3 +1543,179 @@ func TestAddLocalPath(t *testing.T) {
15401543
_ = os.Remove(filepath.Join(path, "kcl.mod"))
15411544
}()
15421545
}
1546+
1547+
func testAddDefaultRegistryDep(t *testing.T) {
1548+
pkgPath := getTestDir("add_with_default_dep")
1549+
1550+
pkgWithSumCheckPathModBak := filepath.Join(pkgPath, "kcl.mod.bak")
1551+
pkgWithSumCheckPathMod := filepath.Join(pkgPath, "kcl.mod")
1552+
pkgWithSumCheckPathModExpect := filepath.Join(pkgPath, "kcl.mod.expect")
1553+
1554+
pkgWithSumCheckPathModLockBak := filepath.Join(pkgPath, "kcl.mod.lock.bak")
1555+
pkgWithSumCheckPathModLock := filepath.Join(pkgPath, "kcl.mod.lock")
1556+
pkgWithSumCheckPathModLockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect")
1557+
1558+
err := copy.Copy(pkgWithSumCheckPathModBak, pkgWithSumCheckPathMod)
1559+
assert.Equal(t, err, nil)
1560+
err = copy.Copy(pkgWithSumCheckPathModLockBak, pkgWithSumCheckPathModLock)
1561+
assert.Equal(t, err, nil)
1562+
1563+
kpmcli, err := NewKpmClient()
1564+
assert.Equal(t, err, nil)
1565+
1566+
kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath)
1567+
assert.Equal(t, err, nil)
1568+
1569+
opts := opt.AddOptions{
1570+
LocalPath: pkgPath,
1571+
RegistryOpts: opt.RegistryOptions{
1572+
Registry: &opt.OciOptions{
1573+
Reg: "ghcr.io",
1574+
Repo: "kcl-lang/helloworld",
1575+
PkgName: "helloworld",
1576+
Tag: "0.1.2",
1577+
},
1578+
},
1579+
}
1580+
1581+
_, err = kpmcli.AddDepWithOpts(kclPkg, &opts)
1582+
assert.Equal(t, err, nil)
1583+
1584+
modContent, err := os.ReadFile(pkgWithSumCheckPathMod)
1585+
modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "")
1586+
modContentStr = strings.ReplaceAll(modContentStr, "\n", "")
1587+
assert.Equal(t, err, nil)
1588+
1589+
modExpectContent, err := os.ReadFile(pkgWithSumCheckPathModExpect)
1590+
modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "")
1591+
modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "")
1592+
1593+
assert.Equal(t, err, nil)
1594+
assert.Equal(t, modContentStr, modExpectContentStr)
1595+
1596+
modLockContent, err := os.ReadFile(pkgWithSumCheckPathModLock)
1597+
modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "")
1598+
modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "")
1599+
assert.Equal(t, err, nil)
1600+
modLockExpectContent, err := os.ReadFile(pkgWithSumCheckPathModLockExpect)
1601+
modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "")
1602+
modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "")
1603+
assert.Equal(t, err, nil)
1604+
assert.Equal(t, modLockContentStr, modLockExpectContentStr)
1605+
1606+
defer func() {
1607+
_ = os.Remove(pkgWithSumCheckPathMod)
1608+
_ = os.Remove(pkgWithSumCheckPathModLock)
1609+
}()
1610+
}
1611+
1612+
func testUpdateDefaultRegistryDep(t *testing.T) {
1613+
pkgPath := getTestDir("update_with_default_dep")
1614+
1615+
pkgWithSumCheckPathModBak := filepath.Join(pkgPath, "kcl.mod.bak")
1616+
pkgWithSumCheckPathMod := filepath.Join(pkgPath, "kcl.mod")
1617+
pkgWithSumCheckPathModExpect := filepath.Join(pkgPath, "kcl.mod.expect")
1618+
1619+
pkgWithSumCheckPathModLockBak := filepath.Join(pkgPath, "kcl.mod.lock.bak")
1620+
pkgWithSumCheckPathModLock := filepath.Join(pkgPath, "kcl.mod.lock")
1621+
pkgWithSumCheckPathModLockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect")
1622+
1623+
err := copy.Copy(pkgWithSumCheckPathModBak, pkgWithSumCheckPathMod)
1624+
assert.Equal(t, err, nil)
1625+
err = copy.Copy(pkgWithSumCheckPathModLockBak, pkgWithSumCheckPathModLock)
1626+
assert.Equal(t, err, nil)
1627+
1628+
kpmcli, err := NewKpmClient()
1629+
assert.Equal(t, err, nil)
1630+
1631+
kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath)
1632+
assert.Equal(t, err, nil)
1633+
1634+
err = kpmcli.UpdateDeps(kclPkg)
1635+
assert.Equal(t, err, nil)
1636+
1637+
modContent, err := os.ReadFile(pkgWithSumCheckPathMod)
1638+
modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "")
1639+
modContentStr = strings.ReplaceAll(modContentStr, "\n", "")
1640+
assert.Equal(t, err, nil)
1641+
1642+
modExpectContent, err := os.ReadFile(pkgWithSumCheckPathModExpect)
1643+
modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "")
1644+
modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "")
1645+
1646+
assert.Equal(t, err, nil)
1647+
assert.Equal(t, modContentStr, modExpectContentStr)
1648+
1649+
modLockContent, err := os.ReadFile(pkgWithSumCheckPathModLock)
1650+
modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "")
1651+
modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "")
1652+
assert.Equal(t, err, nil)
1653+
modLockExpectContent, err := os.ReadFile(pkgWithSumCheckPathModLockExpect)
1654+
modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "")
1655+
modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "")
1656+
assert.Equal(t, err, nil)
1657+
assert.Equal(t, modLockContentStr, modLockExpectContentStr)
1658+
1659+
defer func() {
1660+
_ = os.Remove(pkgWithSumCheckPathMod)
1661+
_ = os.Remove(pkgWithSumCheckPathModLock)
1662+
}()
1663+
}
1664+
1665+
func testRunDefaultRegistryDep(t *testing.T) {
1666+
pkgPath := getTestDir("run_with_default_dep")
1667+
1668+
pkgWithSumCheckPathModBak := filepath.Join(pkgPath, "kcl.mod.bak")
1669+
pkgWithSumCheckPathMod := filepath.Join(pkgPath, "kcl.mod")
1670+
pkgWithSumCheckPathModExpect := filepath.Join(pkgPath, "kcl.mod.expect")
1671+
1672+
pkgWithSumCheckPathModLockBak := filepath.Join(pkgPath, "kcl.mod.lock.bak")
1673+
pkgWithSumCheckPathModLock := filepath.Join(pkgPath, "kcl.mod.lock")
1674+
pkgWithSumCheckPathModLockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect")
1675+
1676+
err := copy.Copy(pkgWithSumCheckPathModBak, pkgWithSumCheckPathMod)
1677+
assert.Equal(t, err, nil)
1678+
err = copy.Copy(pkgWithSumCheckPathModLockBak, pkgWithSumCheckPathModLock)
1679+
assert.Equal(t, err, nil)
1680+
1681+
kpmcli, err := NewKpmClient()
1682+
assert.Equal(t, err, nil)
1683+
1684+
kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath)
1685+
assert.Equal(t, err, nil)
1686+
1687+
opts := opt.DefaultCompileOptions()
1688+
opts.Merge(kcl.WithWorkDir(pkgPath)).Merge(kcl.WithKFilenames(filepath.Join(pkgPath, "main.k")))
1689+
compiler := runner.NewCompilerWithOpts(opts)
1690+
1691+
res, err := kpmcli.Compile(kclPkg, compiler)
1692+
assert.Equal(t, err, nil)
1693+
assert.Equal(t, res.GetRawYamlResult(), "a: Hello World!")
1694+
1695+
modContent, err := os.ReadFile(pkgWithSumCheckPathMod)
1696+
modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "")
1697+
modContentStr = strings.ReplaceAll(modContentStr, "\n", "")
1698+
assert.Equal(t, err, nil)
1699+
1700+
modExpectContent, err := os.ReadFile(pkgWithSumCheckPathModExpect)
1701+
modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "")
1702+
modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "")
1703+
1704+
assert.Equal(t, err, nil)
1705+
assert.Equal(t, modContentStr, modExpectContentStr)
1706+
1707+
modLockContent, err := os.ReadFile(pkgWithSumCheckPathModLock)
1708+
modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "")
1709+
modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "")
1710+
assert.Equal(t, err, nil)
1711+
modLockExpectContent, err := os.ReadFile(pkgWithSumCheckPathModLockExpect)
1712+
modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "")
1713+
modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "")
1714+
assert.Equal(t, err, nil)
1715+
assert.Equal(t, modLockContentStr, modLockExpectContentStr)
1716+
1717+
defer func() {
1718+
_ = os.Remove(pkgWithSumCheckPathMod)
1719+
_ = os.Remove(pkgWithSumCheckPathModLock)
1720+
}()
1721+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "add_with_default"
3+
edition = "v0.9.0"
4+
version = "0.0.1"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "add_with_default"
3+
edition = "v0.9.0"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
helloworld = "0.1.2"

pkg/client/test_data/add_with_default_dep/kcl.mod.lock.bak

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[dependencies]
2+
[dependencies.helloworld]
3+
name = "helloworld"
4+
full_name = "helloworld_0.1.2"
5+
version = "0.1.2"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program = 'Hello World!'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "add_with_default"
3+
edition = "v0.9.0"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
helloworld = "0.1.2"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "add_with_default"
3+
edition = "v0.9.0"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
helloworld = "0.1.2"

pkg/client/test_data/run_with_default_dep/kcl.mod.lock.bak

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[dependencies]
2+
[dependencies.helloworld]
3+
name = "helloworld"
4+
full_name = "helloworld_0.1.2"
5+
version = "0.1.2"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import helloworld as h
2+
3+
a = h.The_first_kcl_program

pkg/client/test_data/test_add_diff_version/no_sum_check/kcl.mod.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ edition = "0.0.1"
44
version = "0.0.1"
55

66
[dependencies]
7-
helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.2" }
7+
helloworld = "0.1.2"

pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ edition = "0.0.1"
44
version = "0.0.1"
55

66
[dependencies]
7-
helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.2" }
7+
helloworld = "0.1.2"
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +0,0 @@
1-
[dependencies]
2-
[dependencies.helloworld]
3-
name = "helloworld"
4-
full_name = "helloworld_0.1.2"
5-
version = "0.1.2"
6-
reg = "ghcr.io"
7-
repo = "kcl-lang/helloworld"
8-
oci_tag = "0.1.2"

pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.expect

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,3 @@
33
name = "helloworld"
44
full_name = "helloworld_0.1.2"
55
version = "0.1.2"
6-
reg = "ghcr.io"
7-
repo = "kcl-lang/helloworld"
8-
oci_tag = "0.1.2"

pkg/client/test_data/test_update/test_update_kcl_mod/expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,3 @@
33
name = "helloworld"
44
full_name = "helloworld_0.1.2"
55
version = "0.1.2"
6-
reg = "ghcr.io"
7-
repo = "kcl-lang/helloworld"
8-
oci_tag = "0.1.2"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "add_with_default"
3+
edition = "v0.9.0"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
helloworld = "0.1.2"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "add_with_default"
3+
edition = "v0.9.0"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
helloworld = "0.1.2"

pkg/client/test_data/update_with_default_dep/kcl.mod.lock.bak

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[dependencies]
2+
[dependencies.helloworld]
3+
name = "helloworld"
4+
full_name = "helloworld_0.1.2"
5+
version = "0.1.2"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program = 'Hello World!'

pkg/cmd/cmd_add.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,15 @@ func parseGitRegistryOptions(c *cli.Context) (*opt.RegistryOptions, *reporter.Kp
216216
}
217217

218218
// parseOciRegistryOptions will parse the oci registry information from user cli inputs.
219-
func parseOciRegistryOptions(c *cli.Context, kpmcli *client.KpmClient) (*opt.RegistryOptions, error) {
219+
func parseRegistryOptions(c *cli.Context, kpmcli *client.KpmClient) (*opt.RegistryOptions, error) {
220220
ociPkgRef := c.Args().First()
221221
name, version, err := opt.ParseOciPkgNameAndVersion(ociPkgRef)
222222
if err != nil {
223223
return nil, err
224224
}
225225

226226
return &opt.RegistryOptions{
227-
Oci: &opt.OciOptions{
227+
Registry: &opt.OciOptions{
228228
Reg: kpmcli.GetSettings().DefaultOciRegistry(),
229229
Repo: kpmcli.GetSettings().DefaultOciRepo(),
230230
PkgName: name,

0 commit comments

Comments
 (0)