yay/pkg/upgrade/sources_test.go

235 lines
6.7 KiB
Go
Raw Normal View History

2020-10-01 14:06:21 +02:00
package upgrade
2020-08-17 02:19:18 +02:00
import (
2021-08-12 18:56:23 +02:00
"context"
"io"
"strings"
2020-08-17 02:19:18 +02:00
"testing"
"time"
2021-05-13 07:27:24 +02:00
aur "github.com/Jguer/aur"
2020-08-17 02:19:18 +02:00
"github.com/stretchr/testify/assert"
2020-10-01 13:38:03 +02:00
alpm "github.com/Jguer/go-alpm/v2"
"github.com/Jguer/yay/v12/pkg/db/mock"
"github.com/Jguer/yay/v12/pkg/text"
"github.com/Jguer/yay/v12/pkg/vcs"
2020-08-17 02:19:18 +02:00
)
func Test_upAUR(t *testing.T) {
2021-08-11 20:13:28 +02:00
t.Parallel()
2021-08-11 22:05:47 +02:00
2020-08-17 02:19:18 +02:00
type args struct {
remote map[string]alpm.IPackage
aurdata map[string]*aur.Pkg
timeUpdate bool
enableDowngrade bool
2020-08-17 02:19:18 +02:00
}
tests := []struct {
name string
args args
2020-10-01 14:06:21 +02:00
want UpSlice
2020-08-17 02:19:18 +02:00
}{
{
name: "No Updates",
2020-08-17 02:19:18 +02:00
args: args{
remote: map[string]alpm.IPackage{
"hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
"local_pkg": &mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
"ignored": &mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true},
},
2021-05-13 07:27:24 +02:00
aurdata: map[string]*aur.Pkg{
2020-08-17 02:19:18 +02:00
"hello": {Version: "2.0.0", Name: "hello"},
"ignored": {Version: "2.0.0", Name: "ignored"},
},
2020-08-17 02:19:18 +02:00
timeUpdate: false,
},
2021-04-19 13:43:13 +02:00
want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{}},
},
{
name: "Simple Update",
2020-08-17 02:19:18 +02:00
args: args{
remote: map[string]alpm.IPackage{
"hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
},
2021-05-13 07:27:24 +02:00
aurdata: map[string]*aur.Pkg{"hello": {Version: "2.1.0", Name: "hello"}},
2020-08-17 02:19:18 +02:00
timeUpdate: false,
},
2021-04-19 13:43:13 +02:00
want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"}}},
},
{
name: "Downgrade",
args: args{
remote: map[string]alpm.IPackage{
"hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
},
aurdata: map[string]*aur.Pkg{"hello": {Version: "1.0.0", Name: "hello"}},
timeUpdate: false,
enableDowngrade: true,
},
want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "1.0.0"}}},
},
{
name: "Downgrade Disabled",
args: args{
remote: map[string]alpm.IPackage{
"hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
},
aurdata: map[string]*aur.Pkg{"hello": {Version: "1.0.0", Name: "hello"}},
timeUpdate: false,
enableDowngrade: false,
},
want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{}},
},
{
name: "Mixed Updates Downgrades",
args: args{
enableDowngrade: true,
remote: map[string]alpm.IPackage{
"up": &mock.Package{PName: "up", PVersion: "2.0.0"},
"same": &mock.Package{PName: "same", PVersion: "3.0.0"},
"down": &mock.Package{PName: "down", PVersion: "1.1.0"},
"ignored": &mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true},
},
aurdata: map[string]*aur.Pkg{
"up": {Version: "2.1.0", Name: "up"},
"same": {Version: "3.0.0", Name: "same"},
"down": {Version: "1.0.0", Name: "down"},
"ignored": {Version: "2.0.0", Name: "ignored"},
},
timeUpdate: false,
},
want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{
{Name: "up", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"},
{Name: "down", Repository: "aur", LocalVersion: "1.1.0", RemoteVersion: "1.0.0"},
}},
},
{
name: "Time Update",
2020-08-17 02:19:18 +02:00
args: args{
remote: map[string]alpm.IPackage{
"hello": &mock.Package{PName: "hello", PVersion: "2.0.0", PBuildDate: time.Now()},
},
2021-05-13 07:27:24 +02:00
aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello", LastModified: int(time.Now().AddDate(0, 0, 2).Unix())}},
2020-08-17 02:19:18 +02:00
timeUpdate: true,
},
2021-04-19 13:43:13 +02:00
want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.0.0"}}},
},
2020-08-17 02:19:18 +02:00
}
for _, tt := range tests {
2021-08-11 20:13:28 +02:00
tt := tt
2020-08-17 02:19:18 +02:00
t.Run(tt.name, func(t *testing.T) {
2021-08-11 20:13:28 +02:00
t.Parallel()
got := UpAUR(text.NewLogger(io.Discard, strings.NewReader(""), false, "test"),
tt.args.remote, tt.args.aurdata, tt.args.timeUpdate, tt.args.enableDowngrade)
2020-08-17 02:19:18 +02:00
assert.EqualValues(t, tt.want, got)
})
}
}
func Test_upDevel(t *testing.T) {
2021-08-11 20:13:28 +02:00
t.Parallel()
2020-08-17 02:19:18 +02:00
type args struct {
remote map[string]alpm.IPackage
2021-05-13 07:27:24 +02:00
aurdata map[string]*aur.Pkg
cached vcs.Store
2020-08-17 02:19:18 +02:00
}
tests := []struct {
name string
args args
2020-10-01 14:06:21 +02:00
want UpSlice
finalLen int
2020-08-17 02:19:18 +02:00
}{
{
name: "No Updates",
2020-08-17 02:19:18 +02:00
args: args{
cached: &vcs.Mock{},
remote: map[string]alpm.IPackage{
"hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
"local_pkg": &mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
"ignored": &mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true},
},
2021-05-13 07:27:24 +02:00
aurdata: map[string]*aur.Pkg{
2020-08-17 02:19:18 +02:00
"hello": {Version: "2.0.0", Name: "hello"},
"ignored": {Version: "2.0.0", Name: "ignored"},
},
2020-08-17 02:19:18 +02:00
},
2021-04-19 13:43:13 +02:00
want: UpSlice{Repos: []string{"devel"}},
},
{
name: "Simple Update",
finalLen: 3,
2020-08-17 02:19:18 +02:00
args: args{
cached: &vcs.Mock{
ToUpgradeReturn: []string{"hello", "hello4"},
},
remote: map[string]alpm.IPackage{
"hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
"hello2": &mock.Package{PName: "hello2", PVersion: "3.0.0"},
"hello4": &mock.Package{PName: "hello4", PVersion: "4.0.0"},
},
2021-05-13 07:27:24 +02:00
aurdata: map[string]*aur.Pkg{
"hello": {Version: "2.0.0", Name: "hello"},
"hello2": {Version: "2.0.0", Name: "hello2"},
"hello4": {Version: "2.0.0", Name: "hello4"},
},
},
2021-08-02 14:07:33 +02:00
want: UpSlice{
Repos: []string{"devel"}, Up: []Upgrade{
{
Name: "hello",
Repository: "devel",
LocalVersion: "2.0.0",
RemoteVersion: "latest-commit",
},
{
Name: "hello4",
Repository: "devel",
LocalVersion: "4.0.0",
RemoteVersion: "latest-commit",
},
},
},
},
{
name: "No update returned",
finalLen: 1,
args: args{
cached: &vcs.Mock{ToUpgradeReturn: []string{}},
remote: map[string]alpm.IPackage{
"hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
},
2021-05-13 07:27:24 +02:00
aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
2020-08-17 02:19:18 +02:00
},
2021-04-19 13:43:13 +02:00
want: UpSlice{Repos: []string{"devel"}},
},
{
name: "No update returned - ignored",
finalLen: 1,
args: args{
cached: &vcs.Mock{
ToUpgradeReturn: []string{"hello"},
},
remote: map[string]alpm.IPackage{
"hello": &mock.Package{PName: "hello", PVersion: "2.0.0", PShouldIgnore: true},
},
2021-05-13 07:27:24 +02:00
aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
},
2021-04-19 13:43:13 +02:00
want: UpSlice{Repos: []string{"devel"}},
},
2020-08-17 02:19:18 +02:00
}
for _, tt := range tests {
2021-08-11 20:13:28 +02:00
tt := tt
2020-08-17 02:19:18 +02:00
t.Run(tt.name, func(t *testing.T) {
2021-08-11 20:13:28 +02:00
t.Parallel()
got := UpDevel(context.Background(),
text.NewLogger(io.Discard, strings.NewReader(""), false, "test"),
tt.args.remote, tt.args.aurdata, tt.args.cached)
2021-04-19 13:43:13 +02:00
assert.ElementsMatch(t, tt.want.Up, got.Up)
2020-08-17 02:19:18 +02:00
})
}
}