sources_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package upgrade
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. aur "github.com/Jguer/aur"
  7. "github.com/stretchr/testify/assert"
  8. alpm "github.com/Jguer/go-alpm/v2"
  9. "github.com/Jguer/yay/v11/pkg/db/mock"
  10. "github.com/Jguer/yay/v11/pkg/vcs"
  11. )
  12. func Test_upAUR(t *testing.T) {
  13. t.Parallel()
  14. type args struct {
  15. remote []alpm.IPackage
  16. aurdata map[string]*aur.Pkg
  17. timeUpdate bool
  18. }
  19. tests := []struct {
  20. name string
  21. args args
  22. want UpSlice
  23. }{
  24. {
  25. name: "No Updates",
  26. args: args{
  27. remote: []alpm.IPackage{
  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. },
  32. aurdata: map[string]*aur.Pkg{
  33. "hello": {Version: "2.0.0", Name: "hello"},
  34. "ignored": {Version: "2.0.0", Name: "ignored"},
  35. },
  36. timeUpdate: false,
  37. },
  38. want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{}},
  39. },
  40. {
  41. name: "Simple Update",
  42. args: args{
  43. remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0"}},
  44. aurdata: map[string]*aur.Pkg{"hello": {Version: "2.1.0", Name: "hello"}},
  45. timeUpdate: false,
  46. },
  47. want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"}}},
  48. },
  49. {
  50. name: "Time Update",
  51. args: args{
  52. remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0", PBuildDate: time.Now()}},
  53. aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello", LastModified: int(time.Now().AddDate(0, 0, 2).Unix())}},
  54. timeUpdate: true,
  55. },
  56. want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.0.0"}}},
  57. },
  58. }
  59. for _, tt := range tests {
  60. tt := tt
  61. t.Run(tt.name, func(t *testing.T) {
  62. t.Parallel()
  63. got := UpAUR(tt.args.remote, tt.args.aurdata, tt.args.timeUpdate)
  64. assert.EqualValues(t, tt.want, got)
  65. })
  66. }
  67. }
  68. func Test_upDevel(t *testing.T) {
  69. t.Parallel()
  70. type args struct {
  71. remote []alpm.IPackage
  72. aurdata map[string]*aur.Pkg
  73. cached vcs.Store
  74. }
  75. tests := []struct {
  76. name string
  77. args args
  78. want UpSlice
  79. finalLen int
  80. }{
  81. {
  82. name: "No Updates",
  83. args: args{
  84. cached: &vcs.Mock{},
  85. remote: []alpm.IPackage{
  86. &mock.Package{PName: "hello", PVersion: "2.0.0"},
  87. &mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
  88. &mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true},
  89. },
  90. aurdata: map[string]*aur.Pkg{
  91. "hello": {Version: "2.0.0", Name: "hello"},
  92. "ignored": {Version: "2.0.0", Name: "ignored"},
  93. },
  94. },
  95. want: UpSlice{Repos: []string{"devel"}},
  96. },
  97. {
  98. name: "Simple Update",
  99. finalLen: 3,
  100. args: args{
  101. cached: &vcs.Mock{
  102. ToUpgradeReturn: []string{"hello", "hello4"},
  103. },
  104. remote: []alpm.IPackage{
  105. &mock.Package{PName: "hello", PVersion: "2.0.0"},
  106. &mock.Package{PName: "hello2", PVersion: "3.0.0"},
  107. &mock.Package{PName: "hello4", PVersion: "4.0.0"},
  108. },
  109. aurdata: map[string]*aur.Pkg{
  110. "hello": {Version: "2.0.0", Name: "hello"},
  111. "hello2": {Version: "2.0.0", Name: "hello2"},
  112. "hello4": {Version: "2.0.0", Name: "hello4"},
  113. },
  114. },
  115. want: UpSlice{
  116. Repos: []string{"devel"}, Up: []Upgrade{
  117. {
  118. Name: "hello",
  119. Repository: "devel",
  120. LocalVersion: "2.0.0",
  121. RemoteVersion: "latest-commit",
  122. },
  123. {
  124. Name: "hello4",
  125. Repository: "devel",
  126. LocalVersion: "4.0.0",
  127. RemoteVersion: "latest-commit",
  128. },
  129. },
  130. },
  131. },
  132. {
  133. name: "No update returned",
  134. finalLen: 1,
  135. args: args{
  136. cached: &vcs.Mock{ToUpgradeReturn: []string{}},
  137. remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0"}},
  138. aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
  139. },
  140. want: UpSlice{Repos: []string{"devel"}},
  141. },
  142. {
  143. name: "No update returned - ignored",
  144. finalLen: 1,
  145. args: args{
  146. cached: &vcs.Mock{
  147. ToUpgradeReturn: []string{"hello"},
  148. },
  149. remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0", PShouldIgnore: true}},
  150. aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
  151. },
  152. want: UpSlice{Repos: []string{"devel"}},
  153. },
  154. }
  155. for _, tt := range tests {
  156. tt := tt
  157. t.Run(tt.name, func(t *testing.T) {
  158. t.Parallel()
  159. got := UpDevel(context.TODO(), tt.args.remote, tt.args.aurdata, tt.args.cached)
  160. assert.ElementsMatch(t, tt.want.Up, got.Up)
  161. })
  162. }
  163. }