test(upgrade): test AUR upgrades

This commit is contained in:
jguer 2020-08-17 02:19:18 +02:00
parent c305e1c196
commit 4a761c287b
No known key found for this signature in database
GPG Key ID: 6D6CC9BEA8556B35
9 changed files with 224 additions and 16 deletions

View File

@ -0,0 +1,2 @@
 -> ignored: ignoring package upgrade (1.0.0 => 2.0.0)

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -19,12 +19,12 @@ import (
// GetPkgbuild gets the pkgbuild of the package 'pkg' trying the ABS first and then the AUR trying the ABS first and then the AUR.
// RemovePackage removes package from VCS information
func removeVCSPackage(pkgs []string) {
func removeVCSPackage(pkgs []string, localCache vcsInfo) {
updated := false
for _, pkgName := range pkgs {
if _, ok := savedInfo[pkgName]; ok {
delete(savedInfo, pkgName)
if _, ok := localCache[pkgName]; ok {
delete(localCache, pkgName)
updated = true
}
}

6
cmd.go
View File

@ -159,7 +159,7 @@ func handleCmd(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
case "Q", "query":
return handleQuery(cmdArgs, dbExecutor)
case "R", "remove":
return handleRemove(cmdArgs)
return handleRemove(cmdArgs, savedInfo)
case "S", "sync":
return handleSync(cmdArgs, dbExecutor)
case "T", "deptest":
@ -285,10 +285,10 @@ func handleSync(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
return nil
}
func handleRemove(cmdArgs *settings.Arguments) error {
func handleRemove(cmdArgs *settings.Arguments, localCache vcsInfo) error {
err := show(passToPacman(cmdArgs))
if err == nil {
removeVCSPackage(cmdArgs.Targets)
removeVCSPackage(cmdArgs.Targets, localCache)
}
return err

41
exec.go
View File

@ -133,3 +133,44 @@ func passToGit(dir string, _args ...string) *exec.Cmd {
func isTty() bool {
return terminal.IsTerminal(int(os.Stdout.Fd()))
}
type Runner interface {
Capture(string, int64, ...string) (string, string, error)
}
type OSRunner struct{}
func (r *OSRunner) Capture(command string, timeout int64, args ...string) (stdout, stderr string, err error) {
var outbuf, errbuf bytes.Buffer
var timer *time.Timer
cmd := exec.Command(command, args...)
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
err = cmd.Start()
if err != nil {
return "", "", err
}
if timeout != 0 {
timer = time.AfterFunc(time.Duration(timeout)*time.Second, func() {
err = cmd.Process.Kill()
if err != nil {
text.Errorln(err)
}
})
}
err = cmd.Wait()
if timeout != 0 {
timer.Stop()
}
if err != nil {
return "", "", err
}
stdout = strings.TrimSpace(outbuf.String())
stderr = strings.TrimSpace(errbuf.String())
return stdout, stderr, err
}

51
pkg/db/mock/repo.go Normal file
View File

@ -0,0 +1,51 @@
package mock
import (
"time"
"github.com/Jguer/go-alpm"
)
type Package struct {
PBase string
PBuildDate time.Time
PDB *alpm.DB
PDescription string
PISize int64
PName string
PShouldIgnore bool
PSize int64
PVersion string
PReason alpm.PkgReason
}
func (p *Package) Base() string {
return p.PBase
}
func (p *Package) BuildDate() time.Time {
return p.PBuildDate
}
func (p *Package) DB() *alpm.DB {
return p.PDB
}
func (p *Package) Description() string {
return p.PDescription
}
func (p *Package) ISize() int64 {
return p.PISize
}
func (p *Package) Name() string {
return p.PName
}
func (p *Package) ShouldIgnore() bool {
return p.PShouldIgnore
}
func (p *Package) Size() int64 {
return p.PSize
}
func (p *Package) Version() string {
return p.PVersion
}
func (p *Package) Reason() alpm.PkgReason {
return p.PReason
}

View File

@ -61,7 +61,7 @@ func upList(warnings *query.AURWarnings, dbExecutor db.Executor, enableDowngrade
wg.Add(1)
go func() {
aurUp = upAUR(remote, aurdata)
aurUp = upAUR(remote, aurdata, config.TimeUpdate)
wg.Done()
}()
@ -69,7 +69,7 @@ func upList(warnings *query.AURWarnings, dbExecutor db.Executor, enableDowngrade
text.OperationInfoln(gotext.Get("Checking development packages..."))
wg.Add(1)
go func() {
develUp = upDevel(remote, aurdata)
develUp = upDevel(remote, aurdata, savedInfo)
wg.Done()
}()
}
@ -97,12 +97,11 @@ func upList(warnings *query.AURWarnings, dbExecutor db.Executor, enableDowngrade
return aurUp, repoUp, errs.Return()
}
func upDevel(remote []db.RepoPackage, aurdata map[string]*rpc.Pkg) upgrade.UpSlice {
toUpdate := make([]db.RepoPackage, 0)
func upDevel(remote []db.RepoPackage, aurdata map[string]*rpc.Pkg, localCache vcsInfo) upgrade.UpSlice {
toUpdate := make([]db.RepoPackage, 0, len(aurdata))
toRemove := make([]string, 0)
var mux1 sync.Mutex
var mux2 sync.Mutex
var mux1, mux2 sync.Mutex
var wg sync.WaitGroup
checkUpdate := func(vcsName string, e shaInfos) {
@ -126,7 +125,7 @@ func upDevel(remote []db.RepoPackage, aurdata map[string]*rpc.Pkg) upgrade.UpSli
}
}
for vcsName, e := range savedInfo {
for vcsName, e := range localCache {
wg.Add(1)
go checkUpdate(vcsName, e)
}
@ -148,7 +147,7 @@ func upDevel(remote []db.RepoPackage, aurdata map[string]*rpc.Pkg) upgrade.UpSli
}
}
removeVCSPackage(toRemove)
removeVCSPackage(toRemove, localCache)
return toUpgrade
}
@ -163,7 +162,7 @@ func printIgnoringPackage(pkg db.RepoPackage, newPkgVersion string) {
// upAUR gathers foreign packages and checks if they have new versions.
// Output: Upgrade type package list.
func upAUR(remote []db.RepoPackage, aurdata map[string]*rpc.Pkg) upgrade.UpSlice {
func upAUR(remote []db.RepoPackage, aurdata map[string]*rpc.Pkg, timeUpdate bool) upgrade.UpSlice {
toUpgrade := make(upgrade.UpSlice, 0)
for _, pkg := range remote {
@ -172,7 +171,7 @@ func upAUR(remote []db.RepoPackage, aurdata map[string]*rpc.Pkg) upgrade.UpSlice
continue
}
if (config.TimeUpdate && (int64(aurPkg.LastModified) > pkg.BuildDate().Unix())) ||
if (timeUpdate && (int64(aurPkg.LastModified) > pkg.BuildDate().Unix())) ||
(alpm.VerCmp(pkg.Version(), aurPkg.Version) < 0) {
if pkg.ShouldIgnore() {
printIgnoringPackage(pkg, aurPkg.Version)

113
upgrade_test.go Normal file
View File

@ -0,0 +1,113 @@
package main
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/Jguer/yay/v10/pkg/db"
"github.com/Jguer/yay/v10/pkg/db/mock"
"github.com/Jguer/yay/v10/pkg/upgrade"
"github.com/bradleyjkemp/cupaloy"
rpc "github.com/mikkeloscar/aur"
"github.com/stretchr/testify/assert"
)
func Test_upAUR(t *testing.T) {
type args struct {
remote []db.RepoPackage
aurdata map[string]*rpc.Pkg
timeUpdate bool
}
tests := []struct {
name string
args args
want upgrade.UpSlice
}{
{name: "No Updates",
args: args{
remote: []db.RepoPackage{
&mock.Package{PName: "hello", PVersion: "2.0.0"},
&mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
&mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true}},
aurdata: map[string]*rpc.Pkg{
"hello": {Version: "2.0.0", Name: "hello"},
"ignored": {Version: "2.0.0", Name: "ignored"}},
timeUpdate: false,
},
want: upgrade.UpSlice{}},
{name: "Simple Update",
args: args{
remote: []db.RepoPackage{&mock.Package{PName: "hello", PVersion: "2.0.0"}},
aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.1.0", Name: "hello"}},
timeUpdate: false,
},
want: upgrade.UpSlice{upgrade.Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"}}},
{name: "Time Update",
args: args{
remote: []db.RepoPackage{&mock.Package{PName: "hello", PVersion: "2.0.0", PBuildDate: time.Now()}},
aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.0.0", Name: "hello", LastModified: int(time.Now().AddDate(0, 0, 2).Unix())}},
timeUpdate: true,
},
want: upgrade.UpSlice{upgrade.Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.0.0"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
got := upAUR(tt.args.remote, tt.args.aurdata, tt.args.timeUpdate)
assert.EqualValues(t, tt.want, got)
w.Close()
out, _ := ioutil.ReadAll(r)
cupaloy.SnapshotT(t, out)
os.Stdout = rescueStdout
})
}
}
func Test_upDevel(t *testing.T) {
type args struct {
remote []db.RepoPackage
aurdata map[string]*rpc.Pkg
cached vcsInfo
}
tests := []struct {
name string
args args
want upgrade.UpSlice
}{
{name: "No Updates",
args: args{
cached: vcsInfo{},
remote: []db.RepoPackage{
&mock.Package{PName: "hello", PVersion: "2.0.0"},
&mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
&mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true}},
aurdata: map[string]*rpc.Pkg{
"hello": {Version: "2.0.0", Name: "hello"},
"ignored": {Version: "2.0.0", Name: "ignored"}},
},
want: upgrade.UpSlice{}},
{name: "Simple Update",
args: args{
cached: vcsInfo{
"hello": shaInfos{
"github.com/Jguer/yay.git": shaInfo{
Protocols: []string{"https"},
Branch: "main",
SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1"}}},
remote: []db.RepoPackage{&mock.Package{PName: "hello", PVersion: "2.0.0"}},
aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.1.0", Name: "hello"}},
},
want: upgrade.UpSlice{upgrade.Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := upDevel(tt.args.remote, tt.args.aurdata, tt.args.cached)
assert.EqualValues(t, tt.want, got)
})
}
}