abs_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //go:build !integration
  2. // +build !integration
  3. package download
  4. import (
  5. "context"
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "testing"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/Jguer/yay/v12/pkg/settings/exe"
  13. )
  14. const gitExtrasPKGBUILD = `pkgname=git-extras
  15. pkgver=6.1.0
  16. pkgrel=1
  17. pkgdesc="GIT utilities -- repo summary, commit counting, repl, changelog population and more"
  18. arch=('any')
  19. url="https://github.com/tj/${pkgname}"
  20. license=('MIT')
  21. depends=('git')
  22. source=("${pkgname}-${pkgver}.tar.gz::${url}/archive/${pkgver}.tar.gz")
  23. sha256sums=('7be0b15ee803d76d2c2e8036f5d9db6677f2232bb8d2c4976691ff7ae026a22f')
  24. b2sums=('3450edecb3116e19ffcf918b118aee04f025c06d812e29e8701f35a3c466b13d2578d41c8e1ee93327743d0019bf98bb3f397189e19435f89e3a259ff1b82747')
  25. package() {
  26. cd "${srcdir}/${pkgname}-${pkgver}"
  27. # avoid annoying interactive prompts if an alias is in your gitconfig
  28. export GIT_CONFIG=/dev/null
  29. make DESTDIR="${pkgdir}" PREFIX=/usr SYSCONFDIR=/etc install
  30. install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  31. }`
  32. func Test_getPackageURL(t *testing.T) {
  33. t.Parallel()
  34. type args struct {
  35. db string
  36. pkgName string
  37. }
  38. tests := []struct {
  39. name string
  40. args args
  41. want string
  42. wantErr bool
  43. }{
  44. {
  45. name: "extra package",
  46. args: args{
  47. db: "extra",
  48. pkgName: "kitty",
  49. },
  50. want: "https://gitlab.archlinux.org/archlinux/packaging/packages/kitty/-/raw/main/PKGBUILD",
  51. wantErr: false,
  52. },
  53. {
  54. name: "core package",
  55. args: args{
  56. db: "core",
  57. pkgName: "linux",
  58. },
  59. want: "https://gitlab.archlinux.org/archlinux/packaging/packages/linux/-/raw/main/PKGBUILD",
  60. wantErr: false,
  61. },
  62. {
  63. name: "personal repo package",
  64. args: args{
  65. db: "sweswe",
  66. pkgName: "zabix",
  67. },
  68. want: "https://gitlab.archlinux.org/archlinux/packaging/packages/zabix/-/raw/main/PKGBUILD",
  69. wantErr: false,
  70. },
  71. }
  72. for _, tt := range tests {
  73. tt := tt
  74. t.Run(tt.name, func(t *testing.T) {
  75. t.Parallel()
  76. got := getPackagePKGBUILDURL(tt.args.pkgName)
  77. assert.Equal(t, tt.want, got)
  78. })
  79. }
  80. }
  81. func TestGetABSPkgbuild(t *testing.T) {
  82. t.Parallel()
  83. type args struct {
  84. dbName string
  85. body string
  86. status int
  87. pkgName string
  88. wantURL string
  89. }
  90. tests := []struct {
  91. name string
  92. args args
  93. want string
  94. wantErr bool
  95. }{
  96. {
  97. name: "found package",
  98. args: args{
  99. dbName: "core",
  100. body: gitExtrasPKGBUILD,
  101. status: 200,
  102. pkgName: "git-extras",
  103. wantURL: "https://gitlab.archlinux.org/archlinux/packaging/packages/git-extras/-/raw/main/PKGBUILD",
  104. },
  105. want: gitExtrasPKGBUILD,
  106. wantErr: false,
  107. },
  108. {
  109. name: "not found package",
  110. args: args{
  111. dbName: "core",
  112. body: "",
  113. status: 404,
  114. pkgName: "git-git",
  115. wantURL: "https://gitlab.archlinux.org/archlinux/packaging/packages/git-git/-/raw/main/PKGBUILD",
  116. },
  117. want: "",
  118. wantErr: true,
  119. },
  120. }
  121. for _, tt := range tests {
  122. tt := tt
  123. t.Run(tt.name, func(t *testing.T) {
  124. t.Parallel()
  125. httpClient := &testClient{
  126. t: t,
  127. wantURL: tt.args.wantURL,
  128. body: tt.args.body,
  129. status: tt.args.status,
  130. }
  131. got, err := ABSPKGBUILD(httpClient, tt.args.dbName, tt.args.pkgName)
  132. if tt.wantErr {
  133. assert.Error(t, err)
  134. } else {
  135. assert.NoError(t, err)
  136. }
  137. assert.Equal(t, tt.want, string(got))
  138. })
  139. }
  140. }
  141. func Test_getPackageRepoURL(t *testing.T) {
  142. t.Parallel()
  143. type args struct {
  144. pkgName string
  145. }
  146. tests := []struct {
  147. name string
  148. args args
  149. want string
  150. wantErr bool
  151. }{
  152. {
  153. name: "extra package",
  154. args: args{pkgName: "zoxide"},
  155. want: "https://gitlab.archlinux.org/archlinux/packaging/packages/zoxide.git",
  156. wantErr: false,
  157. },
  158. {
  159. name: "core package",
  160. args: args{pkgName: "linux"},
  161. want: "https://gitlab.archlinux.org/archlinux/packaging/packages/linux.git",
  162. wantErr: false,
  163. },
  164. {
  165. name: "personal repo package",
  166. args: args{pkgName: "sweswe"},
  167. want: "https://gitlab.archlinux.org/archlinux/packaging/packages/sweswe.git",
  168. wantErr: false,
  169. },
  170. }
  171. for _, tt := range tests {
  172. tt := tt
  173. t.Run(tt.name, func(t *testing.T) {
  174. t.Parallel()
  175. got := getPackageRepoURL(tt.args.pkgName)
  176. assert.Equal(t, tt.want, got)
  177. })
  178. }
  179. }
  180. // GIVEN no previous existing folder
  181. // WHEN ABSPKGBUILDRepo is called
  182. // THEN a clone command should be formed
  183. func TestABSPKGBUILDRepo(t *testing.T) {
  184. t.Parallel()
  185. cmdRunner := &testRunner{}
  186. want := "/usr/local/bin/git --no-replace-objects -C /tmp/doesnt-exist clone --no-progress --single-branch https://gitlab.archlinux.org/archlinux/packaging/packages/linux.git linux"
  187. if os.Getuid() == 0 {
  188. ld := "systemd-run"
  189. if path, _ := exec.LookPath(ld); path != "" {
  190. ld = path
  191. }
  192. want = fmt.Sprintf("%s --service-type=oneshot --pipe --wait --pty --quiet -p DynamicUser=yes -p CacheDirectory=yay -E HOME=/tmp --no-replace-objects -C /tmp/doesnt-exist clone --no-progress --single-branch https://gitlab.archlinux.org/archlinux/packaging/packages/linux.git linux", ld)
  193. }
  194. cmdBuilder := &testGitBuilder{
  195. index: 0,
  196. test: t,
  197. want: want,
  198. parentBuilder: &exe.CmdBuilder{
  199. Runner: cmdRunner,
  200. GitBin: "/usr/local/bin/git",
  201. GitFlags: []string{"--no-replace-objects"},
  202. },
  203. }
  204. newClone, err := ABSPKGBUILDRepo(context.Background(), cmdBuilder, "core", "linux", "/tmp/doesnt-exist", false)
  205. assert.NoError(t, err)
  206. assert.Equal(t, true, newClone)
  207. }
  208. // GIVEN a previous existing folder with permissions
  209. // WHEN ABSPKGBUILDRepo is called
  210. // THEN a pull command should be formed
  211. func TestABSPKGBUILDRepoExistsPerms(t *testing.T) {
  212. t.Parallel()
  213. dir := t.TempDir()
  214. os.MkdirAll(filepath.Join(dir, "linux", ".git"), 0o777)
  215. want := fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/linux pull --rebase --autostash", dir)
  216. if os.Getuid() == 0 {
  217. ld := "systemd-run"
  218. if path, _ := exec.LookPath(ld); path != "" {
  219. ld = path
  220. }
  221. want = fmt.Sprintf("%s --service-type=oneshot --pipe --wait --pty --quiet -p DynamicUser=yes -p CacheDirectory=yay -E HOME=/tmp --no-replace-objects -C %s/linux pull --rebase --autostash", ld, dir)
  222. }
  223. cmdRunner := &testRunner{}
  224. cmdBuilder := &testGitBuilder{
  225. index: 0,
  226. test: t,
  227. want: want,
  228. parentBuilder: &exe.CmdBuilder{
  229. Runner: cmdRunner,
  230. GitBin: "/usr/local/bin/git",
  231. GitFlags: []string{"--no-replace-objects"},
  232. },
  233. }
  234. newClone, err := ABSPKGBUILDRepo(context.Background(), cmdBuilder, "core", "linux", dir, false)
  235. assert.NoError(t, err)
  236. assert.Equal(t, false, newClone)
  237. }