aur_source_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //go:build !integration
  2. // +build !integration
  3. package workdir
  4. import (
  5. "context"
  6. "fmt"
  7. "os/exec"
  8. "path/filepath"
  9. "sync/atomic"
  10. "testing"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/Jguer/yay/v12/pkg/multierror"
  13. "github.com/Jguer/yay/v12/pkg/settings/exe"
  14. )
  15. type TestMakepkgBuilder struct {
  16. exe.ICmdBuilder
  17. parentBuilder *exe.CmdBuilder
  18. test *testing.T
  19. passes uint32
  20. want string
  21. wantDir string
  22. showError error
  23. }
  24. func (z *TestMakepkgBuilder) BuildMakepkgCmd(ctx context.Context, dir string, extraArgs ...string) *exec.Cmd {
  25. cmd := z.parentBuilder.BuildMakepkgCmd(ctx, dir, extraArgs...)
  26. if z.want != "" {
  27. assert.Contains(z.test, cmd.String(), z.want)
  28. }
  29. if z.GetKeepSrc() {
  30. assert.NotContains(z.test, cmd.String(), "-Cc")
  31. }
  32. if z.wantDir != "" {
  33. assert.Equal(z.test, z.wantDir, cmd.Dir)
  34. }
  35. atomic.AddUint32(&z.passes, 1)
  36. return cmd
  37. }
  38. func (z *TestMakepkgBuilder) Show(cmd *exec.Cmd) error {
  39. return z.showError
  40. }
  41. func (z *TestMakepkgBuilder) GetKeepSrc() bool {
  42. return z.parentBuilder.KeepSrc
  43. }
  44. // GIVEN 1 package
  45. // WHEN downloadPKGBUILDSource is called
  46. // THEN 1 call should be made to makepkg with the specified parameters and dir
  47. func Test_downloadPKGBUILDSource(t *testing.T) {
  48. t.Parallel()
  49. type testCase struct {
  50. desc string
  51. keepSrc bool
  52. want string
  53. }
  54. testCases := []testCase{
  55. {
  56. desc: "keepsrc",
  57. keepSrc: true,
  58. want: "makepkg --nocheck --config /etc/not.conf --verifysource --skippgpcheck -f",
  59. },
  60. {
  61. desc: "nokeepsrc",
  62. keepSrc: false,
  63. want: "makepkg --nocheck --config /etc/not.conf --verifysource --skippgpcheck -f -Cc",
  64. },
  65. }
  66. for _, tc := range testCases {
  67. tc := tc
  68. t.Run(tc.desc, func(td *testing.T) {
  69. cmdBuilder := &TestMakepkgBuilder{
  70. parentBuilder: &exe.CmdBuilder{
  71. MakepkgConfPath: "/etc/not.conf",
  72. MakepkgFlags: []string{"--nocheck"},
  73. MakepkgBin: "makepkg",
  74. KeepSrc: tc.keepSrc,
  75. },
  76. test: t,
  77. want: tc.want,
  78. wantDir: "/tmp/yay-bin",
  79. }
  80. err := downloadPKGBUILDSource(context.Background(), cmdBuilder, filepath.Join("/tmp", "yay-bin"), false)
  81. assert.NoError(t, err)
  82. assert.Equal(t, 1, int(cmdBuilder.passes))
  83. })
  84. }
  85. }
  86. // GIVEN 1 package
  87. // WHEN downloadPKGBUILDSource is called
  88. // THEN 1 call should be made to makepkg which should return error
  89. func Test_downloadPKGBUILDSourceError(t *testing.T) {
  90. t.Parallel()
  91. cmdBuilder := &TestMakepkgBuilder{
  92. parentBuilder: &exe.CmdBuilder{MakepkgConfPath: "/etc/not.conf", MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg"},
  93. test: t,
  94. want: "makepkg --nocheck --config /etc/not.conf --verifysource --skippgpcheck -f -Cc",
  95. wantDir: "/tmp/yay-bin",
  96. showError: &exec.ExitError{},
  97. }
  98. err := downloadPKGBUILDSource(context.Background(), cmdBuilder, filepath.Join("/tmp", "yay-bin"), false)
  99. assert.Error(t, err)
  100. assert.EqualError(t, err, "error downloading sources: \x1b[36m/tmp/yay-bin\x1b[0m \n\t context: <nil> \n\t \n")
  101. }
  102. // GIVEN 5 packages
  103. // WHEN downloadPKGBUILDSourceFanout is called
  104. // THEN 5 calls should be made to makepkg
  105. func Test_downloadPKGBUILDSourceFanout(t *testing.T) {
  106. t.Parallel()
  107. pkgBuildDirs := map[string]string{
  108. "yay": "/tmp/yay",
  109. "yay-bin": "/tmp/yay-bin",
  110. "yay-git": "/tmp/yay-git",
  111. "yay-v11": "/tmp/yay-v11",
  112. "yay-v12": "/tmp/yay-v12",
  113. }
  114. for _, maxConcurrentDownloads := range []int{0, 3} {
  115. t.Run(fmt.Sprintf("maxconcurrentdownloads set to %d", maxConcurrentDownloads), func(t *testing.T) {
  116. cmdBuilder := &TestMakepkgBuilder{
  117. parentBuilder: &exe.CmdBuilder{
  118. MakepkgConfPath: "/etc/not.conf",
  119. MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg",
  120. },
  121. test: t,
  122. }
  123. err := downloadPKGBUILDSourceFanout(context.Background(), cmdBuilder, pkgBuildDirs, true, maxConcurrentDownloads)
  124. assert.NoError(t, err)
  125. assert.Equal(t, 5, int(cmdBuilder.passes))
  126. })
  127. }
  128. }
  129. // GIVEN 1 package
  130. // WHEN downloadPKGBUILDSourceFanout is called
  131. // THEN 1 calls should be made to makepkg without concurrency
  132. func Test_downloadPKGBUILDSourceFanoutNoCC(t *testing.T) {
  133. t.Parallel()
  134. cmdBuilder := &TestMakepkgBuilder{
  135. parentBuilder: &exe.CmdBuilder{
  136. MakepkgConfPath: "/etc/not.conf",
  137. MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg",
  138. },
  139. test: t,
  140. }
  141. pkgBuildDirs := map[string]string{"yay": "/tmp/yay"}
  142. err := downloadPKGBUILDSourceFanout(context.Background(), cmdBuilder, pkgBuildDirs, false, 0)
  143. assert.NoError(t, err)
  144. assert.Equal(t, 1, int(cmdBuilder.passes))
  145. }
  146. // GIVEN 5 packages
  147. // WHEN downloadPKGBUILDSourceFanout is called
  148. // THEN 5 calls should be made to makepkg
  149. func Test_downloadPKGBUILDSourceFanoutError(t *testing.T) {
  150. t.Parallel()
  151. cmdBuilder := &TestMakepkgBuilder{
  152. parentBuilder: &exe.CmdBuilder{
  153. MakepkgConfPath: "/etc/not.conf",
  154. MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg",
  155. },
  156. test: t,
  157. showError: &exec.ExitError{},
  158. }
  159. pkgBuildDirs := map[string]string{
  160. "yay": "/tmp/yay",
  161. "yay-bin": "/tmp/yay-bin",
  162. "yay-git": "/tmp/yay-git",
  163. "yay-v11": "/tmp/yay-v11",
  164. "yay-v12": "/tmp/yay-v12",
  165. }
  166. err := downloadPKGBUILDSourceFanout(context.Background(), cmdBuilder, pkgBuildDirs, false, 0)
  167. assert.Error(t, err)
  168. assert.Equal(t, 5, int(cmdBuilder.passes))
  169. assert.Len(t, err.(*multierror.MultiError).Errors, 5)
  170. }