yay/pkg/upgrade/sources_test.go

279 lines
7.4 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 (
"fmt"
"os/exec"
"strconv"
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/v10/pkg/db/mock"
"github.com/Jguer/yay/v10/pkg/settings/exe"
"github.com/Jguer/yay/v10/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)
})
}
}
type MockRunner struct {
Returned []string
Index int
t *testing.T
}
func (r *MockRunner) Show(cmd *exec.Cmd) error {
return nil
}
func (r *MockRunner) Capture(cmd *exec.Cmd, timeout int64) (stdout, stderr string, err error) {
i, _ := strconv.Atoi(cmd.Args[len(cmd.Args)-1])
if i >= len(r.Returned) {
fmt.Println(r.Returned)
fmt.Println(cmd.Args)
fmt.Println(i)
}
stdout = r.Returned[i]
assert.Contains(r.t, cmd.Args, "ls-remote")
return stdout, stderr, err
}
2020-08-17 02:19:18 +02:00
func Test_upDevel(t *testing.T) {
2021-08-11 20:13:28 +02:00
t.Parallel()
2021-08-11 22:05:47 +02:00
returnValue := []string{
"7f4c277ce7149665d1c79b76ca8fbb832a65a03b HEAD",
"7f4c277ce7149665d1c79b76ca8fbb832a65a03b HEAD",
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa HEAD",
"cccccccccccccccccccccccccccccccccccccccc HEAD",
"991c5b4146fd27f4aacf4e3111258a848934aaa1 HEAD",
}
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.InfoStore
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.InfoStore{
2021-08-11 22:05:47 +02:00
CmdBuilder: &exe.CmdBuilder{
Runner: &MockRunner{
Returned: returnValue,
},
},
},
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.InfoStore{
2021-08-11 22:05:47 +02:00
CmdBuilder: &exe.CmdBuilder{
Runner: &MockRunner{
Returned: returnValue,
},
},
OriginsByPackage: map[string]vcs.OriginInfoByURL{
"hello": {
"github.com/Jguer/z.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "0",
SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
},
},
2021-08-11 20:13:28 +02:00
"hello-non-existent": {
"github.com/Jguer/y.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "0",
SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
},
},
"hello2": {
"github.com/Jguer/a.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "1",
SHA: "7f4c277ce7149665d1c79b76ca8fbb832a65a03b",
},
},
"hello4": {
"github.com/Jguer/b.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "2",
SHA: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
},
"github.com/Jguer/c.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "3",
SHA: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
},
},
},
},
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.InfoStore{
2021-08-11 22:05:47 +02:00
CmdBuilder: &exe.CmdBuilder{
Runner: &MockRunner{
Returned: returnValue,
},
},
OriginsByPackage: map[string]vcs.OriginInfoByURL{
"hello": {
"github.com/Jguer/d.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "4",
SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
},
},
},
},
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.InfoStore{
2021-08-11 22:05:47 +02:00
CmdBuilder: &exe.CmdBuilder{
Runner: &MockRunner{
Returned: returnValue,
},
},
OriginsByPackage: map[string]vcs.OriginInfoByURL{
"hello": {
"github.com/Jguer/e.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "3",
SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
},
},
},
},
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()
2021-08-11 22:05:47 +02:00
tt.args.cached.CmdBuilder.(*exe.CmdBuilder).Runner.(*MockRunner).t = t
2020-10-01 14:06:21 +02:00
got := UpDevel(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)
assert.Equal(t, tt.finalLen, len(tt.args.cached.OriginsByPackage))
2020-08-17 02:19:18 +02:00
})
}
}