From 3f9b848e9d9bcef71cb3301d3fa4e90076b3a364 Mon Sep 17 00:00:00 2001 From: zongz Date: Tue, 22 Aug 2023 17:43:09 +0800 Subject: [PATCH] fix: fix the 'No kcl files' error in api. --- pkg/api/kpm_pkg.go | 30 ++++++++++--------- pkg/api/kpm_pkg_test.go | 16 ++++++++++ .../no_kcl_files/ci-test/settings.yaml | 0 .../test_kpm_package/no_kcl_files/kcl.mod | 5 ++++ .../no_kcl_files/kcl.mod.lock | 0 .../test_kpm_package/no_kcl_files/main.k | 2 ++ pkg/errors/errors.go | 3 ++ 7 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 pkg/api/test_data/test_kpm_package/no_kcl_files/ci-test/settings.yaml create mode 100644 pkg/api/test_data/test_kpm_package/no_kcl_files/kcl.mod create mode 100644 pkg/api/test_data/test_kpm_package/no_kcl_files/kcl.mod.lock create mode 100644 pkg/api/test_data/test_kpm_package/no_kcl_files/main.k diff --git a/pkg/api/kpm_pkg.go b/pkg/api/kpm_pkg.go index 5f14850a..89c3fe16 100644 --- a/pkg/api/kpm_pkg.go +++ b/pkg/api/kpm_pkg.go @@ -6,6 +6,7 @@ import ( "kcl-lang.io/kcl-go/pkg/kcl" "kcl-lang.io/kcl-go/pkg/spec/gpyrpc" + "kcl-lang.io/kpm/pkg/errors" pkg "kcl-lang.io/kpm/pkg/package" ) @@ -119,7 +120,7 @@ func (pkg *KclPackage) GetSchemaTypeMappingWithFilters(fileterFuncs []KclTypeFil if info.IsDir() { fileteredKtypeMap := make(map[string]*KclType) schemaTypeMap, err := kcl.GetSchemaTypeMapping(path, "", "") - if err != nil { + if err != nil && err.Error() != errors.NoKclFiles.Error() { return err } @@ -127,23 +128,24 @@ func (pkg *KclPackage) GetSchemaTypeMappingWithFilters(fileterFuncs []KclTypeFil if err != nil { return err } - - for kName, kType := range schemaTypeMap { - kTy := NewKclTypes(kName, relPath, kType) - filterPassed := true - for _, filterFunc := range fileterFuncs { - if !filterFunc(kTy) { - filterPassed = false - break + if schemaTypeMap != nil { + for kName, kType := range schemaTypeMap { + kTy := NewKclTypes(kName, relPath, kType) + filterPassed := true + for _, filterFunc := range fileterFuncs { + if !filterFunc(kTy) { + filterPassed = false + break + } + } + if filterPassed { + fileteredKtypeMap[kName] = kTy } } - if filterPassed { - fileteredKtypeMap[kName] = kTy + if len(fileteredKtypeMap) > 0 { + schemaTypes[relPath] = fileteredKtypeMap } } - if len(fileteredKtypeMap) > 0 { - schemaTypes[relPath] = fileteredKtypeMap - } } return nil diff --git a/pkg/api/kpm_pkg_test.go b/pkg/api/kpm_pkg_test.go index 57a5e584..2951be61 100644 --- a/pkg/api/kpm_pkg_test.go +++ b/pkg/api/kpm_pkg_test.go @@ -135,3 +135,19 @@ func TestGetSchemaTypeMappingWithFilters(t *testing.T) { assert.Equal(t, schemas[filepath.Join(".")]["SchemaWithSameName"].Name, "SchemaWithSameName") assert.Equal(t, schemas[filepath.Join("sub", "sub1")]["SchemaWithSameName"].RelPath, filepath.Join("sub", "sub1")) } + +func TestGetSchemaTypeUnderEmptyDir(t *testing.T) { + pkg_path := filepath.Join(getTestDir("test_kpm_package"), "no_kcl_files") + kcl_pkg_path, err := GetKclPkgPath() + + assert.Equal(t, err, nil) + pkg, err := GetKclPackage(pkg_path) + assert.Equal(t, err, nil) + err = pkg.pkg.ResolveDepsMetadata(kcl_pkg_path, true) + assert.Equal(t, err, nil) + schemas, err := pkg.GetSchemaTypeMappingNamed("SchemaInMain") + assert.Equal(t, err, nil) + assert.Equal(t, len(schemas), 1) + assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMain"].Type, "schema") + assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMain"].SchemaName, "SchemaInMain") +} diff --git a/pkg/api/test_data/test_kpm_package/no_kcl_files/ci-test/settings.yaml b/pkg/api/test_data/test_kpm_package/no_kcl_files/ci-test/settings.yaml new file mode 100644 index 00000000..e69de29b diff --git a/pkg/api/test_data/test_kpm_package/no_kcl_files/kcl.mod b/pkg/api/test_data/test_kpm_package/no_kcl_files/kcl.mod new file mode 100644 index 00000000..b77eb1f4 --- /dev/null +++ b/pkg/api/test_data/test_kpm_package/no_kcl_files/kcl.mod @@ -0,0 +1,5 @@ +[package] +name = "no_kcl_files" +edition = "0.0.1" +version = "0.0.1" + diff --git a/pkg/api/test_data/test_kpm_package/no_kcl_files/kcl.mod.lock b/pkg/api/test_data/test_kpm_package/no_kcl_files/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/pkg/api/test_data/test_kpm_package/no_kcl_files/main.k b/pkg/api/test_data/test_kpm_package/no_kcl_files/main.k new file mode 100644 index 00000000..b2b71d30 --- /dev/null +++ b/pkg/api/test_data/test_kpm_package/no_kcl_files/main.k @@ -0,0 +1,2 @@ +schema SchemaInMain: + msg: str \ No newline at end of file diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index fb2e6cc3..d7ae921e 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -52,3 +52,6 @@ var PathIsEmpty = errors.New("path is empty.") var InvalidPkg = errors.New("invalid kcl package.") var InvalidOciUrl = errors.New("invalid oci url.") var UnknownEnv = errors.New("invalid environment variable.") + +// No kcl files +var NoKclFiles = errors.New("No input KCL files")