abs_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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: "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. tt := tt
  72. t.Run(tt.name, func(t *testing.T) {
  73. t.Parallel()
  74. got, err := getPackageURL(tt.args.db, tt.args.pkgName)
  75. if tt.wantErr {
  76. assert.ErrorIs(t, err, ErrInvalidRepository)
  77. }
  78. assert.Equal(t, tt.want, got)
  79. })
  80. }
  81. }
  82. func TestGetABSPkgbuild(t *testing.T) {
  83. t.Parallel()
  84. type args struct {
  85. dbName string
  86. body string
  87. status int
  88. pkgName string
  89. wantURL string
  90. }
  91. tests := []struct {
  92. name string
  93. args args
  94. want string
  95. wantErr bool
  96. }{
  97. {
  98. name: "found package",
  99. args: args{
  100. dbName: "core",
  101. body: gitExtrasPKGBUILD,
  102. status: 200,
  103. pkgName: "git-extras",
  104. wantURL: "https://github.com/archlinux/svntogit-packages/raw/packages/git-extras/trunk/PKGBUILD",
  105. },
  106. want: gitExtrasPKGBUILD,
  107. wantErr: false,
  108. },
  109. {
  110. name: "not found package",
  111. args: args{
  112. dbName: "core",
  113. body: "",
  114. status: 404,
  115. pkgName: "git-git",
  116. wantURL: "https://github.com/archlinux/svntogit-packages/raw/packages/git-git/trunk/PKGBUILD",
  117. },
  118. want: "",
  119. wantErr: true,
  120. },
  121. }
  122. for _, tt := range tests {
  123. tt := tt
  124. t.Run(tt.name, func(t *testing.T) {
  125. t.Parallel()
  126. httpClient := &testClient{
  127. t: t,
  128. wantURL: tt.args.wantURL,
  129. body: tt.args.body,
  130. status: tt.args.status,
  131. }
  132. got, err := ABSPKGBUILD(httpClient, tt.args.dbName, tt.args.pkgName)
  133. if tt.wantErr {
  134. assert.Error(t, err)
  135. } else {
  136. assert.NoError(t, err)
  137. }
  138. assert.Equal(t, tt.want, string(got))
  139. })
  140. }
  141. }
  142. func Test_getPackageRepoURL(t *testing.T) {
  143. t.Parallel()
  144. type args struct {
  145. db string
  146. }
  147. tests := []struct {
  148. name string
  149. args args
  150. want string
  151. wantErr bool
  152. }{
  153. {
  154. name: "community package",
  155. args: args{db: "community"},
  156. want: "https://github.com/archlinux/svntogit-community.git",
  157. wantErr: false,
  158. },
  159. {
  160. name: "core package",
  161. args: args{db: "core"},
  162. want: "https://github.com/archlinux/svntogit-packages.git",
  163. wantErr: false,
  164. },
  165. {
  166. name: "personal repo package",
  167. args: args{db: "sweswe"},
  168. want: "",
  169. wantErr: true,
  170. },
  171. }
  172. for _, tt := range tests {
  173. tt := tt
  174. t.Run(tt.name, func(t *testing.T) {
  175. t.Parallel()
  176. got, err := getPackageRepoURL(tt.args.db)
  177. if tt.wantErr {
  178. assert.ErrorIs(t, err, ErrInvalidRepository)
  179. }
  180. assert.Equal(t, tt.want, got)
  181. })
  182. }
  183. }
  184. // GIVEN no previous existing folder
  185. // WHEN ABSPKGBUILDRepo is called
  186. // THEN a clone command should be formed
  187. func TestABSPKGBUILDRepo(t *testing.T) {
  188. t.Parallel()
  189. cmdRunner := &testRunner{}
  190. 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"
  191. if os.Getuid() == 0 {
  192. ld := "systemd-run"
  193. if path, _ := exec.LookPath(ld); path != "" {
  194. ld = path
  195. }
  196. 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 -b packages/linux https://github.com/archlinux/svntogit-packages.git linux", ld)
  197. }
  198. cmdBuilder := &testGitBuilder{
  199. index: 0,
  200. test: t,
  201. want: want,
  202. parentBuilder: &exe.CmdBuilder{
  203. Runner: cmdRunner,
  204. GitBin: "/usr/local/bin/git",
  205. GitFlags: []string{"--no-replace-objects"},
  206. },
  207. }
  208. newClone, err := ABSPKGBUILDRepo(context.Background(), cmdBuilder, "core", "linux", "/tmp/doesnt-exist", false)
  209. assert.NoError(t, err)
  210. assert.Equal(t, true, newClone)
  211. }
  212. // GIVEN a previous existing folder with permissions
  213. // WHEN ABSPKGBUILDRepo is called
  214. // THEN a pull command should be formed
  215. func TestABSPKGBUILDRepoExistsPerms(t *testing.T) {
  216. t.Parallel()
  217. dir := t.TempDir()
  218. os.MkdirAll(filepath.Join(dir, "linux", ".git"), 0o777)
  219. want := fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/linux pull --rebase --autostash", dir)
  220. if os.Getuid() == 0 {
  221. ld := "systemd-run"
  222. if path, _ := exec.LookPath(ld); path != "" {
  223. ld = path
  224. }
  225. 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)
  226. }
  227. cmdRunner := &testRunner{}
  228. cmdBuilder := &testGitBuilder{
  229. index: 0,
  230. test: t,
  231. want: want,
  232. parentBuilder: &exe.CmdBuilder{
  233. Runner: cmdRunner,
  234. GitBin: "/usr/local/bin/git",
  235. GitFlags: []string{"--no-replace-objects"},
  236. },
  237. }
  238. newClone, err := ABSPKGBUILDRepo(context.Background(), cmdBuilder, "core", "linux", dir, false)
  239. assert.NoError(t, err)
  240. assert.Equal(t, false, newClone)
  241. }