unified_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package download
  2. import (
  3. "context"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "gopkg.in/h2non/gock.v1"
  11. "github.com/Jguer/yay/v10/pkg/settings/exe"
  12. "github.com/Jguer/yay/v10/pkg/settings/parser"
  13. )
  14. // GIVEN 2 aur packages and 1 in repo
  15. // GIVEN package in repo is already present
  16. // WHEN defining package db as a target
  17. // THEN all should be found and cloned, except the repo one
  18. func TestPKGBUILDReposDefinedDBPull(t *testing.T) {
  19. t.Parallel()
  20. dir, _ := ioutil.TempDir("/tmp/", "yay-test")
  21. defer os.RemoveAll(dir)
  22. os.MkdirAll(filepath.Join(dir, "yay", ".git"), 0o777)
  23. targets := []string{"core/yay", "yay-bin", "yay-git"}
  24. cmdRunner := &testRunner{}
  25. cmdBuilder := &testGitBuilder{
  26. index: 0,
  27. test: t,
  28. parentBuilder: &exe.CmdBuilder{
  29. Runner: cmdRunner,
  30. GitBin: "/usr/local/bin/git",
  31. GitFlags: []string{},
  32. },
  33. }
  34. searcher := &testDBSearcher{
  35. absPackagesDB: map[string]string{"yay": "core"},
  36. }
  37. cloned, err := PKGBUILDRepos(context.TODO(), searcher,
  38. cmdBuilder,
  39. targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
  40. assert.NoError(t, err)
  41. assert.EqualValues(t, map[string]bool{"core/yay": false, "yay-bin": true, "yay-git": true}, cloned)
  42. }
  43. // GIVEN 2 aur packages and 1 in repo
  44. // WHEN defining package db as a target
  45. // THEN all should be found and cloned
  46. func TestPKGBUILDReposDefinedDBClone(t *testing.T) {
  47. t.Parallel()
  48. dir, _ := ioutil.TempDir("/tmp/", "yay-test")
  49. defer os.RemoveAll(dir)
  50. targets := []string{"core/yay", "yay-bin", "yay-git"}
  51. cmdRunner := &testRunner{}
  52. cmdBuilder := &testGitBuilder{
  53. index: 0,
  54. test: t,
  55. parentBuilder: &exe.CmdBuilder{
  56. Runner: cmdRunner,
  57. GitBin: "/usr/local/bin/git",
  58. GitFlags: []string{},
  59. },
  60. }
  61. searcher := &testDBSearcher{
  62. absPackagesDB: map[string]string{"yay": "core"},
  63. }
  64. cloned, err := PKGBUILDRepos(context.TODO(), searcher,
  65. cmdBuilder,
  66. targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
  67. assert.NoError(t, err)
  68. assert.EqualValues(t, map[string]bool{"core/yay": true, "yay-bin": true, "yay-git": true}, cloned)
  69. }
  70. // GIVEN 2 aur packages and 1 in repo
  71. // WHEN defining as non specified targets
  72. // THEN all should be found and cloned
  73. func TestPKGBUILDReposClone(t *testing.T) {
  74. t.Parallel()
  75. dir, _ := ioutil.TempDir("/tmp/", "yay-test")
  76. defer os.RemoveAll(dir)
  77. targets := []string{"yay", "yay-bin", "yay-git"}
  78. cmdRunner := &testRunner{}
  79. cmdBuilder := &testGitBuilder{
  80. index: 0,
  81. test: t,
  82. parentBuilder: &exe.CmdBuilder{
  83. Runner: cmdRunner,
  84. GitBin: "/usr/local/bin/git",
  85. GitFlags: []string{},
  86. },
  87. }
  88. searcher := &testDBSearcher{
  89. absPackagesDB: map[string]string{"yay": "core"},
  90. }
  91. cloned, err := PKGBUILDRepos(context.TODO(), searcher,
  92. cmdBuilder,
  93. targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
  94. assert.NoError(t, err)
  95. assert.EqualValues(t, map[string]bool{"yay": true, "yay-bin": true, "yay-git": true}, cloned)
  96. }
  97. // GIVEN 2 aur packages and 1 in repo but wrong db
  98. // WHEN defining as non specified targets
  99. // THEN all aur be found and cloned
  100. func TestPKGBUILDReposNotFound(t *testing.T) {
  101. t.Parallel()
  102. dir, _ := ioutil.TempDir("/tmp/", "yay-test")
  103. defer os.RemoveAll(dir)
  104. targets := []string{"extra/yay", "yay-bin", "yay-git"}
  105. cmdRunner := &testRunner{}
  106. cmdBuilder := &testGitBuilder{
  107. index: 0,
  108. test: t,
  109. parentBuilder: &exe.CmdBuilder{
  110. Runner: cmdRunner,
  111. GitBin: "/usr/local/bin/git",
  112. GitFlags: []string{},
  113. },
  114. }
  115. searcher := &testDBSearcher{
  116. absPackagesDB: map[string]string{"yay": "core"},
  117. }
  118. cloned, err := PKGBUILDRepos(context.TODO(), searcher,
  119. cmdBuilder,
  120. targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
  121. assert.NoError(t, err)
  122. assert.EqualValues(t, map[string]bool{"yay-bin": true, "yay-git": true}, cloned)
  123. }
  124. // GIVEN 2 aur packages and 1 in repo
  125. // WHEN defining as non specified targets in repo mode
  126. // THEN only repo should be cloned
  127. func TestPKGBUILDReposRepoMode(t *testing.T) {
  128. t.Parallel()
  129. dir, _ := ioutil.TempDir("/tmp/", "yay-test")
  130. defer os.RemoveAll(dir)
  131. targets := []string{"yay", "yay-bin", "yay-git"}
  132. cmdRunner := &testRunner{}
  133. cmdBuilder := &testGitBuilder{
  134. index: 0,
  135. test: t,
  136. parentBuilder: &exe.CmdBuilder{
  137. Runner: cmdRunner,
  138. GitBin: "/usr/local/bin/git",
  139. GitFlags: []string{},
  140. },
  141. }
  142. searcher := &testDBSearcher{
  143. absPackagesDB: map[string]string{"yay": "core"},
  144. }
  145. cloned, err := PKGBUILDRepos(context.TODO(), searcher,
  146. cmdBuilder,
  147. targets, parser.ModeRepo, "https://aur.archlinux.org", dir, false)
  148. assert.NoError(t, err)
  149. assert.EqualValues(t, map[string]bool{"yay": true}, cloned)
  150. }
  151. // GIVEN 2 aur packages and 1 in repo
  152. // WHEN defining as specified targets
  153. // THEN all aur be found and cloned
  154. func TestPKGBUILDFull(t *testing.T) {
  155. t.Parallel()
  156. gock.New("https://aur.archlinux.org").
  157. Get("/cgit/aur.git/plain/PKGBUILD").MatchParam("h", "yay-git").
  158. Reply(200).
  159. BodyString("example_yay-git")
  160. gock.New("https://aur.archlinux.org").
  161. Get("/cgit/aur.git/plain/PKGBUILD").MatchParam("h", "yay-bin").
  162. Reply(200).
  163. BodyString("example_yay-bin")
  164. gock.New("https://github.com/").
  165. Get("/archlinux/svntogit-packages/raw/packages/yay/trunk/PKGBUILD").
  166. Reply(200).
  167. BodyString("example_yay")
  168. defer gock.Off()
  169. targets := []string{"core/yay", "aur/yay-bin", "yay-git"}
  170. searcher := &testDBSearcher{
  171. absPackagesDB: map[string]string{"yay": "core"},
  172. }
  173. fetched, err := PKGBUILDs(searcher, &http.Client{},
  174. targets, "https://aur.archlinux.org", parser.ModeAny)
  175. assert.NoError(t, err)
  176. assert.EqualValues(t, map[string][]byte{
  177. "core/yay": []byte("example_yay"),
  178. "aur/yay-bin": []byte("example_yay-bin"),
  179. "yay-git": []byte("example_yay-git"),
  180. }, fetched)
  181. }