upgrade_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/Jguer/yay/v10/pkg/db"
  8. "github.com/Jguer/yay/v10/pkg/db/mock"
  9. "github.com/Jguer/yay/v10/pkg/upgrade"
  10. "github.com/bradleyjkemp/cupaloy"
  11. rpc "github.com/mikkeloscar/aur"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func Test_upAUR(t *testing.T) {
  15. type args struct {
  16. remote []db.RepoPackage
  17. aurdata map[string]*rpc.Pkg
  18. timeUpdate bool
  19. }
  20. tests := []struct {
  21. name string
  22. args args
  23. want upgrade.UpSlice
  24. }{
  25. {name: "No Updates",
  26. args: args{
  27. remote: []db.RepoPackage{
  28. &mock.Package{PName: "hello", PVersion: "2.0.0"},
  29. &mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
  30. &mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true}},
  31. aurdata: map[string]*rpc.Pkg{
  32. "hello": {Version: "2.0.0", Name: "hello"},
  33. "ignored": {Version: "2.0.0", Name: "ignored"}},
  34. timeUpdate: false,
  35. },
  36. want: upgrade.UpSlice{}},
  37. {name: "Simple Update",
  38. args: args{
  39. remote: []db.RepoPackage{&mock.Package{PName: "hello", PVersion: "2.0.0"}},
  40. aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.1.0", Name: "hello"}},
  41. timeUpdate: false,
  42. },
  43. want: upgrade.UpSlice{upgrade.Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"}}},
  44. {name: "Time Update",
  45. args: args{
  46. remote: []db.RepoPackage{&mock.Package{PName: "hello", PVersion: "2.0.0", PBuildDate: time.Now()}},
  47. aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.0.0", Name: "hello", LastModified: int(time.Now().AddDate(0, 0, 2).Unix())}},
  48. timeUpdate: true,
  49. },
  50. want: upgrade.UpSlice{upgrade.Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.0.0"}}},
  51. }
  52. for _, tt := range tests {
  53. t.Run(tt.name, func(t *testing.T) {
  54. rescueStdout := os.Stdout
  55. r, w, _ := os.Pipe()
  56. os.Stdout = w
  57. got := upAUR(tt.args.remote, tt.args.aurdata, tt.args.timeUpdate)
  58. assert.EqualValues(t, tt.want, got)
  59. w.Close()
  60. out, _ := ioutil.ReadAll(r)
  61. cupaloy.SnapshotT(t, out)
  62. os.Stdout = rescueStdout
  63. })
  64. }
  65. }
  66. func Test_upDevel(t *testing.T) {
  67. type args struct {
  68. remote []db.RepoPackage
  69. aurdata map[string]*rpc.Pkg
  70. cached vcsInfo
  71. }
  72. tests := []struct {
  73. name string
  74. args args
  75. want upgrade.UpSlice
  76. }{
  77. {name: "No Updates",
  78. args: args{
  79. cached: vcsInfo{},
  80. remote: []db.RepoPackage{
  81. &mock.Package{PName: "hello", PVersion: "2.0.0"},
  82. &mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
  83. &mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true}},
  84. aurdata: map[string]*rpc.Pkg{
  85. "hello": {Version: "2.0.0", Name: "hello"},
  86. "ignored": {Version: "2.0.0", Name: "ignored"}},
  87. },
  88. want: upgrade.UpSlice{}},
  89. {name: "Simple Update",
  90. args: args{
  91. cached: vcsInfo{
  92. "hello": shaInfos{
  93. "github.com/Jguer/yay.git": shaInfo{
  94. Protocols: []string{"https"},
  95. Branch: "main",
  96. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1"}}},
  97. remote: []db.RepoPackage{&mock.Package{PName: "hello", PVersion: "2.0.0"}},
  98. aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.1.0", Name: "hello"}},
  99. },
  100. want: upgrade.UpSlice{upgrade.Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"}}},
  101. }
  102. for _, tt := range tests {
  103. t.Run(tt.name, func(t *testing.T) {
  104. got := upDevel(tt.args.remote, tt.args.aurdata, tt.args.cached)
  105. assert.EqualValues(t, tt.want, got)
  106. })
  107. }
  108. }