feat: deterministic match_dirs tie-breaking by config file order
This commit is contained in:
parent
ac780231a8
commit
830eba1c0e
2 changed files with 113 additions and 0 deletions
|
|
@ -567,6 +567,67 @@ func TestResolveHostByPath_TildeExpansion(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestResolveHostByPath_TieBreakByConfigOrder(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Hosts: map[string]HostConfig{
|
||||
"second.org": {
|
||||
Hostname: "second.org",
|
||||
Token: "t2",
|
||||
MatchDirs: []string{"/shared/path"},
|
||||
Order: 1,
|
||||
},
|
||||
"first.org": {
|
||||
Hostname: "first.org",
|
||||
Token: "t1",
|
||||
MatchDirs: []string{"/shared/path"},
|
||||
Order: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
got := cfg.ResolveHostByPath("/shared/path/subdir")
|
||||
if got != "first.org" {
|
||||
t.Errorf("expected first.org (earlier in config), got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssignHostOrder(t *testing.T) {
|
||||
yamlData := []byte(`hosts:
|
||||
alpha.org:
|
||||
hostname: alpha.org
|
||||
token: t1
|
||||
beta.org:
|
||||
hostname: beta.org
|
||||
token: t2
|
||||
gamma.org:
|
||||
hostname: gamma.org
|
||||
token: t3
|
||||
`)
|
||||
cfg, err := LoadFromPath(writeTempConfig(t, yamlData))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if cfg.Hosts["alpha.org"].Order != 0 {
|
||||
t.Errorf("alpha.org order = %d, want 0", cfg.Hosts["alpha.org"].Order)
|
||||
}
|
||||
if cfg.Hosts["beta.org"].Order != 1 {
|
||||
t.Errorf("beta.org order = %d, want 1", cfg.Hosts["beta.org"].Order)
|
||||
}
|
||||
if cfg.Hosts["gamma.org"].Order != 2 {
|
||||
t.Errorf("gamma.org order = %d, want 2", cfg.Hosts["gamma.org"].Order)
|
||||
}
|
||||
}
|
||||
|
||||
func writeTempConfig(t *testing.T, data []byte) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "config.yaml")
|
||||
if err := os.WriteFile(path, data, 0600); err != nil {
|
||||
t.Fatalf("failed to write temp config: %v", err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func TestExpandHome(t *testing.T) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue