sources_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package upgrade
  2. import (
  3. "context"
  4. "fmt"
  5. "os/exec"
  6. "strconv"
  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/v10/pkg/db/mock"
  13. "github.com/Jguer/yay/v10/pkg/settings/exe"
  14. "github.com/Jguer/yay/v10/pkg/vcs"
  15. )
  16. func Test_upAUR(t *testing.T) {
  17. t.Parallel()
  18. type args struct {
  19. remote []alpm.IPackage
  20. aurdata map[string]*aur.Pkg
  21. timeUpdate 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: []alpm.IPackage{
  32. &mock.Package{PName: "hello", PVersion: "2.0.0"},
  33. &mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
  34. &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: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0"}},
  48. aurdata: map[string]*aur.Pkg{"hello": {Version: "2.1.0", Name: "hello"}},
  49. timeUpdate: false,
  50. },
  51. want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"}}},
  52. },
  53. {
  54. name: "Time Update",
  55. args: args{
  56. remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0", PBuildDate: time.Now()}},
  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)
  68. assert.EqualValues(t, tt.want, got)
  69. })
  70. }
  71. }
  72. type MockRunner struct {
  73. Returned []string
  74. Index int
  75. t *testing.T
  76. }
  77. func (r *MockRunner) Show(cmd *exec.Cmd) error {
  78. return nil
  79. }
  80. func (r *MockRunner) Capture(cmd *exec.Cmd) (stdout, stderr string, err error) {
  81. i, _ := strconv.Atoi(cmd.Args[len(cmd.Args)-1])
  82. if i >= len(r.Returned) {
  83. fmt.Println(r.Returned)
  84. fmt.Println(cmd.Args)
  85. fmt.Println(i)
  86. }
  87. stdout = r.Returned[i]
  88. assert.Contains(r.t, cmd.Args, "ls-remote")
  89. return stdout, stderr, err
  90. }
  91. func Test_upDevel(t *testing.T) {
  92. t.Parallel()
  93. returnValue := []string{
  94. "7f4c277ce7149665d1c79b76ca8fbb832a65a03b HEAD",
  95. "7f4c277ce7149665d1c79b76ca8fbb832a65a03b HEAD",
  96. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa HEAD",
  97. "cccccccccccccccccccccccccccccccccccccccc HEAD",
  98. "991c5b4146fd27f4aacf4e3111258a848934aaa1 HEAD",
  99. }
  100. type args struct {
  101. remote []alpm.IPackage
  102. aurdata map[string]*aur.Pkg
  103. cached vcs.InfoStore
  104. }
  105. tests := []struct {
  106. name string
  107. args args
  108. want UpSlice
  109. finalLen int
  110. }{
  111. {
  112. name: "No Updates",
  113. args: args{
  114. cached: vcs.InfoStore{
  115. CmdBuilder: &exe.CmdBuilder{
  116. Runner: &MockRunner{
  117. Returned: returnValue,
  118. },
  119. },
  120. },
  121. remote: []alpm.IPackage{
  122. &mock.Package{PName: "hello", PVersion: "2.0.0"},
  123. &mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
  124. &mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true},
  125. },
  126. aurdata: map[string]*aur.Pkg{
  127. "hello": {Version: "2.0.0", Name: "hello"},
  128. "ignored": {Version: "2.0.0", Name: "ignored"},
  129. },
  130. },
  131. want: UpSlice{Repos: []string{"devel"}},
  132. },
  133. {
  134. name: "Simple Update",
  135. finalLen: 3,
  136. args: args{
  137. cached: vcs.InfoStore{
  138. CmdBuilder: &exe.CmdBuilder{
  139. Runner: &MockRunner{
  140. Returned: returnValue,
  141. },
  142. },
  143. OriginsByPackage: map[string]vcs.OriginInfoByURL{
  144. "hello": {
  145. "github.com/Jguer/z.git": vcs.OriginInfo{
  146. Protocols: []string{"https"},
  147. Branch: "0",
  148. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  149. },
  150. },
  151. "hello-non-existent": {
  152. "github.com/Jguer/y.git": vcs.OriginInfo{
  153. Protocols: []string{"https"},
  154. Branch: "0",
  155. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  156. },
  157. },
  158. "hello2": {
  159. "github.com/Jguer/a.git": vcs.OriginInfo{
  160. Protocols: []string{"https"},
  161. Branch: "1",
  162. SHA: "7f4c277ce7149665d1c79b76ca8fbb832a65a03b",
  163. },
  164. },
  165. "hello4": {
  166. "github.com/Jguer/b.git": vcs.OriginInfo{
  167. Protocols: []string{"https"},
  168. Branch: "2",
  169. SHA: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  170. },
  171. "github.com/Jguer/c.git": vcs.OriginInfo{
  172. Protocols: []string{"https"},
  173. Branch: "3",
  174. SHA: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
  175. },
  176. },
  177. },
  178. },
  179. remote: []alpm.IPackage{
  180. &mock.Package{PName: "hello", PVersion: "2.0.0"},
  181. &mock.Package{PName: "hello2", PVersion: "3.0.0"},
  182. &mock.Package{PName: "hello4", PVersion: "4.0.0"},
  183. },
  184. aurdata: map[string]*aur.Pkg{
  185. "hello": {Version: "2.0.0", Name: "hello"},
  186. "hello2": {Version: "2.0.0", Name: "hello2"},
  187. "hello4": {Version: "2.0.0", Name: "hello4"},
  188. },
  189. },
  190. want: UpSlice{
  191. Repos: []string{"devel"}, Up: []Upgrade{
  192. {
  193. Name: "hello",
  194. Repository: "devel",
  195. LocalVersion: "2.0.0",
  196. RemoteVersion: "latest-commit",
  197. },
  198. {
  199. Name: "hello4",
  200. Repository: "devel",
  201. LocalVersion: "4.0.0",
  202. RemoteVersion: "latest-commit",
  203. },
  204. },
  205. },
  206. },
  207. {
  208. name: "No update returned",
  209. finalLen: 1,
  210. args: args{
  211. cached: vcs.InfoStore{
  212. CmdBuilder: &exe.CmdBuilder{
  213. Runner: &MockRunner{
  214. Returned: returnValue,
  215. },
  216. },
  217. OriginsByPackage: map[string]vcs.OriginInfoByURL{
  218. "hello": {
  219. "github.com/Jguer/d.git": vcs.OriginInfo{
  220. Protocols: []string{"https"},
  221. Branch: "4",
  222. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  223. },
  224. },
  225. },
  226. },
  227. remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0"}},
  228. aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
  229. },
  230. want: UpSlice{Repos: []string{"devel"}},
  231. },
  232. {
  233. name: "No update returned - ignored",
  234. finalLen: 1,
  235. args: args{
  236. cached: vcs.InfoStore{
  237. CmdBuilder: &exe.CmdBuilder{
  238. Runner: &MockRunner{
  239. Returned: returnValue,
  240. },
  241. },
  242. OriginsByPackage: map[string]vcs.OriginInfoByURL{
  243. "hello": {
  244. "github.com/Jguer/e.git": vcs.OriginInfo{
  245. Protocols: []string{"https"},
  246. Branch: "3",
  247. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  248. },
  249. },
  250. },
  251. },
  252. remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0", PShouldIgnore: true}},
  253. aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
  254. },
  255. want: UpSlice{Repos: []string{"devel"}},
  256. },
  257. }
  258. for _, tt := range tests {
  259. tt := tt
  260. t.Run(tt.name, func(t *testing.T) {
  261. t.Parallel()
  262. tt.args.cached.CmdBuilder.(*exe.CmdBuilder).Runner.(*MockRunner).t = t
  263. got := UpDevel(context.TODO(), tt.args.remote, tt.args.aurdata, &tt.args.cached)
  264. assert.ElementsMatch(t, tt.want.Up, got.Up)
  265. assert.Equal(t, tt.finalLen, len(tt.args.cached.OriginsByPackage))
  266. })
  267. }
  268. }