package cli import ( "os" "path/filepath" "testing" ) func TestParseOSReleaseID_Ubuntu(t *testing.T) { content := `NAME="Ubuntu" VERSION="24.04.4 LTS (Noble Numbat)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 24.04.4 LTS"` if got := parseOSReleaseID([]byte(content)); got != "ubuntu" { t.Errorf("got %q, want ubuntu", got) } } func TestParseOSReleaseID_Debian(t *testing.T) { content := `PRETTY_NAME="Debian GNU/Linux 12 (bookworm)" NAME="Debian GNU/Linux" VERSION_ID="12" VERSION="12 (bookworm)" ID=debian` if got := parseOSReleaseID([]byte(content)); got != "debian" { t.Errorf("got %q, want debian", got) } } func TestParseOSReleaseID_Alpine(t *testing.T) { content := `NAME="Alpine Linux" ID=alpine VERSION_ID=3.20.3 PRETTY_NAME="Alpine Linux v3.20"` if got := parseOSReleaseID([]byte(content)); got != "alpine" { t.Errorf("got %q, want alpine", got) } } func TestParseOSReleaseID_PVE(t *testing.T) { content := `NAME="Proxmox Virtual Environment" VERSION="9.2.3" ID=pve ID_LIKE=debian` if got := parseOSReleaseID([]byte(content)); got != "pve" { t.Errorf("got %q, want pve", got) } } func TestParseOSReleaseID_QuotedValue(t *testing.T) { content := `ID="ubuntu"` if got := parseOSReleaseID([]byte(content)); got != "ubuntu" { t.Errorf("got %q, want ubuntu", got) } } func TestParseOSReleaseID_UnquotedValue(t *testing.T) { content := `ID=alpine` if got := parseOSReleaseID([]byte(content)); got != "alpine" { t.Errorf("got %q, want alpine", got) } } func TestParseOSReleaseID_MissingID(t *testing.T) { content := `NAME="Some Distro" VERSION="1.0"` if got := parseOSReleaseID([]byte(content)); got != "" { t.Errorf("got %q, want empty", got) } } func TestParseOSReleaseID_EmptyContent(t *testing.T) { if got := parseOSReleaseID([]byte("")); got != "" { t.Errorf("got %q, want empty", got) } } func TestParseOSReleaseID_CommentsAndBlankLines(t *testing.T) { content := `# This is a comment NAME="Test" # ID is set below ID=arch PRETTY_NAME="Test Arch"` if got := parseOSReleaseID([]byte(content)); got != "arch" { t.Errorf("got %q, want arch", got) } } func TestParseOSReleaseID_UnknownIDReturnedVerbatim(t *testing.T) { content := `ID=fedora` if got := parseOSReleaseID([]byte(content)); got != "fedora" { t.Errorf("got %q, want fedora (unknown IDs returned verbatim)", got) } } func TestDetectOS_FallbackToLinux(t *testing.T) { // Temporarily point osReleasePaths at non-existent files. orig := osReleasePaths defer func() { osReleasePaths = orig }() osReleasePaths = []string{ filepath.Join(t.TempDir(), "nonexistent-os-release"), } if got := detectOS(); got != "linux" { t.Errorf("got %q, want linux (fallback)", got) } } func TestDetectOS_ReadsEtcOSRelease(t *testing.T) { dir := t.TempDir() orig := osReleasePaths defer func() { osReleasePaths = orig }() osReleasePaths = []string{filepath.Join(dir, "os-release")} if err := os.WriteFile(osReleasePaths[0], []byte("ID=ubuntu\n"), 0o644); err != nil { t.Fatalf("write: %v", err) } if got := detectOS(); got != "ubuntu" { t.Errorf("got %q, want ubuntu", got) } } func TestDetectOS_FallbackToUsrLib(t *testing.T) { dir := t.TempDir() orig := osReleasePaths defer func() { osReleasePaths = orig }() osReleasePaths = []string{ filepath.Join(dir, "etc-os-release"), // missing filepath.Join(dir, "usr-lib-os-release"), // fallback } if err := os.WriteFile(osReleasePaths[1], []byte("ID=alpine\n"), 0o644); err != nil { t.Fatalf("write: %v", err) } if got := detectOS(); got != "alpine" { t.Errorf("got %q, want alpine (from fallback path)", got) } }