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:
LordBeerus
2025-12-21 20:24:23 +05:30
committed by GitHub
parent 74e3cf80dd
commit c466b96b94
3 changed files with 79 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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)
})
}

View File

@@ -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")
}