aur_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/v11/pkg/settings/exe"
  11. )
  12. func TestGetAURPkgbuild(t *testing.T) {
  13. t.Parallel()
  14. type args struct {
  15. body string
  16. status int
  17. pkgName string
  18. wantURL string
  19. }
  20. tests := []struct {
  21. name string
  22. args args
  23. want string
  24. wantErr bool
  25. }{
  26. {
  27. name: "found package",
  28. args: args{
  29. body: gitExtrasPKGBUILD,
  30. status: 200,
  31. pkgName: "git-extras",
  32. wantURL: "https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=git-extras",
  33. },
  34. want: gitExtrasPKGBUILD,
  35. wantErr: false,
  36. },
  37. {
  38. name: "not found package",
  39. args: args{
  40. body: "",
  41. status: 404,
  42. pkgName: "git-git",
  43. wantURL: "https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=git-git",
  44. },
  45. want: "",
  46. wantErr: true,
  47. },
  48. }
  49. for _, tt := range tests {
  50. tt := tt
  51. t.Run(tt.name, func(t *testing.T) {
  52. t.Parallel()
  53. httpClient := &testClient{
  54. t: t,
  55. wantURL: tt.args.wantURL,
  56. body: tt.args.body,
  57. status: tt.args.status,
  58. }
  59. got, err := AURPKGBUILD(httpClient, tt.args.pkgName, "https://aur.archlinux.org")
  60. if tt.wantErr {
  61. assert.Error(t, err)
  62. } else {
  63. assert.NoError(t, err)
  64. }
  65. assert.Equal(t, tt.want, string(got))
  66. })
  67. }
  68. }
  69. // GIVEN no previous existing folder
  70. // WHEN AURPKGBUILDRepo is called
  71. // THEN a clone command should be formed
  72. func TestAURPKGBUILDRepo(t *testing.T) {
  73. t.Parallel()
  74. want := "/usr/local/bin/git --no-replace-objects -C /tmp/doesnt-exist clone --no-progress https://aur.archlinux.org/yay-bin.git yay-bin"
  75. if os.Getuid() == 0 {
  76. ld := "systemd-run"
  77. if path, _ := exec.LookPath(ld); path != "" {
  78. ld = path
  79. }
  80. want = fmt.Sprintf("%s --service-type=oneshot --pipe --wait --pty -p DynamicUser=yes -p CacheDirectory=yay -E HOME=/tmp --no-replace-objects -C /tmp/doesnt-exist clone --no-progress https://aur.archlinux.org/yay-bin.git yay-bin", ld)
  81. }
  82. cmdRunner := &testRunner{}
  83. cmdBuilder := &testGitBuilder{
  84. index: 0,
  85. test: t,
  86. want: want,
  87. parentBuilder: &exe.CmdBuilder{
  88. Runner: cmdRunner,
  89. GitBin: "/usr/local/bin/git",
  90. GitFlags: []string{"--no-replace-objects"},
  91. },
  92. }
  93. newCloned, err := AURPKGBUILDRepo(context.TODO(), cmdBuilder, "https://aur.archlinux.org", "yay-bin", "/tmp/doesnt-exist", false)
  94. assert.NoError(t, err)
  95. assert.Equal(t, true, newCloned)
  96. }
  97. // GIVEN a previous existing folder with permissions
  98. // WHEN AURPKGBUILDRepo is called
  99. // THEN a pull command should be formed
  100. func TestAURPKGBUILDRepoExistsPerms(t *testing.T) {
  101. t.Parallel()
  102. dir, _ := os.MkdirTemp("/tmp/", "yay-test")
  103. defer os.RemoveAll(dir)
  104. os.MkdirAll(filepath.Join(dir, "yay-bin", ".git"), 0o777)
  105. want := fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/yay-bin pull --ff-only", dir)
  106. if os.Getuid() == 0 {
  107. ld := "systemd-run"
  108. if path, _ := exec.LookPath(ld); path != "" {
  109. ld = path
  110. }
  111. want = fmt.Sprintf("%s --service-type=oneshot --pipe --wait --pty -p DynamicUser=yes -p CacheDirectory=yay -E HOME=/tmp --no-replace-objects -C %s/yay-bin pull --ff-only", ld, dir)
  112. }
  113. cmdRunner := &testRunner{}
  114. cmdBuilder := &testGitBuilder{
  115. index: 0,
  116. test: t,
  117. want: want,
  118. parentBuilder: &exe.CmdBuilder{
  119. Runner: cmdRunner,
  120. GitBin: "/usr/local/bin/git",
  121. GitFlags: []string{"--no-replace-objects"},
  122. },
  123. }
  124. cloned, err := AURPKGBUILDRepo(context.TODO(), cmdBuilder, "https://aur.archlinux.org", "yay-bin", dir, false)
  125. assert.NoError(t, err)
  126. assert.Equal(t, false, cloned)
  127. }
  128. func TestAURPKGBUILDRepos(t *testing.T) {
  129. t.Parallel()
  130. dir, _ := os.MkdirTemp("/tmp/", "yay-test")
  131. defer os.RemoveAll(dir)
  132. os.MkdirAll(filepath.Join(dir, "yay-bin", ".git"), 0o777)
  133. targets := []string{"yay", "yay-bin", "yay-git"}
  134. cmdRunner := &testRunner{}
  135. cmdBuilder := &testGitBuilder{
  136. index: 0,
  137. test: t,
  138. want: "",
  139. parentBuilder: &exe.CmdBuilder{
  140. Runner: cmdRunner,
  141. GitBin: "/usr/local/bin/git",
  142. GitFlags: []string{},
  143. },
  144. }
  145. cloned, err := AURPKGBUILDRepos(context.TODO(), cmdBuilder, targets, "https://aur.archlinux.org", dir, false)
  146. assert.NoError(t, err)
  147. assert.EqualValues(t, map[string]bool{"yay": true, "yay-bin": false, "yay-git": true}, cloned)
  148. }