aur_source_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //go:build !integration
  2. // +build !integration
  3. package main
  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.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.Background(), cmdBuilder, filepath.Join("/tmp", "yay-bin"), false)
  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.Background(), cmdBuilder, filepath.Join("/tmp", "yay-bin"), false)
  66. assert.Error(t, err)
  67. assert.EqualError(t, err, "error downloading sources: \x1b[36m/tmp/yay-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. pkgBuildDirs := map[string]string{
  75. "yay": "/tmp/yay",
  76. "yay-bin": "/tmp/yay-bin",
  77. "yay-git": "/tmp/yay-git",
  78. "yay-v11": "/tmp/yay-v11",
  79. "yay-v12": "/tmp/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.Background(), cmdBuilder, pkgBuildDirs, false, 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. pkgBuildDirs := map[string]string{"yay": "/tmp/yay"}
  109. err := downloadPKGBUILDSourceFanout(context.Background(), cmdBuilder, pkgBuildDirs, false, 0)
  110. assert.NoError(t, err)
  111. assert.Equal(t, 1, int(cmdBuilder.passes))
  112. }
  113. // GIVEN 5 packages
  114. // WHEN downloadPKGBUILDSourceFanout is called
  115. // THEN 5 calls should be made to makepkg
  116. func Test_downloadPKGBUILDSourceFanoutError(t *testing.T) {
  117. t.Parallel()
  118. cmdBuilder := &TestMakepkgBuilder{
  119. parentBuilder: &exe.CmdBuilder{
  120. MakepkgConfPath: "/etc/not.conf",
  121. MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg",
  122. },
  123. test: t,
  124. showError: &exec.ExitError{},
  125. }
  126. pkgBuildDirs := map[string]string{
  127. "yay": "/tmp/yay",
  128. "yay-bin": "/tmp/yay-bin",
  129. "yay-git": "/tmp/yay-git",
  130. "yay-v11": "/tmp/yay-v11",
  131. "yay-v12": "/tmp/yay-v12",
  132. }
  133. err := downloadPKGBUILDSourceFanout(context.Background(), cmdBuilder, pkgBuildDirs, false, 0)
  134. assert.Error(t, err)
  135. assert.Equal(t, 5, int(cmdBuilder.passes))
  136. assert.Len(t, err.(*multierror.MultiError).Errors, 5)
  137. }