Something went wrong. Try again.
An unofficial terminal client for Tangled, optimized for humans and agents. tgcli.wisp.place
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188package cli
import ( "os" "path/filepath" "reflect" "strings" "testing")
func TestConfigSearchDirs(t *testing.T) { xdg := t.TempDir() home := t.TempDir()
tests := []struct { name string xdg string want []string }{ { name: "xdg config home set", xdg: xdg, want: []string{filepath.Join(xdg, "tg")}, }, { name: "fall back to home", xdg: "", want: []string{filepath.Join(home, ".config", "tg")}, }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", tt.xdg) t.Setenv("HOME", home)
if got := configSearchDirs(); !reflect.DeepEqual(got, tt.want) { t.Fatalf("got %v, want %v", got, tt.want) } }) }}
func TestLoadConfigRequiresExplicitFiles(t *testing.T) { missing := filepath.Join(t.TempDir(), "missing.toml") for name, flags := range map[string]flagSettings{ "flag": {ConfigPath: missing, ConfigSet: true}, "environment": {}, } { t.Run(name, func(t *testing.T) { if name == "environment" { t.Setenv("TG_CONFIG", missing) } else { t.Setenv("TG_CONFIG", "") } if _, err := loadConfig(flags); err == nil || !strings.Contains(err.Error(), "read config") { t.Fatalf("loadConfig() error = %v", err) } }) }}
func TestLoadConfigRejectsMalformedFile(t *testing.T) { path := filepath.Join(t.TempDir(), "config.toml") if err := os.WriteFile(path, []byte("appview = ["), 0o600); err != nil { t.Fatal(err) } if _, err := loadConfig(flagSettings{ConfigPath: path, ConfigSet: true}); err == nil { t.Fatal("loadConfig() accepted malformed explicit config") }}
func TestLoadConfigKnotPrecedence(t *testing.T) { tests := []struct { name string config string env string wantKnot string }{ {name: "unset enables discovery", wantKnot: ""}, {name: "config", config: "config.example", wantKnot: "config.example"}, {name: "environment over config", config: "config.example", env: "env.example", wantKnot: "env.example"}, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { xdg := t.TempDir() t.Setenv("XDG_CONFIG_HOME", xdg) t.Setenv("TG_KNOT", tt.env) if tt.config != "" { configDir := filepath.Join(xdg, "tg") if err := os.MkdirAll(configDir, 0o700); err != nil { t.Fatalf("create config directory: %v", err) } if err := os.WriteFile(filepath.Join(configDir, "config.toml"), []byte("knot = \""+tt.config+"\"\n"), 0o600); err != nil { t.Fatalf("write config: %v", err) } }
got, err := loadConfig(flagSettings{}) if err != nil { t.Fatalf("loadConfig() error = %v", err) } if got.Knot != tt.wantKnot { t.Fatalf("Knot = %q, want %q", got.Knot, tt.wantKnot) } }) }}
func TestLoadConfigSSHPortPrecedence(t *testing.T) { tests := []struct { name string config string env string wantPort string }{ {name: "default", wantPort: "22"}, {name: "config", config: "ssh-port = 2200\n", wantPort: "2200"}, {name: "environment over config", config: "ssh-port = 2200\n", env: "2222", wantPort: "2222"}, {name: "malformed config is preserved", config: "ssh-port = true\n", wantPort: "true"}, {name: "malformed environment is preserved", env: "not-a-port", wantPort: "not-a-port"}, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { xdg := t.TempDir() t.Setenv("XDG_CONFIG_HOME", xdg) t.Setenv("TG_SSH_PORT", tt.env) if tt.config != "" { configDir := filepath.Join(xdg, "tg") if err := os.MkdirAll(configDir, 0o700); err != nil { t.Fatalf("create config directory: %v", err) } if err := os.WriteFile(filepath.Join(configDir, "config.toml"), []byte(tt.config), 0o600); err != nil { t.Fatalf("write config: %v", err) } }
got, err := loadConfig(flagSettings{}) if err != nil { t.Fatalf("loadConfig() error = %v", err) } if got.SSHPort != tt.wantPort { t.Fatalf("SSH port = %q, want %q", got.SSHPort, tt.wantPort) } }) }}
func TestLoadConfigProtocolPrecedence(t *testing.T) { tests := []struct { name string config string environmentValue string expectedProtocol string }{ {name: "default", expectedProtocol: "ssh"}, {name: "config", config: "protocol = \"https\"\n", expectedProtocol: "https"}, {name: "environment over config", config: "protocol = \"https\"\n", environmentValue: "ssh", expectedProtocol: "ssh"}, }
for _, testCase := range tests { t.Run(testCase.name, func(t *testing.T) { xdgConfigHome := t.TempDir() t.Setenv("XDG_CONFIG_HOME", xdgConfigHome) t.Setenv("TG_PROTOCOL", testCase.environmentValue) if testCase.config != "" { configDirectory := filepath.Join(xdgConfigHome, "tg") if err := os.MkdirAll(configDirectory, 0o700); err != nil { t.Fatalf("create config directory: %v", err) } if err := os.WriteFile(filepath.Join(configDirectory, "config.toml"), []byte(testCase.config), 0o600); err != nil { t.Fatalf("write config: %v", err) } }
resolved, err := loadConfig(flagSettings{}) if err != nil { t.Fatalf("loadConfig() error = %v", err) } if resolved.Protocol != testCase.expectedProtocol { t.Fatalf("protocol = %q, want %q", resolved.Protocol, testCase.expectedProtocol) } }) }}