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

Lfx Pretest : Improve test coverage by 90 percent. #596

Merged
merged 1 commit into from
Feb 11, 2025
Merged
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
42 changes: 42 additions & 0 deletions pkg/api/kpm_env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package api

import (
"fmt"
"os"
"path/filepath"
"testing"

"gotest.tools/v3/assert"
)

// TestGetKclPkgPath tests the retrieval of KCL_PKG_PATH
func TestGetKclPkgPath(t *testing.T) {
// Backup original environment variable
originalKclPkgPath, isSet := os.LookupEnv("KCL_PKG_PATH")
if isSet {
defer os.Setenv("KCL_PKG_PATH", originalKclPkgPath) // Restore after test
} else {
defer os.Unsetenv("KCL_PKG_PATH")
}

// Case 1: When KCL_PKG_PATH is set
customPath := filepath.Join(os.TempDir(), "custom_kcl_path")
err := os.Setenv("KCL_PKG_PATH", customPath)
assert.Equal(t, err, nil)

path, err := GetKclPkgPath()
assert.Equal(t, err, nil)
assert.Equal(t, path, customPath)
fmt.Printf("Test Case 1: Expected %v, Got %v\n", customPath, path)

// Case 2: When KCL_PKG_PATH is not set (should return default path)
os.Unsetenv("KCL_PKG_PATH")
homeDir, err := os.UserHomeDir()
assert.Equal(t, err, nil)
expectedDefaultPath := filepath.Join(homeDir, ".kcl", "kpm")

path, err = GetKclPkgPath()
assert.Equal(t, err, nil)
assert.Equal(t, path, expectedDefaultPath)
fmt.Printf("Test Case 2: Expected %v, Got %v\n", expectedDefaultPath, path)
}