aur_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package download
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "net/http/httptest"
  7. "os"
  8. "path/filepath"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/Jguer/yay/v10/pkg/settings/exe"
  12. )
  13. func TestGetAURPkgbuild(t *testing.T) {
  14. pkgBuildHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  15. w.WriteHeader(200)
  16. w.Write([]byte(gitExtrasPKGBUILD))
  17. })
  18. notFoundHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  19. w.WriteHeader(404)
  20. })
  21. PKGBuild := httptest.NewServer(pkgBuildHandler)
  22. type args struct {
  23. handler http.Handler
  24. pkgName string
  25. }
  26. tests := []struct {
  27. name string
  28. args args
  29. want string
  30. wantErr bool
  31. }{
  32. {
  33. name: "found package",
  34. args: args{
  35. handler: pkgBuildHandler,
  36. pkgName: "git-extras",
  37. },
  38. want: gitExtrasPKGBUILD,
  39. wantErr: false,
  40. },
  41. {
  42. name: "not found package",
  43. args: args{
  44. handler: notFoundHandler,
  45. pkgName: "git-extras",
  46. },
  47. want: "",
  48. wantErr: true,
  49. },
  50. }
  51. for _, tt := range tests {
  52. t.Run(tt.name, func(t *testing.T) {
  53. AURPackageURL = PKGBuild.URL
  54. PKGBuild.Config.Handler = tt.args.handler
  55. got, err := AURPKGBUILD(PKGBuild.Client(), tt.args.pkgName)
  56. if tt.wantErr {
  57. assert.Error(t, err)
  58. } else {
  59. assert.NoError(t, err)
  60. }
  61. assert.Equal(t, tt.want, string(got))
  62. })
  63. }
  64. }
  65. // GIVEN no previous existing folder
  66. // WHEN AURPKGBUILDRepo is called
  67. // THEN a clone command should be formed
  68. func TestAURPKGBUILDRepo(t *testing.T) {
  69. cmdRunner := &testRunner{}
  70. cmdBuilder := &testGitBuilder{
  71. index: 0,
  72. test: t,
  73. want: "/usr/local/bin/git --no-replace-objects -C /tmp/doesnt-exist clone --no-progress https://aur.archlinux.org/yay-bin.git yay-bin",
  74. parentBuilder: &exe.CmdBuilder{
  75. GitBin: "/usr/local/bin/git",
  76. GitFlags: []string{"--no-replace-objects"},
  77. },
  78. }
  79. err := AURPKGBUILDRepo(cmdRunner, cmdBuilder, "https://aur.archlinux.org", "yay-bin", "/tmp/doesnt-exist", false)
  80. assert.NoError(t, err)
  81. }
  82. // GIVEN a previous existing folder without permissions
  83. // WHEN AURPKGBUILDRepo is called
  84. // THEN a clone command should be formed
  85. func TestAURPKGBUILDRepoExistsNoPerms(t *testing.T) {
  86. dir, _ := ioutil.TempDir("/tmp/", "yay-test")
  87. defer os.RemoveAll(dir)
  88. os.MkdirAll(filepath.Join(dir, "yay-bin", ".git"), 0o600)
  89. cmdRunner := &testRunner{}
  90. cmdBuilder := &testGitBuilder{
  91. index: 0,
  92. test: t,
  93. parentBuilder: &exe.CmdBuilder{
  94. GitBin: "/usr/local/bin/git",
  95. GitFlags: []string{"--no-replace-objects"},
  96. },
  97. }
  98. err := AURPKGBUILDRepo(cmdRunner, cmdBuilder, "https://aur.archlinux.org", "yay-bin", dir, false)
  99. assert.Error(t, err)
  100. assert.Contains(t, err.Error(), "error fetching yay-bin: error reading")
  101. }
  102. // GIVEN a previous existing folder with permissions
  103. // WHEN AURPKGBUILDRepo is called
  104. // THEN a pull command should be formed
  105. func TestAURPKGBUILDRepoExistsPerms(t *testing.T) {
  106. dir, _ := ioutil.TempDir("/tmp/", "yay-test")
  107. defer os.RemoveAll(dir)
  108. os.MkdirAll(filepath.Join(dir, "yay-bin", ".git"), 0o777)
  109. cmdRunner := &testRunner{}
  110. cmdBuilder := &testGitBuilder{
  111. index: 0,
  112. test: t,
  113. want: fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/yay-bin pull --ff-only", dir),
  114. parentBuilder: &exe.CmdBuilder{
  115. GitBin: "/usr/local/bin/git",
  116. GitFlags: []string{"--no-replace-objects"},
  117. },
  118. }
  119. err := AURPKGBUILDRepo(cmdRunner, cmdBuilder, "https://aur.archlinux.org", "yay-bin", dir, false)
  120. assert.NoError(t, err)
  121. }