yay/pkg/upgrade/sources_test.go

172 lines
4.5 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"
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"
2021-09-08 22:28:08 +02:00
"github.com/Jguer/yay/v11/pkg/db/mock"
"github.com/Jguer/yay/v11/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 {
2020-10-01 13:38:03 +02:00
remote []alpm.IPackage
2021-05-13 07:27:24 +02:00
aurdata map[string]*aur.Pkg
2020-08-17 02:19:18 +02:00
timeUpdate bool
}
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{
2020-10-01 13:38:03 +02:00
remote: []alpm.IPackage{
2020-08-17 02:19:18 +02:00
&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},
},
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{
2020-10-01 13:38:03 +02:00
remote: []alpm.IPackage{&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: "Time Update",
2020-08-17 02:19:18 +02:00
args: args{
2020-10-01 13:38:03 +02:00
remote: []alpm.IPackage{&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()
2020-10-01 14:06:21 +02:00
got := UpAUR(tt.args.remote, tt.args.aurdata, tt.args.timeUpdate)
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 {
2020-10-01 13:38:03 +02:00
remote []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{},
2020-10-01 13:38:03 +02:00
remote: []alpm.IPackage{
2020-08-17 02:19:18 +02:00
&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},
},
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"},
},
2020-10-01 13:38:03 +02:00
remote: []alpm.IPackage{
&mock.Package{PName: "hello", PVersion: "2.0.0"},
&mock.Package{PName: "hello2", PVersion: "3.0.0"},
&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{}},
2020-10-01 13:38:03 +02:00
remote: []alpm.IPackage{&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"},
},
2020-10-01 13:38:03 +02:00
remote: []alpm.IPackage{&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(), 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
})
}
}