sources_test.go 7.6 KB

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