sources_test.go 4.8 KB

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