summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2021-12-31 10:24:53 +1100
committerJesse Duffield <jessedduffield@gmail.com>2022-01-04 09:07:15 +1100
commitad9b2df104737499ae8c2f7099f12d6c3a129628 (patch)
treec78eb0f9a39a6bea283c714967c070d0980dabdc
parent9c4a8196834d40c9b054d957dcfb69851edcb61f (diff)
more test refactoring
-rw-r--r--pkg/commands/commits_test.go52
-rw-r--r--pkg/commands/rebasing_test.go105
-rw-r--r--pkg/commands/stash_entries_test.go28
-rw-r--r--pkg/utils/slice.go6
4 files changed, 74 insertions, 117 deletions
diff --git a/pkg/commands/commits_test.go b/pkg/commands/commits_test.go
index aed0ba5ff..4ca36969a 100644
--- a/pkg/commands/commits_test.go
+++ b/pkg/commands/commits_test.go
@@ -1,41 +1,30 @@
package commands
import (
- "os/exec"
"testing"
- "github.com/jesseduffield/lazygit/pkg/secureexec"
- "github.com/jesseduffield/lazygit/pkg/test"
+ "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/stretchr/testify/assert"
)
-// TestGitCommandRenameCommit is a function.
func TestGitCommandRenameCommit(t *testing.T) {
- gitCmd := NewDummyGitCommand()
- gitCmd.OSCommand.Command = func(cmd string, args ...string) *exec.Cmd {
- assert.EqualValues(t, "git", cmd)
- assert.EqualValues(t, []string{"commit", "--allow-empty", "--amend", "--only", "-m", "test"}, args)
-
- return secureexec.Command("echo")
- }
+ runner := oscommands.NewFakeRunner(t).
+ ExpectArgs([]string{"git", "commit", "--allow-empty", "--amend", "--only", "-m", "test"}, "", nil)
+ gitCmd := NewDummyGitCommandWithRunner(runner)
assert.NoError(t, gitCmd.RenameCommit("test"))
+ runner.CheckForMissingCalls()
}
-// TestGitCommandResetToCommit is a function.
func TestGitCommandResetToCommit(t *testing.T) {
- gitCmd := NewDummyGitCommand()
- gitCmd.OSCommand.Command = func(cmd string, args ...string) *exec.Cmd {
- assert.EqualValues(t, "git", cmd)
- assert.EqualValues(t, []string{"reset", "--hard", "78976bc"}, args)
-
- return secureexec.Command("echo")
- }
+ runner := oscommands.NewFakeRunner(t).
+ ExpectArgs([]string{"git", "reset", "--hard", "78976bc"}, "", nil)
+ gitCmd := NewDummyGitCommandWithRunner(runner)
assert.NoError(t, gitCmd.ResetToCommit("78976bc", "hard", []string{}))
+ runner.CheckForMissingCalls()
}
-// TestGitCommandCommitObj is a function.
func TestGitCommandCommitObj(t *testing.T) {
gitCmd := NewDummyGitCommand()
@@ -75,42 +64,35 @@ func TestGitCommandCommitObj(t *testing.T) {
}
}
-// TestGitCommandCreateFixupCommit is a function.
func TestGitCommandCreateFixupCommit(t *testing.T) {
type scenario struct {
testName string
sha string
- command func(string, ...string) *exec.Cmd
+ runner *oscommands.FakeCmdObjRunner
test func(error)
}
scenarios := []scenario{
{
- "valid case",
- "12345",
- test.CreateMockCommand(t, []*test.CommandSwapper{
- {
- Expect: `git commit --fixup=12345`,
- Replace: "echo",
- },
- }),
- func(err error) {
+ testName: "valid case",
+ sha: "12345",
+ runner: oscommands.NewFakeRunner(t).
+ Expect(`git commit --fixup=12345`, "", nil),
+ test: func(err error) {
assert.NoError(t, err)
},
},
}
- gitCmd := NewDummyGitCommand()
-
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
- gitCmd.OSCommand.Command = s.command
+ gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.CreateFixupCommit(s.sha))
+ s.runner.CheckForMissingCalls()
})
}
}
-// TestGitCommandShowCmdObj is a function.
func TestGitCommandShowCmdObj(t *testing.T) {
type scenario struct {
testName string
diff --git a/pkg/commands/rebasing_test.go b/pkg/commands/rebasing_test.go
index af786f515..3091da9ee 100644
--- a/pkg/commands/rebasing_test.go
+++ b/pkg/commands/rebasing_test.go
@@ -1,95 +1,76 @@
package commands
import (
- "os/exec"
+ "regexp"
"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"
)
-// TestGitCommandRebaseBranch is a function.
func TestGitCommandRebaseBranch(t *testing.T) {
type scenario struct {
testName string
arg string
- command func(string, ...string) *exec.Cmd
+ runner *oscommands.FakeCmdObjRunner
test func(error)
}
scenarios := []scenario{
{
- "successful rebase",
- "master",
- test.CreateMockCommand(t, []*test.CommandSwapper{
- {
- Expect: "git rebase --interactive --autostash --keep-empty master",
- Replace: "echo",
- },
- }),
- func(err error) {
+ testName: "successful rebase",
+ arg: "master",
+ runner: oscommands.NewFakeRunner(t).
+ Expect(`git rebase --interactive --autostash --keep-empty master`, "", nil),
+ test: func(err error) {
assert.NoError(t, err)
},
},
{
- "unsuccessful rebase",
- "master",
- test.CreateMockCommand(t, []*test.CommandSwapper{
- {
- Expect: "git rebase --interactive --autostash --keep-empty master",
- Replace: "test",
- },
- }),
- func(err error) {
+ testName: "unsuccessful rebase",
+ arg: "master",
+ runner: oscommands.NewFakeRunner(t).
+ Expect(`git rebase --interactive --autostash --keep-empty master`, "", errors.New("error")),
+ test: func(err error) {
assert.Error(t, err)
},
},
}
- gitCmd := NewDummyGitCommand()
-
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
- gitCmd.OSCommand.Command = s.command
+ gitCmd := NewDummyGitCommandWithRunner(s.runner)
s.test(gitCmd.RebaseBranch(s.arg))
})
}
}
-// // TestGitCommandSkipEditorCommand confirms that SkipEditorCommand injects
-// // environment variables that suppress an interactive editor
-// func TestGitCommandSkipEditorCommand(t *testing.T) {
-// cmd := NewDummyGitCommand()
-
-// cmd.OSCommand.SetBeforeExecuteCmd(func(cmdObj oscommands.ICmdObj) {
-// test.AssertContainsMatch(
-// t,
-// cmdObj.GetEnvVars(),
-// regexp.MustCompile("^VISUAL="),
-// "expected VISUAL to be set for a non-interactive external command",
-// )
-
-// test.AssertContainsMatch(
-// t,
-// cmdObj.GetEnvVars(),
-// regexp.MustCompile("^EDITOR="),
-// "expected EDITOR to be set for a non-interactive external command",
-// )
-
-// test.AssertContainsMatch(
-// t,
-// cmdObj.GetEnvVars(),
-// regexp.MustCompile("^GIT_EDITOR="),
-// "expected GIT_EDITOR to be set for a non-interactive external command",
-// )
-
-// 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")
-// }
+// TestGitCommandSkipEditorCommand confirms that SkipEditorCommand injects
+// environment variables that suppress an interactive editor
+func TestGitCommandSkipEditorCommand(t *testing.T) {
+ commandStr := "git blah"
+ runner := oscommands.NewFakeRunner(t).ExpectFunc(func(cmdObj oscommands.ICmdObj) (string, error) {
+ assert.Equal(t, commandStr, cmdObj.ToString())
+ envVars := cmdObj.GetEnvVars()
+ for _, regexStr := range []string{
+ `^VISUAL=.*$`,
+ `^EDITOR=.*$`,
+ `^GIT_EDITOR=.*$`,
+ "^LAZYGIT_CLIENT_COMMAND=EXIT_IMMEDIATELY$",
+ } {
+ foundMatch := utils.IncludesStringFunc(envVars, func(envVar string) bool {
+ return regexp.MustCompile(regexStr).MatchString(envVar)
+ })
+ if !foundMatch {
+ t.Errorf("expected environment variable %s to be set", regexStr)
+ }
+ }
+ return "", nil
+ })
+ gitCmd := NewDummyGitCommandWithRunner(runner)
+ err := gitCmd.runSkipEditorCommand(commandStr)
+ assert.NoError(t, err)
+ runner.CheckForMissingCalls()
+}
diff --git a/pkg/commands/stash_entries_test.go b/pkg/commands/stash_entries_test.go
index 03b630b33..f775b98a6 100644
--- a/pkg/commands/stash_entries_test.go
+++ b/pkg/commands/stash_entries_test.go
@@ -1,40 +1,30 @@
package commands
import (
- "os/exec"
"testing"
- "github.com/jesseduffield/lazygit/pkg/secureexec"
+ "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/stretchr/testify/assert"
)
-// TestGitCommandStashDo is a function.
func TestGitCommandStashDo(t *testing.T) {
- gitCmd := NewDummyGitCommand()
- gitCmd.OSCommand.Command = func(cmd string, args ...string) *exec.Cmd {
- assert.EqualValues(t, "git", cmd)
- assert.EqualValues(t, []string{"stash", "drop", "stash@{1}"}, args)
-
- return secureexec.Command("echo")
- }
+ runner := oscommands.NewFakeRunner(t).
+ ExpectArgs([]string{"git", "stash", "drop", "stash@{1}"}, "", nil)
+ gitCmd := NewDummyGitCommandWithRunner(runner)
assert.NoError(t, gitCmd.StashDo(1, "drop"))
+ runner.CheckForMissingCalls()
}
-// TestGitCommandStashSave is a function.
func TestGitCommandStashSave(t *testing.T) {
- gitCmd := NewDummyGitCommand()
- gitCmd.OSCommand.Command = func(cmd string, args ...string) *exec.Cmd {
- assert.EqualValues(t, "git", cmd)
- assert.EqualValues(t, []string{"stash", "save", "A stash message"}, args)
-
- return secureexec.Command("echo")
- }
+ runner := oscommands.NewFakeRunner(t).
+ ExpectArgs([]string{"git", "stash", "save", "A stash message"}, "", nil)
+ gitCmd := NewDummyGitCommandWithRunner(runner)
assert.NoError(t, gitCmd.StashSave("A stash message"))
+ runner.CheckForMissingCalls()
}
-// TestGitCommandShowStashEntryCmdStr is a function.
func TestGitCommandShowStashEntryCmdStr(t *testing.T) {
type scenario struct {
testName string
diff --git a/pkg/utils/slice.go b/pkg/utils/slice.go
index dc3e85069..123fc7df9 100644
--- a/pkg/utils/slice.go
+++ b/pkg/utils/slice.go
@@ -2,8 +2,12 @@ package utils
// IncludesString if the list contains the string
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 {
- if b == a {
+ if fn(b) {
return true
}
}