Files
pmg/config/remedy_test.go
T
Sahilb315 e0580b1f79 ci: pin XDG_CONFIG_HOME for the cross-user e2e step; terse doctor fix
GitHub runners export XDG_CONFIG_HOME=/home/runner/.config and it leaks
through sudo -u, so the pmgtest pmg resolved the runner user's config
dir and fail-closed on its runner-owned log file (run 29289868727 shows
the triaged error catching exactly this). Set it inside the login shell
so it wins regardless of how the leak is delivered.

The remedy now returns a full-help and doctor-table pair from a single
triage, and drops the do-not-chown tail from the leak message.
2026-07-14 04:12:27 +05:30

57 lines
1.7 KiB
Go

package config
import (
"testing"
"github.com/stretchr/testify/assert"
)
func withRealUserHome(t *testing.T, home string) {
t.Helper()
orig := realUserHomeDir
realUserHomeDir = func() (string, error) { return home, nil }
t.Cleanup(func() { realUserHomeDir = orig })
}
func TestUnwritableConfigDirRemedy(t *testing.T) {
t.Run("dir inside real home suggests chown", func(t *testing.T) {
t.Setenv("PMG_CONFIG_DIR", "")
withRealUserHome(t, "/home/alice")
help, fix := UnwritableConfigDirRemedy("/home/alice/.config/safedep/pmg")
assert.Contains(t, help, "sudo chown -R")
assert.Contains(t, help, "/home/alice/.config/safedep/pmg")
assert.Contains(t, fix, "sudo chown -R")
})
t.Run("dir outside real home blames leaked env, never suggests chown", func(t *testing.T) {
t.Setenv("PMG_CONFIG_DIR", "")
withRealUserHome(t, "/home/pmgtest")
help, fix := UnwritableConfigDirRemedy("/home/runner/.config/safedep/pmg")
assert.Contains(t, help, "XDG_CONFIG_HOME")
assert.NotContains(t, help, "chown")
assert.Contains(t, fix, "XDG_CONFIG_HOME")
assert.NotContains(t, fix, "chown")
})
t.Run("explicit PMG_CONFIG_DIR gets its own remedy", func(t *testing.T) {
t.Setenv("PMG_CONFIG_DIR", "/srv/pmg")
withRealUserHome(t, "/home/alice")
help, fix := UnwritableConfigDirRemedy("/srv/pmg")
assert.Contains(t, help, "PMG_CONFIG_DIR")
assert.NotContains(t, help, "chown")
assert.Contains(t, fix, "PMG_CONFIG_DIR")
assert.NotContains(t, fix, "chown")
})
t.Run("sibling dir with home prefix is outside home", func(t *testing.T) {
t.Setenv("PMG_CONFIG_DIR", "")
withRealUserHome(t, "/home/alice")
help, _ := UnwritableConfigDirRemedy("/home/alice-evil/.config/safedep/pmg")
assert.NotContains(t, help, "chown")
})
}