1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/commands/git_commands/deps_test.go

180 lines
4.9 KiB
Go
Raw Normal View History

2022-01-08 05:00:36 +02:00
package git_commands
2022-01-08 04:22:29 +02:00
import (
"os"
2022-01-08 04:22:29 +02:00
"github.com/go-errors/errors"
2022-01-08 05:44:07 +02:00
gogit "github.com/jesseduffield/go-git/v5"
2022-01-08 04:22:29 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2023-05-19 12:18:02 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/patch"
2022-01-08 04:22:29 +02:00
"github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/utils"
)
type commonDeps struct {
runner *oscommands.FakeCmdObjRunner
userConfig *config.UserConfig
gitVersion *GitVersion
2022-01-08 04:22:29 +02:00
gitConfig *git_config.FakeGitConfig
getenv func(string) string
removeFile func(string) error
dotGitDir string
common *common.Common
cmd *oscommands.CmdObjBuilder
}
func buildGitCommon(deps commonDeps) *GitCommon {
gitCommon := &GitCommon{}
2022-01-08 04:22:29 +02:00
gitCommon.Common = deps.common
if gitCommon.Common == nil {
gitCommon.Common = utils.NewDummyCommonWithUserConfig(deps.userConfig)
2022-01-08 04:22:29 +02:00
}
runner := deps.runner
if runner == nil {
runner = oscommands.NewFakeRunner(nil)
2022-01-08 04:22:29 +02:00
}
cmd := deps.cmd
// gotta check deps.cmd because it's not an interface type and an interface value of nil is not considered to be nil
if cmd == nil {
cmd = oscommands.NewDummyCmdObjBuilder(runner)
2022-01-08 04:22:29 +02:00
}
gitCommon.cmd = cmd
2022-01-08 04:22:29 +02:00
gitCommon.Common.UserConfig = deps.userConfig
if gitCommon.Common.UserConfig == nil {
gitCommon.Common.UserConfig = config.GetDefaultConfig()
2022-01-08 04:22:29 +02:00
}
gitCommon.version = deps.gitVersion
if gitCommon.version == nil {
gitCommon.version = &GitVersion{2, 0, 0, ""}
}
gitConfig := deps.gitConfig
if gitConfig == nil {
gitConfig = git_config.NewFakeGitConfig(nil)
2022-01-08 04:22:29 +02:00
}
gitCommon.repo = buildRepo()
gitCommon.config = NewConfigCommands(gitCommon.Common, gitConfig, gitCommon.repo)
2022-01-08 04:22:29 +02:00
getenv := deps.getenv
if getenv == nil {
getenv = func(string) string { return "" }
2022-01-08 04:22:29 +02:00
}
removeFile := deps.removeFile
if removeFile == nil {
removeFile = func(string) error { return errors.New("unexpected call to removeFile") }
}
2022-01-08 04:22:29 +02:00
gitCommon.os = oscommands.NewDummyOSCommandWithDeps(oscommands.OSCommandDeps{
Common: gitCommon.Common,
GetenvFn: getenv,
Cmd: cmd,
RemoveFileFn: removeFile,
TempDir: os.TempDir(),
})
2022-01-08 04:22:29 +02:00
gitCommon.dotGitDir = deps.dotGitDir
if gitCommon.dotGitDir == "" {
gitCommon.dotGitDir = ".git"
}
2022-01-08 05:44:07 +02:00
return gitCommon
2022-01-08 04:22:29 +02:00
}
func buildRepo() *gogit.Repository {
// TODO: think of a way to actually mock this out
var repo *gogit.Repository = nil
return repo
2022-01-08 04:22:29 +02:00
}
func buildFileLoader(gitCommon *GitCommon) *FileLoader {
return NewFileLoader(gitCommon.Common, gitCommon.cmd, gitCommon.config)
2022-01-08 04:22:29 +02:00
}
func buildSubmoduleCommands(deps commonDeps) *SubmoduleCommands {
gitCommon := buildGitCommon(deps)
2022-01-08 04:22:29 +02:00
return NewSubmoduleCommands(gitCommon)
2022-01-08 04:22:29 +02:00
}
func buildCommitCommands(deps commonDeps) *CommitCommands {
gitCommon := buildGitCommon(deps)
return NewCommitCommands(gitCommon)
2022-01-08 04:22:29 +02:00
}
func buildWorkingTreeCommands(deps commonDeps) *WorkingTreeCommands {
gitCommon := buildGitCommon(deps)
2022-01-08 04:22:29 +02:00
submoduleCommands := buildSubmoduleCommands(deps)
fileLoader := buildFileLoader(gitCommon)
2022-01-08 04:22:29 +02:00
return NewWorkingTreeCommands(gitCommon, submoduleCommands, fileLoader)
2022-01-08 04:22:29 +02:00
}
func buildPatchCommands(deps commonDeps) *PatchCommands { //nolint:golint,unused
2023-05-19 12:18:02 +02:00
gitCommon := buildGitCommon(deps)
rebaseCommands := buildRebaseCommands(deps)
commitCommands := buildCommitCommands(deps)
statusCommands := buildStatusCommands(deps)
stashCommands := buildStashCommands(deps)
loadFileFn := func(from string, to string, reverse bool, filename string, plain bool) (string, error) {
return "", nil
}
patchBuilder := patch.NewPatchBuilder(gitCommon.Log, loadFileFn)
return NewPatchCommands(gitCommon, rebaseCommands, commitCommands, statusCommands, stashCommands, patchBuilder)
}
func buildStatusCommands(deps commonDeps) *StatusCommands { //nolint:golint,unused
2023-05-19 12:18:02 +02:00
gitCommon := buildGitCommon(deps)
return NewStatusCommands(gitCommon)
}
2022-01-08 04:22:29 +02:00
func buildStashCommands(deps commonDeps) *StashCommands {
gitCommon := buildGitCommon(deps)
fileLoader := buildFileLoader(gitCommon)
2022-01-08 04:22:29 +02:00
workingTreeCommands := buildWorkingTreeCommands(deps)
return NewStashCommands(gitCommon, fileLoader, workingTreeCommands)
2022-01-08 04:22:29 +02:00
}
func buildRebaseCommands(deps commonDeps) *RebaseCommands {
gitCommon := buildGitCommon(deps)
2022-01-08 04:22:29 +02:00
workingTreeCommands := buildWorkingTreeCommands(deps)
commitCommands := buildCommitCommands(deps)
return NewRebaseCommands(gitCommon, commitCommands, workingTreeCommands)
2022-01-08 04:22:29 +02:00
}
func buildSyncCommands(deps commonDeps) *SyncCommands {
gitCommon := buildGitCommon(deps)
2022-01-08 04:22:29 +02:00
return NewSyncCommands(gitCommon)
2022-01-08 04:22:29 +02:00
}
func buildFileCommands(deps commonDeps) *FileCommands {
gitCommon := buildGitCommon(deps)
return NewFileCommands(gitCommon)
}
func buildBranchCommands(deps commonDeps) *BranchCommands {
gitCommon := buildGitCommon(deps)
2022-01-08 04:22:29 +02:00
return NewBranchCommands(gitCommon)
2022-01-08 04:22:29 +02:00
}
2023-05-20 08:55:07 +02:00
func buildFlowCommands(deps commonDeps) *FlowCommands {
gitCommon := buildGitCommon(deps)
return NewFlowCommands(gitCommon)
}