Skip to content

Commit

Permalink
Add test for kpm_env_test.go file (#596)
Browse files Browse the repository at this point in the history
Signed-off-by: Rohanraj123 <[email protected]>
  • Loading branch information
Rohanraj123 authored Feb 11, 2025
1 parent 2c1249d commit 3950507
Showing 1 changed file with 42 additions and 0 deletions.
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)
}

0 comments on commit 3950507

Please sign in to comment.