sources_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.EqualValues(t, tt.want, got)
  121. })
  122. }
  123. }
  124. func Test_upDevel(t *testing.T) {
  125. t.Parallel()
  126. type args struct {
  127. remote map[string]alpm.IPackage
  128. aurdata map[string]*aur.Pkg
  129. cached vcs.Store
  130. }
  131. tests := []struct {
  132. name string
  133. args args
  134. want UpSlice
  135. finalLen int
  136. }{
  137. {
  138. name: "No Updates",
  139. args: args{
  140. cached: &vcs.Mock{},
  141. remote: map[string]alpm.IPackage{
  142. "hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
  143. "local_pkg": &mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
  144. "ignored": &mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true},
  145. },
  146. aurdata: map[string]*aur.Pkg{
  147. "hello": {Version: "2.0.0", Name: "hello"},
  148. "ignored": {Version: "2.0.0", Name: "ignored"},
  149. },
  150. },
  151. want: UpSlice{Repos: []string{"devel"}},
  152. },
  153. {
  154. name: "Simple Update",
  155. finalLen: 3,
  156. args: args{
  157. cached: &vcs.Mock{
  158. ToUpgradeReturn: []string{"hello", "hello4"},
  159. },
  160. remote: map[string]alpm.IPackage{
  161. "hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
  162. "hello2": &mock.Package{PName: "hello2", PVersion: "3.0.0"},
  163. "hello4": &mock.Package{PName: "hello4", PVersion: "4.0.0"},
  164. },
  165. aurdata: map[string]*aur.Pkg{
  166. "hello": {Version: "2.0.0", Name: "hello"},
  167. "hello2": {Version: "2.0.0", Name: "hello2"},
  168. "hello4": {Version: "2.0.0", Name: "hello4"},
  169. },
  170. },
  171. want: UpSlice{
  172. Repos: []string{"devel"}, Up: []Upgrade{
  173. {
  174. Name: "hello",
  175. Repository: "devel",
  176. LocalVersion: "2.0.0",
  177. RemoteVersion: "latest-commit",
  178. },
  179. {
  180. Name: "hello4",
  181. Repository: "devel",
  182. LocalVersion: "4.0.0",
  183. RemoteVersion: "latest-commit",
  184. },
  185. },
  186. },
  187. },
  188. {
  189. name: "No update returned",
  190. finalLen: 1,
  191. args: args{
  192. cached: &vcs.Mock{ToUpgradeReturn: []string{}},
  193. remote: map[string]alpm.IPackage{
  194. "hello": &mock.Package{PName: "hello", PVersion: "2.0.0"},
  195. },
  196. aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
  197. },
  198. want: UpSlice{Repos: []string{"devel"}},
  199. },
  200. {
  201. name: "No update returned - ignored",
  202. finalLen: 1,
  203. args: args{
  204. cached: &vcs.Mock{
  205. ToUpgradeReturn: []string{"hello"},
  206. },
  207. remote: map[string]alpm.IPackage{
  208. "hello": &mock.Package{PName: "hello", PVersion: "2.0.0", PShouldIgnore: true},
  209. },
  210. aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
  211. },
  212. want: UpSlice{Repos: []string{"devel"}},
  213. },
  214. }
  215. for _, tt := range tests {
  216. tt := tt
  217. t.Run(tt.name, func(t *testing.T) {
  218. t.Parallel()
  219. got := UpDevel(context.Background(),
  220. text.NewLogger(io.Discard, strings.NewReader(""), false, "test"),
  221. tt.args.remote, tt.args.aurdata, tt.args.cached)
  222. assert.ElementsMatch(t, tt.want.Up, got.Up)
  223. })
  224. }
  225. }