返回 DeepSeek-Reasonix
session_cleanup_test.go
根目录 / internal / cli / session_cleanup_test.go
1 package cli
2
3 import (
4 "context"
5 "os"
6 "path/filepath"
7 "testing"
8
9 "reasonix/internal/session"
10 )
11
12 func isolateCLIConfigHome(t *testing.T) string {
13 t.Helper()
14 home := t.TempDir()
15 t.Cleanup(closeTestSessionServices)
16 t.Setenv("HOME", home)
17 // Keep tests on the default-path code path while preventing a caller's
18 // higher-priority REASONIX_HOME from escaping this temporary home.
19 t.Setenv("REASONIX_HOME", "")
20 if err := os.Unsetenv("REASONIX_HOME"); err != nil {
21 t.Fatalf("unset REASONIX_HOME: %v", err)
22 }
23 t.Setenv("REASONIX_CREDENTIALS_STORE", "file")
24 t.Setenv("USERPROFILE", home)
25 t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
26 t.Setenv("AppData", filepath.Join(home, "AppData"))
27 t.Chdir(t.TempDir())
28 return home
29 }
30
31 func closeTestSessionServices() {
32 cliSessionServices.Lock()
33 services := cliSessionServices.byRoot
34 cliSessionServices.byRoot = map[string]*session.Service{}
35 cliSessionServices.Unlock()
36 for _, service := range services {
37 _ = service.CloseAll(context.Background())
38 }
39 }
40
40 lines GO