kcl-lang.io/kpm@v0.8.7-0.20240520061008-9fc4c5efc8c7/pkg/api/kpm_pkg_test.go (about) 1 package api 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "gotest.tools/v3/assert" 8 "kcl-lang.io/kcl-go/pkg/kcl" 9 "kcl-lang.io/kpm/pkg/client" 10 "kcl-lang.io/kpm/pkg/opt" 11 ) 12 13 func TestPackageApi(t *testing.T) { 14 pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg") 15 kcl_pkg_path, err := GetKclPkgPath() 16 assert.Equal(t, err, nil) 17 kpmcli, err := client.NewKpmClient() 18 assert.Equal(t, err, nil) 19 pkg, err := GetKclPackage(pkg_path) 20 assert.Equal(t, err, nil) 21 err = kpmcli.ResolvePkgDepsMetadata(pkg.pkg, true) 22 assert.Equal(t, err, nil) 23 assert.Equal(t, pkg.GetPkgName(), "kcl_pkg") 24 assert.Equal(t, pkg.GetVersion(), "0.0.1") 25 assert.Equal(t, pkg.GetEdition(), "0.0.1") 26 assert.Equal(t, len(pkg.GetDependencies().Deps), 1) 27 28 dep := pkg.GetDependencies().Deps["k8s"] 29 assert.Equal(t, dep.Name, "k8s") 30 assert.Equal(t, dep.FullName, "k8s_1.27") 31 assert.Equal(t, dep.Version, "1.27") 32 assert.Equal(t, dep.Sum, "xnYM1FWHAy3m+KcQMQb2rjZouTxumqYt6FGZpu2T4yM=") 33 assert.Equal(t, dep.Source.Oci.Reg, "ghcr.io") 34 assert.Equal(t, dep.Source.Oci.Repo, "kcl-lang/k8s") 35 assert.Equal(t, dep.Source.Oci.Tag, "1.27") 36 37 assert.Equal(t, dep.GetLocalFullPath(""), filepath.Join(kcl_pkg_path, "k8s_1.27")) 38 39 schemas, err := pkg.GetAllSchemaTypeMapping() 40 assert.Equal(t, err, nil) 41 assert.Equal(t, len(schemas), 3) 42 assert.Equal(t, len(schemas["."]), 4) 43 assert.Equal(t, len(schemas[filepath.Join("sub")]), 1) 44 assert.Equal(t, len(schemas[filepath.Join("sub", "sub1")]), 2) 45 46 // All schema types under the root path 47 assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMainK"].Type, "schema") 48 assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMainK"].SchemaName, "SchemaInMainK") 49 assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].Type, "schema") 50 assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName") 51 52 // All schema types under the root_path/sub path 53 assert.Equal(t, schemas[filepath.Join("sub")]["SchemaInSubK"].Type, "schema") 54 assert.Equal(t, schemas[filepath.Join("sub")]["SchemaInSubK"].SchemaName, "SchemaInSubK") 55 56 // All schema types under the root_path/sub/sub1 path 57 assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaInSubSub1K"].Type, "schema") 58 assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaInSubSub1K"].SchemaName, "SchemaInSubSub1K") 59 assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].Type, "schema") 60 assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName") 61 } 62 63 func TestApiGetDependenciesInModFile(t *testing.T) { 64 pkg_path := filepath.Join(getTestDir("test_get_mod_deps"), "kcl_pkg") 65 pkg, err := GetKclPackage(pkg_path) 66 assert.Equal(t, err, nil) 67 dep := pkg.GetDependenciesInModFile().Deps["k8s"] 68 assert.Equal(t, dep.Name, "k8s") 69 assert.Equal(t, dep.FullName, "k8s_1.27") 70 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") 74 } 75 76 func TestGetAllSchemaTypesMappingNamed(t *testing.T) { 77 pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg") 78 pkg, err := GetKclPackage(pkg_path) 79 assert.Equal(t, err, nil) 80 kpmcli, err := client.NewKpmClient() 81 assert.Equal(t, err, nil) 82 83 err = kpmcli.ResolvePkgDepsMetadata(pkg.pkg, true) 84 assert.Equal(t, err, nil) 85 86 schemas, err := pkg.GetSchemaTypeMappingNamed("SchemaWithSameName") 87 assert.Equal(t, err, nil) 88 assert.Equal(t, len(schemas), 2) 89 assert.Equal(t, len(schemas["."]), 1) 90 assert.Equal(t, len(schemas[filepath.Join("sub", "sub1")]), 1) 91 92 // // All schema types under the root path 93 assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].Type, "schema") 94 assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName") 95 96 // // All schema types under the root_path/sub/sub1 path 97 assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].Type, "schema") 98 assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName") 99 } 100 101 func TestGetSchemaTypeMappingWithFilters(t *testing.T) { 102 pkg_path := filepath.Join(getTestDir("test_kpm_package"), "kcl_pkg") 103 pkg, err := GetKclPackage(pkg_path) 104 assert.Equal(t, err, nil) 105 kpmcli, err := client.NewKpmClient() 106 assert.Equal(t, err, nil) 107 err = kpmcli.ResolvePkgDepsMetadata(pkg.pkg, true) 108 assert.Equal(t, err, nil) 109 110 filterFunc := func(kt *KclType) bool { 111 return kt.Type != "schema" 112 } 113 schemas, err := pkg.GetSchemaTypeMappingWithFilters([]KclTypeFilterFunc{filterFunc}) 114 assert.Equal(t, err, nil) 115 assert.Equal(t, len(schemas), 0) 116 117 filterFunc = func(kt *KclType) bool { 118 return kt.SchemaName == "SchemaWithSameName" 119 } 120 schemas, err = pkg.GetSchemaTypeMappingWithFilters([]KclTypeFilterFunc{filterFunc}) 121 assert.Equal(t, err, nil) 122 assert.Equal(t, len(schemas), 2) 123 assert.Equal(t, len(schemas[filepath.Join(".")]), 3) 124 assert.Equal(t, len(schemas[filepath.Join("sub", "sub1")]), 1) 125 assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].Type, "schema") 126 assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName") 127 assert.Equal(t, schemas[filepath.Join(".")]["schema_with_same_name"].Type, "schema") 128 assert.Equal(t, schemas[filepath.Join(".")]["schema_with_same_name"].SchemaName, "SchemaWithSameName") 129 assert.Equal(t, schemas[filepath.Join(".")]["schema_with_same_name_in_sub"].Type, "schema") 130 assert.Equal(t, schemas[filepath.Join(".")]["schema_with_same_name_in_sub"].SchemaName, "SchemaWithSameName") 131 assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].Type, "schema") 132 assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName") 133 134 filterFunc = func(kt *KclType) bool { 135 return kt.SchemaName == "SchemaWithSameName" && kt.Name == "SchemaWithSameName" 136 } 137 schemas, err = pkg.GetSchemaTypeMappingWithFilters([]KclTypeFilterFunc{filterFunc}) 138 assert.Equal(t, err, nil) 139 assert.Equal(t, len(schemas), 2) 140 assert.Equal(t, len(schemas[filepath.Join(".")]), 1) 141 assert.Equal(t, len(schemas[filepath.Join("sub", "sub1")]), 1) 142 assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].Type, "schema") 143 assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName") 144 assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].Name, "SchemaWithSameName") 145 assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].RelPath, ".") 146 assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].Type, "schema") 147 assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].SchemaName, "SchemaWithSameName") 148 assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].Name, "SchemaWithSameName") 149 assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].RelPath, filepath.Join("sub", "sub1")) 150 } 151 152 func TestGetFullSchemaTypeMappingWithFilters(t *testing.T) { 153 pkg_path := filepath.Join(getTestDir("test_kpm_package"), "get_schema_ty", "aaa") 154 pkg, err := GetKclPackage(pkg_path) 155 assert.Equal(t, err, nil) 156 kpmcli, err := client.NewKpmClient() 157 assert.Equal(t, err, nil) 158 err = kpmcli.ResolvePkgDepsMetadata(pkg.pkg, true) 159 assert.Equal(t, err, nil) 160 161 filterFunc := func(kt *KclType) bool { 162 return kt.Type == "schema" 163 } 164 165 schemas, err := pkg.GetFullSchemaTypeMappingWithFilters(kpmcli, []KclTypeFilterFunc{filterFunc}) 166 assert.Equal(t, err, nil) 167 assert.Equal(t, len(schemas), 1) 168 169 assert.Equal(t, schemas[filepath.Join(".")]["B"].Type, "schema") 170 assert.Equal(t, schemas[filepath.Join(".")]["B"].SchemaName, "B") 171 } 172 173 func TestGetSchemaTypeUnderEmptyDir(t *testing.T) { 174 pkg_path := filepath.Join(getTestDir("test_kpm_package"), "no_kcl_files") 175 pkg, err := GetKclPackage(pkg_path) 176 assert.Equal(t, err, nil) 177 kpmcli, err := client.NewKpmClient() 178 assert.Equal(t, err, nil) 179 err = kpmcli.ResolvePkgDepsMetadata(pkg.pkg, true) 180 assert.Equal(t, err, nil) 181 schemas, err := pkg.GetSchemaTypeMappingNamed("SchemaInMain") 182 assert.Equal(t, err, nil) 183 assert.Equal(t, len(schemas), 1) 184 assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMain"].Type, "schema") 185 assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMain"].SchemaName, "SchemaInMain") 186 } 187 188 func TestGetEntries(t *testing.T) { 189 testPath := getTestDir("test_get_entries") 190 pkgPath := filepath.Join(testPath, "no_entries") 191 pkg, err := GetKclPackage(pkgPath) 192 assert.Equal(t, err, nil) 193 assert.Equal(t, len(pkg.GetPkgProfile().GetEntries()), 0) 194 195 pkgPath = filepath.Join(testPath, "with_path_entries") 196 pkg, err = GetKclPackage(pkgPath) 197 assert.Equal(t, err, nil) 198 assert.Equal(t, len(pkg.GetPkgProfile().GetEntries()), 1) 199 200 res, err := RunWithOpts( 201 opt.WithEntries(pkg.GetPkgProfile().GetEntries()), 202 opt.WithKclOption(kcl.WithWorkDir(pkgPath)), 203 ) 204 205 assert.Equal(t, err, nil) 206 assert.Equal(t, res.GetRawYamlResult(), "sub: test in sub") 207 assert.Equal(t, res.GetRawJsonResult(), "{\"sub\": \"test in sub\"}") 208 }