Skip to content

Commit

Permalink
feat: add api to get the dependencies from kcl.mod
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe committed Mar 21, 2024
1 parent 2f9aaa8 commit a16b3ff
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/api/kpm_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ func (pkg *KclPackage) GetDependencies() pkg.Dependencies {
return pkg.pkg.Dependencies
}

// GetDependenciesInModFile returns the mod file of the package.
func (pkg *KclPackage) GetDependenciesInModFile() *pkg.Dependencies {
return &pkg.pkg.ModFile.Dependencies
}

// GetPkgHomePath returns the home path of the package.
func (pkg *KclPackage) GetPkgHomePath() string {
return pkg.pkg.HomePath
Expand Down
13 changes: 13 additions & 0 deletions pkg/api/kpm_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ func TestPackageApi(t *testing.T) {
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName")
}

func TestApiGetDependenciesInModFile(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_get_mod_deps"), "kcl_pkg")
pkg, err := GetKclPackage(pkg_path)
assert.Equal(t, err, nil)
dep := pkg.GetDependenciesInModFile().Deps["k8s"]
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")
}

func TestGetAllSchemaTypesMappingNamed(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg")
pkg, err := GetKclPackage(pkg_path)
Expand Down
7 changes: 7 additions & 0 deletions pkg/api/test_data/test_get_mod_deps/kcl_pkg/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "kcl_pkg"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
k8s = "1.27"
34 changes: 34 additions & 0 deletions pkg/api/test_data/test_get_mod_deps/kcl_pkg/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import sub
import sub.sub1 as s1
import k8s.api.core.v1 as k8core

schema SchemaInMainK:
msg: str

schema SchemaWithSameName:
msg: str

schema_in_main_k = SchemaInMainK {
msg='I am the instance of SchemaInMainK'
}

schema_in_sub_k = sub.SchemaInSubK {
msg='I am the instance of SchemaInSubK'
}

schema_with_same_name = SchemaWithSameName {
msg='I am the instance of SchemaWithSameName in main.k'
}

schema_with_same_name_in_sub = s1.SchemaWithSameName {
msg='I am the instance of SchemaWithSameName in sub.k'
}

schema_in_k8s = k8core.Pod {
metadata.name = "web-app"
spec.containers = [{
name = "main-container"
image = "nginx"
ports = [{containerPort: 80}]
}]
}
2 changes: 2 additions & 0 deletions pkg/api/test_data/test_get_mod_deps/kcl_pkg/sub/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema SchemaInSubK:
msg: str
2 changes: 2 additions & 0 deletions pkg/api/test_data/test_get_mod_deps/kcl_pkg/sub/sub1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema SchemaInSubSub1K:
msg: str
2 changes: 2 additions & 0 deletions pkg/api/test_data/test_get_mod_deps/kcl_pkg/sub/sub1/main2.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema SchemaWithSameName:
msg: str

0 comments on commit a16b3ff

Please sign in to comment.