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-16 02:11:54 +02:00
|
|
|
ExpectGitArgs([]string{"stash", "drop", "stash@{1}"}, "Dropped refs/stash@{1} (98e9cca532c37c766107093010c72e26f2c24c04)\n", nil)
|
2022-01-08 04:22:29 +02:00
|
|
|
instance := buildStashCommands(commonDeps{runner: runner})
|
2021-04-10 03:40:42 +02:00
|
|
|
|
2022-10-16 02:11:54 +02:00
|
|
|
assert.NoError(t, instance.Drop(1))
|
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).
|
2023-07-01 16:22:32 +02:00
|
|
|
ExpectGitArgs([]string{"stash", "push", "-m", "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
|
|
|
|
2023-07-01 16:22:32 +02:00
|
|
|
assert.NoError(t, instance.Push("A stash message"))
|
2021-12-31 01:24:53 +02:00
|
|
|
runner.CheckForMissingCalls()
|
2021-04-10 03:40:42 +02:00
|
|
|
}
|
2021-09-11 00:47:50 +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",
|
2023-07-01 16:18:38 +02:00
|
|
|
expected: []string{"stash", "store", "-m", "New stash name", "0123456789abcdef"},
|
2022-10-14 15:19:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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-10-16 02:11:54 +02:00
|
|
|
func TestStashSha(t *testing.T) {
|
|
|
|
runner := oscommands.NewFakeRunner(t).
|
|
|
|
ExpectGitArgs([]string{"rev-parse", "refs/stash@{5}"}, "14d94495194651adfd5f070590df566c11d28243\n", nil)
|
|
|
|
instance := buildStashCommands(commonDeps{runner: runner})
|
|
|
|
|
|
|
|
sha, err := instance.Sha(5)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "14d94495194651adfd5f070590df566c11d28243", sha)
|
|
|
|
runner.CheckForMissingCalls()
|
|
|
|
}
|
|
|
|
|
2022-01-08 04:22:29 +02:00
|
|
|
func TestStashStashEntryCmdObj(t *testing.T) {
|
2021-09-11 00:47:50 +02:00
|
|
|
type scenario struct {
|
2023-05-19 11:34:29 +02:00
|
|
|
testName string
|
|
|
|
index int
|
|
|
|
contextSize int
|
|
|
|
ignoreWhitespace bool
|
2023-05-21 09:00:29 +02:00
|
|
|
expected []string
|
2021-09-11 00:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
2023-05-19 11:34:29 +02:00
|
|
|
testName: "Default case",
|
|
|
|
index: 5,
|
|
|
|
contextSize: 3,
|
|
|
|
ignoreWhitespace: false,
|
2023-05-21 09:00:29 +02:00
|
|
|
expected: []string{"git", "stash", "show", "-p", "--stat", "--color=always", "--unified=3", "stash@{5}"},
|
2023-05-19 11:34:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "Show diff with custom context size",
|
|
|
|
index: 5,
|
|
|
|
contextSize: 77,
|
|
|
|
ignoreWhitespace: false,
|
2023-05-21 09:00:29 +02:00
|
|
|
expected: []string{"git", "stash", "show", "-p", "--stat", "--color=always", "--unified=77", "stash@{5}"},
|
2021-09-11 00:47:50 +02:00
|
|
|
},
|
|
|
|
{
|
2023-05-19 11:34:29 +02:00
|
|
|
testName: "Default case",
|
|
|
|
index: 5,
|
|
|
|
contextSize: 3,
|
|
|
|
ignoreWhitespace: true,
|
2023-05-21 09:00:29 +02:00
|
|
|
expected: []string{"git", "stash", "show", "-p", "--stat", "--color=always", "--unified=3", "--ignore-all-space", "stash@{5}"},
|
2021-09-11 00:47:50 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
2022-01-08 06:46:35 +02:00
|
|
|
s := s
|
2021-09-11 00:47:50 +02:00
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
2022-01-08 04:22:29 +02:00
|
|
|
userConfig := config.GetDefaultConfig()
|
2023-08-28 12:39:52 +02:00
|
|
|
appState := &config.AppState{}
|
|
|
|
appState.IgnoreWhitespaceInDiffView = s.ignoreWhitespace
|
2023-08-28 13:09:55 +02:00
|
|
|
appState.DiffContextSize = s.contextSize
|
2023-08-28 12:39:52 +02:00
|
|
|
instance := buildStashCommands(commonDeps{userConfig: userConfig, appState: appState})
|
2022-01-08 04:22:29 +02:00
|
|
|
|
2023-08-28 12:39:52 +02:00
|
|
|
cmdStr := instance.ShowStashEntryCmdObj(s.index).Args()
|
2021-09-11 00:47:50 +02:00
|
|
|
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
|
2022-10-16 02:11:54 +02:00
|
|
|
expectedShaCmd []string
|
|
|
|
shaResult string
|
2022-10-15 04:15:31 +02:00
|
|
|
expectedDropCmd []string
|
|
|
|
expectedStoreCmd []string
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
testName: "Default case",
|
|
|
|
index: 3,
|
|
|
|
message: "New message",
|
2022-10-16 02:11:54 +02:00
|
|
|
expectedShaCmd: []string{"rev-parse", "refs/stash@{3}"},
|
|
|
|
shaResult: "f0d0f20f2f61ffd6d6bfe0752deffa38845a3edd\n",
|
2022-10-15 04:15:31 +02:00
|
|
|
expectedDropCmd: []string{"stash", "drop", "stash@{3}"},
|
2023-07-01 16:18:38 +02:00
|
|
|
expectedStoreCmd: []string{"stash", "store", "-m", "New message", "f0d0f20f2f61ffd6d6bfe0752deffa38845a3edd"},
|
2022-10-15 04:15:31 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "Empty message",
|
|
|
|
index: 4,
|
|
|
|
message: "",
|
2022-10-16 02:11:54 +02:00
|
|
|
expectedShaCmd: []string{"rev-parse", "refs/stash@{4}"},
|
|
|
|
shaResult: "f0d0f20f2f61ffd6d6bfe0752deffa38845a3edd\n",
|
2022-10-15 04:15:31 +02:00
|
|
|
expectedDropCmd: []string{"stash", "drop", "stash@{4}"},
|
|
|
|
expectedStoreCmd: []string{"stash", "store", "f0d0f20f2f61ffd6d6bfe0752deffa38845a3edd"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
s := s
|
|
|
|
t.Run(s.testName, func(t *testing.T) {
|
|
|
|
runner := oscommands.NewFakeRunner(t).
|
2022-10-16 02:11:54 +02:00
|
|
|
ExpectGitArgs(s.expectedShaCmd, s.shaResult, nil).
|
|
|
|
ExpectGitArgs(s.expectedDropCmd, "", nil).
|
2022-10-15 04:15:31 +02:00
|
|
|
ExpectGitArgs(s.expectedStoreCmd, "", nil)
|
|
|
|
instance := buildStashCommands(commonDeps{runner: runner})
|
|
|
|
|
|
|
|
err := instance.Rename(s.index, s.message)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|