| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func loadWithStatusline(t *testing.T, user, project string) *Config { |
| 11 | t.Helper() |
| 12 | home, root := t.TempDir(), t.TempDir() |
| 13 | t.Setenv("REASONIX_HOME", home) |
| 14 | t.Setenv("HOME", home) |
| 15 | t.Setenv("USERPROFILE", home) |
| 16 | t.Setenv("XDG_CONFIG_HOME", home) |
| 17 | if user != "" { |
| 18 | if err := os.WriteFile(filepath.Join(home, "config.toml"), []byte("[statusline]\ncommand = \""+user+"\"\n"), 0o600); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | } |
| 22 | if err := os.WriteFile(filepath.Join(root, "reasonix.toml"), []byte("[statusline]\ncommand = \""+project+"\"\n"), 0o600); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | cfg, err := LoadForRoot(root) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | return cfg |
| 30 | } |
| 31 | |
| 32 | func TestProjectStatuslineIsIgnored(t *testing.T) { |
| 33 | if got := loadWithStatusline(t, "", "project-statusline").Statusline.Command; got != "" { |
| 34 | t.Fatalf("statusline command = %q, want none from a project file", got) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestUserStatuslineWinsOverProject(t *testing.T) { |
| 39 | if got := loadWithStatusline(t, "user-statusline", "project-statusline").Statusline.Command; got != "user-statusline" { |
| 40 | t.Fatalf("statusline command = %q, want the user value", got) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestStatuslineNeverRendersIntoAProjectFile(t *testing.T) { |
| 45 | cfg := Default() |
| 46 | cfg.Statusline.Command = "user-statusline" |
| 47 | if out := RenderTOMLForScope(cfg, RenderScopeProject); strings.Contains(out, "user-statusline") { |
| 48 | t.Fatalf("project render carries the statusline command:\n%s", out) |
| 49 | } |
| 50 | if out := RenderTOMLProjectDelta(cfg); strings.Contains(out, "user-statusline") { |
| 51 | t.Fatalf("project delta carries the statusline command:\n%s", out) |
| 52 | } |
| 53 | if out := RenderTOMLForScope(cfg, RenderScopeUser); !strings.Contains(out, "user-statusline") { |
| 54 | t.Fatalf("user render lost the statusline command:\n%s", out) |
| 55 | } |
| 56 | } |
| 57 |