Skip to content

Commit

Permalink
fix: defensive programming has been added to api 'GetEntries()' to re…
Browse files Browse the repository at this point in the history
…duce NPE (#238)

Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe authored Dec 25, 2023
1 parent 0796c98 commit 7aa8c2c
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/api/kpm_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (pkg *KclPackage) GetPkgHomePath() string {
}

// GetPkgProfile returns the profile of the package.
func (pkg *KclPackage) GetPkgProfile() pkg.Profile {
func (pkg *KclPackage) GetPkgProfile() *pkg.Profile {
return pkg.pkg.GetPkgProfile()
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/api/kpm_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"testing"

"gotest.tools/v3/assert"
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/opt"
)

func TestPackageApi(t *testing.T) {
Expand Down Expand Up @@ -169,3 +171,25 @@ func TestGetSchemaTypeUnderEmptyDir(t *testing.T) {
assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMain"].Type, "schema")
assert.Equal(t, schemas[filepath.Join(".")]["SchemaInMain"].SchemaName, "SchemaInMain")
}

func TestGetEntries(t *testing.T) {
testPath := getTestDir("test_get_entries")
pkgPath := filepath.Join(testPath, "no_entries")
pkg, err := GetKclPackage(pkgPath)
assert.Equal(t, err, nil)
assert.Equal(t, len(pkg.GetPkgProfile().GetEntries()), 0)

pkgPath = filepath.Join(testPath, "with_path_entries")
pkg, err = GetKclPackage(pkgPath)
assert.Equal(t, err, nil)
assert.Equal(t, len(pkg.GetPkgProfile().GetEntries()), 1)

res, err := RunWithOpts(
opt.WithEntries(pkg.GetPkgProfile().GetEntries()),
opt.WithKclOption(kcl.WithWorkDir(pkgPath)),
)

assert.Equal(t, err, nil)
assert.Equal(t, res.GetRawYamlResult(), "sub: test in sub")
assert.Equal(t, res.GetRawJsonResult(), "[{\"sub\": \"test in sub\"}]")
}
5 changes: 5 additions & 0 deletions pkg/api/test_data/test_get_entries/no_entries/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "no_entries"
edition = "0.0.1"
version = "0.0.1"

Empty file.
1 change: 1 addition & 0 deletions pkg/api/test_data/test_get_entries/no_entries/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
8 changes: 8 additions & 0 deletions pkg/api/test_data/test_get_entries/with_path_entries/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "with_path_entries"
edition = "0.0.1"
version = "0.0.1"

[profile]
entries = ["sub/sub.k"]

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sub = "test in sub"
2 changes: 1 addition & 1 deletion pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (profile *Profile) IntoKclOptions() *kcl.Option {

// GetEntries will get the entry kcl files from profile.
func (profile *Profile) GetEntries() []string {
if profile.Entries == nil {
if profile == nil || profile.Entries == nil {
return []string{}
}
return *profile.Entries
Expand Down
4 changes: 2 additions & 2 deletions pkg/package/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ func (kclPkg *KclPkg) GetPkgEdition() string {
}

// GetPkgProfile returns the profile of package.
func (kclPkg *KclPkg) GetPkgProfile() Profile {
return *kclPkg.ModFile.Profiles
func (kclPkg *KclPkg) GetPkgProfile() *Profile {
return kclPkg.ModFile.Profiles
}

// GetPkgTarName returns the kcl package tar name "<package_name>-v<package_version>.tar"
Expand Down

0 comments on commit 7aa8c2c

Please sign in to comment.