Files
pmg/config/remedy_test.go
T
Sahilb315 cd9b45b3bc fix: triage the unwritable config dir remedy by cause
The chown hint is only correct when another account created files
inside the current user's own home. When a leaked HOME or
XDG_CONFIG_HOME points at another user's home (e.g. sudo -u on GitHub
runners), following it would chown that user's directory and brick
their pmg instead. Classify the failure against the passwd home,
which the leaked environment cannot influence, and prescribe:

- dir inside own home: restore ownership with chown
- dir outside own home: fix the leaked environment, never chown
- explicit PMG_CONFIG_DIR: make it writable

Used by both the fatal event-log error and the doctor check, and the
docs troubleshooting now carries the same two-case triage.
2026-07-14 03:56:07 +05:30

52 lines
1.5 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")
remedy := UnwritableConfigDirRemedy("/home/alice/.config/safedep/pmg")
assert.Contains(t, remedy, "sudo chown -R")
assert.Contains(t, remedy, "/home/alice/.config/safedep/pmg")
})
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")
remedy := UnwritableConfigDirRemedy("/home/runner/.config/safedep/pmg")
assert.Contains(t, remedy, "XDG_CONFIG_HOME")
assert.NotContains(t, remedy, "sudo 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")
remedy := UnwritableConfigDirRemedy("/srv/pmg")
assert.Contains(t, remedy, "PMG_CONFIG_DIR")
assert.NotContains(t, remedy, "sudo chown")
})
t.Run("sibling dir with home prefix is outside home", func(t *testing.T) {
t.Setenv("PMG_CONFIG_DIR", "")
withRealUserHome(t, "/home/alice")
remedy := UnwritableConfigDirRemedy("/home/alice-evil/.config/safedep/pmg")
assert.NotContains(t, remedy, "sudo chown")
})
}