aur_source_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "os/exec"
  6. "sync/atomic"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/Jguer/aur"
  10. "github.com/Jguer/yay/v11/pkg/dep"
  11. "github.com/Jguer/yay/v11/pkg/multierror"
  12. "github.com/Jguer/yay/v11/pkg/settings/exe"
  13. "github.com/Jguer/yay/v11/pkg/stringset"
  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.wantDir != "" {
  30. assert.Equal(z.test, z.wantDir, cmd.Dir)
  31. }
  32. atomic.AddUint32(&z.passes, 1)
  33. return cmd
  34. }
  35. func (z *TestMakepkgBuilder) Show(cmd *exec.Cmd) error {
  36. return z.showError
  37. }
  38. // GIVEN 1 package
  39. // WHEN downloadPKGBUILDSource is called
  40. // THEN 1 call should be made to makepkg with the specified parameters and dir
  41. func Test_downloadPKGBUILDSource(t *testing.T) {
  42. t.Parallel()
  43. cmdBuilder := &TestMakepkgBuilder{
  44. parentBuilder: &exe.CmdBuilder{MakepkgConfPath: "/etc/not.conf", MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg"},
  45. test: t,
  46. want: "makepkg --nocheck --config /etc/not.conf --verifysource -Ccf",
  47. wantDir: "/tmp/yay-bin",
  48. }
  49. err := downloadPKGBUILDSource(context.TODO(), cmdBuilder, "/tmp", "yay-bin", stringset.Make())
  50. assert.NoError(t, err)
  51. assert.Equal(t, 1, int(cmdBuilder.passes))
  52. }
  53. // GIVEN 1 package
  54. // WHEN downloadPKGBUILDSource is called
  55. // THEN 1 call should be made to makepkg which should return error
  56. func Test_downloadPKGBUILDSourceError(t *testing.T) {
  57. t.Parallel()
  58. cmdBuilder := &TestMakepkgBuilder{
  59. parentBuilder: &exe.CmdBuilder{MakepkgConfPath: "/etc/not.conf", MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg"},
  60. test: t,
  61. want: "makepkg --nocheck --config /etc/not.conf --verifysource -Ccf",
  62. wantDir: "/tmp/yay-bin",
  63. showError: &exec.ExitError{},
  64. }
  65. err := downloadPKGBUILDSource(context.TODO(), cmdBuilder, "/tmp", "yay-bin", stringset.Make())
  66. assert.Error(t, err)
  67. assert.EqualError(t, err, "error downloading sources: \x1b[36myay-bin\x1b[0m \n\t context: <nil> \n\t \n")
  68. }
  69. // GIVEN 5 packages
  70. // WHEN downloadPKGBUILDSourceFanout is called
  71. // THEN 5 calls should be made to makepkg
  72. func Test_downloadPKGBUILDSourceFanout(t *testing.T) {
  73. t.Parallel()
  74. bases := []dep.Base{
  75. {&aur.Pkg{PackageBase: "yay"}},
  76. {&aur.Pkg{PackageBase: "yay-bin"}},
  77. {&aur.Pkg{PackageBase: "yay-git"}},
  78. {&aur.Pkg{PackageBase: "yay-v11"}},
  79. {&aur.Pkg{PackageBase: "yay-v12"}},
  80. }
  81. for _, maxConcurrentDownloads := range []int{0, 3} {
  82. t.Run(fmt.Sprintf("maxconcurrentdownloads set to %d", maxConcurrentDownloads), func(t *testing.T) {
  83. cmdBuilder := &TestMakepkgBuilder{
  84. parentBuilder: &exe.CmdBuilder{
  85. MakepkgConfPath: "/etc/not.conf",
  86. MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg",
  87. },
  88. test: t,
  89. }
  90. err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, "/tmp", bases, stringset.Make(), maxConcurrentDownloads)
  91. assert.NoError(t, err)
  92. assert.Equal(t, 5, int(cmdBuilder.passes))
  93. })
  94. }
  95. }
  96. // GIVEN 1 package
  97. // WHEN downloadPKGBUILDSourceFanout is called
  98. // THEN 1 calls should be made to makepkg without concurrency
  99. func Test_downloadPKGBUILDSourceFanoutNoCC(t *testing.T) {
  100. t.Parallel()
  101. cmdBuilder := &TestMakepkgBuilder{
  102. parentBuilder: &exe.CmdBuilder{
  103. MakepkgConfPath: "/etc/not.conf",
  104. MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg",
  105. },
  106. test: t,
  107. }
  108. bases := []dep.Base{
  109. {&aur.Pkg{PackageBase: "yay"}},
  110. }
  111. err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, "/tmp", bases, stringset.Make(), 0)
  112. assert.NoError(t, err)
  113. assert.Equal(t, 1, int(cmdBuilder.passes))
  114. }
  115. // GIVEN 5 packages
  116. // WHEN downloadPKGBUILDSourceFanout is called
  117. // THEN 5 calls should be made to makepkg
  118. func Test_downloadPKGBUILDSourceFanoutError(t *testing.T) {
  119. t.Parallel()
  120. cmdBuilder := &TestMakepkgBuilder{
  121. parentBuilder: &exe.CmdBuilder{
  122. MakepkgConfPath: "/etc/not.conf",
  123. MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg",
  124. },
  125. test: t,
  126. showError: &exec.ExitError{},
  127. }
  128. bases := []dep.Base{
  129. {&aur.Pkg{PackageBase: "yay"}},
  130. {&aur.Pkg{PackageBase: "yay-bin"}},
  131. {&aur.Pkg{PackageBase: "yay-git"}},
  132. {&aur.Pkg{PackageBase: "yay-v11"}},
  133. {&aur.Pkg{PackageBase: "yay-v12"}},
  134. }
  135. err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, "/tmp", bases, stringset.Make(), 0)
  136. assert.Error(t, err)
  137. assert.Equal(t, 5, int(cmdBuilder.passes))
  138. assert.Len(t, err.(*multierror.MultiError).Errors, 5)
  139. }