abs_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package download
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. const gitExtrasPKGBUILD = `pkgname=git-extras
  9. pkgver=6.1.0
  10. pkgrel=1
  11. pkgdesc="GIT utilities -- repo summary, commit counting, repl, changelog population and more"
  12. arch=('any')
  13. url="https://github.com/tj/${pkgname}"
  14. license=('MIT')
  15. depends=('git')
  16. source=("${pkgname}-${pkgver}.tar.gz::${url}/archive/${pkgver}.tar.gz")
  17. sha256sums=('7be0b15ee803d76d2c2e8036f5d9db6677f2232bb8d2c4976691ff7ae026a22f')
  18. b2sums=('3450edecb3116e19ffcf918b118aee04f025c06d812e29e8701f35a3c466b13d2578d41c8e1ee93327743d0019bf98bb3f397189e19435f89e3a259ff1b82747')
  19. package() {
  20. cd "${srcdir}/${pkgname}-${pkgver}"
  21. # avoid annoying interactive prompts if an alias is in your gitconfig
  22. export GIT_CONFIG=/dev/null
  23. make DESTDIR="${pkgdir}" PREFIX=/usr SYSCONFDIR=/etc install
  24. install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  25. }`
  26. func Test_getPackageURL(t *testing.T) {
  27. type args struct {
  28. db string
  29. pkgName string
  30. }
  31. tests := []struct {
  32. name string
  33. args args
  34. want string
  35. wantErr bool
  36. }{
  37. {
  38. name: "community package",
  39. args: args{
  40. db: "community",
  41. pkgName: "kitty",
  42. },
  43. want: "https://github.com/archlinux/svntogit-community.git/plain/trunk/PKGBUILD?h=packages%2Fkitty",
  44. wantErr: false,
  45. },
  46. {
  47. name: "core package",
  48. args: args{
  49. db: "core",
  50. pkgName: "linux",
  51. },
  52. want: "https://github.com/archlinux/svntogit-packages.git/plain/trunk/PKGBUILD?h=packages%2Flinux",
  53. wantErr: false,
  54. },
  55. {
  56. name: "personal repo package",
  57. args: args{
  58. db: "sweswe",
  59. pkgName: "linux",
  60. },
  61. want: "",
  62. wantErr: true,
  63. },
  64. }
  65. for _, tt := range tests {
  66. t.Run(tt.name, func(t *testing.T) {
  67. got, err := getPackageURL(tt.args.db, tt.args.pkgName)
  68. if tt.wantErr {
  69. assert.ErrorIs(t, err, ErrInvalidRepository)
  70. }
  71. assert.Equal(t, tt.want, got)
  72. })
  73. }
  74. }
  75. func TestGetABSPkgbuild(t *testing.T) {
  76. pkgBuildHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  77. w.WriteHeader(200)
  78. w.Write([]byte(gitExtrasPKGBUILD))
  79. })
  80. notFoundHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  81. w.WriteHeader(404)
  82. })
  83. PKGBuild := httptest.NewServer(pkgBuildHandler)
  84. type args struct {
  85. handler http.Handler
  86. dbName string
  87. pkgName string
  88. }
  89. tests := []struct {
  90. name string
  91. args args
  92. want string
  93. wantErr bool
  94. }{
  95. {
  96. name: "found package",
  97. args: args{
  98. handler: pkgBuildHandler,
  99. dbName: "core",
  100. pkgName: "git-extras",
  101. },
  102. want: gitExtrasPKGBUILD,
  103. wantErr: false,
  104. },
  105. {
  106. name: "not found package",
  107. args: args{
  108. handler: notFoundHandler,
  109. dbName: "core",
  110. pkgName: "git-extras",
  111. },
  112. want: "",
  113. wantErr: true,
  114. },
  115. }
  116. for _, tt := range tests {
  117. t.Run(tt.name, func(t *testing.T) {
  118. ABSPackageURL = PKGBuild.URL
  119. PKGBuild.Config.Handler = tt.args.handler
  120. got, err := GetABSPkgbuild(PKGBuild.Client(), tt.args.dbName, tt.args.pkgName)
  121. if tt.wantErr {
  122. assert.Error(t, err)
  123. } else {
  124. assert.NoError(t, err)
  125. }
  126. assert.Equal(t, tt.want, string(got))
  127. })
  128. }
  129. }