aur_source_test.go 4.6 KB

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