sources_test.go 6.9 KB

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