Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ func LoadConfigWithHost(host string) (*Config, error) {

// getDefaultConfigPathInternal is the actual implementation for GetDefaultConfigPath.
func getDefaultConfigPathInternal() (string, error) {
// RALPH_CONFIG takes precedence over the XDG path. If set, the file must
// exist; we fail loudly rather than silently falling back to the default.
if envPath := os.Getenv("RALPH_CONFIG"); envPath != "" {
expanded, err := ExpandPath(envPath)
if err != nil {
return "", fmt.Errorf("RALPH_CONFIG=%s: %w", envPath, err)
}
if _, err := os.Stat(expanded); err != nil {
return "", fmt.Errorf("RALPH_CONFIG=%s: %w", envPath, err)
}
return expanded, nil
}

xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
if xdgConfigHome == "" {
homeDir, err := os.UserHomeDir()
Expand Down
45 changes: 45 additions & 0 deletions internal/config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,52 @@ func TestLoadConfig_WithEnableField(t *testing.T) {
}
}

func TestGetDefaultConfigPath_RalphConfigEnv(t *testing.T) {
t.Run("RALPH_CONFIG points to an existing file", func(t *testing.T) {
cfgPath, cleanup := createTempConfigFile(t, "")
defer cleanup()

t.Setenv("RALPH_CONFIG", cfgPath)

got, err := GetDefaultConfigPath()
if err != nil {
t.Fatalf("GetDefaultConfigPath() error = %v", err)
}
if got != cfgPath {
t.Errorf("GetDefaultConfigPath() = %v, want %v", got, cfgPath)
}
})

t.Run("RALPH_CONFIG points to a nonexistent file", func(t *testing.T) {
missing := filepath.Join(t.TempDir(), "does-not-exist.toml")
t.Setenv("RALPH_CONFIG", missing)

if _, err := GetDefaultConfigPath(); err == nil {
t.Fatal("GetDefaultConfigPath() expected error for missing RALPH_CONFIG file, got nil")
}
})

t.Run("RALPH_CONFIG takes precedence over XDG_CONFIG_HOME", func(t *testing.T) {
cfgPath, cleanup := createTempConfigFile(t, "")
defer cleanup()

t.Setenv("XDG_CONFIG_HOME", "/tmp/custom_xdg_config")
t.Setenv("RALPH_CONFIG", cfgPath)

got, err := GetDefaultConfigPath()
if err != nil {
t.Fatalf("GetDefaultConfigPath() error = %v", err)
}
if got != cfgPath {
t.Errorf("GetDefaultConfigPath() = %v, want %v", got, cfgPath)
}
})
}

func TestGetDefaultConfigPath(t *testing.T) {
// Ensure RALPH_CONFIG doesn't leak in and override the XDG-based logic.
t.Setenv("RALPH_CONFIG", "")

homeDir, err := os.UserHomeDir()
if err != nil {
t.Fatalf("Failed to get user home dir: %v", err)
Expand Down
Loading