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. 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/settings/exe"
  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]*aur.Pkg
  23. timeUpdate bool
  24. }
  25. tests := []struct {
  26. name string
  27. args args
  28. want 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]*aur.Pkg{
  39. "hello": {Version: "2.0.0", Name: "hello"},
  40. "ignored": {Version: "2.0.0", Name: "ignored"},
  41. },
  42. timeUpdate: false,
  43. },
  44. want: UpSlice{Repos: []string{"aur"}, Up: []Upgrade{}},
  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]*aur.Pkg{"hello": {Version: "2.1.0", Name: "hello"}},
  51. timeUpdate: false,
  52. },
  53. want: UpSlice{Repos: []string{"aur"}, Up: []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]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello", LastModified: int(time.Now().AddDate(0, 0, 2).Unix())}},
  60. timeUpdate: true,
  61. },
  62. want: UpSlice{Repos: []string{"aur"}, Up: []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("v0")
  101. assert.NoError(t, err)
  102. config.Runtime.CmdBuilder = config.CmdBuilder(&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]*aur.Pkg
  114. cached vcs.InfoStore
  115. }
  116. tests := []struct {
  117. name string
  118. args args
  119. want UpSlice
  120. finalLen int
  121. }{
  122. {
  123. name: "No Updates",
  124. args: args{
  125. cached: vcs.InfoStore{
  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. CmdBuilder: config.Runtime.CmdBuilder,
  146. OriginsByPackage: map[string]vcs.OriginInfoByURL{
  147. "hello": {
  148. "github.com/Jguer/z.git": vcs.OriginInfo{
  149. Protocols: []string{"https"},
  150. Branch: "0",
  151. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  152. },
  153. },
  154. "hello-non-existant": {
  155. "github.com/Jguer/y.git": vcs.OriginInfo{
  156. Protocols: []string{"https"},
  157. Branch: "0",
  158. SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
  159. },
  160. },
  161. "hello2": {
  162. "github.com/Jguer/a.git": vcs.OriginInfo{
  163. Protocols: []string{"https"},
  164. Branch: "1",
  165. SHA: "7f4c277ce7149665d1c79b76ca8fbb832a65a03b",
  166. },
  167. },
  168. "hello4": {
  169. "github.com/Jguer/b.git": vcs.OriginInfo{
  170. Protocols: []string{"https"},
  171. Branch: "2",
  172. SHA: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  173. },
  174. "github.com/Jguer/c.git": vcs.OriginInfo{
  175. Protocols: []string{"https"},
  176. Branch: "3",
  177. SHA: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
  178. },
  179. },
  180. },
  181. },
  182. remote: []alpm.IPackage{
  183. &mock.Package{PName: "hello", PVersion: "2.0.0"},
  184. &mock.Package{PName: "hello2", PVersion: "3.0.0"},
  185. &mock.Package{PName: "hello4", PVersion: "4.0.0"},
  186. },
  187. aurdata: map[string]*aur.Pkg{
  188. "hello": {Version: "2.0.0", Name: "hello"},
  189. "hello2": {Version: "2.0.0", Name: "hello2"},
  190. "hello4": {Version: "2.0.0", Name: "hello4"},
  191. },
  192. },
  193. want: UpSlice{
  194. Repos: []string{"devel"}, Up: []Upgrade{
  195. {
  196. Name: "hello",
  197. Repository: "devel",
  198. LocalVersion: "2.0.0",
  199. RemoteVersion: "latest-commit",
  200. },
  201. {
  202. Name: "hello4",
  203. Repository: "devel",
  204. LocalVersion: "4.0.0",
  205. RemoteVersion: "latest-commit",
  206. },
  207. },
  208. },
  209. },
  210. {
  211. name: "No update returned",
  212. finalLen: 1,
  213. args: args{
  214. cached: vcs.InfoStore{
  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]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
  228. },
  229. want: UpSlice{Repos: []string{"devel"}},
  230. },
  231. {
  232. name: "No update returned - ignored",
  233. finalLen: 1,
  234. args: args{
  235. cached: vcs.InfoStore{
  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]*aur.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.CmdBuilder.(*exe.CmdBuilder).Runner.(*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. }