vcom/internal/paths/paths_test.go

183 lines
5.7 KiB
Go
Raw Normal View History

package paths
import (
"errors"
"os"
"path/filepath"
"strconv"
"testing"
)
// fake installs a fake system for one test.
func fake(t *testing.T, env map[string]string, euid int, homes map[string]string) {
t.Helper()
orig := sys
t.Cleanup(func() { sys = orig })
sys.getenv = func(k string) string { return env[k] }
sys.geteuid = func() int { return euid }
sys.homeOfUser = func(name string) (string, error) {
if h, ok := homes[name]; ok {
return h, nil
}
return "", errors.New("no such user")
}
sys.homeOfUID = func(uid int) (string, error) {
if h, ok := homes["uid:"+strconv.Itoa(uid)]; ok {
return h, nil
}
return "", errors.New("no such uid")
}
}
func write(t *testing.T, p string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte("[ui]\n"), 0o644); err != nil {
t.Fatal(err)
}
}
func TestConfigLookupIgnoresCwd(t *testing.T) {
home := t.TempDir()
cwd := t.TempDir()
write(t, filepath.Join(cwd, "vcom.toml"))
write(t, filepath.Join(cwd, "config", "vcom.toml"))
t.Chdir(cwd)
fake(t, map[string]string{"HOME": home}, 1000, nil)
p, found, err := ConfigFile("")
if err != nil {
t.Fatal(err)
}
if found {
t.Fatalf("found %s: the working directory must not be searched", p)
}
if want := filepath.Join(home, ".config", "vcom", "vcom.toml"); p != want {
t.Fatalf("default path %s, want %s", p, want)
}
if stray, ok := StrayCwdConfig(); !ok || stray != filepath.Join(cwd, "vcom.toml") {
t.Fatalf("stray = %q %v", stray, ok)
}
// The real XDG file is found; explicit and $VCOM_CONFIG win in that order.
write(t, filepath.Join(home, ".config", "vcom", "vcom.toml"))
if p, found, _ := ConfigFile(""); !found || p != filepath.Join(home, ".config", "vcom", "vcom.toml") {
t.Fatalf("home config: %s %v", p, found)
}
xdg := filepath.Join(home, "xdg")
write(t, filepath.Join(xdg, "vcom", "vcom.toml"))
env := map[string]string{"HOME": home, "XDG_CONFIG_HOME": xdg}
fake(t, env, 1000, nil)
if p, _, _ := ConfigFile(""); p != filepath.Join(xdg, "vcom", "vcom.toml") {
t.Fatalf("XDG_CONFIG_HOME not preferred: %s", p)
}
env["VCOM_CONFIG"] = filepath.Join(home, "env.toml")
if p, found, _ := ConfigFile(""); found || p != env["VCOM_CONFIG"] {
t.Fatalf("VCOM_CONFIG: %s %v", p, found)
}
if p, _, _ := ConfigFile("rel.toml"); p != filepath.Join(cwd, "rel.toml") {
t.Fatalf("explicit relative path: %s", p)
}
}
func TestSudoWritePathsEffectiveUser(t *testing.T) {
tmp := t.TempDir()
bob, root := filepath.Join(tmp, "bob"), filepath.Join(tmp, "root")
write(t, filepath.Join(bob, ".config", "vcom", "vcom.toml"))
// sudo -E keeps bob's HOME and XDG variables.
env := map[string]string{
"HOME": bob,
"SUDO_USER": "bob",
"XDG_STATE_HOME": filepath.Join(bob, ".local", "state"),
"XDG_CACHE_HOME": filepath.Join(bob, ".cache"),
"XDG_DATA_HOME": filepath.Join(bob, ".local", "share"),
"XDG_CONFIG_HOME": filepath.Join(bob, ".config"),
}
fake(t, env, 0, map[string]string{"bob": bob, "uid:0": root})
if p, found, _ := ConfigFile(""); !found || p != filepath.Join(bob, ".config", "vcom", "vcom.toml") {
t.Fatalf("config must be read from the invoking user: %s %v", p, found)
}
if h, _ := ReadHome(); h != bob {
t.Fatalf("ReadHome = %s", h)
}
checks := map[string]func() (string, error){
filepath.Join(root, ".local", "state", "vcom"): StateDir,
filepath.Join(root, ".cache", "vcom"): CacheDir,
filepath.Join(root, ".local", "share"): DataHome,
filepath.Join(root, ".config", "vcom", "vcom.toml"): DefaultConfigFile,
root: WriteHome,
}
for want, f := range checks {
if got, err := f(); err != nil || got != want {
t.Errorf("got %s (%v), want %s", got, err, want)
}
}
// Without sudo the XDG variables are honoured.
delete(env, "SUDO_USER")
fake(t, env, 1000, nil)
if d, _ := StateDir(); d != filepath.Join(bob, ".local", "state", "vcom") {
t.Fatalf("StateDir = %s", d)
}
// sudo with SUDO_USER=root (sudo -s from root) is not "sudo".
env["SUDO_USER"] = "root"
fake(t, env, 0, nil)
if _, ok := SudoUser(); ok {
t.Fatal("SUDO_USER=root must not count")
}
}
func TestRunDirSweepDeadPID(t *testing.T) {
cacheHome := t.TempDir()
fake(t, map[string]string{"HOME": t.TempDir(), "XDG_CACHE_HOME": cacheHome}, 1000, nil)
sys.getpid = func() int { return 4242 }
sys.alive = func(pid int) bool { return pid == 222 }
cache := filepath.Join(cacheHome, "vcom")
for _, d := range []string{"run-111", "run-222", "run-abc", "keep"} {
write(t, filepath.Join(cache, d, "junk"))
}
d, err := CreateRunDir()
if err != nil {
t.Fatal(err)
}
if d != filepath.Join(cache, "run-4242") {
t.Fatalf("run dir %s", d)
}
if fi, err := os.Stat(d); err != nil || fi.Mode().Perm() != 0o700 {
t.Fatalf("run dir mode: %v %v", fi, err)
}
for name, want := range map[string]bool{"run-111": false, "run-222": true, "run-abc": true, "keep": true} {
_, err := os.Stat(filepath.Join(cache, name))
if (err == nil) != want {
t.Errorf("%s exists=%v, want %v", name, err == nil, want)
}
}
if err := RemoveRunDir(d); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(d); !os.IsNotExist(err) {
t.Fatal("run dir not removed")
}
if err := RemoveRunDir(cache); err == nil {
t.Fatal("RemoveRunDir must refuse a non-run dir")
}
}
func TestPidAlive(t *testing.T) {
if !pidAlive(os.Getpid()) {
t.Fatal("self not alive")
}
}
func TestDefaultConfigFileHonoursVCOMConfig(t *testing.T) {
home := t.TempDir()
fake(t, map[string]string{"HOME": home, "VCOM_CONFIG": filepath.Join(home, "custom", "v.toml")}, 1000, nil)
if p, _ := DefaultConfigFile(); p != filepath.Join(home, "custom", "v.toml") {
t.Fatalf("DefaultConfigFile = %s (QA-15)", p)
}
}