aur_test.go 4.2 KB

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