Skip to content

Commit 3950507

Browse files
authored
Add test for kpm_env_test.go file (#596)
Signed-off-by: Rohanraj123 <[email protected]>
1 parent 2c1249d commit 3950507

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

pkg/api/kpm_env_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package api
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"gotest.tools/v3/assert"
10+
)
11+
12+
// TestGetKclPkgPath tests the retrieval of KCL_PKG_PATH
13+
func TestGetKclPkgPath(t *testing.T) {
14+
// Backup original environment variable
15+
originalKclPkgPath, isSet := os.LookupEnv("KCL_PKG_PATH")
16+
if isSet {
17+
defer os.Setenv("KCL_PKG_PATH", originalKclPkgPath) // Restore after test
18+
} else {
19+
defer os.Unsetenv("KCL_PKG_PATH")
20+
}
21+
22+
// Case 1: When KCL_PKG_PATH is set
23+
customPath := filepath.Join(os.TempDir(), "custom_kcl_path")
24+
err := os.Setenv("KCL_PKG_PATH", customPath)
25+
assert.Equal(t, err, nil)
26+
27+
path, err := GetKclPkgPath()
28+
assert.Equal(t, err, nil)
29+
assert.Equal(t, path, customPath)
30+
fmt.Printf("Test Case 1: Expected %v, Got %v\n", customPath, path)
31+
32+
// Case 2: When KCL_PKG_PATH is not set (should return default path)
33+
os.Unsetenv("KCL_PKG_PATH")
34+
homeDir, err := os.UserHomeDir()
35+
assert.Equal(t, err, nil)
36+
expectedDefaultPath := filepath.Join(homeDir, ".kcl", "kpm")
37+
38+
path, err = GetKclPkgPath()
39+
assert.Equal(t, err, nil)
40+
assert.Equal(t, path, expectedDefaultPath)
41+
fmt.Printf("Test Case 2: Expected %v, Got %v\n", expectedDefaultPath, path)
42+
}

0 commit comments

Comments
 (0)