diff options
author | Slack Coder <slackcoder@server.ky> | 2022-03-11 13:08:24 -0500 |
---|---|---|
committer | Slack Coder <slackcoder@server.ky> | 2022-03-30 11:34:46 -0500 |
commit | 29589a24b13fb223b113e94eca2c4fff0e56a4d9 (patch) | |
tree | e1754d195463439ae2834cd502b170648e47cdb8 /removepkg_integration_test.go | |
download | pkgtools-go-29589a24b13fb223b113e94eca2c4fff0e56a4d9.tar.xz |
Initial commit
Diffstat (limited to 'removepkg_integration_test.go')
-rw-r--r-- | removepkg_integration_test.go | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/removepkg_integration_test.go b/removepkg_integration_test.go new file mode 100644 index 0000000..15801bd --- /dev/null +++ b/removepkg_integration_test.go @@ -0,0 +1,122 @@ +//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, 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") + + _ = os.RemoveAll(root) + + installFlags := DefaultInstallPkgFlags + installFlags.chmod = false + installFlags.chown = false + 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) + } + } + } +} |