Files
yay/pkg/settings/exe/mock.go
T
Joey Holtzmanandjguer 04c76a404e feat(install): add --keepsrc to keep pkg/ and src/ directories (#2272)
* feat(install): add --nocleanbuild to keep pkg/ and src/ directories for
AUR packages

Providing this flag during installation of AUR packages allows for keeping
the src/ and pkg/ directories produced my makepkg. If the user wants to
delete the directories, they can either select to cleanBuild in the
cleanmenu or run the installation without the --nocleanbuild flag (yay
will only remove the directories if the package is rebuilt)

* fix(completion): simplify description for --nocleanbuild in fish

This makes the description consistent with the descriptions in the
man page, --help, and zsh completion.

* refactor(install): Rename --nocleanbuild to --keepsrc

This naming scheme is more familiar to users since it is the name of the
flag in Paru.

---------

Co-authored-by: jguer <me@jguer.space>
2023-09-18 09:21:42 +02:00

137 lines
2.9 KiB
Go

package exe
import (
"context"
"fmt"
"os/exec"
"sync"
"github.com/Jguer/yay/v12/pkg/settings/parser"
)
type Call struct {
Res []interface{}
Args []interface{}
Dir string
}
func (c *Call) String() string {
return fmt.Sprintf("%+v", c.Args)
}
type MockBuilder struct {
Runner Runner
BuildMakepkgCmdCallsMu sync.Mutex
BuildMakepkgCmdCalls []Call
BuildMakepkgCmdFn func(ctx context.Context, dir string, extraArgs ...string) *exec.Cmd
BuildPacmanCmdFn func(ctx context.Context, args *parser.Arguments, mode parser.TargetMode, noConfirm bool) *exec.Cmd
GetKeepSrcFn func() bool
}
type MockRunner struct {
ShowCallsMu sync.Mutex
ShowCalls []Call
CaptureCallsMu sync.Mutex
CaptureCalls []Call
ShowFn func(cmd *exec.Cmd) error
CaptureFn func(cmd *exec.Cmd) (stdout string, stderr string, err error)
}
func (m *MockBuilder) BuildGPGCmd(ctx context.Context, extraArgs ...string) *exec.Cmd {
return exec.CommandContext(ctx, "gpg", extraArgs...)
}
func (m *MockBuilder) BuildMakepkgCmd(ctx context.Context, dir string, extraArgs ...string) *exec.Cmd {
var res *exec.Cmd
if m.BuildMakepkgCmdFn != nil {
res = m.BuildMakepkgCmdFn(ctx, dir, extraArgs...)
} else {
res = exec.CommandContext(ctx, "makepkg", extraArgs...)
}
m.BuildMakepkgCmdCallsMu.Lock()
m.BuildMakepkgCmdCalls = append(m.BuildMakepkgCmdCalls, Call{
Res: []interface{}{res},
Args: []interface{}{
ctx,
dir,
extraArgs,
},
})
m.BuildMakepkgCmdCallsMu.Unlock()
return res
}
func (m *MockBuilder) AddMakepkgFlag(flag string) {
}
func (m *MockBuilder) BuildGitCmd(ctx context.Context, dir string, extraArgs ...string) *exec.Cmd {
return exec.CommandContext(ctx, "git", extraArgs...)
}
func (m *MockBuilder) BuildPacmanCmd(ctx context.Context, args *parser.Arguments, mode parser.TargetMode, noConfirm bool) *exec.Cmd {
var res *exec.Cmd
if m.BuildPacmanCmdFn != nil {
res = m.BuildPacmanCmdFn(ctx, args, mode, noConfirm)
} else {
res = exec.CommandContext(ctx, "pacman")
}
return res
}
func (m *MockBuilder) SetPacmanDBPath(path string) {
}
func (m *MockBuilder) SudoLoop() {
}
func (m *MockBuilder) Capture(cmd *exec.Cmd) (stdout, stderr string, err error) {
return m.Runner.Capture(cmd)
}
func (m *MockBuilder) Show(cmd *exec.Cmd) error {
return m.Runner.Show(cmd)
}
func (m *MockBuilder) GetKeepSrc() bool {
return false
}
func (m *MockRunner) Capture(cmd *exec.Cmd) (stdout, stderr string, err error) {
m.CaptureCallsMu.Lock()
m.CaptureCalls = append(m.CaptureCalls, Call{
Args: []interface{}{
cmd,
},
Dir: cmd.Dir,
})
m.CaptureCallsMu.Unlock()
if m.CaptureFn != nil {
return m.CaptureFn(cmd)
}
return "", "", nil
}
func (m *MockRunner) Show(cmd *exec.Cmd) error {
var err error
if m.ShowFn != nil {
err = m.ShowFn(cmd)
}
m.ShowCallsMu.Lock()
m.ShowCalls = append(m.ShowCalls, Call{
Args: []interface{}{
cmd,
},
Dir: cmd.Dir,
})
m.ShowCallsMu.Unlock()
return err
}