abs_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package download
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "net/http/httptest"
  7. "os"
  8. "path/filepath"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/Jguer/yay/v10/pkg/settings/exe"
  12. )
  13. const gitExtrasPKGBUILD = `pkgname=git-extras
  14. pkgver=6.1.0
  15. pkgrel=1
  16. pkgdesc="GIT utilities -- repo summary, commit counting, repl, changelog population and more"
  17. arch=('any')
  18. url="https://github.com/tj/${pkgname}"
  19. license=('MIT')
  20. depends=('git')
  21. source=("${pkgname}-${pkgver}.tar.gz::${url}/archive/${pkgver}.tar.gz")
  22. sha256sums=('7be0b15ee803d76d2c2e8036f5d9db6677f2232bb8d2c4976691ff7ae026a22f')
  23. b2sums=('3450edecb3116e19ffcf918b118aee04f025c06d812e29e8701f35a3c466b13d2578d41c8e1ee93327743d0019bf98bb3f397189e19435f89e3a259ff1b82747')
  24. package() {
  25. cd "${srcdir}/${pkgname}-${pkgver}"
  26. # avoid annoying interactive prompts if an alias is in your gitconfig
  27. export GIT_CONFIG=/dev/null
  28. make DESTDIR="${pkgdir}" PREFIX=/usr SYSCONFDIR=/etc install
  29. install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  30. }`
  31. func Test_getPackageURL(t *testing.T) {
  32. type args struct {
  33. db string
  34. pkgName string
  35. }
  36. tests := []struct {
  37. name string
  38. args args
  39. want string
  40. wantErr bool
  41. }{
  42. {
  43. name: "community package",
  44. args: args{
  45. db: "community",
  46. pkgName: "kitty",
  47. },
  48. want: "https://github.com/archlinux/svntogit-community/raw/packages/kitty/trunk/PKGBUILD",
  49. wantErr: false,
  50. },
  51. {
  52. name: "core package",
  53. args: args{
  54. db: "core",
  55. pkgName: "linux",
  56. },
  57. want: "https://github.com/archlinux/svntogit-packages/raw/packages/linux/trunk/PKGBUILD",
  58. wantErr: false,
  59. },
  60. {
  61. name: "personal repo package",
  62. args: args{
  63. db: "sweswe",
  64. pkgName: "linux",
  65. },
  66. want: "",
  67. wantErr: true,
  68. },
  69. }
  70. for _, tt := range tests {
  71. t.Run(tt.name, func(t *testing.T) {
  72. got, err := getPackageURL(tt.args.db, tt.args.pkgName)
  73. if tt.wantErr {
  74. assert.ErrorIs(t, err, ErrInvalidRepository)
  75. }
  76. assert.Equal(t, tt.want, got)
  77. })
  78. }
  79. }
  80. func TestGetABSPkgbuild(t *testing.T) {
  81. pkgBuildHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  82. w.WriteHeader(200)
  83. w.Write([]byte(gitExtrasPKGBUILD))
  84. })
  85. notFoundHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  86. w.WriteHeader(404)
  87. })
  88. PKGBuild := httptest.NewServer(pkgBuildHandler)
  89. type args struct {
  90. handler http.Handler
  91. dbName string
  92. pkgName string
  93. }
  94. tests := []struct {
  95. name string
  96. args args
  97. want string
  98. wantErr bool
  99. }{
  100. {
  101. name: "found package",
  102. args: args{
  103. handler: pkgBuildHandler,
  104. dbName: "core",
  105. pkgName: "git-extras",
  106. },
  107. want: gitExtrasPKGBUILD,
  108. wantErr: false,
  109. },
  110. {
  111. name: "not found package",
  112. args: args{
  113. handler: notFoundHandler,
  114. dbName: "core",
  115. pkgName: "git-extras",
  116. },
  117. want: "",
  118. wantErr: true,
  119. },
  120. }
  121. for _, tt := range tests {
  122. t.Run(tt.name, func(t *testing.T) {
  123. ABSPackageURL = PKGBuild.URL
  124. PKGBuild.Config.Handler = tt.args.handler
  125. got, err := ABSPKGBUILD(PKGBuild.Client(), tt.args.dbName, tt.args.pkgName)
  126. if tt.wantErr {
  127. assert.Error(t, err)
  128. } else {
  129. assert.NoError(t, err)
  130. }
  131. assert.Equal(t, tt.want, string(got))
  132. })
  133. }
  134. }
  135. func Test_getPackageRepoURL(t *testing.T) {
  136. ABSPackageURL = "https://github.com/archlinux/svntogit-packages"
  137. ABSCommunityURL = "https://github.com/archlinux/svntogit-community"
  138. type args struct {
  139. db string
  140. }
  141. tests := []struct {
  142. name string
  143. args args
  144. want string
  145. wantErr bool
  146. }{
  147. {
  148. name: "community package",
  149. args: args{db: "community"},
  150. want: "https://github.com/archlinux/svntogit-community.git",
  151. wantErr: false,
  152. },
  153. {
  154. name: "core package",
  155. args: args{db: "core"},
  156. want: "https://github.com/archlinux/svntogit-packages.git",
  157. wantErr: false,
  158. },
  159. {
  160. name: "personal repo package",
  161. args: args{db: "sweswe"},
  162. want: "",
  163. wantErr: true,
  164. },
  165. }
  166. for _, tt := range tests {
  167. t.Run(tt.name, func(t *testing.T) {
  168. got, err := getPackageRepoURL(tt.args.db)
  169. if tt.wantErr {
  170. assert.ErrorIs(t, err, ErrInvalidRepository)
  171. }
  172. assert.Equal(t, tt.want, got)
  173. })
  174. }
  175. }
  176. // GIVEN no previous existing folder
  177. // WHEN ABSPKGBUILDRepo is called
  178. // THEN a clone command should be formed
  179. func TestABSPKGBUILDRepo(t *testing.T) {
  180. cmdRunner := &testRunner{}
  181. cmdBuilder := &testGitBuilder{
  182. index: 0,
  183. test: t,
  184. want: "/usr/local/bin/git --no-replace-objects -C /tmp/doesnt-exist clone --no-progress --single-branch -b packages/linux https://github.com/archlinux/svntogit-packages.git linux",
  185. parentBuilder: &exe.CmdBuilder{
  186. GitBin: "/usr/local/bin/git",
  187. GitFlags: []string{"--no-replace-objects"},
  188. },
  189. }
  190. newClone, err := ABSPKGBUILDRepo(cmdRunner, cmdBuilder, "core", "linux", "/tmp/doesnt-exist", false)
  191. assert.NoError(t, err)
  192. assert.Equal(t, true, newClone)
  193. }
  194. // GIVEN a previous existing folder with permissions
  195. // WHEN ABSPKGBUILDRepo is called
  196. // THEN a pull command should be formed
  197. func TestABSPKGBUILDRepoExistsPerms(t *testing.T) {
  198. dir, _ := ioutil.TempDir("/tmp/", "yay-test")
  199. defer os.RemoveAll(dir)
  200. os.MkdirAll(filepath.Join(dir, "linux", ".git"), 0o777)
  201. cmdRunner := &testRunner{}
  202. cmdBuilder := &testGitBuilder{
  203. index: 0,
  204. test: t,
  205. want: fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/linux pull --ff-only", dir),
  206. parentBuilder: &exe.CmdBuilder{
  207. GitBin: "/usr/local/bin/git",
  208. GitFlags: []string{"--no-replace-objects"},
  209. },
  210. }
  211. newClone, err := ABSPKGBUILDRepo(cmdRunner, cmdBuilder, "core", "linux", dir, false)
  212. assert.NoError(t, err)
  213. assert.Equal(t, false, newClone)
  214. }