//go:build integration // +build integration package pkgtools import ( "fmt" "io/fs" "log" "os" "path/filepath" "sort" "testing" "github.com/stretchr/testify/require" ) func findSlackwarePackages(root string) ([]string, error) { var pkgs []string err := filepath.WalkDir( root, func(path string, d fs.DirEntry, err error) error { if err != nil { log.Fatal(err) } if IsSlackwarePkg(d.Name()) { pkgs = append(pkgs, filepath.Join(path)) } return nil }, ) if err != nil { return nil, err } sort.Strings(pkgs) return pkgs, nil } func removePkg(pkgs []string, p func(str string) bool) []string { var ret []string for _, v := range pkgs { if p(v) { ret = append(ret, v) } } return ret } func saveError(t *testing.T, file string, repErr error) { f, err := os.Create(filepath.Join("error", file)) require.NoError(t, err) defer f.Close() fmt.Fprintln(f, repErr) } // TODO: Adapt to test against official installpkg, removepkg func TestRemoveInstalledPkg(t *testing.T) { require.NotEmpty(t, os.Getenv("ROOT"), "expected ROOT environment variable to be set") root := os.Getenv("ROOT") require.NotEqual(t, "/", root, "a test directory should be used for ROOT. All files there will be deleted") pkgName := "bzip2" removeFlags := DefaultRemovePkgFlags removeFlags.Root = root err := RemovePkg(&removeFlags, pkgName) require.NoError(t, err) } func TestInstallAll(t *testing.T) { pkgsPerRound := 50 require.NotEmpty(t, os.Getenv("SOURCE"), "expected SOURCE environment variable to be set") from := os.Getenv("SOURCE") pkgs, err := findSlackwarePackages(from) require.NoError(t, err) require.NotEmpty(t, pkgs, "expected some Slackware packages to test installation") require.NotEmpty(t, os.Getenv("ROOT"), "expected ROOT environment variable to be set") root := os.Getenv("ROOT") require.NotEqual(t, "/", root, "a test directory should be used for ROOT. All files there will be deleted") installFlags := DefaultInstallPkgFlags installFlags.chmod = false installFlags.chown = false installFlags.Strict = true installFlags.SetEnvValues() removeFlags := DefaultRemovePkgFlags removeFlags.SetEnvValues() for i := 0; i < len(pkgs); i += pkgsPerRound { end := (i + pkgsPerRound) if end >= len(pkgs) { end = len(pkgs) - 1 } roundPkgs := pkgs[i:end] for _, pkg := range roundPkgs { name, err := PackageName(pkg) require.NoError(t, err) err = InstallPkg(&installFlags, pkg) if err != nil { saveError(t, "installpkg-"+name, err) continue } _, err = GetPackageInfo(installFlags.Root, PackageBase(pkg)) require.NoError(t, err, "package must be installed") } for _, pkg := range roundPkgs { name, err := PackageName(pkg) require.NoError(t, err) err = RemovePkg(&removeFlags, name) if err != nil { saveError(t, "removepkg-"+name, err) } } } }