aur_source_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/v11/pkg/multierror"
  11. "github.com/Jguer/yay/v11/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.TODO(), 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.TODO(), cmdBuilder, filepath.Join("/tmp", "yay-bin"), false)
  64. assert.Error(t, err)
  65. assert.EqualError(t, err, "error downloading sources: \x1b[36myay-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 := []string{"/tmp/yay", "/tmp/yay-bin", "/tmp/yay-git", "/tmp/yay-v11", "/tmp/yay-v12"}
  73. for _, maxConcurrentDownloads := range []int{0, 3} {
  74. t.Run(fmt.Sprintf("maxconcurrentdownloads set to %d", maxConcurrentDownloads), func(t *testing.T) {
  75. cmdBuilder := &TestMakepkgBuilder{
  76. parentBuilder: &exe.CmdBuilder{
  77. MakepkgConfPath: "/etc/not.conf",
  78. MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg",
  79. },
  80. test: t,
  81. }
  82. err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, pkgBuildDirs, false, maxConcurrentDownloads)
  83. assert.NoError(t, err)
  84. assert.Equal(t, 5, int(cmdBuilder.passes))
  85. })
  86. }
  87. }
  88. // GIVEN 1 package
  89. // WHEN downloadPKGBUILDSourceFanout is called
  90. // THEN 1 calls should be made to makepkg without concurrency
  91. func Test_downloadPKGBUILDSourceFanoutNoCC(t *testing.T) {
  92. t.Parallel()
  93. cmdBuilder := &TestMakepkgBuilder{
  94. parentBuilder: &exe.CmdBuilder{
  95. MakepkgConfPath: "/etc/not.conf",
  96. MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg",
  97. },
  98. test: t,
  99. }
  100. pkgBuildDirs := []string{"/tmp/yay"}
  101. err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, pkgBuildDirs, false, 0)
  102. assert.NoError(t, err)
  103. assert.Equal(t, 1, int(cmdBuilder.passes))
  104. }
  105. // GIVEN 5 packages
  106. // WHEN downloadPKGBUILDSourceFanout is called
  107. // THEN 5 calls should be made to makepkg
  108. func Test_downloadPKGBUILDSourceFanoutError(t *testing.T) {
  109. t.Parallel()
  110. cmdBuilder := &TestMakepkgBuilder{
  111. parentBuilder: &exe.CmdBuilder{
  112. MakepkgConfPath: "/etc/not.conf",
  113. MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg",
  114. },
  115. test: t,
  116. showError: &exec.ExitError{},
  117. }
  118. pkgBuildDirs := []string{
  119. "/tmp/yay",
  120. "/tmp/yay-bin",
  121. "/tmp/yay-git",
  122. "/tmp/yay-v11",
  123. "/tmp/yay-v12",
  124. }
  125. err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, pkgBuildDirs, false, 0)
  126. assert.Error(t, err)
  127. assert.Equal(t, 5, int(cmdBuilder.passes))
  128. assert.Len(t, err.(*multierror.MultiError).Errors, 5)
  129. }