Move control and utility executable names plus the QEMU environment key into runtime kind specs. Read capabilities only from manifest.json and declare them when packing reims runtimes.
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package runtime
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
)
|
|
|
|
func TestBuildChildEnvRedirectsTemp(t *testing.T) {
|
|
root := t.TempDir()
|
|
cfg := &config.Config{DataRoot: root}
|
|
rt := Installed{
|
|
Dir: filepath.Join(root, "runtimes", "3731afd"),
|
|
}
|
|
|
|
t.Setenv("TEMP", `C:\Users\中文用户\AppData\Local\Temp`)
|
|
t.Setenv("TMP", `C:\Users\中文用户\AppData\Local\Temp`)
|
|
t.Setenv("TMPDIR", `C:\Users\中文用户\AppData\Local\Temp`)
|
|
|
|
env := BuildChildEnv(cfg, rt)
|
|
wantTmp := cfg.TempDir()
|
|
|
|
if info, err := os.Stat(wantTmp); err != nil || !info.IsDir() {
|
|
t.Fatalf("expected TempDir %q to exist: err=%v", wantTmp, err)
|
|
}
|
|
|
|
counts := map[string]int{}
|
|
values := map[string]string{}
|
|
for _, kv := range env {
|
|
idx := strings.IndexByte(kv, '=')
|
|
if idx <= 0 {
|
|
continue
|
|
}
|
|
k := strings.ToUpper(kv[:idx])
|
|
v := kv[idx+1:]
|
|
if k == "TEMP" || k == "TMP" || k == "TMPDIR" {
|
|
counts[k]++
|
|
values[k] = v
|
|
}
|
|
}
|
|
|
|
for _, key := range []string{"TEMP", "TMP", "TMPDIR"} {
|
|
if counts[key] != 1 {
|
|
t.Errorf("%s appeared %d times in child env, want 1", key, counts[key])
|
|
}
|
|
if values[key] != wantTmp {
|
|
t.Errorf("%s = %q, want %q", key, values[key], wantTmp)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestKindSpecControlsExecutablesAndEnvironment(t *testing.T) {
|
|
root := t.TempDir()
|
|
inst := Installed{Dir: root, Kind: DefaultKind}
|
|
if got := inst.ControlPath(); !strings.Contains(strings.ToLower(got), "reims-ctl") {
|
|
t.Fatalf("ControlPath = %q", got)
|
|
}
|
|
if got := inst.UtilityPath(); !strings.Contains(strings.ToLower(got), "reims-util") {
|
|
t.Fatalf("UtilityPath = %q", got)
|
|
}
|
|
custom := Installed{Dir: root, Spec: KindSpec{Kind: "custom", ControlExecutable: "custom-ctl", UtilityExecutable: "custom-util", ControlQemuEnv: "CUSTOM_QEMU"}}
|
|
if got := custom.ControlPath(); !strings.Contains(got, "custom-ctl") {
|
|
t.Fatalf("custom ControlPath = %q", got)
|
|
}
|
|
env := BuildChildEnv(&config.Config{DataRoot: t.TempDir()}, custom)
|
|
found := false
|
|
for _, item := range env {
|
|
if strings.HasPrefix(item, "CUSTOM_QEMU=") {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("custom QEMU environment declaration was not applied")
|
|
}
|
|
}
|