Skip to content

Commit

Permalink
chore(yay): use new aur client for -Si (#2112)
Browse files Browse the repository at this point in the history
* chore(yay): use new aur client for -Si

* chore(yay): use new client for clean
  • Loading branch information
Jguer authored Apr 11, 2023
1 parent 26c9ab5 commit 161fede
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 15 deletions.
25 changes: 14 additions & 11 deletions clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"os"
"path/filepath"

"github.com/Jguer/aur"
"github.com/leonelquinteros/gotext"

"github.com/Jguer/yay/v12/pkg/db"
"github.com/Jguer/yay/v12/pkg/query"
"github.com/Jguer/yay/v12/pkg/settings"
"github.com/Jguer/yay/v12/pkg/settings/exe"
"github.com/Jguer/yay/v12/pkg/settings/parser"
Expand Down Expand Up @@ -100,17 +100,17 @@ func syncClean(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser
return nil
}

func cleanAUR(ctx context.Context, config *settings.Configuration,
func cleanAUR(ctx context.Context, cfg *settings.Configuration,
keepInstalled, keepCurrent, removeAll bool, dbExecutor db.Executor,
) error {
fmt.Println(gotext.Get("removing AUR packages from cache..."))
cfg.Runtime.Logger.Println(gotext.Get("removing AUR packages from cache..."))

installedBases := make(stringset.StringSet)
inAURBases := make(stringset.StringSet)

remotePackages := dbExecutor.InstalledRemotePackages()

files, err := os.ReadDir(config.BuildDir)
files, err := os.ReadDir(cfg.BuildDir)
if err != nil {
return err
}
Expand All @@ -130,7 +130,9 @@ func cleanAUR(ctx context.Context, config *settings.Configuration,
// Querying the AUR is slow and needs internet so don't do it if we
// don't need to.
if keepCurrent {
info, errInfo := query.AURInfo(ctx, config.Runtime.AURClient, cachedPackages, query.NewWarnings(nil), config.RequestSplitN)
info, errInfo := cfg.Runtime.AURCache.Get(ctx, &aur.Query{
Needles: cachedPackages,
})
if errInfo != nil {
return errInfo
}
Expand Down Expand Up @@ -163,18 +165,18 @@ func cleanAUR(ctx context.Context, config *settings.Configuration,
}
}

dir := filepath.Join(config.BuildDir, file.Name())
err = os.RemoveAll(dir)
if err != nil {
text.Warnln(gotext.Get("Unable to remove %s: %s", dir, err))
dir := filepath.Join(cfg.BuildDir, file.Name())
cfg.Runtime.Logger.Debugln("removing", dir)
if err = os.RemoveAll(dir); err != nil {
cfg.Runtime.Logger.Warnln(gotext.Get("Unable to remove %s: %s", dir, err))
}
}

return nil
}

func cleanUntracked(ctx context.Context, cfg *settings.Configuration) error {
fmt.Println(gotext.Get("removing untracked AUR files from cache..."))
cfg.Runtime.Logger.Println(gotext.Get("removing untracked AUR files from cache..."))

files, err := os.ReadDir(cfg.BuildDir)
if err != nil {
Expand All @@ -187,9 +189,10 @@ func cleanUntracked(ctx context.Context, cfg *settings.Configuration) error {
}

dir := filepath.Join(cfg.BuildDir, file.Name())
cfg.Runtime.Logger.Debugln("cleaning", dir)
if isGitRepository(dir) {
if err := cfg.Runtime.CmdBuilder.Show(cfg.Runtime.CmdBuilder.BuildGitCmd(ctx, dir, "clean", "-fx")); err != nil {
text.Warnln(gotext.Get("Unable to clean:"), dir)
cfg.Runtime.Logger.Warnln(gotext.Get("Unable to clean:"), dir)

return err
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/db/mock/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,8 @@ func (t *DBExecutor) SyncSatisfier(s string) IPackage {
}

func (t *DBExecutor) SyncSatisfierExists(s string) bool {
if t.SyncSatisfierFn != nil {
return t.SyncSatisfierFn(s) != nil
}
panic("implement me")
}
8 changes: 4 additions & 4 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"

aur "github.com/Jguer/aur"
Expand Down Expand Up @@ -53,15 +52,16 @@ func syncInfo(ctx context.Context, cfg *settings.Configuration,
noDB = append(noDB, name)
}

info, err = query.AURInfoPrint(ctx, cfg.Runtime.AURClient, noDB, cfg.RequestSplitN)
info, err = cfg.Runtime.AURCache.Get(ctx, &aur.Query{
Needles: noDB,
})
if err != nil {
missing = true

fmt.Fprintln(os.Stderr, err)
cfg.Runtime.Logger.Errorln(err)
}
}

// Repo always goes first
if len(repoS) != 0 {
arguments := cmdArgs.Copy()
arguments.ClearTargets()
Expand Down
162 changes: 162 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package main

import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"strings"
"testing"

"github.com/Jguer/yay/v12/pkg/db/mock"
mockaur "github.com/Jguer/yay/v12/pkg/dep/mock"
"github.com/Jguer/yay/v12/pkg/settings"
"github.com/Jguer/yay/v12/pkg/settings/exe"
"github.com/Jguer/yay/v12/pkg/settings/parser"

"github.com/Jguer/aur"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func getFromFile(t *testing.T, filePath string) mockaur.GetFunc {
f, err := os.Open(filePath)
require.NoError(t, err)

fBytes, err := io.ReadAll(f)
require.NoError(t, err)

pkgs := []aur.Pkg{}
err = json.Unmarshal(fBytes, &pkgs)
require.NoError(t, err)

return func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
return pkgs, nil
}
}

func TestSyncInfo(t *testing.T) {
pacmanBin := t.TempDir() + "/pacman"

t.Parallel()

testCases := []struct {
name string
args []string
targets []string
wantShow []string
wantErr bool
}{
{
name: "Si linux",
args: []string{"S", "i"},
targets: []string{"linux"},
wantShow: []string{"pacman", "-S", "-i", "--config", "/etc/pacman.conf", "--", "linux"},
},
{
name: "Si jellyfin",
args: []string{"S", "i"},
targets: []string{"jellyfin"},
wantShow: []string{},
},
{
name: "Si linux jellyfin",
args: []string{"S", "i"},
targets: []string{"linux", "jellyfin"},
wantShow: []string{"pacman", "-S", "-i", "--config", "/etc/pacman.conf", "--", "linux"},
},
{
name: "Si jellyfin",
args: []string{"S", "i"},
targets: []string{"jellyfin"},
wantShow: []string{},
},
{
name: "Si missing",
args: []string{"S", "i"},
targets: []string{"missing"},
wantShow: []string{},
wantErr: true,
},
}

dbExc := &mock.DBExecutor{
SyncSatisfierFn: func(s string) mock.IPackage {
if s == "linux" {
return &mock.Package{
PName: "linux",
PBase: "linux",
}
}
return nil
},
PackagesFromGroupFn: func(s string) []mock.IPackage {
return nil
},
}

mockAUR := &mockaur.MockAUR{GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
if query.Needles[0] == "jellyfin" {
jfinFn := getFromFile(t, "pkg/dep/testdata/jellyfin.json")
return jfinFn(ctx, query)
}

return nil, fmt.Errorf("not found")
}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockRunner := &exe.MockRunner{
CaptureFn: func(cmd *exec.Cmd) (stdout string, stderr string, err error) {
return "", "", nil
},
ShowFn: func(cmd *exec.Cmd) error { return nil },
}
cmdBuilder := &exe.CmdBuilder{
SudoBin: "su",
PacmanBin: pacmanBin,
PacmanConfigPath: "/etc/pacman.conf",
GitBin: "git",
Runner: mockRunner,
SudoLoopEnabled: false,
}
cfg := &settings.Configuration{
Runtime: &settings.Runtime{
CmdBuilder: cmdBuilder,
AURCache: mockAUR,
Logger: NewTestLogger(),
},
}

cmdArgs := parser.MakeArguments()
cmdArgs.AddArg(tc.args...)
cmdArgs.AddTarget(tc.targets...)

err := handleCmd(context.Background(),
cfg, cmdArgs, dbExc,
)

if tc.wantErr {
require.Error(t, err)
assert.EqualError(t, err, "")
} else {
require.NoError(t, err)
}
if len(tc.wantShow) == 0 {
assert.Empty(t, mockRunner.ShowCalls)
return
}
for i, call := range mockRunner.ShowCalls {
show := call.Args[0].(*exec.Cmd).String()
show = strings.ReplaceAll(show, pacmanBin, "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(tc.wantShow[i], " "),
fmt.Sprintf("%d - %s", i, show))
}
})
}
}

0 comments on commit 161fede

Please sign in to comment.