package pkgtools import ( "encoding/json" "io" "os" "testing" "time" "github.com/stretchr/testify/require" ) func TestPackageBase(t *testing.T) { tests := []struct { FileName string Exp string }{ { FileName: "kernel-firmware-20220124_eb8ea1b-noarch-1.txz", Exp: "kernel-firmware-20220124_eb8ea1b-noarch-1", }, { FileName: "tar-1.34-x86_64-1.txz", Exp: "tar-1.34-x86_64-1", }, { FileName: "tar-1.34-x86_64-1.other", Exp: "tar-1.34-x86_64-1.other", }, } for _, test := range tests { name := PackageBase(test.FileName) require.Equal(t, test.Exp, name) } } func TestPackageName(t *testing.T) { tests := []struct { FileName string Exp string }{ { FileName: "kernel-firmware-20220124_eb8ea1b-noarch-1.txz", Exp: "kernel-firmware", }, { FileName: "tar-1.34-x86_64-1.txz", Exp: "tar", }, { FileName: "tar-1.34-x86_64-1", Exp: "tar", }, } for _, test := range tests { name, err := PackageName(test.FileName) require.NoError(t, err) require.Equal(t, test.Exp, name) } } func TestReadPackageSoftLinks(t *testing.T) { tests := []struct { File string Exp []string }{ { File: "/var/lib/pkgtools/scripts/acl-2.3.1-x86_64-1", Exp: []string{ "lib64/libacl.so.1", "usr/lib64/libacl.so", "usr/lib64/libacl.so.1", "usr/lib64/libacl.so.1.1.2301", }, }, } for _, test := range tests { links, err := ReadPackageSoftLinks(test.File) require.NoError(t, err) require.Equal(t, test.Exp, links) } } func getInfo(fp string) (*PackageInfo, error) { slackPkg, err := OpenSlackwarePkg("testdata/tar-1.34-x86_64-1.tar") if err != nil { return nil, err } defer slackPkg.Close() for { entry, err := slackPkg.Next() if err == io.EOF { break } else if err != nil { return nil, err } if entry == nil { continue } io.Copy(io.Discard, slackPkg) } return slackPkg.PkgInfo(), nil } func jsonRead(obj interface{}, fp string) error { buf, err := os.ReadFile(fp) if err != nil { return err } return json.Unmarshal(buf, obj) } func TestReadPackageInfo(t *testing.T) { tarFile := "testdata/kernel-source-5.15.27-noarch-1" expFile := "testdata/kernel-source-5.15.27-noarch-1_exp.json" start := time.Now() info, err := ReadPackageInfo(tarFile) require.NoError(t, err) d := time.Now().Sub(start) require.Less(t, d, 5*time.Second) var exp PackageInfo require.NoError(t, jsonRead(&exp, expFile)) _ = info require.Equal(t, &exp, info) }