Files
zeta-reconstructor/internal/depmap/depmap_test.go
T

203 lines
4.5 KiB
Go

package depmap
import (
"strings"
"testing"
)
func TestMapDep(t *testing.T) {
tests := []struct {
name string
spec string
want string
wantOK bool
warns int
warnMsg string // exact expected warn message ("" = only check count)
}{
{
name: "zlib renames to libz",
spec: "zlib",
want: "libz",
wantOK: true,
warns: 1,
},
{
name: "glibc dropped as base system",
spec: "glibc",
want: "",
wantOK: false,
warns: 1,
warnMsg: `dropping base-system dependency "glibc"`,
},
{
name: "verbatim index name keeps constraint",
spec: "pcre2>=10.42",
want: "pcre2>=10.42",
wantOK: true,
warns: 0,
},
{
name: "optdepend description stripped",
spec: "python: for scripting",
want: "python",
wantOK: true,
warns: 1,
},
{
name: "unknown dep passes through with warn",
spec: "someunknownpkg123",
want: "someunknownpkg123",
wantOK: true,
warns: 1,
warnMsg: `unmapped dependency "someunknownpkg123" (passed through)`,
},
{
name: "rename preserves constraint",
spec: "zlib>=1.3.1",
want: "libz>=1.3.1",
wantOK: true,
warns: 1,
},
{
name: "x11 case fix libx11",
spec: "libx11",
want: "libX11",
wantOK: true,
warns: 1,
},
{
name: "xz renames to xz-utils",
spec: "xz",
want: "xz-utils",
wantOK: true,
warns: 1,
},
{
name: "x11 case fix libice",
spec: "libice",
want: "libICE",
wantOK: true,
warns: 1,
},
{
name: "freetype2 renames to freetype",
spec: "freetype2",
want: "freetype",
wantOK: true,
warns: 1,
},
{
name: "libxkbcommon renames to xkbcommon",
spec: "libxkbcommon",
want: "xkbcommon",
wantOK: true,
warns: 1,
},
{
name: "libxcb already correct not renamed",
spec: "libxcb",
want: "libxcb",
wantOK: true,
warns: 0,
},
{
name: "zeta casing input passes through verbatim",
spec: "libX11",
want: "libX11",
wantOK: true,
warns: 0,
},
{
name: "bare = normalized to ==",
spec: "foo=1.0",
want: "foo==1.0",
wantOK: true,
warns: 1,
},
{
name: "description plus constraint plus rename",
spec: "zlib>=1.2: for compression",
want: "libz>=1.2",
wantOK: true,
warns: 1,
},
{
name: "drop applies regardless of constraint",
spec: "glibc>=2.38",
want: "",
wantOK: false,
warns: 1,
},
{
name: "gcc-libs dropped",
spec: "gcc-libs",
want: "",
wantOK: false,
warns: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var warns []string
got, ok := MapDep(tt.spec, func(msg string) {
warns = append(warns, msg)
})
if got != tt.want {
t.Errorf("MapDep(%q) = %q, want %q", tt.spec, got, tt.want)
}
if ok != tt.wantOK {
t.Errorf("MapDep(%q) ok = %v, want %v", tt.spec, ok, tt.wantOK)
}
if len(warns) != tt.warns {
t.Errorf("MapDep(%q) fired %d warns (%v), want %d",
tt.spec, len(warns), warns, tt.warns)
}
if tt.warnMsg != "" && len(warns) == 1 && warns[0] != tt.warnMsg {
t.Errorf("MapDep(%q) warn = %q, want %q", tt.spec, warns[0], tt.warnMsg)
}
})
}
}
// TestMapDepNilWarn ensures MapDep tolerates a nil warn callback.
func TestMapDepNilWarn(t *testing.T) {
got, ok := MapDep("zlib", nil)
if got != "libz" || !ok {
t.Fatalf("MapDep(zlib, nil) = %q, %v; want %q, true", got, ok, "libz")
}
}
// TestRenameTargetsExistInIndex guards the checked-in tables against typos:
// every rename target must be a real Zeta package, and every dropped name must
// NOT be one.
func TestRenameTargetsExistInIndex(t *testing.T) {
for from, to := range renames {
if _, ok := zetapkgs[to]; !ok {
t.Errorf("rename %q -> %q: target not in zetapkgs index", from, to)
}
if _, ok := zetapkgs[from]; ok {
t.Errorf("rename %q -> %q: source already in zetapkgs (rename is dead code)", from, to)
}
if _, ok := dropped[to]; ok {
t.Errorf("rename %q -> %q: target also in drop list", from, to)
}
}
for name := range dropped {
if _, ok := zetapkgs[name]; ok {
t.Errorf("drop %q: present in zetapkgs index (drop is wrong)", name)
}
}
}
// TestRenamesSortedLint is a cheap self-check that rename sources are
// lowercase (Arch convention) so case fixes are keyed from the Arch spelling.
func TestRenamesLowercaseKeys(t *testing.T) {
for from := range renames {
if strings.ToLower(from) != from {
t.Errorf("rename key %q is not lowercase", from)
}
}
}