package xbps import ( "reflect" "testing" ) func TestParseDryRun(t *testing.T) { out := `[*] Updating repository ` + "`https://repo-default.voidlinux.org/current/x86_64-repodata'" + ` ... firefox-144.0_1 update x86_64 https://repo-default.voidlinux.org/current 245MB 70MB libfoo-2.1_3 install x86_64 https://repo-default.voidlinux.org/current 1024KB 300KB linux6.12-6.12.50_1 update x86_64 https://repo-default.voidlinux.org/current gtk+3-3.24.50_1 update x86_64 https://repo-default.voidlinux.org/current oldpkg-1.0_1 remove x86_64 https://repo-default.voidlinux.org/current bash-5.3_2 configure x86_64 https://repo-default.voidlinux.org/current ` got := ParseDryRun(out) want := []Update{ {Name: "firefox", NewVersion: "144.0_1", Action: "update"}, {Name: "gtk+3", NewVersion: "3.24.50_1", Action: "update"}, {Name: "libfoo", NewVersion: "2.1_3", Action: "install"}, {Name: "linux6.12", NewVersion: "6.12.50_1", Action: "update"}, {Name: "oldpkg", NewVersion: "1.0_1", Action: "remove"}, } if !reflect.DeepEqual(got, want) { t.Errorf("ParseDryRun:\n got %+v\nwant %+v", got, want) } if got := ParseDryRun(""); len(got) != 0 { t.Errorf("ParseDryRun(empty) = %+v, want none", got) } } func TestParseInstalled(t *testing.T) { out := "ii acl-2.4.0_1 Access Control List filesystem support\n" + "ii base-container-0.3_3 Void Linux base system meta\n" + "\n" want := map[string]string{"acl": "2.4.0_1", "base-container": "0.3_3"} if got := ParseInstalled(out); !reflect.DeepEqual(got, want) { t.Errorf("ParseInstalled = %v, want %v", got, want) } } func TestSplitPkgver(t *testing.T) { tests := []struct { in, name, version string ok bool }{ {"xbps-0.60.5_1", "xbps", "0.60.5_1", true}, {"font-jetbrains-mono-2.304_1", "font-jetbrains-mono", "2.304_1", true}, {"[*]", "", "", false}, {"Updating", "", "", false}, {"name-", "", "", false}, {"no-revision-1.0", "", "", false}, } for _, tt := range tests { name, version, ok := SplitPkgver(tt.in) if name != tt.name || version != tt.version || ok != tt.ok { t.Errorf("SplitPkgver(%q) = %q, %q, %v; want %q, %q, %v", tt.in, name, version, ok, tt.name, tt.version, tt.ok) } } } func TestNeedsReboot(t *testing.T) { tests := []struct { names []string want bool }{ {[]string{"firefox", "mousepad"}, false}, {[]string{"linux6.12"}, true}, {[]string{"linux"}, true}, {[]string{"glibc"}, true}, {[]string{"linux-headers", "linux-firmware-network"}, false}, } for _, tt := range tests { var updates []Update for _, n := range tt.names { updates = append(updates, Update{Name: n}) } if got := NeedsReboot(updates); got != tt.want { t.Errorf("NeedsReboot(%v) = %v, want %v", tt.names, got, tt.want) } } }