upgrade_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "strconv"
  8. "testing"
  9. "time"
  10. "github.com/bradleyjkemp/cupaloy"
  11. rpc "github.com/mikkeloscar/aur"
  12. "github.com/stretchr/testify/assert"
  13. alpm "github.com/Jguer/go-alpm/v2"
  14. "github.com/Jguer/yay/v10/pkg/db/mock"
  15. "github.com/Jguer/yay/v10/pkg/settings"
  16. "github.com/Jguer/yay/v10/pkg/upgrade"
  17. "github.com/Jguer/yay/v10/pkg/vcs"
  18. )
  19. func Test_upAUR(t *testing.T) {
  20. type args struct {
  21. remote []alpm.IPackage
  22. aurdata map[string]*rpc.Pkg
  23. timeUpdate bool
  24. }
  25. tests := []struct {
  26. name string
  27. args args
  28. want upgrade.UpSlice
  29. }{
  30. {
  31. name: "No Updates",
  32. args: args{
  33. remote: []alpm.IPackage{
  34. &mock.Package{PName: "hello", PVersion: "2.0.0"},
  35. &mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
  36. &mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true},
  37. },
  38. aurdata: map[string]*rpc.Pkg{
  39. "hello": {Version: "2.0.0", Name: "hello"},
  40. "ignored": {Version: "2.0.0", Name: "ignored"},
  41. },
  42. timeUpdate: false,
  43. },
  44. want: upgrade.UpSlice{},
  45. },
  46. {
  47. name: "Simple Update",
  48. args: args{
  49. remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0"}},
  50. aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.1.0", Name: "hello"}},
  51. timeUpdate: false,
  52. },
  53. want: upgrade.UpSlice{upgrade.Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.1.0"}},
  54. },
  55. {
  56. name: "Time Update",
  57. args: args{
  58. remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0", PBuildDate: time.Now()}},
  59. aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.0.0", Name: "hello", LastModified: int(time.Now().AddDate(0, 0, 2).Unix())}},
  60. timeUpdate: true,
  61. },
  62. want: upgrade.UpSlice{upgrade.Upgrade{Name: "hello", Repository: "aur", LocalVersion: "2.0.0", RemoteVersion: "2.0.0"}},
  63. },
  64. }
  65. for _, tt := range tests {
  66. t.Run(tt.name, func(t *testing.T) {
  67. rescueStdout := os.Stdout
  68. r, w, _ := os.Pipe()
  69. os.Stdout = w
  70. got := upAUR(tt.args.remote, tt.args.aurdata, tt.args.timeUpdate)
  71. assert.EqualValues(t, tt.want, got)
  72. w.Close()
  73. out, _ := ioutil.ReadAll(r)
  74. cupaloy.SnapshotT(t, out)
  75. os.Stdout = rescueStdout
  76. })
  77. }
  78. }
  79. type MockRunner struct {
  80. Returned []string
  81. Index int
  82. t *testing.T
  83. }
  84. func (r *MockRunner) Show(cmd *exec.Cmd) error {
  85. return nil
  86. }
  87. func (r *MockRunner) Capture(cmd *exec.Cmd, timeout int64) (stdout, stderr string, err error) {
  88. i, _ := strconv.Atoi(cmd.Args[len(cmd.Args)-1])
  89. if i >= len(r.Returned) {
  90. fmt.Println(r.Returned)
  91. fmt.Println(cmd.Args)
  92. fmt.Println(i)
  93. }
  94. stdout = r.Returned[i]
  95. assert.Contains(r.t, cmd.Args, "ls-remote")
  96. return stdout, stderr, err
  97. }
  98. func Test_upDevel(t *testing.T) {
  99. var err error
  100. config, err = settings.NewConfig()
  101. assert.NoError(t, err)
  102. config.Runtime.CmdRunner = &MockRunner{
  103. Returned: []string{
  104. "7f4c277ce7149665d1c79b76ca8fbb832a65a03b HEAD",
  105. "7f4c277ce7149665d1c79b76ca8fbb832a65a03b HEAD",
  106. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa HEAD",
  107. "cccccccccccccccccccccccccccccccccccccccc HEAD",
  108. "991c5b4146fd27f4aacf4e3111258a848934aaa1 HEAD",
  109. },
  110. }
  111. type args struct {
  112. remote []alpm.IPackage
  113. aurdata map[string]*rpc.Pkg
  114. cached vcs.InfoStore
  115. }
  116. tests := []struct {
  117. name string
  118. args args
  119. want upgrade.UpSlice
  120. finalLen int
  121. }{
  122. {
  123. name: "No Updates",
  124. args: args{
  125. cached: vcs.InfoStore{
  126. Runner: config.Runtime.CmdRunner,
  127. CmdBuilder: config.Runtime.CmdBuilder,
  128. },
  129. remote: []alpm.IPackage{
  130. &mock.Package{PName: "hello", PVersion: "2.0.0"},
  131. &mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
  132. &mock.Package{PName: "ignored", PVersion: "1.0.0", PShouldIgnore: true},
  133. },
  134. aurdata: map[string]*rpc.Pkg{
  135. "hello": {Version: "2.0.0", Name: "hello"},
  136. "ignored": {Version: "2.0.0", Name: "ignored"},
  137. },
  138. },
  139. want: upgrade.UpSlice{},
  140. },
  141. {
  142. name: "Simple Update",
  143. finalLen: 3,
  144. args: args{
  145. cached: vcs.InfoStore{
  146. Runner: config.Runtime.CmdRunner,
  147. CmdBuilder: config.Runtime.CmdBuilder,
  148. OriginsByPackage: map[string]vcs.OriginInfoByURL{
  149. "hello": {
  150. "github.com/Jguer/z.git": vcs.OriginInfo{
  151. Protocols: []string{"https"},
  152. Branch: "0",
  153. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  154. },
  155. },
  156. "hello-non-existant": {
  157. "github.com/Jguer/y.git": vcs.OriginInfo{
  158. Protocols: []string{"https"},
  159. Branch: "0",
  160. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  161. },
  162. },
  163. "hello2": {
  164. "github.com/Jguer/a.git": vcs.OriginInfo{
  165. Protocols: []string{"https"},
  166. Branch: "1",
  167. SHA: "7f4c277ce7149665d1c79b76ca8fbb832a65a03b",
  168. },
  169. },
  170. "hello4": {
  171. "github.com/Jguer/b.git": vcs.OriginInfo{
  172. Protocols: []string{"https"},
  173. Branch: "2",
  174. SHA: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  175. },
  176. "github.com/Jguer/c.git": vcs.OriginInfo{
  177. Protocols: []string{"https"},
  178. Branch: "3",
  179. SHA: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
  180. },
  181. },
  182. },
  183. },
  184. remote: []alpm.IPackage{
  185. &mock.Package{PName: "hello", PVersion: "2.0.0"},
  186. &mock.Package{PName: "hello2", PVersion: "3.0.0"},
  187. &mock.Package{PName: "hello4", PVersion: "4.0.0"},
  188. },
  189. aurdata: map[string]*rpc.Pkg{
  190. "hello": {Version: "2.0.0", Name: "hello"},
  191. "hello2": {Version: "2.0.0", Name: "hello2"},
  192. "hello4": {Version: "2.0.0", Name: "hello4"},
  193. },
  194. },
  195. want: upgrade.UpSlice{
  196. upgrade.Upgrade{
  197. Name: "hello",
  198. Repository: "devel",
  199. LocalVersion: "2.0.0",
  200. RemoteVersion: "latest-commit",
  201. },
  202. upgrade.Upgrade{
  203. Name: "hello4",
  204. Repository: "devel",
  205. LocalVersion: "4.0.0",
  206. RemoteVersion: "latest-commit",
  207. },
  208. },
  209. },
  210. {
  211. name: "No update returned",
  212. finalLen: 1,
  213. args: args{
  214. cached: vcs.InfoStore{
  215. Runner: config.Runtime.CmdRunner,
  216. CmdBuilder: config.Runtime.CmdBuilder,
  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]*rpc.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
  229. },
  230. want: upgrade.UpSlice{},
  231. },
  232. {
  233. name: "No update returned - ignored",
  234. finalLen: 1,
  235. args: args{
  236. cached: vcs.InfoStore{
  237. Runner: config.Runtime.CmdRunner,
  238. CmdBuilder: config.Runtime.CmdBuilder,
  239. OriginsByPackage: map[string]vcs.OriginInfoByURL{
  240. "hello": {
  241. "github.com/Jguer/e.git": vcs.OriginInfo{
  242. Protocols: []string{"https"},
  243. Branch: "3",
  244. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  245. },
  246. },
  247. },
  248. },
  249. remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0", PShouldIgnore: true}},
  250. aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
  251. },
  252. want: upgrade.UpSlice{},
  253. },
  254. }
  255. for _, tt := range tests {
  256. t.Run(tt.name, func(t *testing.T) {
  257. config.Runtime.CmdRunner.(*MockRunner).t = t
  258. got := upDevel(tt.args.remote, tt.args.aurdata, &tt.args.cached)
  259. assert.ElementsMatch(t, tt.want, got)
  260. assert.Equal(t, tt.finalLen, len(tt.args.cached.OriginsByPackage))
  261. })
  262. }
  263. }