1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-06 03:53:59 +02:00

more test refactoring

This commit is contained in:
Jesse Duffield 2021-12-31 10:24:53 +11:00
parent 9c4a819683
commit ad9b2df104
4 changed files with 74 additions and 117 deletions

View File

@ -1,41 +1,30 @@
package commands package commands
import ( import (
"os/exec"
"testing" "testing"
"github.com/jesseduffield/lazygit/pkg/secureexec" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/test"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// TestGitCommandRenameCommit is a function.
func TestGitCommandRenameCommit(t *testing.T) { func TestGitCommandRenameCommit(t *testing.T) {
gitCmd := NewDummyGitCommand() runner := oscommands.NewFakeRunner(t).
gitCmd.OSCommand.Command = func(cmd string, args ...string) *exec.Cmd { ExpectArgs([]string{"git", "commit", "--allow-empty", "--amend", "--only", "-m", "test"}, "", nil)
assert.EqualValues(t, "git", cmd) gitCmd := NewDummyGitCommandWithRunner(runner)
assert.EqualValues(t, []string{"commit", "--allow-empty", "--amend", "--only", "-m", "test"}, args)
return secureexec.Command("echo")
}
assert.NoError(t, gitCmd.RenameCommit("test")) assert.NoError(t, gitCmd.RenameCommit("test"))
runner.CheckForMissingCalls()
} }
// TestGitCommandResetToCommit is a function.
func TestGitCommandResetToCommit(t *testing.T) { func TestGitCommandResetToCommit(t *testing.T) {
gitCmd := NewDummyGitCommand() runner := oscommands.NewFakeRunner(t).
gitCmd.OSCommand.Command = func(cmd string, args ...string) *exec.Cmd { ExpectArgs([]string{"git", "reset", "--hard", "78976bc"}, "", nil)
assert.EqualValues(t, "git", cmd) gitCmd := NewDummyGitCommandWithRunner(runner)
assert.EqualValues(t, []string{"reset", "--hard", "78976bc"}, args)
return secureexec.Command("echo")
}
assert.NoError(t, gitCmd.ResetToCommit("78976bc", "hard", []string{})) assert.NoError(t, gitCmd.ResetToCommit("78976bc", "hard", []string{}))
runner.CheckForMissingCalls()
} }
// TestGitCommandCommitObj is a function.
func TestGitCommandCommitObj(t *testing.T) { func TestGitCommandCommitObj(t *testing.T) {
gitCmd := NewDummyGitCommand() gitCmd := NewDummyGitCommand()
@ -75,42 +64,35 @@ func TestGitCommandCommitObj(t *testing.T) {
} }
} }
// TestGitCommandCreateFixupCommit is a function.
func TestGitCommandCreateFixupCommit(t *testing.T) { func TestGitCommandCreateFixupCommit(t *testing.T) {
type scenario struct { type scenario struct {
testName string testName string
sha string sha string
command func(string, ...string) *exec.Cmd runner *oscommands.FakeCmdObjRunner
test func(error) test func(error)
} }
scenarios := []scenario{ scenarios := []scenario{
{ {
"valid case", testName: "valid case",
"12345", sha: "12345",
test.CreateMockCommand(t, []*test.CommandSwapper{ runner: oscommands.NewFakeRunner(t).
{ Expect(`git commit --fixup=12345`, "", nil),
Expect: `git commit --fixup=12345`, test: func(err error) {
Replace: "echo",
},
}),
func(err error) {
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
} }
gitCmd := NewDummyGitCommand()
for _, s := range scenarios { for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.Command = s.command gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.CreateFixupCommit(s.sha)) s.test(gitCmd.CreateFixupCommit(s.sha))
s.runner.CheckForMissingCalls()
}) })
} }
} }
// TestGitCommandShowCmdObj is a function.
func TestGitCommandShowCmdObj(t *testing.T) { func TestGitCommandShowCmdObj(t *testing.T) {
type scenario struct { type scenario struct {
testName string testName string

View File

@ -1,95 +1,76 @@
package commands package commands
import ( import (
"os/exec" "regexp"
"testing" "testing"
"github.com/jesseduffield/lazygit/pkg/test" "github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// TestGitCommandRebaseBranch is a function.
func TestGitCommandRebaseBranch(t *testing.T) { func TestGitCommandRebaseBranch(t *testing.T) {
type scenario struct { type scenario struct {
testName string testName string
arg string arg string
command func(string, ...string) *exec.Cmd runner *oscommands.FakeCmdObjRunner
test func(error) test func(error)
} }
scenarios := []scenario{ scenarios := []scenario{
{ {
"successful rebase", testName: "successful rebase",
"master", arg: "master",
test.CreateMockCommand(t, []*test.CommandSwapper{ runner: oscommands.NewFakeRunner(t).
{ Expect(`git rebase --interactive --autostash --keep-empty master`, "", nil),
Expect: "git rebase --interactive --autostash --keep-empty master", test: func(err error) {
Replace: "echo",
},
}),
func(err error) {
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"unsuccessful rebase", testName: "unsuccessful rebase",
"master", arg: "master",
test.CreateMockCommand(t, []*test.CommandSwapper{ runner: oscommands.NewFakeRunner(t).
{ Expect(`git rebase --interactive --autostash --keep-empty master`, "", errors.New("error")),
Expect: "git rebase --interactive --autostash --keep-empty master", test: func(err error) {
Replace: "test",
},
}),
func(err error) {
assert.Error(t, err) assert.Error(t, err)
}, },
}, },
} }
gitCmd := NewDummyGitCommand()
for _, s := range scenarios { for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
gitCmd.OSCommand.Command = s.command gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.RebaseBranch(s.arg)) s.test(gitCmd.RebaseBranch(s.arg))
}) })
} }
} }
// // TestGitCommandSkipEditorCommand confirms that SkipEditorCommand injects // TestGitCommandSkipEditorCommand confirms that SkipEditorCommand injects
// // environment variables that suppress an interactive editor // environment variables that suppress an interactive editor
// func TestGitCommandSkipEditorCommand(t *testing.T) { func TestGitCommandSkipEditorCommand(t *testing.T) {
// cmd := NewDummyGitCommand() commandStr := "git blah"
runner := oscommands.NewFakeRunner(t).ExpectFunc(func(cmdObj oscommands.ICmdObj) (string, error) {
// cmd.OSCommand.SetBeforeExecuteCmd(func(cmdObj oscommands.ICmdObj) { assert.Equal(t, commandStr, cmdObj.ToString())
// test.AssertContainsMatch( envVars := cmdObj.GetEnvVars()
// t, for _, regexStr := range []string{
// cmdObj.GetEnvVars(), `^VISUAL=.*$`,
// regexp.MustCompile("^VISUAL="), `^EDITOR=.*$`,
// "expected VISUAL to be set for a non-interactive external command", `^GIT_EDITOR=.*$`,
// ) "^LAZYGIT_CLIENT_COMMAND=EXIT_IMMEDIATELY$",
} {
// test.AssertContainsMatch( foundMatch := utils.IncludesStringFunc(envVars, func(envVar string) bool {
// t, return regexp.MustCompile(regexStr).MatchString(envVar)
// cmdObj.GetEnvVars(), })
// regexp.MustCompile("^EDITOR="), if !foundMatch {
// "expected EDITOR to be set for a non-interactive external command", t.Errorf("expected environment variable %s to be set", regexStr)
// ) }
}
// test.AssertContainsMatch( return "", nil
// t, })
// cmdObj.GetEnvVars(), gitCmd := NewDummyGitCommandWithRunner(runner)
// regexp.MustCompile("^GIT_EDITOR="), err := gitCmd.runSkipEditorCommand(commandStr)
// "expected GIT_EDITOR to be set for a non-interactive external command", assert.NoError(t, err)
// ) runner.CheckForMissingCalls()
}
// test.AssertContainsMatch(
// t,
// cmdObj.GetEnvVars(),
// regexp.MustCompile("^LAZYGIT_CLIENT_COMMAND=EXIT_IMMEDIATELY$"),
// "expected LAZYGIT_CLIENT_COMMAND to be set for a non-interactive external command",
// )
// })
// _ = cmd.runSkipEditorCommand("true")
// }

View File

@ -1,40 +1,30 @@
package commands package commands
import ( import (
"os/exec"
"testing" "testing"
"github.com/jesseduffield/lazygit/pkg/secureexec" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// TestGitCommandStashDo is a function.
func TestGitCommandStashDo(t *testing.T) { func TestGitCommandStashDo(t *testing.T) {
gitCmd := NewDummyGitCommand() runner := oscommands.NewFakeRunner(t).
gitCmd.OSCommand.Command = func(cmd string, args ...string) *exec.Cmd { ExpectArgs([]string{"git", "stash", "drop", "stash@{1}"}, "", nil)
assert.EqualValues(t, "git", cmd) gitCmd := NewDummyGitCommandWithRunner(runner)
assert.EqualValues(t, []string{"stash", "drop", "stash@{1}"}, args)
return secureexec.Command("echo")
}
assert.NoError(t, gitCmd.StashDo(1, "drop")) assert.NoError(t, gitCmd.StashDo(1, "drop"))
runner.CheckForMissingCalls()
} }
// TestGitCommandStashSave is a function.
func TestGitCommandStashSave(t *testing.T) { func TestGitCommandStashSave(t *testing.T) {
gitCmd := NewDummyGitCommand() runner := oscommands.NewFakeRunner(t).
gitCmd.OSCommand.Command = func(cmd string, args ...string) *exec.Cmd { ExpectArgs([]string{"git", "stash", "save", "A stash message"}, "", nil)
assert.EqualValues(t, "git", cmd) gitCmd := NewDummyGitCommandWithRunner(runner)
assert.EqualValues(t, []string{"stash", "save", "A stash message"}, args)
return secureexec.Command("echo")
}
assert.NoError(t, gitCmd.StashSave("A stash message")) assert.NoError(t, gitCmd.StashSave("A stash message"))
runner.CheckForMissingCalls()
} }
// TestGitCommandShowStashEntryCmdStr is a function.
func TestGitCommandShowStashEntryCmdStr(t *testing.T) { func TestGitCommandShowStashEntryCmdStr(t *testing.T) {
type scenario struct { type scenario struct {
testName string testName string

View File

@ -2,8 +2,12 @@ package utils
// IncludesString if the list contains the string // IncludesString if the list contains the string
func IncludesString(list []string, a string) bool { func IncludesString(list []string, a string) bool {
return IncludesStringFunc(list, func(b string) bool { return b == a })
}
func IncludesStringFunc(list []string, fn func(string) bool) bool {
for _, b := range list { for _, b := range list {
if b == a { if fn(b) {
return true return true
} }
} }