abs_test.go 6.4 KB

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