sources_test.go 7.5 KB

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