mirror of
https://github.com/Jguer/yay.git
synced 2025-12-27 10:01:53 -05:00
fix: Correctly return exit status 1 when no packages are found (#2705)
* fix: Correctly return exit status 1 when no packages are found * add testing for targets not found
This commit is contained in:
@@ -423,6 +423,8 @@ func (g *Grapher) GraphFromAUR(ctx context.Context,
|
||||
|
||||
aurPkgsAdded := []*aurc.Pkg{}
|
||||
|
||||
var packagesNotFound int
|
||||
|
||||
for _, target := range targets {
|
||||
if cachedProvidePkg, ok := g.providerCache[target]; ok {
|
||||
aurPkgs = cachedProvidePkg
|
||||
@@ -436,6 +438,7 @@ func (g *Grapher) GraphFromAUR(ctx context.Context,
|
||||
|
||||
if len(aurPkgs) == 0 {
|
||||
g.logger.Errorln(gotext.Get("No AUR package found for"), " ", target)
|
||||
packagesNotFound++
|
||||
|
||||
continue
|
||||
}
|
||||
@@ -470,6 +473,10 @@ func (g *Grapher) GraphFromAUR(ctx context.Context,
|
||||
|
||||
g.AddDepsForPkgs(ctx, aurPkgsAdded, graph)
|
||||
|
||||
if packagesNotFound == len(targets) {
|
||||
return graph, &aur.ErrTargetNotFound{}
|
||||
}
|
||||
|
||||
return graph, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -837,3 +837,69 @@ func TestGrapher_GraphFromTargets_ReinstalledDeps(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrapher_GraphFromTargets_TargetNotFound(t *testing.T) {
|
||||
mockDB := &mock.DBExecutor{
|
||||
SyncSatisfierFn: func(string) mock.IPackage { return nil },
|
||||
PackagesFromGroupFn: func(string) []mock.IPackage { return nil },
|
||||
LocalPackageFn: func(string) mock.IPackage { return nil },
|
||||
LocalSatisfierExistsFn: func(string) bool { return false },
|
||||
}
|
||||
|
||||
mockAUR := &mockaur.MockAUR{GetFn: func(ctx context.Context, query *aurc.Query) ([]aur.Pkg, error) {
|
||||
ok := aur.Pkg{
|
||||
Name: "okpkg",
|
||||
PackageBase: "okpkg",
|
||||
Version: "1.0.0",
|
||||
}
|
||||
|
||||
switch query.By {
|
||||
case aurc.Name:
|
||||
// Return only packages that exist.
|
||||
pkgs := make([]aur.Pkg, 0, len(query.Needles))
|
||||
for _, needle := range query.Needles {
|
||||
if needle == ok.Name {
|
||||
pkgs = append(pkgs, ok)
|
||||
}
|
||||
}
|
||||
return pkgs, nil
|
||||
case aurc.Provides:
|
||||
// Provider lookup is done per-target.
|
||||
if len(query.Needles) > 0 && query.Needles[0] == ok.Name {
|
||||
return []aur.Pkg{ok}, nil
|
||||
}
|
||||
return []aur.Pkg{}, nil
|
||||
default:
|
||||
return []aur.Pkg{}, nil
|
||||
}
|
||||
}}
|
||||
|
||||
g := NewGrapher(mockDB, mockAUR,
|
||||
false, true, true, true, false,
|
||||
text.NewLogger(io.Discard, io.Discard, &os.File{}, true, "test"))
|
||||
|
||||
t.Run("returns error when all targets are missing", func(t *testing.T) {
|
||||
_, err := g.GraphFromTargets(context.Background(), nil, []string{"missing1", "missing2"})
|
||||
require.Error(t, err)
|
||||
|
||||
var targetNotFound *aur.ErrTargetNotFound
|
||||
require.ErrorAs(t, err, &targetNotFound)
|
||||
})
|
||||
|
||||
t.Run("does not error when at least one target is found", func(t *testing.T) {
|
||||
got, err := g.GraphFromTargets(context.Background(), nil, []string{"missing1", "okpkg"})
|
||||
require.NoError(t, err)
|
||||
|
||||
layers := got.TopoSortedLayers(nil)
|
||||
require.EqualValues(t, []map[string]*InstallInfo{
|
||||
{
|
||||
"okpkg": {
|
||||
Source: AUR,
|
||||
Reason: Explicit,
|
||||
Version: "1.0.0",
|
||||
AURBase: ptrString("okpkg"),
|
||||
},
|
||||
},
|
||||
}, layers, layers)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -19,3 +19,9 @@ type ErrNoQuery struct{}
|
||||
func (e ErrNoQuery) Error() string {
|
||||
return gotext.Get("no query was executed")
|
||||
}
|
||||
|
||||
type ErrTargetNotFound struct{}
|
||||
|
||||
func (e ErrTargetNotFound) Error() string {
|
||||
return gotext.Get("no package found for targets")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user