1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-05-31 23:19:40 +02:00

fix tests

This commit is contained in:
Jesse Duffield 2022-01-08 13:22:29 +11:00
parent c6b57d9b57
commit 3621854dc7
16 changed files with 373 additions and 149 deletions

View File

@ -174,3 +174,7 @@ func (self *BranchCommands) Merge(branchName string, opts MergeOpts) error {
return self.cmd.New(command).Run()
}
func (self *BranchCommands) AllBranchesLogCmdObj() oscommands.ICmdObj {
return self.cmd.New(self.UserConfig.Git.AllBranchesLogCmd).DontLog()
}

View File

@ -5,10 +5,16 @@ import (
"github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/stretchr/testify/assert"
)
func TestGitCommandGetCommitDifferences(t *testing.T) {
func NewBranchCommandsWithRunner(runner *oscommands.FakeCmdObjRunner) *BranchCommands {
builder := oscommands.NewDummyCmdObjBuilder(runner)
return NewBranchCommands(utils.NewDummyCommon(), builder)
}
func TestBranchGetCommitDifferences(t *testing.T) {
type scenario struct {
testName string
runner *oscommands.FakeCmdObjRunner
@ -41,8 +47,8 @@ func TestGitCommandGetCommitDifferences(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
pushables, pullables := gitCmd.Branch.GetCommitDifferences("HEAD", "@{u}")
instance := NewBranchCommandsWithRunner(s.runner)
pushables, pullables := instance.GetCommitDifferences("HEAD", "@{u}")
assert.EqualValues(t, s.expectedPushables, pushables)
assert.EqualValues(t, s.expectedPullables, pullables)
s.runner.CheckForMissingCalls()
@ -50,16 +56,16 @@ func TestGitCommandGetCommitDifferences(t *testing.T) {
}
}
func TestGitCommandNewBranch(t *testing.T) {
func TestBranchNewBranch(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
Expect(`git checkout -b "test" "master"`, "", nil)
gitCmd := NewDummyGitCommandWithRunner(runner)
instance := NewBranchCommandsWithRunner(runner)
assert.NoError(t, gitCmd.Branch.New("test", "master"))
assert.NoError(t, instance.New("test", "master"))
runner.CheckForMissingCalls()
}
func TestGitCommandDeleteBranch(t *testing.T) {
func TestBranchDeleteBranch(t *testing.T) {
type scenario struct {
testName string
force bool
@ -88,24 +94,24 @@ func TestGitCommandDeleteBranch(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
instance := NewBranchCommandsWithRunner(s.runner)
s.test(gitCmd.Branch.Delete("test", s.force))
s.test(instance.Delete("test", s.force))
s.runner.CheckForMissingCalls()
})
}
}
func TestGitCommandMerge(t *testing.T) {
func TestBranchMerge(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
Expect(`git merge --no-edit "test"`, "", nil)
gitCmd := NewDummyGitCommandWithRunner(runner)
instance := NewBranchCommandsWithRunner(runner)
assert.NoError(t, gitCmd.Branch.Merge("test", MergeOpts{}))
assert.NoError(t, instance.Merge("test", MergeOpts{}))
runner.CheckForMissingCalls()
}
func TestGitCommandCheckout(t *testing.T) {
func TestBranchCheckout(t *testing.T) {
type scenario struct {
testName string
runner *oscommands.FakeCmdObjRunner
@ -134,33 +140,32 @@ func TestGitCommandCheckout(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.Branch.Checkout("test", CheckoutOptions{Force: s.force}))
instance := NewBranchCommandsWithRunner(s.runner)
s.test(instance.Checkout("test", CheckoutOptions{Force: s.force}))
s.runner.CheckForMissingCalls()
})
}
}
func TestGitCommandGetBranchGraph(t *testing.T) {
func TestBranchGetBranchGraph(t *testing.T) {
runner := oscommands.NewFakeRunner(t).ExpectGitArgs([]string{
"log", "--graph", "--color=always", "--abbrev-commit", "--decorate", "--date=relative", "--pretty=medium", "test", "--",
}, "", nil)
gitCmd := NewDummyGitCommandWithRunner(runner)
_, err := gitCmd.Branch.GetGraph("test")
instance := NewBranchCommandsWithRunner(runner)
_, err := instance.GetGraph("test")
assert.NoError(t, err)
}
func TestGitCommandGetAllBranchGraph(t *testing.T) {
func TestBranchGetAllBranchGraph(t *testing.T) {
runner := oscommands.NewFakeRunner(t).ExpectGitArgs([]string{
"log", "--graph", "--all", "--color=always", "--abbrev-commit", "--decorate", "--date=relative", "--pretty=medium",
}, "", nil)
gitCmd := NewDummyGitCommandWithRunner(runner)
cmdStr := gitCmd.UserConfig.Git.AllBranchesLogCmd
_, err := gitCmd.Cmd.New(cmdStr).RunWithOutput()
instance := NewBranchCommandsWithRunner(runner)
err := instance.AllBranchesLogCmdObj().Run()
assert.NoError(t, err)
}
func TestGitCommandCurrentBranchName(t *testing.T) {
func TestBranchCurrentBranchName(t *testing.T) {
type scenario struct {
testName string
runner *oscommands.FakeCmdObjRunner
@ -214,8 +219,8 @@ func TestGitCommandCurrentBranchName(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.Branch.CurrentBranchName())
instance := NewBranchCommandsWithRunner(s.runner)
s.test(instance.CurrentBranchName())
s.runner.CheckForMissingCalls()
})
}

View File

@ -4,66 +4,91 @@ import (
"testing"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/stretchr/testify/assert"
)
func TestGitCommandRewordCommit(t *testing.T) {
func TestCommitRewordCommit(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"commit", "--allow-empty", "--amend", "--only", "-m", "test"}, "", nil)
gitCmd := NewDummyGitCommandWithRunner(runner)
instance := buildCommitCommands(commonDeps{runner: runner})
assert.NoError(t, gitCmd.Commit.RewordLastCommit("test"))
assert.NoError(t, instance.RewordLastCommit("test"))
runner.CheckForMissingCalls()
}
func TestGitCommandResetToCommit(t *testing.T) {
func TestCommitResetToCommit(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"reset", "--hard", "78976bc"}, "", nil)
gitCmd := NewDummyGitCommandWithRunner(runner)
assert.NoError(t, gitCmd.Commit.ResetToCommit("78976bc", "hard", []string{}))
instance := buildCommitCommands(commonDeps{runner: runner})
assert.NoError(t, instance.ResetToCommit("78976bc", "hard", []string{}))
runner.CheckForMissingCalls()
}
func TestGitCommandCommitObj(t *testing.T) {
func TestCommitCommitObj(t *testing.T) {
type scenario struct {
testName string
message string
flags string
expected string
testName string
message string
configSignoff bool
configSkipHookPrefix string
expected string
}
scenarios := []scenario{
{
testName: "Commit",
message: "test",
flags: "",
expected: `git commit -m "test"`,
testName: "Commit",
message: "test",
configSignoff: false,
configSkipHookPrefix: "",
expected: `git commit -m "test"`,
},
{
testName: "Commit with --no-verify flag",
message: "test",
flags: "--no-verify",
expected: `git commit --no-verify -m "test"`,
testName: "Commit with --no-verify flag",
message: "WIP: test",
configSignoff: false,
configSkipHookPrefix: "WIP",
expected: `git commit --no-verify -m "WIP: test"`,
},
{
testName: "Commit with multiline message",
message: "line1\nline2",
flags: "",
expected: `git commit -m "line1" -m "line2"`,
testName: "Commit with multiline message",
message: "line1\nline2",
configSignoff: false,
configSkipHookPrefix: "",
expected: `git commit -m "line1" -m "line2"`,
},
{
testName: "Commit with signoff",
message: "test",
configSignoff: true,
configSkipHookPrefix: "",
expected: `git commit --signoff -m "test"`,
},
{
testName: "Commit with signoff and no-verify",
message: "WIP: test",
configSignoff: true,
configSkipHookPrefix: "WIP",
expected: `git commit --no-verify --signoff -m "WIP: test"`,
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
cmdStr := gitCmd.Commit.CommitCmdObj(s.message, s.flags).ToString()
userConfig := config.GetDefaultConfig()
userConfig.Git.Commit.SignOff = s.configSignoff
userConfig.Git.SkipHookPrefix = s.configSkipHookPrefix
instance := buildCommitCommands(commonDeps{userConfig: userConfig})
cmdStr := instance.CommitCmdObj(s.message).ToString()
assert.Equal(t, s.expected, cmdStr)
})
}
}
func TestGitCommandCreateFixupCommit(t *testing.T) {
func TestCommitCreateFixupCommit(t *testing.T) {
type scenario struct {
testName string
sha string
@ -85,14 +110,14 @@ func TestGitCommandCreateFixupCommit(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.Commit.CreateFixupCommit(s.sha))
instance := buildCommitCommands(commonDeps{runner: s.runner})
s.test(instance.CreateFixupCommit(s.sha))
s.runner.CheckForMissingCalls()
})
}
}
func TestGitCommandShowCmdObj(t *testing.T) {
func TestCommitShowCmdObj(t *testing.T) {
type scenario struct {
testName string
filterPath string
@ -123,9 +148,12 @@ func TestGitCommandShowCmdObj(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.UserConfig.Git.DiffContextSize = s.contextSize
cmdStr := gitCmd.Commit.ShowCmdObj("1234567890", s.filterPath).ToString()
userConfig := config.GetDefaultConfig()
userConfig.Git.DiffContextSize = s.contextSize
instance := buildCommitCommands(commonDeps{userConfig: userConfig})
cmdStr := instance.ShowCmdObj("1234567890", s.filterPath).ToString()
assert.Equal(t, s.expected, cmdStr)
})
}

137
pkg/commands/deps_test.go Normal file
View File

@ -0,0 +1,137 @@
package commands
import (
"github.com/go-errors/errors"
"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 completeDeps(deps commonDeps) commonDeps {
if deps.runner == nil {
deps.runner = oscommands.NewFakeRunner(nil)
}
if deps.userConfig == nil {
deps.userConfig = config.GetDefaultConfig()
}
if deps.gitConfig == nil {
deps.gitConfig = git_config.NewFakeGitConfig(nil)
}
if deps.getenv == nil {
deps.getenv = func(string) string { return "" }
}
if deps.removeFile == nil {
deps.removeFile = func(string) error { return errors.New("unexpected call to removeFile") }
}
if deps.dotGitDir == "" {
deps.dotGitDir = ".git"
}
if deps.common == nil {
deps.common = utils.NewDummyCommonWithUserConfig(deps.userConfig)
}
if deps.cmd == nil {
deps.cmd = oscommands.NewDummyCmdObjBuilder(deps.runner)
}
return deps
}
func buildConfigCommands(deps commonDeps) *ConfigCommands {
deps = completeDeps(deps)
common := utils.NewDummyCommonWithUserConfig(deps.userConfig)
return NewConfigCommands(common, deps.gitConfig)
}
func buildOSCommand(deps commonDeps) *oscommands.OSCommand {
deps = completeDeps(deps)
return oscommands.NewDummyOSCommandWithDeps(oscommands.OSCommandDeps{
Common: deps.common,
GetenvFn: deps.getenv,
Cmd: deps.cmd,
RemoveFileFn: deps.removeFile,
})
}
func buildFileLoader(deps commonDeps) *loaders.FileLoader {
deps = completeDeps(deps)
configCommands := buildConfigCommands(deps)
return loaders.NewFileLoader(deps.common, deps.cmd, configCommands)
}
func buildSubmoduleCommands(deps commonDeps) *SubmoduleCommands {
deps = completeDeps(deps)
return NewSubmoduleCommands(deps.common, deps.cmd, deps.dotGitDir)
}
func buildCommitCommands(deps commonDeps) *CommitCommands {
deps = completeDeps(deps)
return NewCommitCommands(deps.common, deps.cmd)
}
func buildWorkingTreeCommands(deps commonDeps) *WorkingTreeCommands {
deps = completeDeps(deps)
osCommand := buildOSCommand(deps)
submoduleCommands := buildSubmoduleCommands(deps)
fileLoader := buildFileLoader(deps)
return NewWorkingTreeCommands(deps.common, deps.cmd, submoduleCommands, osCommand, fileLoader)
}
func buildStashCommands(deps commonDeps) *StashCommands {
deps = completeDeps(deps)
osCommand := buildOSCommand(deps)
fileLoader := buildFileLoader(deps)
workingTreeCommands := buildWorkingTreeCommands(deps)
return NewStashCommands(deps.common, deps.cmd, osCommand, fileLoader, workingTreeCommands)
}
func buildRebaseCommands(deps commonDeps) *RebaseCommands {
deps = completeDeps(deps)
configCommands := buildConfigCommands(deps)
osCommand := buildOSCommand(deps)
workingTreeCommands := buildWorkingTreeCommands(deps)
commitCommands := buildCommitCommands(deps)
return NewRebaseCommands(deps.common, deps.cmd, osCommand, commitCommands, workingTreeCommands, configCommands, deps.dotGitDir)
}
func buildSyncCommands(deps commonDeps) *SyncCommands {
deps = completeDeps(deps)
return NewSyncCommands(deps.common, deps.cmd)
}
func buildFileCommands(deps commonDeps) *FileCommands {
deps = completeDeps(deps)
configCommands := buildConfigCommands(deps)
osCommand := buildOSCommand(deps)
return NewFileCommands(deps.common, deps.cmd, configCommands, osCommand)
}

View File

@ -51,7 +51,6 @@ func (c *FileCommands) GetEditCmdStr(filename string, lineNumber int) (string, e
if editor == "" {
editor = c.config.GetCoreEditor()
}
if editor == "" {
editor = c.os.Getenv("GIT_EDITOR")
}

View File

@ -6,6 +6,7 @@ import (
"github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/stretchr/testify/assert"
)
@ -145,12 +146,18 @@ func TestEditFileCmdStr(t *testing.T) {
}
for _, s := range scenarios {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
gitCmd.UserConfig.OS.EditCommand = s.configEditCommand
gitCmd.UserConfig.OS.EditCommandTemplate = s.configEditCommandTemplate
gitCmd.OSCommand.GetenvFn = s.getenv
gitCmd.gitConfig = git_config.NewFakeGitConfig(s.gitConfigMockResponses)
s.test(gitCmd.File.GetEditCmdStr(s.filename, 1))
userConfig := config.GetDefaultConfig()
userConfig.OS.EditCommand = s.configEditCommand
userConfig.OS.EditCommandTemplate = s.configEditCommandTemplate
instance := buildFileCommands(commonDeps{
runner: s.runner,
userConfig: userConfig,
gitConfig: git_config.NewFakeGitConfig(s.gitConfigMockResponses),
getenv: s.getenv,
})
s.test(instance.GetEditCmdStr(s.filename, 1))
s.runner.CheckForMissingCalls()
}
}

View File

@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
)
func TestGitCommandGetStatusFiles(t *testing.T) {
func TestFileGetStatusFiles(t *testing.T) {
type scenario struct {
testName string
runner oscommands.ICmdObjRunner

View File

@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/assert"
)
func TestGitCommandGetStashEntries(t *testing.T) {
func TestGetStashEntries(t *testing.T) {
type scenario struct {
testName string
filterPath string

View File

@ -1,6 +1,7 @@
package oscommands
import (
"github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/utils"
)
@ -11,6 +12,34 @@ func NewDummyOSCommand() *OSCommand {
return osCmd
}
type OSCommandDeps struct {
Common *common.Common
Platform *Platform
GetenvFn func(string) string
RemoveFileFn func(string) error
Cmd *CmdObjBuilder
}
func NewDummyOSCommandWithDeps(deps OSCommandDeps) *OSCommand {
common := deps.Common
if common == nil {
common = utils.NewDummyCommon()
}
platform := deps.Platform
if platform == nil {
platform = dummyPlatform
}
return &OSCommand{
Common: common,
Platform: platform,
getenvFn: deps.GetenvFn,
removeFileFn: deps.RemoveFileFn,
guiIO: NewNullGuiIO(utils.NewDummyLog()),
}
}
func NewDummyCmdObjBuilder(runner ICmdObjRunner) *CmdObjBuilder {
return &CmdObjBuilder{
runner: runner,

View File

@ -20,10 +20,10 @@ import (
type OSCommand struct {
*common.Common
Platform *Platform
GetenvFn func(string) string
getenvFn func(string) string
guiIO *guiIO
removeFile func(string) error
removeFileFn func(string) error
Cmd *CmdObjBuilder
}
@ -40,11 +40,11 @@ type Platform struct {
// NewOSCommand os command runner
func NewOSCommand(common *common.Common, platform *Platform, guiIO *guiIO) *OSCommand {
c := &OSCommand{
Common: common,
Platform: platform,
GetenvFn: os.Getenv,
removeFile: os.RemoveAll,
guiIO: guiIO,
Common: common,
Platform: platform,
getenvFn: os.Getenv,
removeFileFn: os.RemoveAll,
guiIO: guiIO,
}
runner := &cmdObjRunner{log: common.Log, guiIO: guiIO}
@ -59,11 +59,6 @@ func (c *OSCommand) LogCommand(cmdStr string, commandLine bool) {
c.guiIO.logCommandFn(cmdStr, commandLine)
}
// To be used for testing only
func (c *OSCommand) SetRemoveFile(f func(string) error) {
c.removeFile = f
}
// FileType tells us if the file is a file, directory or other
func FileType(path string) string {
fileInfo, err := os.Stat(path)
@ -253,11 +248,11 @@ func (c *OSCommand) CopyToClipboard(str string) error {
func (c *OSCommand) RemoveFile(path string) error {
c.LogCommand(fmt.Sprintf("Deleting path '%s'", path), false)
return c.removeFile(path)
return c.removeFileFn(path)
}
func (c *OSCommand) Getenv(key string) string {
return c.GetenvFn(key)
return c.getenvFn(key)
}
func GetTempDir() string {

View File

@ -12,7 +12,7 @@ import (
"github.com/stretchr/testify/assert"
)
func TestGitCommandRebaseBranch(t *testing.T) {
func TestRebaseRebaseBranch(t *testing.T) {
type scenario struct {
testName string
arg string
@ -43,15 +43,15 @@ func TestGitCommandRebaseBranch(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.Rebase.RebaseBranch(s.arg))
instance := buildRebaseCommands(commonDeps{runner: s.runner})
s.test(instance.RebaseBranch(s.arg))
})
}
}
// TestGitCommandSkipEditorCommand confirms that SkipEditorCommand injects
// TestRebaseSkipEditorCommand confirms that SkipEditorCommand injects
// environment variables that suppress an interactive editor
func TestGitCommandSkipEditorCommand(t *testing.T) {
func TestRebaseSkipEditorCommand(t *testing.T) {
commandStr := "git blah"
runner := oscommands.NewFakeRunner(t).ExpectFunc(func(cmdObj oscommands.ICmdObj) (string, error) {
assert.Equal(t, commandStr, cmdObj.ToString())
@ -71,13 +71,13 @@ func TestGitCommandSkipEditorCommand(t *testing.T) {
}
return "", nil
})
gitCmd := NewDummyGitCommandWithRunner(runner)
err := gitCmd.Rebase.runSkipEditorCommand(commandStr)
instance := buildRebaseCommands(commonDeps{runner: runner})
err := instance.runSkipEditorCommand(instance.cmd.New(commandStr))
assert.NoError(t, err)
runner.CheckForMissingCalls()
}
func TestGitCommandDiscardOldFileChanges(t *testing.T) {
func TestRebaseDiscardOldFileChanges(t *testing.T) {
type scenario struct {
testName string
gitConfigMockResponses map[string]string
@ -136,9 +136,12 @@ func TestGitCommandDiscardOldFileChanges(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
gitCmd.gitConfig = git_config.NewFakeGitConfig(s.gitConfigMockResponses)
s.test(gitCmd.Rebase.DiscardOldFileChanges(s.commits, s.commitIndex, s.fileName))
instance := buildRebaseCommands(commonDeps{
runner: s.runner,
gitConfig: git_config.NewFakeGitConfig(s.gitConfigMockResponses),
})
s.test(instance.DiscardOldFileChanges(s.commits, s.commitIndex, s.fileName))
s.runner.CheckForMissingCalls()
})
}

View File

@ -4,46 +4,47 @@ import (
"testing"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/stretchr/testify/assert"
)
func TestGitCommandStashDrop(t *testing.T) {
func TestStashDrop(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"stash", "drop", "stash@{1}"}, "", nil)
gitCmd := NewDummyGitCommandWithRunner(runner)
instance := buildStashCommands(commonDeps{runner: runner})
assert.NoError(t, gitCmd.Stash.Drop(1))
assert.NoError(t, instance.Drop(1))
runner.CheckForMissingCalls()
}
func TestGitCommandStashApply(t *testing.T) {
func TestStashApply(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"stash", "apply", "stash@{1}"}, "", nil)
gitCmd := NewDummyGitCommandWithRunner(runner)
instance := buildStashCommands(commonDeps{runner: runner})
assert.NoError(t, gitCmd.Stash.Apply(1))
assert.NoError(t, instance.Apply(1))
runner.CheckForMissingCalls()
}
func TestGitCommandStashPop(t *testing.T) {
func TestStashPop(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"stash", "pop", "stash@{1}"}, "", nil)
gitCmd := NewDummyGitCommandWithRunner(runner)
instance := buildStashCommands(commonDeps{runner: runner})
assert.NoError(t, gitCmd.Stash.Pop(1))
assert.NoError(t, instance.Pop(1))
runner.CheckForMissingCalls()
}
func TestGitCommandStashSave(t *testing.T) {
func TestStashSave(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"stash", "save", "A stash message"}, "", nil)
gitCmd := NewDummyGitCommandWithRunner(runner)
instance := buildStashCommands(commonDeps{runner: runner})
assert.NoError(t, gitCmd.Stash.Save("A stash message"))
assert.NoError(t, instance.Save("A stash message"))
runner.CheckForMissingCalls()
}
func TestGitCommandShowStashEntryCmdObj(t *testing.T) {
func TestStashStashEntryCmdObj(t *testing.T) {
type scenario struct {
testName string
index int
@ -68,9 +69,11 @@ func TestGitCommandShowStashEntryCmdObj(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
gitCmd.UserConfig.Git.DiffContextSize = s.contextSize
cmdStr := gitCmd.Stash.ShowStashEntryCmdObj(s.index).ToString()
userConfig := config.GetDefaultConfig()
userConfig.Git.DiffContextSize = s.contextSize
instance := buildStashCommands(commonDeps{userConfig: userConfig})
cmdStr := instance.ShowStashEntryCmdObj(s.index).ToString()
assert.Equal(t, s.expected, cmdStr)
})
}

View File

@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)
func TestGitCommandPush(t *testing.T) {
func TestSyncPush(t *testing.T) {
type scenario struct {
testName string
opts PushOpts
@ -86,8 +86,8 @@ func TestGitCommandPush(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(oscommands.NewFakeRunner(t))
s.test(gitCmd.Sync.PushCmdObj(s.opts))
instance := buildSyncCommands(commonDeps{})
s.test(instance.PushCmdObj(s.opts))
})
}
}

View File

@ -9,19 +9,21 @@ import (
"github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/stretchr/testify/assert"
)
func TestGitCommandStageFile(t *testing.T) {
func TestWorkingTreeStageFile(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
ExpectArgs([]string{"git", "add", "--", "test.txt"}, "", nil)
gitCmd := NewDummyGitCommandWithRunner(runner)
assert.NoError(t, gitCmd.WorkingTree.StageFile("test.txt"))
instance := buildWorkingTreeCommands(commonDeps{runner: runner})
assert.NoError(t, instance.StageFile("test.txt"))
runner.CheckForMissingCalls()
}
func TestGitCommandUnstageFile(t *testing.T) {
func TestWorkingTreeUnstageFile(t *testing.T) {
type scenario struct {
testName string
reset bool
@ -52,8 +54,8 @@ func TestGitCommandUnstageFile(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.WorkingTree.UnStageFile([]string{"test.txt"}, s.reset))
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
s.test(instance.UnStageFile([]string{"test.txt"}, s.reset))
})
}
}
@ -61,7 +63,7 @@ func TestGitCommandUnstageFile(t *testing.T) {
// these tests don't cover everything, in part because we already have an integration
// test which does cover everything. I don't want to unnecessarily assert on the 'how'
// when the 'what' is what matters
func TestGitCommandDiscardAllFileChanges(t *testing.T) {
func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
type scenario struct {
testName string
file *models.File
@ -180,9 +182,8 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
gitCmd.OSCommand.SetRemoveFile(s.removeFile)
err := gitCmd.WorkingTree.DiscardAllFileChanges(s.file)
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner, removeFile: s.removeFile})
err := instance.DiscardAllFileChanges(s.file)
if s.expectedError == "" {
assert.Nil(t, err)
@ -194,7 +195,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
}
}
func TestGitCommandDiff(t *testing.T) {
func TestWorkingTreeDiff(t *testing.T) {
type scenario struct {
testName string
file *models.File
@ -296,16 +297,18 @@ func TestGitCommandDiff(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
gitCmd.UserConfig.Git.DiffContextSize = s.contextSize
result := gitCmd.WorkingTree.WorktreeFileDiff(s.file, s.plain, s.cached, s.ignoreWhitespace)
userConfig := config.GetDefaultConfig()
userConfig.Git.DiffContextSize = s.contextSize
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner, userConfig: userConfig})
result := instance.WorktreeFileDiff(s.file, s.plain, s.cached, s.ignoreWhitespace)
assert.Equal(t, expectedResult, result)
s.runner.CheckForMissingCalls()
})
}
}
func TestGitCommandShowFileDiff(t *testing.T) {
func TestWorkingTreeShowFileDiff(t *testing.T) {
type scenario struct {
testName string
from string
@ -343,9 +346,12 @@ func TestGitCommandShowFileDiff(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
gitCmd.UserConfig.Git.DiffContextSize = s.contextSize
result, err := gitCmd.WorkingTree.ShowFileDiff(s.from, s.to, s.reverse, "test.txt", s.plain)
userConfig := config.GetDefaultConfig()
userConfig.Git.DiffContextSize = s.contextSize
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner, userConfig: userConfig})
result, err := instance.ShowFileDiff(s.from, s.to, s.reverse, "test.txt", s.plain)
assert.NoError(t, err)
assert.Equal(t, expectedResult, result)
s.runner.CheckForMissingCalls()
@ -353,7 +359,7 @@ func TestGitCommandShowFileDiff(t *testing.T) {
}
}
func TestGitCommandCheckoutFile(t *testing.T) {
func TestWorkingTreeCheckoutFile(t *testing.T) {
type scenario struct {
testName string
commitSha string
@ -387,14 +393,15 @@ func TestGitCommandCheckoutFile(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.WorkingTree.CheckoutFile(s.commitSha, s.fileName))
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
s.test(instance.CheckoutFile(s.commitSha, s.fileName))
s.runner.CheckForMissingCalls()
})
}
}
func TestGitCommandApplyPatch(t *testing.T) {
func TestWorkingTreeApplyPatch(t *testing.T) {
type scenario struct {
testName string
runner *oscommands.FakeCmdObjRunner
@ -439,14 +446,14 @@ func TestGitCommandApplyPatch(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.WorkingTree.ApplyPatch("test", "cached"))
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
s.test(instance.ApplyPatch("test", "cached"))
s.runner.CheckForMissingCalls()
})
}
}
func TestGitCommandDiscardUnstagedFileChanges(t *testing.T) {
func TestWorkingTreeDiscardUnstagedFileChanges(t *testing.T) {
type scenario struct {
testName string
file *models.File
@ -468,14 +475,14 @@ func TestGitCommandDiscardUnstagedFileChanges(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.WorkingTree.DiscardUnstagedFileChanges(s.file))
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
s.test(instance.DiscardUnstagedFileChanges(s.file))
s.runner.CheckForMissingCalls()
})
}
}
func TestGitCommandDiscardAnyUnstagedFileChanges(t *testing.T) {
func TestWorkingTreeDiscardAnyUnstagedFileChanges(t *testing.T) {
type scenario struct {
testName string
runner *oscommands.FakeCmdObjRunner
@ -495,14 +502,14 @@ func TestGitCommandDiscardAnyUnstagedFileChanges(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.WorkingTree.DiscardAnyUnstagedFileChanges())
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
s.test(instance.DiscardAnyUnstagedFileChanges())
s.runner.CheckForMissingCalls()
})
}
}
func TestGitCommandRemoveUntrackedFiles(t *testing.T) {
func TestWorkingTreeRemoveUntrackedFiles(t *testing.T) {
type scenario struct {
testName string
runner *oscommands.FakeCmdObjRunner
@ -522,14 +529,14 @@ func TestGitCommandRemoveUntrackedFiles(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.WorkingTree.RemoveUntrackedFiles())
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
s.test(instance.RemoveUntrackedFiles())
s.runner.CheckForMissingCalls()
})
}
}
func TestGitCommandResetHard(t *testing.T) {
func TestWorkingTreeResetHard(t *testing.T) {
type scenario struct {
testName string
ref string
@ -551,8 +558,8 @@ func TestGitCommandResetHard(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.WorkingTree.ResetHard(s.ref))
instance := buildWorkingTreeCommands(commonDeps{runner: s.runner})
s.test(instance.ResetHard(s.ref))
})
}
}

View File

@ -38,9 +38,7 @@ func (gui *Gui) handleCreateRecentReposMenu() error {
}
func (gui *Gui) handleShowAllBranchLogs() error {
cmdObj := gui.OSCommand.Cmd.New(
gui.UserConfig.Git.AllBranchesLogCmd,
)
cmdObj := gui.GitCommand.Branch.AllBranchesLogCmdObj()
task := NewRunPtyTask(cmdObj.GetCmd())
return gui.refreshMainViews(refreshMainOpts{

View File

@ -26,6 +26,15 @@ func NewDummyCommon() *common.Common {
}
}
func NewDummyCommonWithUserConfig(userConfig *config.UserConfig) *common.Common {
tr := i18n.EnglishTranslationSet()
return &common.Common{
Log: NewDummyLog(),
Tr: &tr,
UserConfig: userConfig,
}
}
func NewDummyGitConfig() git_config.IGitConfig {
return git_config.NewFakeGitConfig(map[string]string{})
}