Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(clean): modify clean args #2039

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,34 @@ import (

// CleanDependencies removes all dangling dependencies in system.
func cleanDependencies(ctx context.Context, cfg *settings.Configuration,
cmdArgs *parser.Arguments, dbExecutor db.Executor, removeOptional bool,
cmdBuilder exe.ICmdBuilder, cmdArgs *parser.Arguments, dbExecutor db.Executor,
removeOptional bool,
) error {
hanging := hangingPackages(removeOptional, dbExecutor)
if len(hanging) != 0 {
return cleanRemove(ctx, cfg, cmdArgs, hanging)
return cleanRemove(ctx, cfg, cmdBuilder, cmdArgs, hanging)
}

return nil
}

// CleanRemove sends a full removal command to pacman with the pkgName slice.
func cleanRemove(ctx context.Context, config *settings.Configuration, cmdArgs *parser.Arguments, pkgNames []string) error {
func cleanRemove(ctx context.Context, cfg *settings.Configuration,
cmdBuilder exe.ICmdBuilder, cmdArgs *parser.Arguments, pkgNames []string,
) error {
if len(pkgNames) == 0 {
return nil
}

arguments := cmdArgs.CopyGlobal()
_ = arguments.AddArg("R")
if err := arguments.AddArg("R", "s", "u"); err != nil {
return err
}
arguments.AddTarget(pkgNames...)

return config.Runtime.CmdBuilder.Show(
config.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
arguments, config.Mode, settings.NoConfirm))
return cmdBuilder.Show(
cmdBuilder.BuildPacmanCmd(ctx,
arguments, cfg.Mode, settings.NoConfirm))
}

func syncClean(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser.Arguments, dbExecutor db.Executor) error {
Expand Down
114 changes: 114 additions & 0 deletions clean_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package main

import (
"context"
"fmt"
"os/exec"
"strings"
"testing"

"github.com/Jguer/go-alpm/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

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

t.Parallel()

testCases := []struct {
name string
args []string
wantShow []string
}{
{
name: "clean",
args: []string{"Y", "c"},
wantShow: []string{"pacman", "-R", "-s", "-u", "--config", "/etc/pacman.conf", "--", "lsp-plugins"},
},
{
name: "clean double",
args: []string{"Y", "c", "c"},
wantShow: []string{"pacman", "-R", "-s", "-u", "--config", "/etc/pacman.conf", "--", "lsp-plugins", "linux-headers"},
},
}

dbExc := &mock.DBExecutor{
PackageOptionalDependsFn: func(i alpm.IPackage) []alpm.Depend {
if i.Name() == "linux" {
return []alpm.Depend{
{
Name: "linux-headers",
},
}
}

return []alpm.Depend{}
},
PackageProvidesFn: func(p alpm.IPackage) []alpm.Depend { return []alpm.Depend{} },
PackageDependsFn: func(p alpm.IPackage) []alpm.Depend { return []alpm.Depend{} },
LocalPackagesFn: func() []mock.IPackage {
return []mock.IPackage{
&mock.Package{
PReason: alpm.PkgReasonExplicit,
PName: "linux",
},
&mock.Package{
PReason: alpm.PkgReasonDepend,
PName: "lsp-plugins",
},
&mock.Package{
PReason: alpm.PkgReasonDepend,
PName: "linux-headers",
},
}
},
}

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

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

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

require.NoError(t, err)

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))
}
})
}
}
15 changes: 10 additions & 5 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/Jguer/yay/v12/pkg/news"
"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"
"github.com/Jguer/yay/v12/pkg/text"
"github.com/Jguer/yay/v12/pkg/upgrade"
Expand Down Expand Up @@ -149,7 +150,9 @@ getpkgbuild specific options:
-p --print Print pkgbuild of packages`)
}

func handleCmd(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser.Arguments, dbExecutor db.Executor) error {
func handleCmd(ctx context.Context, cfg *settings.Configuration,
cmdArgs *parser.Arguments, dbExecutor db.Executor,
) error {
if cmdArgs.ExistsArg("h", "help") {
return handleHelp(ctx, cfg, cmdArgs)
}
Expand Down Expand Up @@ -187,7 +190,8 @@ func handleCmd(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser
case "P", "show":
return handlePrint(ctx, cfg, cmdArgs, dbExecutor)
case "Y", "yay":
return handleYay(ctx, cfg, cmdArgs, dbExecutor, cfg.Runtime.QueryBuilder)
return handleYay(ctx, cfg, cmdArgs, cfg.Runtime.CmdBuilder,
dbExecutor, cfg.Runtime.QueryBuilder)
case "W", "web":
return handleWeb(ctx, cfg, cmdArgs)
}
Expand Down Expand Up @@ -285,15 +289,16 @@ func handlePrint(ctx context.Context, cfg *settings.Configuration, cmdArgs *pars
}

func handleYay(ctx context.Context, cfg *settings.Configuration,
cmdArgs *parser.Arguments, dbExecutor db.Executor, queryBuilder query.Builder,
cmdArgs *parser.Arguments, cmdBuilder exe.ICmdBuilder,
dbExecutor db.Executor, queryBuilder query.Builder,
) error {
switch {
case cmdArgs.ExistsArg("gendb"):
return createDevelDB(ctx, cfg, dbExecutor)
case cmdArgs.ExistsDouble("c"):
return cleanDependencies(ctx, cfg, cmdArgs, dbExecutor, true)
return cleanDependencies(ctx, cfg, cmdBuilder, cmdArgs, dbExecutor, true)
case cmdArgs.ExistsArg("c", "clean"):
return cleanDependencies(ctx, cfg, cmdArgs, dbExecutor, false)
return cleanDependencies(ctx, cfg, cmdBuilder, cmdArgs, dbExecutor, false)
case len(cmdArgs.Targets) > 0:
return displayNumberMenu(ctx, cfg, cmdArgs.Targets, dbExecutor, queryBuilder, cmdArgs)
}
Expand Down
2 changes: 1 addition & 1 deletion install.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func removeMake(ctx context.Context, config *settings.Configuration,
) error {
removeArguments := cmdArgs.CopyGlobal()

err := removeArguments.AddArg("R", "u")
err := removeArguments.AddArg("R", "s", "u")
if err != nil {
return err
}
Expand Down
34 changes: 27 additions & 7 deletions pkg/db/mock/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ type (

type DBExecutor struct {
db.Executor
LocalPackageFn func(string) IPackage
IsCorrectVersionInstalledFn func(string, string) bool
SyncPackageFn func(string) IPackage
PackagesFromGroupFn func(string) []IPackage
LocalSatisfierExistsFn func(string) bool
SyncSatisfierFn func(string) IPackage
AlpmArchitecturesFn func() ([]string, error)
InstalledRemotePackageNamesFn func() []string
InstalledRemotePackagesFn func() map[string]IPackage
SyncUpgradesFn func(bool) (map[string]db.SyncUpgrade, error)
IsCorrectVersionInstalledFn func(string, string) bool
LocalPackageFn func(string) IPackage
LocalPackagesFn func() []IPackage
LocalSatisfierExistsFn func(string) bool
PackageDependsFn func(IPackage) []Depend
PackageOptionalDependsFn func(alpm.IPackage) []alpm.Depend
PackageProvidesFn func(IPackage) []Depend
PackagesFromGroupFn func(string) []IPackage
RefreshHandleFn func() error
ReposFn func() []string
SyncPackageFn func(string) IPackage
SyncSatisfierFn func(string) IPackage
SyncUpgradesFn func(bool) (map[string]db.SyncUpgrade, error)
}

func (t *DBExecutor) InstalledRemotePackageNames() []string {
Expand Down Expand Up @@ -79,6 +83,10 @@ func (t *DBExecutor) LocalPackage(s string) IPackage {
}

func (t *DBExecutor) LocalPackages() []IPackage {
if t.LocalPackagesFn != nil {
return t.LocalPackagesFn()
}

panic("implement me")
}

Expand All @@ -94,6 +102,10 @@ func (t *DBExecutor) PackageConflicts(iPackage IPackage) []Depend {
}

func (t *DBExecutor) PackageDepends(iPackage IPackage) []Depend {
if t.PackageDependsFn != nil {
return t.PackageDependsFn(iPackage)
}

panic("implement me")
}

Expand All @@ -102,10 +114,18 @@ func (t *DBExecutor) PackageGroups(iPackage IPackage) []string {
}

func (t *DBExecutor) PackageOptionalDepends(iPackage IPackage) []Depend {
if t.PackageOptionalDependsFn != nil {
return t.PackageOptionalDependsFn(iPackage)
}

panic("implement me")
}

func (t *DBExecutor) PackageProvides(iPackage IPackage) []Depend {
if t.PackageProvidesFn != nil {
return t.PackageProvidesFn(iPackage)
}

panic("implement me")
}

Expand Down