mirror of
https://github.com/Jguer/yay.git
synced 2025-12-09 00:03:52 -05:00
test(upgrade): test AUR upgrades
This commit is contained in:
parent
c305e1c196
commit
4a761c287b
2
.snapshots/Test_upAUR-No_Updates
Normal file
2
.snapshots/Test_upAUR-No_Updates
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[1m[33m ->[0m[0m [36mignored[0m: ignoring package upgrade ([31m1.0.0[0m => [32m2.0.0[0m)
|
||||||
|
|
||||||
1
.snapshots/Test_upAUR-Simple_Update
Normal file
1
.snapshots/Test_upAUR-Simple_Update
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
.snapshots/Test_upAUR-Time_Update
Normal file
1
.snapshots/Test_upAUR-Time_Update
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
||||||
6
clean.go
6
clean.go
@ -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.
|
// 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
|
// RemovePackage removes package from VCS information
|
||||||
func removeVCSPackage(pkgs []string) {
|
func removeVCSPackage(pkgs []string, localCache vcsInfo) {
|
||||||
updated := false
|
updated := false
|
||||||
|
|
||||||
for _, pkgName := range pkgs {
|
for _, pkgName := range pkgs {
|
||||||
if _, ok := savedInfo[pkgName]; ok {
|
if _, ok := localCache[pkgName]; ok {
|
||||||
delete(savedInfo, pkgName)
|
delete(localCache, pkgName)
|
||||||
updated = true
|
updated = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
cmd.go
6
cmd.go
@ -159,7 +159,7 @@ func handleCmd(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
|
|||||||
case "Q", "query":
|
case "Q", "query":
|
||||||
return handleQuery(cmdArgs, dbExecutor)
|
return handleQuery(cmdArgs, dbExecutor)
|
||||||
case "R", "remove":
|
case "R", "remove":
|
||||||
return handleRemove(cmdArgs)
|
return handleRemove(cmdArgs, savedInfo)
|
||||||
case "S", "sync":
|
case "S", "sync":
|
||||||
return handleSync(cmdArgs, dbExecutor)
|
return handleSync(cmdArgs, dbExecutor)
|
||||||
case "T", "deptest":
|
case "T", "deptest":
|
||||||
@ -285,10 +285,10 @@ func handleSync(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleRemove(cmdArgs *settings.Arguments) error {
|
func handleRemove(cmdArgs *settings.Arguments, localCache vcsInfo) error {
|
||||||
err := show(passToPacman(cmdArgs))
|
err := show(passToPacman(cmdArgs))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
removeVCSPackage(cmdArgs.Targets)
|
removeVCSPackage(cmdArgs.Targets, localCache)
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
|||||||
41
exec.go
41
exec.go
@ -133,3 +133,44 @@ func passToGit(dir string, _args ...string) *exec.Cmd {
|
|||||||
func isTty() bool {
|
func isTty() bool {
|
||||||
return terminal.IsTerminal(int(os.Stdout.Fd()))
|
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
51
pkg/db/mock/repo.go
Normal 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
|
||||||
|
}
|
||||||
19
upgrade.go
19
upgrade.go
@ -61,7 +61,7 @@ func upList(warnings *query.AURWarnings, dbExecutor db.Executor, enableDowngrade
|
|||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
aurUp = upAUR(remote, aurdata)
|
aurUp = upAUR(remote, aurdata, config.TimeUpdate)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ func upList(warnings *query.AURWarnings, dbExecutor db.Executor, enableDowngrade
|
|||||||
text.OperationInfoln(gotext.Get("Checking development packages..."))
|
text.OperationInfoln(gotext.Get("Checking development packages..."))
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
develUp = upDevel(remote, aurdata)
|
develUp = upDevel(remote, aurdata, savedInfo)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@ -97,12 +97,11 @@ func upList(warnings *query.AURWarnings, dbExecutor db.Executor, enableDowngrade
|
|||||||
return aurUp, repoUp, errs.Return()
|
return aurUp, repoUp, errs.Return()
|
||||||
}
|
}
|
||||||
|
|
||||||
func upDevel(remote []db.RepoPackage, aurdata map[string]*rpc.Pkg) upgrade.UpSlice {
|
func upDevel(remote []db.RepoPackage, aurdata map[string]*rpc.Pkg, localCache vcsInfo) upgrade.UpSlice {
|
||||||
toUpdate := make([]db.RepoPackage, 0)
|
toUpdate := make([]db.RepoPackage, 0, len(aurdata))
|
||||||
toRemove := make([]string, 0)
|
toRemove := make([]string, 0)
|
||||||
|
|
||||||
var mux1 sync.Mutex
|
var mux1, mux2 sync.Mutex
|
||||||
var mux2 sync.Mutex
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
checkUpdate := func(vcsName string, e shaInfos) {
|
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)
|
wg.Add(1)
|
||||||
go checkUpdate(vcsName, e)
|
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
|
return toUpgrade
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,7 +162,7 @@ func printIgnoringPackage(pkg db.RepoPackage, newPkgVersion string) {
|
|||||||
|
|
||||||
// upAUR gathers foreign packages and checks if they have new versions.
|
// upAUR gathers foreign packages and checks if they have new versions.
|
||||||
// Output: Upgrade type package list.
|
// 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)
|
toUpgrade := make(upgrade.UpSlice, 0)
|
||||||
|
|
||||||
for _, pkg := range remote {
|
for _, pkg := range remote {
|
||||||
@ -172,7 +171,7 @@ func upAUR(remote []db.RepoPackage, aurdata map[string]*rpc.Pkg) upgrade.UpSlice
|
|||||||
continue
|
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) {
|
(alpm.VerCmp(pkg.Version(), aurPkg.Version) < 0) {
|
||||||
if pkg.ShouldIgnore() {
|
if pkg.ShouldIgnore() {
|
||||||
printIgnoringPackage(pkg, aurPkg.Version)
|
printIgnoringPackage(pkg, aurPkg.Version)
|
||||||
|
|||||||
113
upgrade_test.go
Normal file
113
upgrade_test.go
Normal 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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user