sources_test.go 7.6 KB

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