Skip to content

Commit

Permalink
refactor: replaced smaller test cases and added locks for some test c…
Browse files Browse the repository at this point in the history
…ases that could not be concurrent

Signed-off-by: zongzhe <[email protected]>
  • Loading branch information
zong-zhe committed Oct 14, 2024
1 parent 167011e commit 7cb9a4f
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 76 deletions.
37 changes: 27 additions & 10 deletions pkg/api/kpm_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,24 @@ import (
"kcl-lang.io/kpm/pkg/client"
)

func TestPackageApi(t *testing.T) {
func TestAllSchemaTypeMappings(t *testing.T) {
t.Run("TestPackageApi", func(t *testing.T) {
testPackageApi(t)
fmt.Println("TestPackageApi completed")
})

t.Run("TestGetAllSchemaTypesMappingNamed", func(t *testing.T) {
testGetAllSchemaTypesMappingNamed(t)
fmt.Println("TestGetAllSchemaTypesMappingNamed completed")
})

t.Run("TestGetSchemaTypeMappingWithFilters", func(t *testing.T) {
testGetSchemaTypeMappingWithFilters(t)
fmt.Println("TestGetSchemaTypeMappingWithFilters completed")
})
}

func testPackageApi(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg")
kcl_pkg_path, err := GetKclPkgPath()
assert.Equal(t, err, nil)
Expand All @@ -24,15 +41,15 @@ func TestPackageApi(t *testing.T) {
assert.Equal(t, pkg.GetEdition(), "0.0.1")
assert.Equal(t, pkg.GetDependencies().Deps.Len(), 1)

dep, _ := pkg.GetDependencies().Deps.Get("k8s")
assert.Equal(t, dep.Name, "k8s")
assert.Equal(t, dep.FullName, "k8s_1.27")
assert.Equal(t, dep.Version, "1.27")
dep, _ := pkg.GetDependencies().Deps.Get("helloworld")
assert.Equal(t, dep.Name, "helloworld")
assert.Equal(t, dep.FullName, "helloworld_0.1.3")
assert.Equal(t, dep.Version, "0.1.3")
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")
assert.Equal(t, dep.Source.Registry.Oci.Repo, "kcl-lang/helloworld")
assert.Equal(t, dep.Source.Registry.Oci.Tag, "0.1.3")

assert.Equal(t, dep.GetLocalFullPath(""), filepath.Join(kcl_pkg_path, "k8s_1.27"))
assert.Equal(t, dep.GetLocalFullPath(""), filepath.Join(kcl_pkg_path, "helloworld_0.1.3"))

schemas, err := pkg.GetAllSchemaTypeMapping()
assert.Equal(t, err, nil)
Expand Down Expand Up @@ -71,7 +88,7 @@ func TestApiGetDependenciesInModFile(t *testing.T) {
assert.Equal(t, dep.Source.Registry.Oci.Tag, "1.27")
}

func TestGetAllSchemaTypesMappingNamed(t *testing.T) {
func testGetAllSchemaTypesMappingNamed(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg")
pkg, err := GetKclPackage(pkg_path)
assert.Equal(t, err, nil)
Expand All @@ -96,7 +113,7 @@ func TestGetAllSchemaTypesMappingNamed(t *testing.T) {
assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName")
}

func TestGetSchemaTypeMappingWithFilters(t *testing.T) {
func testGetSchemaTypeMappingWithFilters(t *testing.T) {
pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg")
pkg, err := GetKclPackage(pkg_path)
assert.Equal(t, err, nil)
Expand Down
7 changes: 6 additions & 1 deletion pkg/api/kpm_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/opt"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/test"
"kcl-lang.io/kpm/pkg/utils"
)

Expand Down Expand Up @@ -155,7 +156,7 @@ func TestRunTarPkg(t *testing.T) {
}
}

func TestRunWithNoSumCheck(t *testing.T) {
func testRunWithNoSumCheck(t *testing.T) {
pkgPath := getTestDir("test_run_with_nosumcheck")
opts := opt.DefaultCompileOptions()
opts.SetPkgPath(pkgPath)
Expand Down Expand Up @@ -224,3 +225,7 @@ func TestStoreModAndModLockFile(t *testing.T) {
err = testPackage.StoreModLockFile()
assert.Equal(t, err, nil)
}

func TestRunWithLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "TestRunWithNoSumCheck", testRunWithNoSumCheck)
}
2 changes: 1 addition & 1 deletion pkg/api/test_data/test_kpm_package/kcl_pkg/kcl.mod
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]
k8s = "1.27"
helloworld = "0.1.3"
9 changes: 4 additions & 5 deletions pkg/api/test_data/test_kpm_package/kcl_pkg/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[dependencies]
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.27"
version = "1.27"
sum = "xnYM1FWHAy3m+KcQMQb2rjZouTxumqYt6FGZpu2T4yM="
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.3"
version = "0.1.3"
11 changes: 3 additions & 8 deletions pkg/api/test_data/test_kpm_package/kcl_pkg/main.k
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sub
import sub.sub1 as s1
import k8s.api.core.v1 as k8core
import helloworld

schema SchemaInMainK:
msg: str
Expand All @@ -24,11 +24,6 @@ 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}]
}]
schema_in_k8s = helloworld.HelloWorldSchema {
msg='I am the instance of HelloWorldSchema'
}
Loading

0 comments on commit 7cb9a4f

Please sign in to comment.