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

145 lines
3.8 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 (
"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/loaders"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"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
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
}
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,
})
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) *loaders.FileLoader {
return loaders.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 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
}