upgrade_test.go 6.6 KB

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