sources_test.go 6.9 KB

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