Skip to content

Commit

Permalink
Merge pull request #365 from zong-zhe/fix-oci-replace
Browse files Browse the repository at this point in the history
feat: remove the replace for default registry dep
  • Loading branch information
Peefy committed Jun 19, 2024
2 parents 02fd238 + 32df960 commit e7f583a
Show file tree
Hide file tree
Showing 39 changed files with 377 additions and 60 deletions.
6 changes: 5 additions & 1 deletion pkg/api/kpm_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func NewKclTypes(name, path string, tys *gpyrpc.KclType) *KclType {
//
// 'kcl_pkg_path' is the path of dependencies download by kpm.
func GetKclPackage(pkgPath string) (*KclPackage, error) {
kclPkg, err := pkg.LoadKclPkg(pkgPath)
kpmcli, err := client.NewKpmClient()
if err != nil {
return nil, err
}
kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/kpm_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ func TestApiGetDependenciesInModFile(t *testing.T) {
assert.Equal(t, dep.Name, "k8s")
assert.Equal(t, dep.FullName, "k8s_1.27")
assert.Equal(t, dep.Version, "1.27")
assert.Equal(t, dep.Source.Oci.Reg, "ghcr.io")
assert.Equal(t, dep.Source.Oci.Repo, "kcl-lang/k8s")
assert.Equal(t, dep.Source.Oci.Tag, "1.27")
assert.Equal(t, dep.Source.Registry.Oci.Reg, "ghcr.io")
assert.Equal(t, dep.Source.Registry.Oci.Repo, "kcl-lang/k8s")
assert.Equal(t, dep.Source.Registry.Oci.Tag, "1.27")
}

func TestGetAllSchemaTypesMappingNamed(t *testing.T) {
Expand Down
32 changes: 28 additions & 4 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ func (c *KpmClient) getDepStorePath(search_path string, d *pkg.Dependency, isVen
} else {
storePkgName = fmt.Sprintf(PKG_NAME_PATTERN, d.Name, d.Source.Git.Branch)
}
} else if d.Source.Registry != nil {
storePkgName = fmt.Sprintf(PKG_NAME_PATTERN, d.Name, d.Source.Registry.Version)
} else {
storePkgName = fmt.Sprintf(PKG_NAME_PATTERN, d.Name, d.Version)
}
Expand Down Expand Up @@ -891,6 +893,18 @@ func (c *KpmClient) FillDepInfo(dep *pkg.Dependency, homepath string) error {
dep.Source.Oci.Repo = urlpath
}
}
if dep.Source.Registry != nil {
if len(dep.Source.Registry.Reg) == 0 {
dep.Source.Registry.Reg = c.GetSettings().DefaultOciRegistry()
}

if len(dep.Source.Registry.Repo) == 0 {
urlpath := utils.JoinPath(c.GetSettings().DefaultOciRepo(), dep.Name)
dep.Source.Registry.Repo = urlpath
}

dep.Version = dep.Source.Registry.Version
}
return nil
}

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

if dep.Source.Oci != nil {
if dep.Source.Oci != nil || dep.Source.Registry != nil {
var ociSource *pkg.Oci
if dep.Source.Oci != nil {
ociSource = dep.Source.Oci
} else if dep.Source.Registry != nil {
ociSource = dep.Source.Registry.Oci
}
// Select the latest tag, if the tag, the user inputed, is empty.
if dep.Source.Oci.Tag == "" || dep.Source.Oci.Tag == constants.LATEST {
latestTag, err := c.AcquireTheLatestOciVersion(*dep.Source.Oci)
if ociSource.Tag == "" || ociSource.Tag == constants.LATEST {
latestTag, err := c.AcquireTheLatestOciVersion(*ociSource)
if err != nil {
return nil, err
}
dep.Source.Oci.Tag = latestTag
ociSource.Tag = latestTag

if dep.Source.Registry != nil {
dep.Source.Registry.Tag = latestTag
}

// Complete some information that the local three dependencies depend on.
// The invalid path such as '$HOME/.kcl/kpm/k8s_' is placed because the version field is missing.
Expand Down
179 changes: 179 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,9 @@ func TestOciDownloader(t *testing.T) {
// make test case running in order to test the log output
testRunWithOciDownloader(t)
testAddWithOciDownloader(t)
testAddDefaultRegistryDep(t)
testUpdateDefaultRegistryDep(t)
testRunDefaultRegistryDep(t)
}

func testAddWithOciDownloader(t *testing.T) {
Expand Down Expand Up @@ -1540,3 +1543,179 @@ func TestAddLocalPath(t *testing.T) {
_ = os.Remove(filepath.Join(path, "kcl.mod"))
}()
}

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")

pkgWithSumCheckPathModLockBak := filepath.Join(pkgPath, "kcl.mod.lock.bak")
pkgWithSumCheckPathModLock := filepath.Join(pkgPath, "kcl.mod.lock")
pkgWithSumCheckPathModLockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect")

err := copy.Copy(pkgWithSumCheckPathModBak, pkgWithSumCheckPathMod)
assert.Equal(t, err, nil)
err = copy.Copy(pkgWithSumCheckPathModLockBak, pkgWithSumCheckPathModLock)
assert.Equal(t, err, nil)

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

kclPkg, err := kpmcli.LoadPkgFromPath(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",
},
},
}

_, 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)

modExpectContent, err := os.ReadFile(pkgWithSumCheckPathModExpect)
modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "")
modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "")

assert.Equal(t, err, nil)
assert.Equal(t, modContentStr, modExpectContentStr)

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", "")
assert.Equal(t, err, nil)
assert.Equal(t, modLockContentStr, modLockExpectContentStr)

defer func() {
_ = os.Remove(pkgWithSumCheckPathMod)
_ = os.Remove(pkgWithSumCheckPathModLock)
}()
}

func testUpdateDefaultRegistryDep(t *testing.T) {
pkgPath := getTestDir("update_with_default_dep")

pkgWithSumCheckPathModBak := filepath.Join(pkgPath, "kcl.mod.bak")
pkgWithSumCheckPathMod := filepath.Join(pkgPath, "kcl.mod")
pkgWithSumCheckPathModExpect := filepath.Join(pkgPath, "kcl.mod.expect")

pkgWithSumCheckPathModLockBak := filepath.Join(pkgPath, "kcl.mod.lock.bak")
pkgWithSumCheckPathModLock := filepath.Join(pkgPath, "kcl.mod.lock")
pkgWithSumCheckPathModLockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect")

err := copy.Copy(pkgWithSumCheckPathModBak, pkgWithSumCheckPathMod)
assert.Equal(t, err, nil)
err = copy.Copy(pkgWithSumCheckPathModLockBak, pkgWithSumCheckPathModLock)
assert.Equal(t, err, nil)

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

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

err = kpmcli.UpdateDeps(kclPkg)
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)

modExpectContent, err := os.ReadFile(pkgWithSumCheckPathModExpect)
modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "")
modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "")

assert.Equal(t, err, nil)
assert.Equal(t, modContentStr, modExpectContentStr)

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", "")
assert.Equal(t, err, nil)
assert.Equal(t, modLockContentStr, modLockExpectContentStr)

defer func() {
_ = os.Remove(pkgWithSumCheckPathMod)
_ = os.Remove(pkgWithSumCheckPathModLock)
}()
}

func testRunDefaultRegistryDep(t *testing.T) {
pkgPath := getTestDir("run_with_default_dep")

pkgWithSumCheckPathModBak := filepath.Join(pkgPath, "kcl.mod.bak")
pkgWithSumCheckPathMod := filepath.Join(pkgPath, "kcl.mod")
pkgWithSumCheckPathModExpect := filepath.Join(pkgPath, "kcl.mod.expect")

pkgWithSumCheckPathModLockBak := filepath.Join(pkgPath, "kcl.mod.lock.bak")
pkgWithSumCheckPathModLock := filepath.Join(pkgPath, "kcl.mod.lock")
pkgWithSumCheckPathModLockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect")

err := copy.Copy(pkgWithSumCheckPathModBak, pkgWithSumCheckPathMod)
assert.Equal(t, err, nil)
err = copy.Copy(pkgWithSumCheckPathModLockBak, pkgWithSumCheckPathModLock)
assert.Equal(t, err, nil)

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

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

opts := opt.DefaultCompileOptions()
opts.Merge(kcl.WithWorkDir(pkgPath)).Merge(kcl.WithKFilenames(filepath.Join(pkgPath, "main.k")))
compiler := runner.NewCompilerWithOpts(opts)

res, err := kpmcli.Compile(kclPkg, compiler)
assert.Equal(t, err, nil)
assert.Equal(t, res.GetRawYamlResult(), "a: Hello World!")

modContent, err := os.ReadFile(pkgWithSumCheckPathMod)
modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "")
modContentStr = strings.ReplaceAll(modContentStr, "\n", "")
assert.Equal(t, err, nil)

modExpectContent, err := os.ReadFile(pkgWithSumCheckPathModExpect)
modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "")
modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "")

assert.Equal(t, err, nil)
assert.Equal(t, modContentStr, modExpectContentStr)

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", "")
assert.Equal(t, err, nil)
assert.Equal(t, modLockContentStr, modLockExpectContentStr)

defer func() {
_ = os.Remove(pkgWithSumCheckPathMod)
_ = os.Remove(pkgWithSumCheckPathModLock)
}()
}
4 changes: 4 additions & 0 deletions pkg/client/test_data/add_with_default_dep/kcl.mod.bak
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"
7 changes: 7 additions & 0 deletions pkg/client/test_data/add_with_default_dep/kcl.mod.expect
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.
5 changes: 5 additions & 0 deletions pkg/client/test_data/add_with_default_dep/kcl.mod.lock.expect
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"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_default_dep/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
7 changes: 7 additions & 0 deletions pkg/client/test_data/run_with_default_dep/kcl.mod.bak
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"
7 changes: 7 additions & 0 deletions pkg/client/test_data/run_with_default_dep/kcl.mod.expect
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.
5 changes: 5 additions & 0 deletions pkg/client/test_data/run_with_default_dep/kcl.mod.lock.expect
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"
3 changes: 3 additions & 0 deletions pkg/client/test_data/run_with_default_dep/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import helloworld as h

a = h.The_first_kcl_program
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ edition = "0.0.1"
version = "0.0.1"

[dependencies]
helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.2" }
helloworld = "0.1.2"
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ edition = "0.0.1"
version = "0.0.1"

[dependencies]
helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.2" }
helloworld = "0.1.2"
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.2"
version = "0.1.2"
reg = "ghcr.io"
repo = "kcl-lang/helloworld"
oci_tag = "0.1.2"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@
name = "helloworld"
full_name = "helloworld_0.1.2"
version = "0.1.2"
reg = "ghcr.io"
repo = "kcl-lang/helloworld"
oci_tag = "0.1.2"
3 changes: 0 additions & 3 deletions pkg/client/test_data/test_update/test_update_kcl_mod/expected
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@
name = "helloworld"
full_name = "helloworld_0.1.2"
version = "0.1.2"
reg = "ghcr.io"
repo = "kcl-lang/helloworld"
oci_tag = "0.1.2"
7 changes: 7 additions & 0 deletions pkg/client/test_data/update_with_default_dep/kcl.mod.bak
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"
7 changes: 7 additions & 0 deletions pkg/client/test_data/update_with_default_dep/kcl.mod.expect
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"
1 change: 1 addition & 0 deletions pkg/client/test_data/update_with_default_dep/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
4 changes: 2 additions & 2 deletions pkg/cmd/cmd_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@ func parseGitRegistryOptions(c *cli.Context) (*opt.RegistryOptions, *reporter.Kp
}

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

return &opt.RegistryOptions{
Oci: &opt.OciOptions{
Registry: &opt.OciOptions{
Reg: kpmcli.GetSettings().DefaultOciRegistry(),
Repo: kpmcli.GetSettings().DefaultOciRepo(),
PkgName: name,
Expand Down
Loading

0 comments on commit e7f583a

Please sign in to comment.