aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/app/env_test.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-10-29 04:09:42 +0900
committerOphestra <cat@gensokyo.uk>2025-10-29 04:11:49 +0900
commita52f7038e5a607dd0901244abb791c96deaf9c2d (patch)
tree2184b7313cf3b2d783778e8272ac22d0eaa1603f /internal/app/env_test.go
parent274686d10d3386a41f8fb6bc3363269a734afe0e (diff)
internal/env: relocate from app
This package is much cleaner to stub independently, and makes no sense to lump into app. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/app/env_test.go')
-rw-r--r--internal/app/env_test.go137
1 files changed, 0 insertions, 137 deletions
diff --git a/internal/app/env_test.go b/internal/app/env_test.go
deleted file mode 100644
index 9e2c959e..00000000
--- a/internal/app/env_test.go
+++ /dev/null
@@ -1,137 +0,0 @@
-package app
-
-import (
- "fmt"
- "reflect"
- "testing"
-
- "hakurei.app/container"
- "hakurei.app/container/check"
- "hakurei.app/container/fhs"
- "hakurei.app/container/stub"
- "hakurei.app/hst"
-)
-
-func TestEnvPaths(t *testing.T) {
- t.Parallel()
-
- testCases := []struct {
- name string
- env *EnvPaths
- want hst.Paths
-
- wantPanic string
- }{
- {"nil", nil, hst.Paths{}, "attempting to use an invalid EnvPaths"},
- {"zero", new(EnvPaths), hst.Paths{}, "attempting to use an invalid EnvPaths"},
-
- {"nil tempdir", &EnvPaths{
- RuntimePath: fhs.AbsTmp,
- }, hst.Paths{}, "attempting to use an invalid EnvPaths"},
-
- {"nil runtime", &EnvPaths{
- 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", &EnvPaths{
- 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 EnvPaths
- }{
- {"invalid tempdir", nil, "\x00",
- "invalid TMPDIR: path \"\\x00\" is not absolute", EnvPaths{}},
- {"empty environment", make(map[string]string), container.Nonexistent,
- "", EnvPaths{TempDir: check.MustAbs(container.Nonexistent)}},
- {"invalid XDG_RUNTIME_DIR", map[string]string{"XDG_RUNTIME_DIR": "\x00"}, container.Nonexistent,
- "", EnvPaths{TempDir: check.MustAbs(container.Nonexistent)}},
- {"full", map[string]string{"XDG_RUNTIME_DIR": "/\x00"}, container.Nonexistent,
- "", EnvPaths{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)
- }
-
- k := copyPathsDispatcher{t: t, env: tc.env, tmp: tc.tmp, expectsFatal: tc.fatal}
- got := copyPaths(k)
-
- 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)
- }
- })
- }
-}
-
-// copyPathsDispatcher implements enough of syscallDispatcher for all copyPaths code paths.
-type copyPathsDispatcher struct {
- env map[string]string
- tmp string
-
- // must be checked at the conclusion of the test
- expectsFatal string
-
- t *testing.T
- panicDispatcher
-}
-
-func (k copyPathsDispatcher) tempdir() string { return k.tmp }
-func (k copyPathsDispatcher) lookupEnv(key string) (value string, ok bool) {
- value, ok = k.env[key]
- return
-}
-func (k copyPathsDispatcher) fatalf(format string, v ...any) {
- if k.expectsFatal == "" {
- k.t.Fatalf("unexpected call to fatalf: format = %q, v = %#v", format, v)
- }
-
- if got := fmt.Sprintf(format, v...); got != k.expectsFatal {
- k.t.Fatalf("fatalf: %q, want %q", got, k.expectsFatal)
- }
- panic(stub.PanicExit)
-}