internal/env: relocate from app
All checks were successful
Test / Create distribution (push) Successful in 32s
Test / Sandbox (push) Successful in 2m8s
Test / Hakurei (push) Successful in 3m10s
Test / Hpkg (push) Successful in 4m1s
Test / Sandbox (race detector) (push) Successful in 4m7s
Test / Hakurei (race detector) (push) Successful in 4m53s
Test / Flake checks (push) Successful in 1m27s
All checks were successful
Test / Create distribution (push) Successful in 32s
Test / Sandbox (push) Successful in 2m8s
Test / Hakurei (push) Successful in 3m10s
Test / Hpkg (push) Successful in 4m1s
Test / Sandbox (race detector) (push) Successful in 4m7s
Test / Hakurei (race detector) (push) Successful in 4m53s
Test / Flake checks (push) Successful in 1m27s
This package is much cleaner to stub independently, and makes no sense to lump into app. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
66
internal/env/env.go
vendored
Normal file
66
internal/env/env.go
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
// Package env provides the [Paths] struct for efficiently building paths from the environment.
|
||||
package env
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"hakurei.app/container/check"
|
||||
"hakurei.app/hst"
|
||||
)
|
||||
|
||||
// Paths holds paths copied from the environment and is used to create [hst.Paths].
|
||||
type Paths struct {
|
||||
// TempDir is returned by [os.TempDir].
|
||||
TempDir *check.Absolute
|
||||
// RuntimePath is copied from $XDG_RUNTIME_DIR.
|
||||
RuntimePath *check.Absolute
|
||||
}
|
||||
|
||||
// Copy expands [Paths] into [hst.Paths].
|
||||
func (env *Paths) Copy(v *hst.Paths, userid int) {
|
||||
if env == nil || env.TempDir == nil || v == nil {
|
||||
panic("attempting to use an invalid Paths")
|
||||
}
|
||||
|
||||
v.TempDir = env.TempDir
|
||||
v.SharePath = env.TempDir.Append("hakurei." + strconv.Itoa(userid))
|
||||
|
||||
if env.RuntimePath == nil {
|
||||
// fall back to path in share since hakurei has no hard XDG dependency
|
||||
v.RunDirPath = v.SharePath.Append("run")
|
||||
v.RuntimePath = v.RunDirPath.Append("compat")
|
||||
} else {
|
||||
v.RuntimePath = env.RuntimePath
|
||||
v.RunDirPath = env.RuntimePath.Append("hakurei")
|
||||
}
|
||||
}
|
||||
|
||||
// CopyPaths returns a populated [Paths].
|
||||
func CopyPaths() *Paths { return CopyPathsFunc(log.Fatalf, os.TempDir, os.Getenv) }
|
||||
|
||||
// CopyPathsFunc returns a populated [Paths],
|
||||
// using the provided [log.Fatalf], [os.TempDir], [os.Getenv] functions.
|
||||
func CopyPathsFunc(
|
||||
fatalf func(format string, v ...any),
|
||||
tempdir func() string,
|
||||
getenv func(key string) string,
|
||||
) *Paths {
|
||||
const xdgRuntimeDir = "XDG_RUNTIME_DIR"
|
||||
|
||||
var env Paths
|
||||
|
||||
if tempDir, err := check.NewAbs(tempdir()); err != nil {
|
||||
fatalf("invalid TMPDIR: %v", err)
|
||||
panic("unreachable")
|
||||
} else {
|
||||
env.TempDir = tempDir
|
||||
}
|
||||
|
||||
if a, err := check.NewAbs(getenv(xdgRuntimeDir)); err == nil {
|
||||
env.RuntimePath = a
|
||||
}
|
||||
|
||||
return &env
|
||||
}
|
||||
118
internal/env/env_test.go
vendored
Normal file
118
internal/env/env_test.go
vendored
Normal file
@@ -0,0 +1,118 @@
|
||||
package env_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"hakurei.app/container"
|
||||
"hakurei.app/container/check"
|
||||
"hakurei.app/container/fhs"
|
||||
"hakurei.app/container/stub"
|
||||
"hakurei.app/hst"
|
||||
"hakurei.app/internal/env"
|
||||
)
|
||||
|
||||
func TestPaths(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
env *env.Paths
|
||||
want hst.Paths
|
||||
|
||||
wantPanic string
|
||||
}{
|
||||
{"nil", nil, hst.Paths{}, "attempting to use an invalid Paths"},
|
||||
{"zero", new(env.Paths), hst.Paths{}, "attempting to use an invalid Paths"},
|
||||
|
||||
{"nil tempdir", &env.Paths{
|
||||
RuntimePath: fhs.AbsTmp,
|
||||
}, hst.Paths{}, "attempting to use an invalid Paths"},
|
||||
|
||||
{"nil runtime", &env.Paths{
|
||||
TempDir: fhs.AbsTmp,
|
||||
}, hst.Paths{
|
||||
TempDir: fhs.AbsTmp,
|
||||
SharePath: fhs.AbsTmp.Append("hakurei.3735928559"),
|
||||
RuntimePath: fhs.AbsTmp.Append("hakurei.3735928559/run/compat"),
|
||||
RunDirPath: fhs.AbsTmp.Append("hakurei.3735928559/run"),
|
||||
}, ""},
|
||||
|
||||
{"full", &env.Paths{
|
||||
TempDir: fhs.AbsTmp,
|
||||
RuntimePath: fhs.AbsRunUser.Append("1000"),
|
||||
}, hst.Paths{
|
||||
TempDir: fhs.AbsTmp,
|
||||
SharePath: fhs.AbsTmp.Append("hakurei.3735928559"),
|
||||
RuntimePath: fhs.AbsRunUser.Append("1000"),
|
||||
RunDirPath: fhs.AbsRunUser.Append("1000/hakurei"),
|
||||
}, ""},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if tc.wantPanic != "" {
|
||||
defer func() {
|
||||
if r := recover(); r != tc.wantPanic {
|
||||
t.Errorf("Copy: panic = %#v, want %q", r, tc.wantPanic)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
var sc hst.Paths
|
||||
tc.env.Copy(&sc, 0xdeadbeef)
|
||||
if !reflect.DeepEqual(&sc, &tc.want) {
|
||||
t.Errorf("Copy: %#v, want %#v", sc, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyPaths(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
env map[string]string
|
||||
tmp string
|
||||
fatal string
|
||||
want env.Paths
|
||||
}{
|
||||
{"invalid tempdir", nil, "\x00",
|
||||
"invalid TMPDIR: path \"\\x00\" is not absolute", env.Paths{}},
|
||||
{"empty environment", make(map[string]string), container.Nonexistent,
|
||||
"", env.Paths{TempDir: check.MustAbs(container.Nonexistent)}},
|
||||
{"invalid XDG_RUNTIME_DIR", map[string]string{"XDG_RUNTIME_DIR": "\x00"}, container.Nonexistent,
|
||||
"", env.Paths{TempDir: check.MustAbs(container.Nonexistent)}},
|
||||
{"full", map[string]string{"XDG_RUNTIME_DIR": "/\x00"}, container.Nonexistent,
|
||||
"", env.Paths{TempDir: check.MustAbs(container.Nonexistent), RuntimePath: check.MustAbs("/\x00")}},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if tc.fatal != "" {
|
||||
defer stub.HandleExit(t)
|
||||
}
|
||||
|
||||
got := env.CopyPathsFunc(func(format string, v ...any) {
|
||||
if tc.fatal == "" {
|
||||
t.Fatalf("unexpected call to fatalf: format = %q, v = %#v", format, v)
|
||||
}
|
||||
|
||||
if got := fmt.Sprintf(format, v...); got != tc.fatal {
|
||||
t.Fatalf("fatalf: %q, want %q", got, tc.fatal)
|
||||
}
|
||||
panic(stub.PanicExit)
|
||||
}, func() string { return tc.tmp }, func(key string) string { return tc.env[key] })
|
||||
|
||||
if tc.fatal != "" {
|
||||
t.Fatalf("copyPaths: expected fatal %q", tc.fatal)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(got, &tc.want) {
|
||||
t.Errorf("copyPaths: %#v, want %#v", got, &tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user