mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-12 12:55:47 +02:00
Support random order of command execution in unit tests
Now that we run code concurrently in our loaders, we need to handle that in our tests. We could enforce a deterministic ordering by mocking waitgroup or something like that, but I think it's fine to let our tests handle some randomness given that prod itself will have that randomness. I've removed the patch test file because it was clunky, not providing much value, and it would have been hard to refactor to the new pattern
This commit is contained in:
parent
39b77c0fca
commit
429225da80
5 changed files with 97 additions and 109 deletions
|
@ -1,12 +1,12 @@
|
|||
package git_commands
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/fsmiamoto/git-todo-parser/todo"
|
||||
"github.com/go-errors/errors"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
|
||||
|
|
|
@ -118,7 +118,7 @@ func buildWorkingTreeCommands(deps commonDeps) *WorkingTreeCommands {
|
|||
return NewWorkingTreeCommands(gitCommon, submoduleCommands, fileLoader)
|
||||
}
|
||||
|
||||
func buildPatchCommands(deps commonDeps) *PatchCommands {
|
||||
func buildPatchCommands(deps commonDeps) *PatchCommands { //nolint:golint,unused
|
||||
gitCommon := buildGitCommon(deps)
|
||||
rebaseCommands := buildRebaseCommands(deps)
|
||||
commitCommands := buildCommitCommands(deps)
|
||||
|
@ -132,7 +132,7 @@ func buildPatchCommands(deps commonDeps) *PatchCommands {
|
|||
return NewPatchCommands(gitCommon, rebaseCommands, commitCommands, statusCommands, stashCommands, patchBuilder)
|
||||
}
|
||||
|
||||
func buildStatusCommands(deps commonDeps) *StatusCommands {
|
||||
func buildStatusCommands(deps commonDeps) *StatusCommands { //nolint:golint,unused
|
||||
gitCommon := buildGitCommon(deps)
|
||||
|
||||
return NewStatusCommands(gitCommon)
|
||||
|
|
|
@ -1,68 +0,0 @@
|
|||
package git_commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/go-errors/errors"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPatchApplyPatch(t *testing.T) {
|
||||
type scenario struct {
|
||||
testName string
|
||||
opts ApplyPatchOpts
|
||||
runner *oscommands.FakeCmdObjRunner
|
||||
test func(error)
|
||||
}
|
||||
|
||||
// expectedArgs excludes the last argument which is an indeterminate filename
|
||||
expectFn := func(expectedArgs []string, errToReturn error) func(cmdObj oscommands.ICmdObj) (string, error) {
|
||||
return func(cmdObj oscommands.ICmdObj) (string, error) {
|
||||
args := cmdObj.Args()
|
||||
|
||||
assert.Equal(t, len(args), len(expectedArgs)+1, fmt.Sprintf("unexpected command: %s", cmdObj.ToString()))
|
||||
|
||||
filename := args[len(args)-1]
|
||||
|
||||
content, err := os.ReadFile(filename)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "test", string(content))
|
||||
|
||||
return "", errToReturn
|
||||
}
|
||||
}
|
||||
|
||||
scenarios := []scenario{
|
||||
{
|
||||
testName: "valid case",
|
||||
opts: ApplyPatchOpts{Cached: true},
|
||||
runner: oscommands.NewFakeRunner(t).
|
||||
ExpectFunc(expectFn([]string{"git", "apply", "--cached"}, nil)),
|
||||
test: func(err error) {
|
||||
assert.NoError(t, err)
|
||||
},
|
||||
},
|
||||
{
|
||||
testName: "command returns error",
|
||||
opts: ApplyPatchOpts{Cached: true},
|
||||
runner: oscommands.NewFakeRunner(t).
|
||||
ExpectFunc(expectFn([]string{"git", "apply", "--cached"}, errors.New("error"))),
|
||||
test: func(err error) {
|
||||
assert.Error(t, err)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarios {
|
||||
s := s
|
||||
t.Run(s.testName, func(t *testing.T) {
|
||||
instance := buildPatchCommands(commonDeps{runner: s.runner})
|
||||
s.test(instance.ApplyPatch("test", s.opts))
|
||||
s.runner.CheckForMissingCalls()
|
||||
})
|
||||
}
|
||||
}
|
|
@ -79,7 +79,7 @@ func TestRebaseRebaseBranch(t *testing.T) {
|
|||
// environment variables that suppress an interactive editor
|
||||
func TestRebaseSkipEditorCommand(t *testing.T) {
|
||||
cmdArgs := []string{"git", "blah"}
|
||||
runner := oscommands.NewFakeRunner(t).ExpectFunc(func(cmdObj oscommands.ICmdObj) (string, error) {
|
||||
runner := oscommands.NewFakeRunner(t).ExpectFunc("matches editor env var", func(cmdObj oscommands.ICmdObj) bool {
|
||||
assert.EqualValues(t, cmdArgs, cmdObj.Args())
|
||||
envVars := cmdObj.GetEnvVars()
|
||||
for _, regexStr := range []string{
|
||||
|
@ -94,11 +94,11 @@ func TestRebaseSkipEditorCommand(t *testing.T) {
|
|||
return regexp.MustCompile(regexStr).MatchString(envVar)
|
||||
})
|
||||
if !foundMatch {
|
||||
t.Errorf("expected environment variable %s to be set", regexStr)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
})
|
||||
return true
|
||||
}, "", nil)
|
||||
instance := buildRebaseCommands(commonDeps{runner: runner})
|
||||
err := instance.runSkipEditorCommand(instance.cmd.New(cmdArgs))
|
||||
assert.NoError(t, err)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue