61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package config
|
||
|
||
import (
|
||
"os"
|
||
"path/filepath"
|
||
"testing"
|
||
)
|
||
|
||
func TestGetConfigRoot_Default(t *testing.T) {
|
||
// 清除环境变量
|
||
os.Unsetenv("SKILLMGR_TEST_ROOT")
|
||
|
||
root, err := GetConfigRoot()
|
||
if err != nil {
|
||
t.Fatalf("GetConfigRoot 失败: %v", err)
|
||
}
|
||
|
||
home, _ := os.UserHomeDir()
|
||
expected := filepath.Join(home, ConfigDir)
|
||
|
||
if root != expected {
|
||
t.Errorf("期望 %s,得到 %s", expected, root)
|
||
}
|
||
}
|
||
|
||
func TestGetConfigRoot_WithEnvOverride(t *testing.T) {
|
||
testRoot := "/tmp/skillmgr-test"
|
||
os.Setenv("SKILLMGR_TEST_ROOT", testRoot)
|
||
defer os.Unsetenv("SKILLMGR_TEST_ROOT")
|
||
|
||
root, err := GetConfigRoot()
|
||
if err != nil {
|
||
t.Fatalf("GetConfigRoot 失败: %v", err)
|
||
}
|
||
|
||
if root != testRoot {
|
||
t.Errorf("期望 %s,得到 %s", testRoot, root)
|
||
}
|
||
}
|
||
|
||
func TestEnsureConfigDirs(t *testing.T) {
|
||
tmpDir, err := os.MkdirTemp("", "skillmgr-test-*")
|
||
if err != nil {
|
||
t.Fatalf("创建临时目录失败: %v", err)
|
||
}
|
||
defer os.RemoveAll(tmpDir)
|
||
|
||
os.Setenv("SKILLMGR_TEST_ROOT", tmpDir)
|
||
defer os.Unsetenv("SKILLMGR_TEST_ROOT")
|
||
|
||
if err := EnsureConfigDirs(); err != nil {
|
||
t.Fatalf("EnsureConfigDirs 失败: %v", err)
|
||
}
|
||
|
||
// 检查目录是否存在
|
||
cacheDir := filepath.Join(tmpDir, CacheDir)
|
||
if _, err := os.Stat(cacheDir); os.IsNotExist(err) {
|
||
t.Errorf("缓存目录未创建: %s", cacheDir)
|
||
}
|
||
}
|