abs_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package download
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/Jguer/yay/v10/pkg/settings/exe"
  10. )
  11. const gitExtrasPKGBUILD = `pkgname=git-extras
  12. pkgver=6.1.0
  13. pkgrel=1
  14. pkgdesc="GIT utilities -- repo summary, commit counting, repl, changelog population and more"
  15. arch=('any')
  16. url="https://github.com/tj/${pkgname}"
  17. license=('MIT')
  18. depends=('git')
  19. source=("${pkgname}-${pkgver}.tar.gz::${url}/archive/${pkgver}.tar.gz")
  20. sha256sums=('7be0b15ee803d76d2c2e8036f5d9db6677f2232bb8d2c4976691ff7ae026a22f')
  21. b2sums=('3450edecb3116e19ffcf918b118aee04f025c06d812e29e8701f35a3c466b13d2578d41c8e1ee93327743d0019bf98bb3f397189e19435f89e3a259ff1b82747')
  22. package() {
  23. cd "${srcdir}/${pkgname}-${pkgver}"
  24. # avoid annoying interactive prompts if an alias is in your gitconfig
  25. export GIT_CONFIG=/dev/null
  26. make DESTDIR="${pkgdir}" PREFIX=/usr SYSCONFDIR=/etc install
  27. install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  28. }`
  29. func Test_getPackageURL(t *testing.T) {
  30. t.Parallel()
  31. type args struct {
  32. db string
  33. pkgName string
  34. }
  35. tests := []struct {
  36. name string
  37. args args
  38. want string
  39. wantErr bool
  40. }{
  41. {
  42. name: "community package",
  43. args: args{
  44. db: "community",
  45. pkgName: "kitty",
  46. },
  47. want: "https://github.com/archlinux/svntogit-community/raw/packages/kitty/trunk/PKGBUILD",
  48. wantErr: false,
  49. },
  50. {
  51. name: "core package",
  52. args: args{
  53. db: "core",
  54. pkgName: "linux",
  55. },
  56. want: "https://github.com/archlinux/svntogit-packages/raw/packages/linux/trunk/PKGBUILD",
  57. wantErr: false,
  58. },
  59. {
  60. name: "personal repo package",
  61. args: args{
  62. db: "sweswe",
  63. pkgName: "linux",
  64. },
  65. want: "",
  66. wantErr: true,
  67. },
  68. }
  69. for _, tt := range tests {
  70. tt := tt
  71. t.Run(tt.name, func(t *testing.T) {
  72. t.Parallel()
  73. got, err := getPackageURL(tt.args.db, tt.args.pkgName)
  74. if tt.wantErr {
  75. assert.ErrorIs(t, err, ErrInvalidRepository)
  76. }
  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://github.com/archlinux/svntogit-packages/raw/packages/git-extras/trunk/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://github.com/archlinux/svntogit-packages/raw/packages/git-git/trunk/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. db string
  145. }
  146. tests := []struct {
  147. name string
  148. args args
  149. want string
  150. wantErr bool
  151. }{
  152. {
  153. name: "community package",
  154. args: args{db: "community"},
  155. want: "https://github.com/archlinux/svntogit-community.git",
  156. wantErr: false,
  157. },
  158. {
  159. name: "core package",
  160. args: args{db: "core"},
  161. want: "https://github.com/archlinux/svntogit-packages.git",
  162. wantErr: false,
  163. },
  164. {
  165. name: "personal repo package",
  166. args: args{db: "sweswe"},
  167. want: "",
  168. wantErr: true,
  169. },
  170. }
  171. for _, tt := range tests {
  172. tt := tt
  173. t.Run(tt.name, func(t *testing.T) {
  174. t.Parallel()
  175. got, err := getPackageRepoURL(tt.args.db)
  176. if tt.wantErr {
  177. assert.ErrorIs(t, err, ErrInvalidRepository)
  178. }
  179. assert.Equal(t, tt.want, got)
  180. })
  181. }
  182. }
  183. // GIVEN no previous existing folder
  184. // WHEN ABSPKGBUILDRepo is called
  185. // THEN a clone command should be formed
  186. func TestABSPKGBUILDRepo(t *testing.T) {
  187. t.Parallel()
  188. cmdRunner := &testRunner{}
  189. cmdBuilder := &testGitBuilder{
  190. index: 0,
  191. test: t,
  192. 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",
  193. parentBuilder: &exe.CmdBuilder{
  194. Runner: cmdRunner,
  195. GitBin: "/usr/local/bin/git",
  196. GitFlags: []string{"--no-replace-objects"},
  197. },
  198. }
  199. newClone, err := ABSPKGBUILDRepo(cmdBuilder, "core", "linux", "/tmp/doesnt-exist", false)
  200. assert.NoError(t, err)
  201. assert.Equal(t, true, newClone)
  202. }
  203. // GIVEN a previous existing folder with permissions
  204. // WHEN ABSPKGBUILDRepo is called
  205. // THEN a pull command should be formed
  206. func TestABSPKGBUILDRepoExistsPerms(t *testing.T) {
  207. t.Parallel()
  208. dir, _ := ioutil.TempDir("/tmp/", "yay-test")
  209. defer os.RemoveAll(dir)
  210. os.MkdirAll(filepath.Join(dir, "linux", ".git"), 0o777)
  211. cmdRunner := &testRunner{}
  212. cmdBuilder := &testGitBuilder{
  213. index: 0,
  214. test: t,
  215. want: fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/linux pull --ff-only", dir),
  216. parentBuilder: &exe.CmdBuilder{
  217. Runner: cmdRunner,
  218. GitBin: "/usr/local/bin/git",
  219. GitFlags: []string{"--no-replace-objects"},
  220. },
  221. }
  222. newClone, err := ABSPKGBUILDRepo(cmdBuilder, "core", "linux", dir, false)
  223. assert.NoError(t, err)
  224. assert.Equal(t, false, newClone)
  225. }