2020-08-17 00:19:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-08-17 23:19:51 +00:00
|
|
|
"fmt"
|
2020-08-17 00:19:18 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-08-17 23:19:51 +00:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
2020-08-17 00:19:18 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bradleyjkemp/cupaloy"
|
|
|
|
rpc "github.com/mikkeloscar/aur"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-08-17 23:19:51 +00:00
|
|
|
|
|
|
|
"github.com/Jguer/yay/v10/pkg/db"
|
|
|
|
"github.com/Jguer/yay/v10/pkg/db/mock"
|
|
|
|
"github.com/Jguer/yay/v10/pkg/settings"
|
|
|
|
"github.com/Jguer/yay/v10/pkg/upgrade"
|
2020-08-21 00:39:52 +00:00
|
|
|
"github.com/Jguer/yay/v10/pkg/vcs"
|
2020-08-17 00:19:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}{
|
2020-08-21 00:39:52 +00:00
|
|
|
{
|
|
|
|
name: "No Updates",
|
2020-08-17 00:19:18 +00:00
|
|
|
args: args{
|
|
|
|
remote: []db.RepoPackage{
|
|
|
|
&mock.Package{PName: "hello", PVersion: "2.0.0"},
|
|
|
|
&mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
|
2020-08-21 00:39:52 +00:00
|
|
|
&mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true},
|
|
|
|
},
|
2020-08-17 00:19:18 +00:00
|
|
|
aurdata: map[string]*rpc.Pkg{
|
|
|
|
"hello": {Version: "2.0.0", Name: "hello"},
|
2020-08-21 00:39:52 +00:00
|
|
|
"ignored": {Version: "2.0.0", Name: "ignored"},
|
|
|
|
},
|
2020-08-17 00:19:18 +00:00
|
|
|
timeUpdate: false,
|
|
|
|
},
|
2020-08-21 00:39:52 +00:00
|
|
|
want: upgrade.UpSlice{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Simple Update",
|
2020-08-17 00:19:18 +00:00
|
|
|
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,
|
|
|
|
},
|
2020-08-21 00:39:52 +00:00
|
|
|
want: upgrade.UpSlice{upgrade.Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Time Update",
|
2020-08-17 00:19:18 +00:00
|
|
|
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,
|
|
|
|
},
|
2020-08-21 00:39:52 +00:00
|
|
|
want: upgrade.UpSlice{upgrade.Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.0.0"}},
|
|
|
|
},
|
2020-08-17 00:19:18 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 23:19:51 +00:00
|
|
|
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 00:19:18 +00:00
|
|
|
func Test_upDevel(t *testing.T) {
|
2020-08-21 00:39:52 +00:00
|
|
|
var err error
|
|
|
|
config, err = settings.NewConfig()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-08-17 23:19:51 +00:00
|
|
|
config.Runtime.CmdRunner = &MockRunner{
|
|
|
|
Returned: []string{
|
|
|
|
"7f4c277ce7149665d1c79b76ca8fbb832a65a03b HEAD",
|
|
|
|
"7f4c277ce7149665d1c79b76ca8fbb832a65a03b HEAD",
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa HEAD",
|
|
|
|
"cccccccccccccccccccccccccccccccccccccccc HEAD",
|
|
|
|
"991c5b4146fd27f4aacf4e3111258a848934aaa1 HEAD",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-17 00:19:18 +00:00
|
|
|
type args struct {
|
|
|
|
remote []db.RepoPackage
|
|
|
|
aurdata map[string]*rpc.Pkg
|
2020-08-21 00:39:52 +00:00
|
|
|
cached vcs.InfoStore
|
2020-08-17 00:19:18 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
2020-08-17 23:19:51 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want upgrade.UpSlice
|
|
|
|
finalLen int
|
2020-08-17 00:19:18 +00:00
|
|
|
}{
|
2020-08-21 00:39:52 +00:00
|
|
|
{
|
|
|
|
name: "No Updates",
|
2020-08-17 00:19:18 +00:00
|
|
|
args: args{
|
2020-08-21 00:39:52 +00:00
|
|
|
cached: vcs.InfoStore{
|
|
|
|
Runner: config.Runtime.CmdRunner,
|
|
|
|
CmdBuilder: config.Runtime.CmdBuilder,
|
|
|
|
},
|
2020-08-17 00:19:18 +00:00
|
|
|
remote: []db.RepoPackage{
|
|
|
|
&mock.Package{PName: "hello", PVersion: "2.0.0"},
|
|
|
|
&mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
|
2020-08-21 00:39:52 +00:00
|
|
|
&mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true},
|
|
|
|
},
|
2020-08-17 00:19:18 +00:00
|
|
|
aurdata: map[string]*rpc.Pkg{
|
|
|
|
"hello": {Version: "2.0.0", Name: "hello"},
|
2020-08-21 00:39:52 +00:00
|
|
|
"ignored": {Version: "2.0.0", Name: "ignored"},
|
|
|
|
},
|
2020-08-17 00:19:18 +00:00
|
|
|
},
|
2020-08-21 00:39:52 +00:00
|
|
|
want: upgrade.UpSlice{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Simple Update",
|
2020-08-17 23:19:51 +00:00
|
|
|
finalLen: 3,
|
2020-08-17 00:19:18 +00:00
|
|
|
args: args{
|
2020-08-21 00:39:52 +00:00
|
|
|
cached: vcs.InfoStore{
|
|
|
|
Runner: config.Runtime.CmdRunner,
|
|
|
|
CmdBuilder: config.Runtime.CmdBuilder,
|
|
|
|
OriginsByPackage: map[string]vcs.OriginInfoByURL{
|
|
|
|
"hello": {
|
|
|
|
"github.com/Jguer/z.git": vcs.OriginInfo{
|
|
|
|
Protocols: []string{"https"},
|
|
|
|
Branch: "0",
|
|
|
|
SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"hello-non-existant": {
|
|
|
|
"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-08-17 23:19:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
remote: []db.RepoPackage{
|
|
|
|
&mock.Package{PName: "hello", PVersion: "2.0.0"},
|
|
|
|
&mock.Package{PName: "hello2", PVersion: "3.0.0"},
|
2020-08-21 00:39:52 +00:00
|
|
|
&mock.Package{PName: "hello4", PVersion: "4.0.0"},
|
|
|
|
},
|
2020-08-17 23:19:51 +00:00
|
|
|
aurdata: map[string]*rpc.Pkg{
|
|
|
|
"hello": {Version: "2.0.0", Name: "hello"},
|
|
|
|
"hello2": {Version: "2.0.0", Name: "hello2"},
|
|
|
|
"hello4": {Version: "2.0.0", Name: "hello4"},
|
|
|
|
},
|
|
|
|
},
|
2020-08-21 00:39:52 +00:00
|
|
|
want: upgrade.UpSlice{
|
|
|
|
upgrade.Upgrade{
|
|
|
|
Name: "hello",
|
|
|
|
Repository: "devel",
|
|
|
|
LocalVersion: "2.0.0",
|
|
|
|
RemoteVersion: "latest-commit",
|
|
|
|
},
|
2020-08-17 23:19:51 +00:00
|
|
|
upgrade.Upgrade{
|
|
|
|
Name: "hello4",
|
|
|
|
Repository: "devel",
|
|
|
|
LocalVersion: "4.0.0",
|
2020-08-21 00:39:52 +00:00
|
|
|
RemoteVersion: "latest-commit",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "No update returned",
|
2020-08-17 23:19:51 +00:00
|
|
|
finalLen: 1,
|
|
|
|
args: args{
|
2020-08-21 00:39:52 +00:00
|
|
|
cached: vcs.InfoStore{
|
|
|
|
Runner: config.Runtime.CmdRunner,
|
|
|
|
CmdBuilder: config.Runtime.CmdBuilder,
|
|
|
|
OriginsByPackage: map[string]vcs.OriginInfoByURL{
|
|
|
|
"hello": {
|
|
|
|
"github.com/Jguer/d.git": vcs.OriginInfo{
|
|
|
|
Protocols: []string{"https"},
|
|
|
|
Branch: "4",
|
|
|
|
SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-08-17 00:19:18 +00:00
|
|
|
remote: []db.RepoPackage{&mock.Package{PName: "hello", PVersion: "2.0.0"}},
|
2020-08-17 23:19:51 +00:00
|
|
|
aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
|
2020-08-17 00:19:18 +00:00
|
|
|
},
|
2020-08-21 00:39:52 +00:00
|
|
|
want: upgrade.UpSlice{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "No update returned - ignored",
|
2020-08-17 23:19:51 +00:00
|
|
|
finalLen: 1,
|
|
|
|
args: args{
|
2020-08-21 00:39:52 +00:00
|
|
|
cached: vcs.InfoStore{
|
|
|
|
Runner: config.Runtime.CmdRunner,
|
|
|
|
CmdBuilder: config.Runtime.CmdBuilder,
|
|
|
|
OriginsByPackage: map[string]vcs.OriginInfoByURL{
|
|
|
|
"hello": {
|
|
|
|
"github.com/Jguer/e.git": vcs.OriginInfo{
|
|
|
|
Protocols: []string{"https"},
|
|
|
|
Branch: "3",
|
|
|
|
SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-08-17 23:19:51 +00:00
|
|
|
remote: []db.RepoPackage{&mock.Package{PName: "hello", PVersion: "2.0.0", PShouldIgnore: true}},
|
|
|
|
aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
|
|
|
|
},
|
2020-08-21 00:39:52 +00:00
|
|
|
want: upgrade.UpSlice{},
|
|
|
|
},
|
2020-08-17 00:19:18 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-08-17 23:19:51 +00:00
|
|
|
config.Runtime.CmdRunner.(*MockRunner).t = t
|
2020-08-21 22:39:26 +00:00
|
|
|
got := upDevel(tt.args.remote, tt.args.aurdata, &tt.args.cached)
|
2020-08-17 23:19:51 +00:00
|
|
|
assert.ElementsMatch(t, tt.want, got)
|
2020-08-21 00:39:52 +00:00
|
|
|
assert.Equal(t, tt.finalLen, len(tt.args.cached.OriginsByPackage))
|
2020-08-17 00:19:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|