sources_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package upgrade
  2. import (
  3. "context"
  4. "io"
  5. "strings"
  6. "testing"
  7. "time"
  8. aur "github.com/Jguer/aur"
  9. "github.com/stretchr/testify/assert"
  10. alpm "github.com/Jguer/go-alpm/v2"
  11. "github.com/Jguer/yay/v12/pkg/db/mock"
  12. "github.com/Jguer/yay/v12/pkg/text"
  13. "github.com/Jguer/yay/v12/pkg/vcs"
  14. )
  15. func Test_upAUR(t *testing.T) {
  16. t.Parallel()
  17. type args struct {
  18. remote map[string]alpm.IPackage
  19. aurdata map[string]*aur.Pkg
  20. timeUpdate bool
  21. enableDowngrade bool
  22. }
  23. tests := []struct {
  24. name string
  25. args args
  26. want UpSlice
  27. }{
  28. {
  29. name: "No Updates",
  30. args: args{
  31. remote: map[string]alpm.IPackage{
  32. "hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
  33. "local_pkg": &mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
  34. "ignored": &mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true},
  35. },
  36. aurdata: map[string]*aur.Pkg{
  37. "hello": {Version: "2.0.0", Name: "hello"},
  38. "ignored": {Version: "2.0.0", Name: "ignored"},
  39. },
  40. timeUpdate: false,
  41. },
  42. want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{}},
  43. },
  44. {
  45. name: "Simple Update",
  46. args: args{
  47. remote: map[string]alpm.IPackage{
  48. "hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
  49. },
  50. aurdata: map[string]*aur.Pkg{"hello": {Version: "2.1.0", Name: "hello"}},
  51. timeUpdate: false,
  52. },
  53. want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"}}},
  54. },
  55. {
  56. name: "Downgrade",
  57. args: args{
  58. remote: map[string]alpm.IPackage{
  59. "hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
  60. },
  61. aurdata: map[string]*aur.Pkg{"hello": {Version: "1.0.0", Name: "hello"}},
  62. timeUpdate: false,
  63. enableDowngrade: true,
  64. },
  65. want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "1.0.0"}}},
  66. },
  67. {
  68. name: "Downgrade Disabled",
  69. args: args{
  70. remote: map[string]alpm.IPackage{
  71. "hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
  72. },
  73. aurdata: map[string]*aur.Pkg{"hello": {Version: "1.0.0", Name: "hello"}},
  74. timeUpdate: false,
  75. enableDowngrade: false,
  76. },
  77. want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{}},
  78. },
  79. {
  80. name: "Mixed Updates Downgrades",
  81. args: args{
  82. enableDowngrade: true,
  83. remote: map[string]alpm.IPackage{
  84. "up": &mock.Package{PName: "up", PVersion: "2.0.0"},
  85. "same": &mock.Package{PName: "same", PVersion: "3.0.0"},
  86. "down": &mock.Package{PName: "down", PVersion: "1.1.0"},
  87. "ignored": &mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true},
  88. },
  89. aurdata: map[string]*aur.Pkg{
  90. "up": {Version: "2.1.0", Name: "up"},
  91. "same": {Version: "3.0.0", Name: "same"},
  92. "down": {Version: "1.0.0", Name: "down"},
  93. "ignored": {Version: "2.0.0", Name: "ignored"},
  94. },
  95. timeUpdate: false,
  96. },
  97. want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{
  98. {Name: "up", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"},
  99. {Name: "down", Repository: "aur", LocalVersion: "1.1.0", RemoteVersion: "1.0.0"},
  100. }},
  101. },
  102. {
  103. name: "Time Update",
  104. args: args{
  105. remote: map[string]alpm.IPackage{
  106. "hello": &mock.Package{PName: "hello", PVersion: "2.0.0", PBuildDate: time.Now()},
  107. },
  108. aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello", LastModified: int(time.Now().AddDate(0, 0, 2).Unix())}},
  109. timeUpdate: true,
  110. },
  111. want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.0.0"}}},
  112. },
  113. }
  114. for _, tt := range tests {
  115. tt := tt
  116. t.Run(tt.name, func(t *testing.T) {
  117. t.Parallel()
  118. got := UpAUR(text.NewLogger(io.Discard, strings.NewReader(""), false, "test"),
  119. tt.args.remote, tt.args.aurdata, tt.args.timeUpdate, tt.args.enableDowngrade)
  120. assert.ElementsMatch(t, tt.want.Repos, got.Repos)
  121. assert.ElementsMatch(t, tt.want.Up, got.Up)
  122. assert.Equal(t, tt.want.Len(), got.Len())
  123. })
  124. }
  125. }
  126. func Test_upDevel(t *testing.T) {
  127. t.Parallel()
  128. type args struct {
  129. remote map[string]alpm.IPackage
  130. aurdata map[string]*aur.Pkg
  131. cached vcs.Store
  132. }
  133. tests := []struct {
  134. name string
  135. args args
  136. want UpSlice
  137. finalLen int
  138. }{
  139. {
  140. name: "No Updates",
  141. args: args{
  142. cached: &vcs.Mock{},
  143. remote: map[string]alpm.IPackage{
  144. "hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
  145. "local_pkg": &mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
  146. "ignored": &mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true},
  147. },
  148. aurdata: map[string]*aur.Pkg{
  149. "hello": {Version: "2.0.0", Name: "hello"},
  150. "ignored": {Version: "2.0.0", Name: "ignored"},
  151. },
  152. },
  153. want: UpSlice{Repos: []string{"devel"}},
  154. },
  155. {
  156. name: "Simple Update",
  157. finalLen: 3,
  158. args: args{
  159. cached: &vcs.Mock{
  160. ToUpgradeReturn: []string{"hello", "hello4"},
  161. },
  162. remote: map[string]alpm.IPackage{
  163. "hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
  164. "hello2": &mock.Package{PName: "hello2", PVersion: "3.0.0"},
  165. "hello4": &mock.Package{PName: "hello4", PVersion: "4.0.0"},
  166. },
  167. aurdata: map[string]*aur.Pkg{
  168. "hello": {Version: "2.0.0", Name: "hello"},
  169. "hello2": {Version: "2.0.0", Name: "hello2"},
  170. "hello4": {Version: "2.0.0", Name: "hello4"},
  171. },
  172. },
  173. want: UpSlice{
  174. Repos: []string{"devel"}, Up: []Upgrade{
  175. {
  176. Name: "hello",
  177. Repository: "devel",
  178. LocalVersion: "2.0.0",
  179. RemoteVersion: "latest-commit",
  180. },
  181. {
  182. Name: "hello4",
  183. Repository: "devel",
  184. LocalVersion: "4.0.0",
  185. RemoteVersion: "latest-commit",
  186. },
  187. },
  188. },
  189. },
  190. {
  191. name: "No update returned",
  192. finalLen: 1,
  193. args: args{
  194. cached: &vcs.Mock{ToUpgradeReturn: []string{}},
  195. remote: map[string]alpm.IPackage{
  196. "hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
  197. },
  198. aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
  199. },
  200. want: UpSlice{Repos: []string{"devel"}},
  201. },
  202. {
  203. name: "No update returned - ignored",
  204. finalLen: 1,
  205. args: args{
  206. cached: &vcs.Mock{
  207. ToUpgradeReturn: []string{"hello"},
  208. },
  209. remote: map[string]alpm.IPackage{
  210. "hello": &mock.Package{PName: "hello", PVersion: "2.0.0", PShouldIgnore: true},
  211. },
  212. aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
  213. },
  214. want: UpSlice{Repos: []string{"devel"}},
  215. },
  216. }
  217. for _, tt := range tests {
  218. tt := tt
  219. t.Run(tt.name, func(t *testing.T) {
  220. t.Parallel()
  221. got := UpDevel(context.Background(),
  222. text.NewLogger(io.Discard, strings.NewReader(""), false, "test"),
  223. tt.args.remote, tt.args.aurdata, tt.args.cached)
  224. assert.ElementsMatch(t, tt.want.Up, got.Up)
  225. })
  226. }
  227. }