fix(getpkgbuild): check AUR pkgs exist before GetPKGBUILD (#1921)

check AUR pkgs exist before GetPKGBUILD
This commit is contained in:
Jo 2023-02-21 00:58:13 +01:00 committed by GitHub
parent 7490836991
commit fad26c078d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 119 additions and 33 deletions

6
cmd.go
View file

@ -323,10 +323,12 @@ func handleWeb(ctx context.Context, cmdArgs *parser.Arguments) error {
func handleGetpkgbuild(ctx context.Context, cmdArgs *parser.Arguments, dbExecutor download.DBSearcher) error {
if cmdArgs.ExistsArg("p", "print") {
return printPkgbuilds(dbExecutor, config.Runtime.HTTPClient, cmdArgs.Targets, config.Runtime.Mode, config.AURURL)
return printPkgbuilds(dbExecutor, config.Runtime.AURCache,
config.Runtime.HTTPClient, cmdArgs.Targets, config.Runtime.Mode, config.AURURL)
}
return getPkgbuilds(ctx, dbExecutor, config, cmdArgs.Targets, cmdArgs.ExistsArg("f", "force"))
return getPkgbuilds(ctx, dbExecutor, config.Runtime.AURCache, config,
cmdArgs.Targets, cmdArgs.ExistsArg("f", "force"))
}
func handleUpgrade(ctx context.Context,

13
get.go
View file

@ -7,6 +7,7 @@ import (
"os"
"strings"
"github.com/Jguer/aur"
"github.com/leonelquinteros/gotext"
"github.com/Jguer/yay/v11/pkg/download"
@ -16,10 +17,10 @@ import (
)
// yay -Gp.
func printPkgbuilds(dbExecutor download.DBSearcher, httpClient *http.Client, targets []string,
func printPkgbuilds(dbExecutor download.DBSearcher, aurClient aur.QueryClient, httpClient *http.Client, targets []string,
mode parser.TargetMode, aurURL string,
) error {
pkgbuilds, err := download.PKGBUILDs(dbExecutor, httpClient, targets, aurURL, mode)
pkgbuilds, err := download.PKGBUILDs(dbExecutor, aurClient, httpClient, targets, aurURL, mode)
if err != nil {
text.Errorln(err)
}
@ -40,7 +41,7 @@ func printPkgbuilds(dbExecutor download.DBSearcher, httpClient *http.Client, tar
}
}
text.Warnln(gotext.Get("Unable to find the following packages:"), strings.Join(missing, ", "))
text.Warnln(gotext.Get("Unable to find the following packages:"), " ", strings.Join(missing, ", "))
return fmt.Errorf("")
}
@ -49,7 +50,7 @@ func printPkgbuilds(dbExecutor download.DBSearcher, httpClient *http.Client, tar
}
// yay -G.
func getPkgbuilds(ctx context.Context, dbExecutor download.DBSearcher,
func getPkgbuilds(ctx context.Context, dbExecutor download.DBSearcher, aurClient aur.QueryClient,
config *settings.Configuration, targets []string, force bool,
) error {
wd, err := os.Getwd()
@ -57,7 +58,7 @@ func getPkgbuilds(ctx context.Context, dbExecutor download.DBSearcher,
return err
}
cloned, errD := download.PKGBUILDRepos(ctx, dbExecutor,
cloned, errD := download.PKGBUILDRepos(ctx, dbExecutor, aurClient,
config.Runtime.CmdBuilder, targets, config.Runtime.Mode, config.AURURL, wd, force)
if errD != nil {
text.Errorln(errD)
@ -72,7 +73,7 @@ func getPkgbuilds(ctx context.Context, dbExecutor download.DBSearcher,
}
}
text.Warnln(gotext.Get("Unable to find the following packages:"), strings.Join(missing, ", "))
text.Warnln(gotext.Get("Unable to find the following packages:"), " ", strings.Join(missing, ", "))
err = fmt.Errorf("")
}

View file

@ -9,6 +9,8 @@ import (
"github.com/leonelquinteros/gotext"
"github.com/Jguer/aur"
"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/multierror"
"github.com/Jguer/yay/v11/pkg/settings/exe"
@ -79,7 +81,7 @@ func getURLName(pkg db.IPackage) string {
return name
}
func PKGBUILDs(dbExecutor DBSearcher, httpClient *http.Client, targets []string,
func PKGBUILDs(dbExecutor DBSearcher, aurClient aur.QueryClient, httpClient *http.Client, targets []string,
aurURL string, mode parser.TargetMode,
) (map[string][]byte, error) {
pkgbuilds := make(map[string][]byte, len(targets))
@ -94,7 +96,7 @@ func PKGBUILDs(dbExecutor DBSearcher, httpClient *http.Client, targets []string,
for _, target := range targets {
// Probably replaceable by something in query.
dbName, name, aur, toSkip := getPackageUsableName(dbExecutor, target, mode)
dbName, name, isAUR, toSkip := getPackageUsableName(dbExecutor, aurClient, target, mode)
if toSkip {
continue
}
@ -125,7 +127,7 @@ func PKGBUILDs(dbExecutor DBSearcher, httpClient *http.Client, targets []string,
<-sem
wg.Done()
}(target, dbName, name, aur)
}(target, dbName, name, isAUR)
}
wg.Wait()
@ -133,7 +135,7 @@ func PKGBUILDs(dbExecutor DBSearcher, httpClient *http.Client, targets []string,
return pkgbuilds, errs.Return()
}
func PKGBUILDRepos(ctx context.Context, dbExecutor DBSearcher,
func PKGBUILDRepos(ctx context.Context, dbExecutor DBSearcher, aurClient aur.QueryClient,
cmdBuilder exe.GitCmdBuilder,
targets []string, mode parser.TargetMode, aurURL, dest string, force bool,
) (map[string]bool, error) {
@ -149,7 +151,7 @@ func PKGBUILDRepos(ctx context.Context, dbExecutor DBSearcher,
for _, target := range targets {
// Probably replaceable by something in query.
dbName, name, aur, toSkip := getPackageUsableName(dbExecutor, target, mode)
dbName, name, isAUR, toSkip := getPackageUsableName(dbExecutor, aurClient, target, mode)
if toSkip {
continue
}
@ -194,7 +196,7 @@ func PKGBUILDRepos(ctx context.Context, dbExecutor DBSearcher,
<-sem
wg.Done()
}(target, dbName, name, aur)
}(target, dbName, name, isAUR)
}
wg.Wait()
@ -203,34 +205,47 @@ func PKGBUILDRepos(ctx context.Context, dbExecutor DBSearcher,
}
// TODO: replace with dep.ResolveTargets.
func getPackageUsableName(dbExecutor DBSearcher, target string, mode parser.TargetMode) (dbname, pkgname string, aur, toSkip bool) {
aur = true
func getPackageUsableName(dbExecutor DBSearcher, aurClient aur.QueryClient,
target string, mode parser.TargetMode,
) (dbname, pkgname string, isAUR, toSkip bool) {
dbName, name := text.SplitDBFromName(target)
if dbName != "aur" && mode.AtLeastRepo() {
var pkg db.IPackage
if dbName != "" {
pkg = dbExecutor.SatisfierFromDB(name, dbName)
if pkg == nil {
// if the user precised a db but the package is not in the db
// then it is missing
// Mode does not allow AUR packages
return dbName, name, aur, true
}
} else {
pkg = dbExecutor.SyncPackage(name)
}
if pkg != nil {
aur = false
name = getURLName(pkg)
dbName = pkg.DB().Name()
return dbName, name, false, false
}
// If the package is not found in the database and it was expected to be
if pkg == nil && dbName != "" {
return dbName, name, true, true
}
}
if aur && mode == parser.ModeRepo {
return dbName, name, aur, true
if mode == parser.ModeRepo {
return dbName, name, true, true
}
return dbName, name, aur, false
pkgs, err := aurClient.Get(context.Background(), &aur.Query{
By: aur.Name,
Contains: false,
Needles: []string{name},
})
if err != nil {
text.Warnln(err)
return dbName, name, true, true
}
if len(pkgs) == 0 {
return dbName, name, true, true
}
return "aur", name, true, false
}

View file

@ -10,6 +10,9 @@ import (
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
"github.com/Jguer/aur"
mockaur "github.com/Jguer/yay/v11/pkg/dep/mock"
"github.com/Jguer/yay/v11/pkg/settings/exe"
"github.com/Jguer/yay/v11/pkg/settings/parser"
)
@ -22,6 +25,12 @@ func TestPKGBUILDReposDefinedDBPull(t *testing.T) {
t.Parallel()
dir := t.TempDir()
mockClient := &mockaur.MockAUR{
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
return []aur.Pkg{{}}, nil // fakes a package found for all
},
}
os.MkdirAll(filepath.Join(dir, "yay", ".git"), 0o777)
targets := []string{"core/yay", "yay-bin", "yay-git"}
@ -38,7 +47,7 @@ func TestPKGBUILDReposDefinedDBPull(t *testing.T) {
searcher := &testDBSearcher{
absPackagesDB: map[string]string{"yay": "core"},
}
cloned, err := PKGBUILDRepos(context.Background(), searcher,
cloned, err := PKGBUILDRepos(context.Background(), searcher, mockClient,
cmdBuilder,
targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
@ -53,6 +62,11 @@ func TestPKGBUILDReposDefinedDBClone(t *testing.T) {
t.Parallel()
dir := t.TempDir()
mockClient := &mockaur.MockAUR{
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
return []aur.Pkg{{}}, nil // fakes a package found for all
},
}
targets := []string{"core/yay", "yay-bin", "yay-git"}
cmdRunner := &testRunner{}
cmdBuilder := &testGitBuilder{
@ -67,7 +81,7 @@ func TestPKGBUILDReposDefinedDBClone(t *testing.T) {
searcher := &testDBSearcher{
absPackagesDB: map[string]string{"yay": "core"},
}
cloned, err := PKGBUILDRepos(context.Background(), searcher,
cloned, err := PKGBUILDRepos(context.Background(), searcher, mockClient,
cmdBuilder,
targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
@ -82,6 +96,11 @@ func TestPKGBUILDReposClone(t *testing.T) {
t.Parallel()
dir := t.TempDir()
mockClient := &mockaur.MockAUR{
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
return []aur.Pkg{{}}, nil // fakes a package found for all
},
}
targets := []string{"yay", "yay-bin", "yay-git"}
cmdRunner := &testRunner{}
cmdBuilder := &testGitBuilder{
@ -96,7 +115,7 @@ func TestPKGBUILDReposClone(t *testing.T) {
searcher := &testDBSearcher{
absPackagesDB: map[string]string{"yay": "core"},
}
cloned, err := PKGBUILDRepos(context.Background(), searcher,
cloned, err := PKGBUILDRepos(context.Background(), searcher, mockClient,
cmdBuilder,
targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
@ -111,6 +130,11 @@ func TestPKGBUILDReposNotFound(t *testing.T) {
t.Parallel()
dir := t.TempDir()
mockClient := &mockaur.MockAUR{
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
return []aur.Pkg{{}}, nil // fakes a package found for all
},
}
targets := []string{"extra/yay", "yay-bin", "yay-git"}
cmdRunner := &testRunner{}
cmdBuilder := &testGitBuilder{
@ -125,7 +149,7 @@ func TestPKGBUILDReposNotFound(t *testing.T) {
searcher := &testDBSearcher{
absPackagesDB: map[string]string{"yay": "core"},
}
cloned, err := PKGBUILDRepos(context.Background(), searcher,
cloned, err := PKGBUILDRepos(context.Background(), searcher, mockClient,
cmdBuilder,
targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
@ -140,6 +164,11 @@ func TestPKGBUILDReposRepoMode(t *testing.T) {
t.Parallel()
dir := t.TempDir()
mockClient := &mockaur.MockAUR{
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
return []aur.Pkg{}, nil // fakes a package found for all
},
}
targets := []string{"yay", "yay-bin", "yay-git"}
cmdRunner := &testRunner{}
cmdBuilder := &testGitBuilder{
@ -154,7 +183,7 @@ func TestPKGBUILDReposRepoMode(t *testing.T) {
searcher := &testDBSearcher{
absPackagesDB: map[string]string{"yay": "core"},
}
cloned, err := PKGBUILDRepos(context.Background(), searcher,
cloned, err := PKGBUILDRepos(context.Background(), searcher, mockClient,
cmdBuilder,
targets, parser.ModeRepo, "https://aur.archlinux.org", dir, false)
@ -168,6 +197,11 @@ func TestPKGBUILDReposRepoMode(t *testing.T) {
func TestPKGBUILDFull(t *testing.T) {
t.Parallel()
mockClient := &mockaur.MockAUR{
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
return []aur.Pkg{{}}, nil
},
}
gock.New("https://aur.archlinux.org").
Get("/cgit/aur.git/plain/PKGBUILD").MatchParam("h", "yay-git").
Reply(200).
@ -188,7 +222,7 @@ func TestPKGBUILDFull(t *testing.T) {
absPackagesDB: map[string]string{"yay": "core"},
}
fetched, err := PKGBUILDs(searcher, &http.Client{},
fetched, err := PKGBUILDs(searcher, mockClient, &http.Client{},
targets, "https://aur.archlinux.org", parser.ModeAny)
assert.NoError(t, err)
@ -198,3 +232,37 @@ func TestPKGBUILDFull(t *testing.T) {
"yay-git": []byte("example_yay-git"),
}, fetched)
}
// GIVEN 2 aur packages and 1 in repo
// WHEN aur packages are not found
// only repo should be cloned
func TestPKGBUILDReposMissingAUR(t *testing.T) {
t.Parallel()
dir := t.TempDir()
mockClient := &mockaur.MockAUR{
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
return []aur.Pkg{}, nil // fakes a package found for all
},
}
targets := []string{"core/yay", "aur/yay-bin", "aur/yay-git"}
cmdRunner := &testRunner{}
cmdBuilder := &testGitBuilder{
index: 0,
test: t,
parentBuilder: &exe.CmdBuilder{
Runner: cmdRunner,
GitBin: "/usr/local/bin/git",
GitFlags: []string{},
},
}
searcher := &testDBSearcher{
absPackagesDB: map[string]string{"yay": "core"},
}
cloned, err := PKGBUILDRepos(context.Background(), searcher, mockClient,
cmdBuilder,
targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
assert.NoError(t, err)
assert.EqualValues(t, map[string]bool{"core/yay": true}, cloned)
}