| 57 | } |
| 58 | |
| 59 | func TestNewEmptyEditorFallback(t *testing.T) { |
| 60 | // Skip if required environment variables would interfere |
| 61 | oldVisual := os.Getenv("VISUAL") |
| 62 | oldEditor := os.Getenv("EDITOR") |
| 63 | os.Unsetenv("VISUAL") |
| 64 | os.Unsetenv("EDITOR") |
| 65 | defer func() { |
| 66 | os.Setenv("VISUAL", oldVisual) |
| 67 | os.Setenv("EDITOR", oldEditor) |
| 68 | }() |
| 69 | |
| 70 | // Create a config with whitespace-only editor |
| 71 | tmpDir := t.TempDir() |
| 72 | configPath := filepath.Join(tmpDir, "config.yml") |
| 73 | |
| 74 | configContent := `--- |
| 75 | editor: " " |
| 76 | pager: less |
| 77 | style: monokai |
| 78 | formatter: terminal |
| 79 | cheatpaths: |
| 80 | - name: personal |
| 81 | path: ~/cheat |
| 82 | tags: [] |
| 83 | readonly: false |
| 84 | ` |
| 85 | |
| 86 | if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil { |
| 87 | t.Fatalf("failed to write test config: %v", err) |
| 88 | } |
| 89 | |
| 90 | // Load the config |
| 91 | conf, err := New(configPath, false) |
| 92 | if err != nil { |
| 93 | // It's OK if this fails due to no editor being found |
| 94 | // The important thing is it doesn't panic |
| 95 | return |
| 96 | } |
| 97 | |
| 98 | // If it succeeded, editor should not be empty (fallback was used) |
| 99 | if conf.Editor == "" { |
| 100 | t.Error("editor should not be empty after fallback") |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func TestNewWhitespaceOnlyPager(t *testing.T) { |
| 105 | // Create a config with whitespace-only pager |