aur_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. 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 --quiet -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.Background(), 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 := t.TempDir()
  103. os.MkdirAll(filepath.Join(dir, "yay-bin", ".git"), 0o777)
  104. want := fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/yay-bin pull --rebase --autostash", dir)
  105. if os.Getuid() == 0 {
  106. ld := "systemd-run"
  107. if path, _ := exec.LookPath(ld); path != "" {
  108. ld = path
  109. }
  110. 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/yay-bin pull --rebase --autostash", ld, dir)
  111. }
  112. cmdRunner := &testRunner{}
  113. cmdBuilder := &testGitBuilder{
  114. index: 0,
  115. test: t,
  116. want: want,
  117. parentBuilder: &exe.CmdBuilder{
  118. Runner: cmdRunner,
  119. GitBin: "/usr/local/bin/git",
  120. GitFlags: []string{"--no-replace-objects"},
  121. },
  122. }
  123. cloned, err := AURPKGBUILDRepo(context.Background(), cmdBuilder, "https://aur.archlinux.org", "yay-bin", dir, false)
  124. assert.NoError(t, err)
  125. assert.Equal(t, false, cloned)
  126. }
  127. func TestAURPKGBUILDRepos(t *testing.T) {
  128. t.Parallel()
  129. dir := t.TempDir()
  130. os.MkdirAll(filepath.Join(dir, "yay-bin", ".git"), 0o777)
  131. targets := []string{"yay", "yay-bin", "yay-git"}
  132. cmdRunner := &testRunner{}
  133. cmdBuilder := &testGitBuilder{
  134. index: 0,
  135. test: t,
  136. want: "",
  137. parentBuilder: &exe.CmdBuilder{
  138. Runner: cmdRunner,
  139. GitBin: "/usr/local/bin/git",
  140. GitFlags: []string{},
  141. },
  142. }
  143. cloned, err := AURPKGBUILDRepos(context.Background(), cmdBuilder, targets, "https://aur.archlinux.org", dir, false)
  144. assert.NoError(t, err)
  145. assert.EqualValues(t, map[string]bool{"yay": true, "yay-bin": false, "yay-git": true}, cloned)
  146. }