From 1d903b6c7ecfdbf35785829427341f657a21830f Mon Sep 17 00:00:00 2001 From: jguer Date: Sat, 14 Aug 2021 00:09:43 +0200 Subject: [PATCH] test(sources): add PKGBUILD source tests --- aur_source.go | 14 ++--- aur_source_test.go | 154 +++++++++++++++++++++++++++++++++++++++++++++ install.go | 2 +- 3 files changed, 162 insertions(+), 8 deletions(-) create mode 100644 aur_source_test.go diff --git a/aur_source.go b/aur_source.go index aa6d0028..821ad968 100644 --- a/aur_source.go +++ b/aur_source.go @@ -31,9 +31,9 @@ func (e *ErrDownloadSource) Unwrap() error { return e.inner } -func downloadPKGBUILDSource(ctx context.Context, cmdBuilder exe.ICmdBuilder, +func downloadPKGBUILDSource(ctx context.Context, cmdBuilder exe.ICmdBuilder, dest, base string, incompatible stringset.StringSet) (err error) { - dir := filepath.Join(config.BuildDir, base) + dir := filepath.Join(dest, base) args := []string{"--verifysource", "-Ccf"} if incompatible.Get(base) { @@ -49,11 +49,11 @@ func downloadPKGBUILDSource(ctx context.Context, cmdBuilder exe.ICmdBuilder, return nil } -func downloadPKGBUILDSourceWorker(ctx context.Context, wg *sync.WaitGroup, +func downloadPKGBUILDSourceWorker(ctx context.Context, wg *sync.WaitGroup, dest string, cBase <-chan string, valOut chan<- string, errOut chan<- error, cmdBuilder exe.ICmdBuilder, incompatible stringset.StringSet) { for base := range cBase { - err := downloadPKGBUILDSource(ctx, cmdBuilder, base, incompatible) + err := downloadPKGBUILDSource(ctx, cmdBuilder, dest, base, incompatible) if err != nil { errOut <- ErrDownloadSource{inner: err, pkgName: base, errOut: ""} } else { @@ -64,10 +64,10 @@ func downloadPKGBUILDSourceWorker(ctx context.Context, wg *sync.WaitGroup, wg.Done() } -func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilder, +func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilder, dest string, bases []dep.Base, incompatible stringset.StringSet) error { if len(bases) == 1 { - return downloadPKGBUILDSource(ctx, cmdBuilder, bases[0].Pkgbase(), incompatible) + return downloadPKGBUILDSource(ctx, cmdBuilder, dest, bases[0].Pkgbase(), incompatible) } var ( @@ -90,7 +90,7 @@ func downloadPKGBUILDSourceFanout(ctx context.Context, cmdBuilder exe.ICmdBuilde wg.Add(numOfWorkers) for s := 0; s < numOfWorkers; s++ { - go downloadPKGBUILDSourceWorker(ctx, wg, c, + go downloadPKGBUILDSourceWorker(ctx, wg, dest, c, fanInChanValues, fanInChanErrors, cmdBuilder, incompatible) } diff --git a/aur_source_test.go b/aur_source_test.go new file mode 100644 index 00000000..89a841b4 --- /dev/null +++ b/aur_source_test.go @@ -0,0 +1,154 @@ +package main + +import ( + "context" + "os/exec" + "sync/atomic" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/Jguer/aur" + + "github.com/Jguer/yay/v10/pkg/dep" + "github.com/Jguer/yay/v10/pkg/multierror" + "github.com/Jguer/yay/v10/pkg/settings/exe" + "github.com/Jguer/yay/v10/pkg/stringset" +) + +type TestMakepkgBuilder struct { + exe.ICmdBuilder + parentBuilder *exe.CmdBuilder + test *testing.T + passes uint32 + want string + wantDir string + showError error +} + +func (z *TestMakepkgBuilder) BuildMakepkgCmd(ctx context.Context, dir string, extraArgs ...string) *exec.Cmd { + cmd := z.parentBuilder.BuildMakepkgCmd(ctx, dir, extraArgs...) + if z.want != "" { + assert.Contains(z.test, cmd.String(), z.want) + } + if z.wantDir != "" { + assert.Equal(z.test, z.wantDir, cmd.Dir) + } + + atomic.AddUint32(&z.passes, 1) + + return cmd +} + +func (z *TestMakepkgBuilder) Show(cmd *exec.Cmd) error { + return z.showError +} + +// GIVEN 1 package +// WHEN downloadPKGBUILDSource is called +// THEN 1 call should be made to makepkg with the specified parameters and dir +func Test_downloadPKGBUILDSource(t *testing.T) { + t.Parallel() + cmdBuilder := &TestMakepkgBuilder{ + parentBuilder: &exe.CmdBuilder{MakepkgConfPath: "/etc/not.conf", MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg"}, + test: t, + want: "makepkg --nocheck --config /etc/not.conf --verifysource -Ccf", + wantDir: "/tmp/yay-bin", + } + err := downloadPKGBUILDSource(context.TODO(), cmdBuilder, "/tmp", "yay-bin", stringset.Make()) + assert.NoError(t, err) + assert.Equal(t, 1, int(cmdBuilder.passes)) +} + +// GIVEN 1 package +// WHEN downloadPKGBUILDSource is called +// THEN 1 call should be made to makepkg which should return error +func Test_downloadPKGBUILDSourceError(t *testing.T) { + t.Parallel() + cmdBuilder := &TestMakepkgBuilder{ + parentBuilder: &exe.CmdBuilder{MakepkgConfPath: "/etc/not.conf", MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg"}, + test: t, + want: "makepkg --nocheck --config /etc/not.conf --verifysource -Ccf", + wantDir: "/tmp/yay-bin", + showError: &exec.ExitError{}, + } + err := downloadPKGBUILDSource(context.TODO(), cmdBuilder, "/tmp", "yay-bin", stringset.Make()) + assert.Error(t, err) + assert.EqualError(t, err, "error downloading sources: \x1b[36myay-bin\x1b[0m \n\t context: \n\t \n") +} + +// GIVEN 5 packages +// WHEN downloadPKGBUILDSourceFanout is called +// THEN 5 calls should be made to makepkg +func Test_downloadPKGBUILDSourceFanout(t *testing.T) { + t.Parallel() + cmdBuilder := &TestMakepkgBuilder{ + parentBuilder: &exe.CmdBuilder{ + MakepkgConfPath: "/etc/not.conf", + MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg", + }, + test: t, + } + + bases := []dep.Base{ + {&aur.Pkg{PackageBase: "yay"}}, + {&aur.Pkg{PackageBase: "yay-bin"}}, + {&aur.Pkg{PackageBase: "yay-git"}}, + {&aur.Pkg{PackageBase: "yay-v11"}}, + {&aur.Pkg{PackageBase: "yay-v12"}}, + } + + err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, "/tmp", bases, stringset.Make()) + assert.NoError(t, err) + assert.Equal(t, 5, int(cmdBuilder.passes)) +} + +// GIVEN 1 package +// WHEN downloadPKGBUILDSourceFanout is called +// THEN 1 calls should be made to makepkg without concurrency +func Test_downloadPKGBUILDSourceFanoutNoCC(t *testing.T) { + t.Parallel() + cmdBuilder := &TestMakepkgBuilder{ + parentBuilder: &exe.CmdBuilder{ + MakepkgConfPath: "/etc/not.conf", + MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg", + }, + test: t, + } + + bases := []dep.Base{ + {&aur.Pkg{PackageBase: "yay"}}, + } + + err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, "/tmp", bases, stringset.Make()) + assert.NoError(t, err) + assert.Equal(t, 1, int(cmdBuilder.passes)) +} + +// GIVEN 5 packages +// WHEN downloadPKGBUILDSourceFanout is called +// THEN 5 calls should be made to makepkg +func Test_downloadPKGBUILDSourceFanoutError(t *testing.T) { + t.Parallel() + cmdBuilder := &TestMakepkgBuilder{ + parentBuilder: &exe.CmdBuilder{ + MakepkgConfPath: "/etc/not.conf", + MakepkgFlags: []string{"--nocheck"}, MakepkgBin: "makepkg", + }, + test: t, + showError: &exec.ExitError{}, + } + + bases := []dep.Base{ + {&aur.Pkg{PackageBase: "yay"}}, + {&aur.Pkg{PackageBase: "yay-bin"}}, + {&aur.Pkg{PackageBase: "yay-git"}}, + {&aur.Pkg{PackageBase: "yay-v11"}}, + {&aur.Pkg{PackageBase: "yay-v12"}}, + } + + err := downloadPKGBUILDSourceFanout(context.TODO(), cmdBuilder, "/tmp", bases, stringset.Make()) + assert.Error(t, err) + assert.Equal(t, 5, int(cmdBuilder.passes)) + assert.Len(t, err.(*multierror.MultiError).Errors, 5) +} diff --git a/install.go b/install.go index 5230689a..30fcd1a0 100644 --- a/install.go +++ b/install.go @@ -396,7 +396,7 @@ func install(ctx context.Context, cmdArgs *parser.Arguments, dbExecutor db.Execu config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, false) }() - err = downloadPKGBUILDSourceFanout(ctx, config.Runtime.CmdBuilder, do.Aur, incompatible) + err = downloadPKGBUILDSourceFanout(ctx, config.Runtime.CmdBuilder, config.BuildDir, do.Aur, incompatible) if err != nil { text.Errorln(err) }