1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/commands/git_commands/stash_test.go

169 lines
4.6 KiB
Go
Raw Normal View History

2022-01-08 05:00:36 +02:00
package git_commands
2021-04-10 03:40:42 +02:00
import (
"testing"
2021-12-31 01:24:53 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2022-01-08 04:22:29 +02:00
"github.com/jesseduffield/lazygit/pkg/config"
2021-04-10 03:40:42 +02:00
"github.com/stretchr/testify/assert"
)
2022-01-08 04:22:29 +02:00
func TestStashDrop(t *testing.T) {
2021-12-31 01:24:53 +02:00
runner := oscommands.NewFakeRunner(t).
2022-10-14 15:19:53 +02:00
ExpectGitArgs([]string{"stash", "drop", "stash@{1}"}, "Dropped refs/stash@{1} (98e9cca532c37c766107093010c72e26f2c24c04)", nil)
2022-01-08 04:22:29 +02:00
instance := buildStashCommands(commonDeps{runner: runner})
2021-04-10 03:40:42 +02:00
2022-10-14 15:19:53 +02:00
output, err := instance.Drop(1)
assert.NoError(t, err)
assert.Equal(t, "Dropped refs/stash@{1} (98e9cca532c37c766107093010c72e26f2c24c04)", output)
2022-01-02 01:34:33 +02:00
runner.CheckForMissingCalls()
}
2022-01-08 04:22:29 +02:00
func TestStashApply(t *testing.T) {
2022-01-02 01:34:33 +02:00
runner := oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"stash", "apply", "stash@{1}"}, "", nil)
2022-01-08 04:22:29 +02:00
instance := buildStashCommands(commonDeps{runner: runner})
2022-01-02 01:34:33 +02:00
2022-01-08 04:22:29 +02:00
assert.NoError(t, instance.Apply(1))
2022-01-02 01:34:33 +02:00
runner.CheckForMissingCalls()
}
2022-01-08 04:22:29 +02:00
func TestStashPop(t *testing.T) {
2022-01-02 01:34:33 +02:00
runner := oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"stash", "pop", "stash@{1}"}, "", nil)
2022-01-08 04:22:29 +02:00
instance := buildStashCommands(commonDeps{runner: runner})
2022-01-02 01:34:33 +02:00
2022-01-08 04:22:29 +02:00
assert.NoError(t, instance.Pop(1))
2021-12-31 01:24:53 +02:00
runner.CheckForMissingCalls()
2021-04-10 03:40:42 +02:00
}
2022-01-08 04:22:29 +02:00
func TestStashSave(t *testing.T) {
2021-12-31 01:24:53 +02:00
runner := oscommands.NewFakeRunner(t).
2022-01-03 06:15:26 +02:00
ExpectGitArgs([]string{"stash", "save", "A stash message"}, "", nil)
2022-01-08 04:22:29 +02:00
instance := buildStashCommands(commonDeps{runner: runner})
2021-04-10 03:40:42 +02:00
2022-01-08 04:22:29 +02:00
assert.NoError(t, instance.Save("A stash message"))
2021-12-31 01:24:53 +02:00
runner.CheckForMissingCalls()
2021-04-10 03:40:42 +02:00
}
2022-10-14 15:19:53 +02:00
func TestStashStore(t *testing.T) {
type scenario struct {
testName string
sha string
message string
expected []string
}
scenarios := []scenario{
{
testName: "Non-empty message",
sha: "0123456789abcdef",
message: "New stash name",
expected: []string{"stash", "store", "0123456789abcdef", "-m", "New stash name"},
},
{
testName: "Empty message",
sha: "0123456789abcdef",
message: "",
expected: []string{"stash", "store", "0123456789abcdef"},
},
{
testName: "Space message",
sha: "0123456789abcdef",
message: " ",
expected: []string{"stash", "store", "0123456789abcdef"},
},
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
ExpectGitArgs(s.expected, "", nil)
instance := buildStashCommands(commonDeps{runner: runner})
assert.NoError(t, instance.Store(s.sha, s.message))
runner.CheckForMissingCalls()
})
}
}
2022-01-08 04:22:29 +02:00
func TestStashStashEntryCmdObj(t *testing.T) {
type scenario struct {
testName string
index int
contextSize int
expected string
}
scenarios := []scenario{
{
testName: "Default case",
index: 5,
contextSize: 3,
expected: "git stash show -p --stat --color=always --unified=3 stash@{5}",
},
{
testName: "Show diff with custom context size",
index: 5,
contextSize: 77,
expected: "git stash show -p --stat --color=always --unified=77 stash@{5}",
},
}
for _, s := range scenarios {
2022-01-08 06:46:35 +02:00
s := s
t.Run(s.testName, func(t *testing.T) {
2022-01-08 04:22:29 +02:00
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)
})
}
}
2022-10-15 04:15:31 +02:00
func TestStashRename(t *testing.T) {
type scenario struct {
testName string
index int
message string
expectedDropCmd []string
dropResult string
expectedStoreCmd []string
}
scenarios := []scenario{
{
testName: "Default case",
index: 3,
message: "New message",
expectedDropCmd: []string{"stash", "drop", "stash@{3}"},
dropResult: "Dropped refs/stash@{3} (f0d0f20f2f61ffd6d6bfe0752deffa38845a3edd)\n",
expectedStoreCmd: []string{"stash", "store", "f0d0f20f2f61ffd6d6bfe0752deffa38845a3edd", "-m", "New message"},
},
{
testName: "Empty message",
index: 4,
message: "",
expectedDropCmd: []string{"stash", "drop", "stash@{4}"},
dropResult: "Dropped refs/stash@{4} (f0d0f20f2f61ffd6d6bfe0752deffa38845a3edd)\n",
expectedStoreCmd: []string{"stash", "store", "f0d0f20f2f61ffd6d6bfe0752deffa38845a3edd"},
},
}
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
ExpectGitArgs(s.expectedDropCmd, s.dropResult, nil).
ExpectGitArgs(s.expectedStoreCmd, "", nil)
instance := buildStashCommands(commonDeps{runner: runner})
err := instance.Rename(s.index, s.message)
assert.NoError(t, err)
})
}
}