-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for kpm_env_test.go file (#596)
Signed-off-by: Rohanraj123 <[email protected]>
- Loading branch information
1 parent
2c1249d
commit 3950507
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |