Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: defensive programming has been added to api 'GetEntries()' to reduce NPE #238

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading