sources_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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{},
  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{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{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{},
  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{
  195. Upgrade{
  196. Name: "hello",
  197. Repository: "devel",
  198. LocalVersion: "2.0.0",
  199. RemoteVersion: "latest-commit",
  200. },
  201. Upgrade{
  202. Name: "hello4",
  203. Repository: "devel",
  204. LocalVersion: "4.0.0",
  205. RemoteVersion: "latest-commit",
  206. },
  207. },
  208. },
  209. {
  210. name: "No update returned",
  211. finalLen: 1,
  212. args: args{
  213. cached: vcs.InfoStore{
  214. Runner: config.Runtime.CmdRunner,
  215. CmdBuilder: config.Runtime.CmdBuilder,
  216. OriginsByPackage: map[string]vcs.OriginInfoByURL{
  217. "hello": {
  218. "github.com/Jguer/d.git": vcs.OriginInfo{
  219. Protocols: []string{"https"},
  220. Branch: "4",
  221. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  222. },
  223. },
  224. },
  225. },
  226. remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0"}},
  227. aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
  228. },
  229. want: UpSlice{},
  230. },
  231. {
  232. name: "No update returned - ignored",
  233. finalLen: 1,
  234. args: args{
  235. cached: vcs.InfoStore{
  236. Runner: config.Runtime.CmdRunner,
  237. CmdBuilder: config.Runtime.CmdBuilder,
  238. OriginsByPackage: map[string]vcs.OriginInfoByURL{
  239. "hello": {
  240. "github.com/Jguer/e.git": vcs.OriginInfo{
  241. Protocols: []string{"https"},
  242. Branch: "3",
  243. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  244. },
  245. },
  246. },
  247. },
  248. remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0", PShouldIgnore: true}},
  249. aurdata: map[string]*rpc.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
  250. },
  251. want: UpSlice{},
  252. },
  253. }
  254. for _, tt := range tests {
  255. t.Run(tt.name, func(t *testing.T) {
  256. config.Runtime.CmdRunner.(*MockRunner).t = t
  257. got := UpDevel(tt.args.remote, tt.args.aurdata, &tt.args.cached)
  258. assert.ElementsMatch(t, tt.want, got)
  259. assert.Equal(t, tt.finalLen, len(tt.args.cached.OriginsByPackage))
  260. })
  261. }
  262. }