sources_test.go 5.0 KB

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