100 lines
2.3 KiB
Go
100 lines
2.3 KiB
Go
package platform
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
func TestDetect_MatchesRuntime(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
info := Detect()
|
|
if info.GOOS != runtime.GOOS {
|
|
t.Errorf("Detect().GOOS = %q, want %q", info.GOOS, runtime.GOOS)
|
|
}
|
|
if info.GOARCH != runtime.GOARCH {
|
|
t.Errorf("Detect().GOARCH = %q, want %q", info.GOARCH, runtime.GOARCH)
|
|
}
|
|
if info.CPUs <= 0 {
|
|
t.Errorf("Detect().CPUs = %d, want > 0", info.CPUs)
|
|
}
|
|
if info.Name == "" {
|
|
t.Error("Detect().Name is empty")
|
|
}
|
|
}
|
|
|
|
func TestOSPredicates_ConsistentWithRuntime(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
wantWindows := runtime.GOOS == "windows"
|
|
wantDarwin := runtime.GOOS == "darwin"
|
|
wantLinux := runtime.GOOS == "linux"
|
|
|
|
if IsWindows() != wantWindows {
|
|
t.Errorf("IsWindows() = %v, want %v", IsWindows(), wantWindows)
|
|
}
|
|
if IsDarwin() != wantDarwin {
|
|
t.Errorf("IsDarwin() = %v, want %v", IsDarwin(), wantDarwin)
|
|
}
|
|
if IsLinux() != wantLinux {
|
|
t.Errorf("IsLinux() = %v, want %v", IsLinux(), wantLinux)
|
|
}
|
|
if IsUnix() != (runtime.GOOS != "windows") {
|
|
t.Errorf("IsUnix() = %v, want %v", IsUnix(), runtime.GOOS != "windows")
|
|
}
|
|
}
|
|
|
|
func TestArchLabel_Mapping(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
goarch string
|
|
want string
|
|
}{
|
|
{goarch: "amd64", want: "x86_64"},
|
|
{goarch: "arm64", want: "arm64"},
|
|
{goarch: "386", want: "x86"},
|
|
{goarch: "riscv64", want: "riscv64"},
|
|
}
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.goarch, func(t *testing.T) {
|
|
t.Parallel()
|
|
if got := archLabel(tt.goarch); got != tt.want {
|
|
t.Fatalf("archLabel(%q) = %q, want %q", tt.goarch, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDefaultDataDir_XDGOnLinux(t *testing.T) {
|
|
if runtime.GOOS != "linux" {
|
|
t.Skip("XDG_DATA_HOME only applies on linux")
|
|
}
|
|
|
|
t.Setenv("XDG_DATA_HOME", "/tmp/cyrene-xdg")
|
|
got := DefaultDataDir()
|
|
want := filepath.Join("/tmp/cyrene-xdg", "cyrene")
|
|
if got != want {
|
|
t.Errorf("DefaultDataDir() = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestSignalContext_Cancelable(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx, cancel := SignalContext()
|
|
if ctx == nil {
|
|
t.Fatal("SignalContext() returned nil context")
|
|
}
|
|
if cancel == nil {
|
|
t.Fatal("SignalContext() returned nil cancel func")
|
|
}
|
|
cancel()
|
|
if ctx.Err() != context.Canceled {
|
|
t.Errorf("ctx.Err() = %v, want context.Canceled", ctx.Err())
|
|
}
|
|
}
|