package platform import ( "runtime" "testing" ) func TestDetectLinuxDistro(t *testing.T) { if runtime.GOOS != "linux" { t.Skip("Linux-only path") } t.Run("os-release ID maps to family", func(t *testing.T) { withOSRelease(t, "ID=opensuse-tumbleweed\nID_LIKE=\"opensuse suse\"\n") id, family := detectLinuxDistro() if id != "opensuse-tumbleweed" { t.Errorf("detectLinuxDistro() id = %q, want opensuse-tumbleweed", id) } if family != "suse" { t.Errorf("detectLinuxDistro() family = %q, want suse", family) } }) t.Run("quoted ID is unquoted", func(t *testing.T) { withOSRelease(t, "ID=\"cachyos\"\n") id, family := detectLinuxDistro() if id != "cachyos" || family != "arch" { t.Errorf("detectLinuxDistro() = (%q, %q), want (cachyos, arch)", id, family) } }) t.Run("derivative resolves through ID_LIKE", func(t *testing.T) { withOSRelease(t, "ID=myowndistro\nID_LIKE=\"debian\"\n") id, family := detectLinuxDistro() if id != "myowndistro" || family != "debian" { t.Errorf("detectLinuxDistro() = (%q, %q), want (myowndistro, debian)", id, family) } }) t.Run("host detection returns a family", func(t *testing.T) { // Against the real host files: every supported CI host is Linux with // os-release present, so detection must succeed. id, family := detectLinuxDistro() if id == "" { t.Error("detectLinuxDistro() returned an empty ID on a Linux host") } if family == "" { t.Error("detectLinuxDistro() returned an empty family on a Linux host") } }) } func TestGetLinuxDistroFamily(t *testing.T) { if runtime.GOOS != "linux" { t.Skip("Linux-only path") } t.Run("from os-release", func(t *testing.T) { withOSRelease(t, "ID=fedora\n") if got := GetLinuxDistroFamily(); got != "rhel" { t.Errorf("GetLinuxDistroFamily() = %q, want rhel", got) } }) t.Run("missing os-release returns empty", func(t *testing.T) { withOSRelease(t, "") // file with no ID line if got := GetLinuxDistroFamily(); got != "" { t.Errorf("GetLinuxDistroFamily() = %q, want empty", got) } }) }