Skip to content

Commit

Permalink
fix(new_engine): respect --needed on target gathering (fixes Jguer#1552
Browse files Browse the repository at this point in the history
…) (Jguer#1920)

* use logger in dep graph

* use logger in dep graph

* use logger in dep graph

* only query for AUR packages once per tier. useful for rpc

* fix performance regression for ros-melodic

* prefer name search first

* implement needed at target gathering

* set default config

* fixup tests for needed
  • Loading branch information
Jguer authored Feb 20, 2023
1 parent 8d18f1b commit 7490836
Show file tree
Hide file tree
Showing 12 changed files with 447 additions and 148 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/Jguer/yay/v11

require (
github.com/Jguer/aur v1.2.1
github.com/Jguer/aur v1.2.3
github.com/Jguer/go-alpm/v2 v2.1.2
github.com/Jguer/votar v1.0.0
github.com/Morganamilo/go-pacmanconf v0.0.0-20210502114700-cff030e927a5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/Jguer/aur v1.2.1 h1:7ViPn4EN10iMAhtUSugxytloCynyVI12HzfhnGG1xGE=
github.com/Jguer/aur v1.2.1/go.mod h1:Dahvb6L1yr0rR7svyYSDwaRJoQMeyvJblwJ3QH/7CUs=
github.com/Jguer/aur v1.2.3 h1:D+OGgLxnAnZnw88DsRvnRQsn0Poxsy9ng7pBcsA0krM=
github.com/Jguer/aur v1.2.3/go.mod h1:Dahvb6L1yr0rR7svyYSDwaRJoQMeyvJblwJ3QH/7CUs=
github.com/Jguer/go-alpm/v2 v2.1.2 h1:CGTIxzuEpT9Q3a7IBrx0E6acoYoaHX2Z93UOApPDhgU=
github.com/Jguer/go-alpm/v2 v2.1.2/go.mod h1:uLQcTMNM904dRiGU+/JDtDdd7Nd8mVbEVaHjhmziT7w=
github.com/Jguer/votar v1.0.0 h1:drPYpV5Py5BeAQS8xezmT6uCEfLzotNjLf5yfmlHKTg=
Expand Down
5 changes: 3 additions & 2 deletions local_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package main

import (
"context"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -35,7 +34,9 @@ func installLocalPKGBUILD(
return errors.New(gotext.Get("no target directories specified"))
}

grapher := dep.NewGrapher(dbExecutor, aurCache, false, settings.NoConfirm, os.Stdout, cmdArgs.ExistsDouble("d", "nodeps"), noCheck)
grapher := dep.NewGrapher(dbExecutor, aurCache, false, settings.NoConfirm,
cmdArgs.ExistsDouble("d", "nodeps"), noCheck, cmdArgs.ExistsArg("needed"),
config.Runtime.Logger.Child("grapher"))
graph := topo.New[string, *dep.InstallInfo]()
for _, target := range cmdArgs.Targets {
var errG error
Expand Down
164 changes: 163 additions & 1 deletion local_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/Jguer/yay/v11/pkg/settings"
"github.com/Jguer/yay/v11/pkg/settings/exe"
"github.com/Jguer/yay/v11/pkg/settings/parser"
"github.com/Jguer/yay/v11/pkg/text"
"github.com/Jguer/yay/v11/pkg/vcs"
)

Expand Down Expand Up @@ -137,6 +139,7 @@ func TestIntegrationLocalInstall(t *testing.T) {
config := &settings.Configuration{
RemoveMake: "no",
Runtime: &settings.Runtime{
Logger: text.NewLogger(io.Discard, strings.NewReader(""), true, "test"),
CmdBuilder: cmdBuilder,
VCSStore: &vcs.Mock{},
AURCache: &mockaur.MockAUR{
Expand Down Expand Up @@ -166,7 +169,7 @@ func TestIntegrationLocalInstall(t *testing.T) {
}

func TestIntegrationLocalInstallMissingDep(t *testing.T) {
wantErr := "could not find dotnet-sdk>=6"
wantErr := "could not find dotnet-sdk<7"
makepkgBin := t.TempDir() + "/makepkg"
pacmanBin := t.TempDir() + "/pacman"
gitBin := t.TempDir() + "/git"
Expand Down Expand Up @@ -255,6 +258,7 @@ func TestIntegrationLocalInstallMissingDep(t *testing.T) {

config := &settings.Configuration{
Runtime: &settings.Runtime{
Logger: text.NewLogger(io.Discard, strings.NewReader(""), true, "test"),
CmdBuilder: cmdBuilder,
VCSStore: &vcs.Mock{},
AURCache: &mockaur.MockAUR{
Expand Down Expand Up @@ -283,3 +287,161 @@ func TestIntegrationLocalInstallMissingDep(t *testing.T) {
assert.Subset(t, strings.Split(show, " "), strings.Split(wantShow[i], " "), fmt.Sprintf("%d - %s", i, show))
}
}

func TestIntegrationLocalInstallNeeded(t *testing.T) {
makepkgBin := t.TempDir() + "/makepkg"
pacmanBin := t.TempDir() + "/pacman"
gitBin := t.TempDir() + "/git"
tmpDir := t.TempDir()
f, err := os.OpenFile(makepkgBin, os.O_RDONLY|os.O_CREATE, 0o755)
require.NoError(t, err)
require.NoError(t, f.Close())

f, err = os.OpenFile(pacmanBin, os.O_RDONLY|os.O_CREATE, 0o755)
require.NoError(t, err)
require.NoError(t, f.Close())

f, err = os.OpenFile(gitBin, os.O_RDONLY|os.O_CREATE, 0o755)
require.NoError(t, err)
require.NoError(t, f.Close())

tars := []string{
tmpDir + "/jellyfin-10.8.4-1-x86_64.pkg.tar.zst",
tmpDir + "/jellyfin-web-10.8.4-1-x86_64.pkg.tar.zst",
tmpDir + "/jellyfin-server-10.8.4-1-x86_64.pkg.tar.zst",
}

wantShow := []string{
"makepkg --verifysource -Ccf",
"pacman -S --config /etc/pacman.conf -- community/dotnet-sdk-6.0 community/dotnet-runtime-6.0",
"pacman -D -q --asdeps --config /etc/pacman.conf -- dotnet-runtime-6.0 dotnet-sdk-6.0",
"makepkg --nobuild -fC --ignorearch",
"makepkg -c --nobuild --noextract --ignorearch",
"makepkg --nobuild -fC --ignorearch",
"makepkg -c --nobuild --noextract --ignorearch",
"makepkg --nobuild -fC --ignorearch",
"makepkg -c --nobuild --noextract --ignorearch",
}

wantCapture := []string{
"makepkg --packagelist",
"git -C testdata/jfin git reset --hard HEAD",
"git -C testdata/jfin git merge --no-edit --ff",
"makepkg --packagelist",
"makepkg --packagelist",
}

captureOverride := func(cmd *exec.Cmd) (stdout string, stderr string, err error) {
return strings.Join(tars, "\n"), "", nil
}

once := sync.Once{}

showOverride := func(cmd *exec.Cmd) error {
once.Do(func() {
for _, tar := range tars {
f, err := os.OpenFile(tar, os.O_RDONLY|os.O_CREATE, 0o666)
require.NoError(t, err)
require.NoError(t, f.Close())
}
})
return nil
}

mockRunner := &exe.MockRunner{CaptureFn: captureOverride, ShowFn: showOverride}
cmdBuilder := &exe.CmdBuilder{
MakepkgBin: makepkgBin,
SudoBin: "su",
PacmanBin: pacmanBin,
PacmanConfigPath: "/etc/pacman.conf",
GitBin: "git",
Runner: mockRunner,
SudoLoopEnabled: false,
}

cmdArgs := parser.MakeArguments()
cmdArgs.AddArg("B")
cmdArgs.AddArg("i")
cmdArgs.AddArg("needed")
cmdArgs.AddTarget("testdata/jfin")
settings.NoConfirm = true
defer func() { settings.NoConfirm = false }()
db := &mock.DBExecutor{
AlpmArchitecturesFn: func() ([]string, error) {
return []string{"x86_64"}, nil
},
IsCorrectVersionInstalledFn: func(s1, s2 string) bool {
return true
},
LocalPackageFn: func(s string) mock.IPackage {
if s == "jellyfin-server" {
return &mock.Package{
PName: "jellyfin-server",
PBase: "jellyfin-server",
PVersion: "10.8.4-1",
PDB: mock.NewDB("community"),
}
}
return nil
},
LocalSatisfierExistsFn: func(s string) bool {
switch s {
case "dotnet-sdk>=6", "dotnet-sdk<7", "dotnet-runtime>=6", "dotnet-runtime<7", "jellyfin-server=10.8.4", "jellyfin-web=10.8.4":
return false
}

return true
},
SyncSatisfierFn: func(s string) mock.IPackage {
switch s {
case "dotnet-runtime>=6", "dotnet-runtime<7":
return &mock.Package{
PName: "dotnet-runtime-6.0",
PBase: "dotnet-runtime-6.0",
PVersion: "6.0.100-1",
PDB: mock.NewDB("community"),
}
case "dotnet-sdk>=6", "dotnet-sdk<7":
return &mock.Package{
PName: "dotnet-sdk-6.0",
PBase: "dotnet-sdk-6.0",
PVersion: "6.0.100-1",
PDB: mock.NewDB("community"),
}
}

return nil
},
}

config := &settings.Configuration{
RemoveMake: "no",
Runtime: &settings.Runtime{
Logger: text.NewLogger(io.Discard, strings.NewReader(""), true, "test"),
CmdBuilder: cmdBuilder,
VCSStore: &vcs.Mock{},
AURCache: &mockaur.MockAUR{
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
return []aur.Pkg{}, nil
},
},
},
}

err = handleCmd(context.Background(), config, cmdArgs, db)
require.NoError(t, err)

require.Len(t, mockRunner.ShowCalls, len(wantShow), "show calls: %v", mockRunner.ShowCalls)
require.Len(t, mockRunner.CaptureCalls, len(wantCapture))

for i, call := range mockRunner.ShowCalls {
show := call.Args[0].(*exec.Cmd).String()
show = strings.ReplaceAll(show, tmpDir, "/testdir") // replace the temp dir with a static path
show = strings.ReplaceAll(show, makepkgBin, "makepkg")
show = strings.ReplaceAll(show, pacmanBin, "pacman")
show = strings.ReplaceAll(show, gitBin, "pacman")

// options are in a different order on different systems and on CI root user is used
assert.Subset(t, strings.Split(show, " "), strings.Split(wantShow[i], " "), fmt.Sprintf("%d - %s", i, show))
}
}
6 changes: 4 additions & 2 deletions pkg/cmd/graph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ func handleCmd() error {
return errors.Wrap(err, gotext.Get("failed to retrieve aur Cache"))
}

grapher := dep.NewGrapher(dbExecutor, aurCache, true, settings.NoConfirm, os.Stdout, cmdArgs.ExistsDouble("d", "nodeps"), false)
grapher := dep.NewGrapher(dbExecutor, aurCache, true, settings.NoConfirm,
cmdArgs.ExistsDouble("d", "nodeps"), false, false,
config.Runtime.Logger.Child("grapher"))

return graphPackage(context.Background(), grapher, cmdArgs.Targets)
}
Expand All @@ -66,7 +68,7 @@ func graphPackage(
return errors.New(gotext.Get("only one target is allowed"))
}

graph, err := grapher.GraphFromAURCache(ctx, nil, []string{targets[0]})
graph, err := grapher.GraphFromAUR(ctx, nil, []string{targets[0]})
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/db/mock/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type (

type DBExecutor struct {
db.Executor
LocalPackageFn func(string) IPackage
IsCorrectVersionInstalledFn func(string, string) bool
SyncPackageFn func(string) IPackage
PackagesFromGroupFn func(string) []IPackage
Expand Down Expand Up @@ -69,7 +70,11 @@ func (t *DBExecutor) LastBuildTime() time.Time {
}

func (t *DBExecutor) LocalPackage(s string) IPackage {
return nil
if t.LocalPackageFn != nil {
return t.LocalPackageFn(s)
}

panic("implement me")
}

func (t *DBExecutor) LocalPackages() []IPackage {
Expand Down
Loading

0 comments on commit 7490836

Please sign in to comment.