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

378 lines
9.5 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 00:45:29 +02:00
"github.com/go-errors/errors"
2021-10-23 00:52:19 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
2021-12-31 00:45:29 +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"
)
2023-03-26 14:04:09 +02:00
func TestEditFileCmdStrLegacy(t *testing.T) {
type scenario struct {
2021-08-04 11:43:34 +02:00
filename string
configEditCommand string
configEditCommandTemplate string
2021-12-31 00:45:29 +02:00
runner *oscommands.FakeCmdObjRunner
2021-08-04 11:43:34 +02:00
getenv func(string) string
2021-10-23 00:52:19 +02:00
gitConfigMockResponses map[string]string
2021-08-04 11:43:34 +02:00
test func(string, error)
}
scenarios := []scenario{
{
2021-12-31 00:45:29 +02:00
filename: "test",
configEditCommand: "",
configEditCommandTemplate: "{{editor}} {{filename}}",
runner: oscommands.NewFakeRunner(t).
ExpectArgs([]string{"which", "vi"}, "", errors.New("error")),
2021-12-31 00:45:29 +02:00
getenv: func(env string) string {
return ""
},
2021-12-31 00:45:29 +02:00
gitConfigMockResponses: nil,
test: func(cmdStr string, err error) {
2021-06-05 02:58:36 +02:00
assert.EqualError(t, err, "No editor defined in config file, $GIT_EDITOR, $VISUAL, $EDITOR, or git config")
},
},
{
2021-12-31 00:45:29 +02:00
filename: "test",
configEditCommand: "nano",
configEditCommandTemplate: "{{editor}} {{filename}}",
runner: oscommands.NewFakeRunner(t),
getenv: func(env string) string {
2021-05-20 08:44:58 +02:00
return ""
},
2021-12-31 00:45:29 +02:00
gitConfigMockResponses: nil,
test: func(cmdStr string, err error) {
2021-05-20 08:44:58 +02:00
assert.NoError(t, err)
assert.Equal(t, `nano "test"`, cmdStr)
2021-05-20 08:44:58 +02:00
},
},
{
2021-12-31 00:45:29 +02:00
filename: "test",
configEditCommand: "",
configEditCommandTemplate: "{{editor}} {{filename}}",
runner: oscommands.NewFakeRunner(t),
getenv: func(env string) string {
return ""
},
2021-12-31 00:45:29 +02:00
gitConfigMockResponses: map[string]string{"core.editor": "nano"},
test: func(cmdStr string, err error) {
assert.NoError(t, err)
assert.Equal(t, `nano "test"`, cmdStr)
},
},
{
2021-12-31 00:45:29 +02:00
filename: "test",
configEditCommand: "",
configEditCommandTemplate: "{{editor}} {{filename}}",
runner: oscommands.NewFakeRunner(t),
getenv: func(env string) string {
if env == "VISUAL" {
return "nano"
}
return ""
},
2021-12-31 00:45:29 +02:00
gitConfigMockResponses: nil,
test: func(cmdStr string, err error) {
assert.NoError(t, err)
assert.Equal(t, `nano "test"`, cmdStr)
},
},
{
2021-12-31 00:45:29 +02:00
filename: "test",
configEditCommand: "",
configEditCommandTemplate: "{{editor}} {{filename}}",
runner: oscommands.NewFakeRunner(t),
getenv: func(env string) string {
if env == "EDITOR" {
return "emacs"
}
return ""
},
2021-12-31 00:45:29 +02:00
gitConfigMockResponses: nil,
test: func(cmdStr string, err error) {
assert.NoError(t, err)
assert.Equal(t, `emacs "test"`, cmdStr)
},
},
{
2021-12-31 00:45:29 +02:00
filename: "test",
configEditCommand: "",
configEditCommandTemplate: "{{editor}} {{filename}}",
runner: oscommands.NewFakeRunner(t).
ExpectArgs([]string{"which", "vi"}, "/usr/bin/vi", nil),
2021-12-31 00:45:29 +02:00
getenv: func(env string) string {
return ""
},
2021-12-31 00:45:29 +02:00
gitConfigMockResponses: nil,
test: func(cmdStr string, err error) {
assert.NoError(t, err)
assert.Equal(t, `vi "test"`, cmdStr)
},
},
{
2021-12-31 00:45:29 +02:00
filename: "file/with space",
configEditCommand: "",
configEditCommandTemplate: "{{editor}} {{filename}}",
runner: oscommands.NewFakeRunner(t).
ExpectArgs([]string{"which", "vi"}, "/usr/bin/vi", nil),
2021-12-31 00:45:29 +02:00
getenv: func(env string) string {
return ""
},
2021-12-31 00:45:29 +02:00
gitConfigMockResponses: nil,
test: func(cmdStr string, err error) {
assert.NoError(t, err)
assert.Equal(t, `vi "file/with space"`, cmdStr)
},
},
{
2021-12-31 00:45:29 +02:00
filename: "open file/at line",
configEditCommand: "vim",
configEditCommandTemplate: "{{editor}} +{{line}} {{filename}}",
runner: oscommands.NewFakeRunner(t),
getenv: func(env string) string {
return ""
},
2021-12-31 00:45:29 +02:00
gitConfigMockResponses: nil,
test: func(cmdStr string, err error) {
assert.NoError(t, err)
2021-12-31 00:45:29 +02:00
assert.Equal(t, `vim +1 "open file/at line"`, cmdStr)
},
},
{
filename: "default edit command template",
configEditCommand: "vim",
configEditCommandTemplate: "",
runner: oscommands.NewFakeRunner(t),
getenv: func(env string) string {
return ""
},
gitConfigMockResponses: nil,
test: func(cmdStr string, err error) {
assert.NoError(t, err)
assert.Equal(t, `vim +1 -- "default edit command template"`, cmdStr)
},
},
}
for _, s := range scenarios {
2022-01-08 04:22:29 +02:00
userConfig := config.GetDefaultConfig()
userConfig.OS.EditCommand = s.configEditCommand
userConfig.OS.EditCommandTemplate = s.configEditCommandTemplate
instance := buildFileCommands(commonDeps{
runner: s.runner,
userConfig: userConfig,
gitConfig: git_config.NewFakeGitConfig(s.gitConfigMockResponses),
getenv: s.getenv,
})
2023-03-26 14:04:09 +02:00
s.test(instance.GetEditCmdStrLegacy(s.filename, 1))
2021-12-31 00:45:29 +02:00
s.runner.CheckForMissingCalls()
}
}
2023-03-26 14:04:09 +02:00
func TestEditFileCmd(t *testing.T) {
type scenario struct {
filename string
osConfig config.OSConfig
expectedCmdStr string
expectedEditInTerminal bool
}
scenarios := []scenario{
{
filename: "test",
osConfig: config.OSConfig{},
expectedCmdStr: `vim -- "test"`,
expectedEditInTerminal: true,
},
{
filename: "test",
osConfig: config.OSConfig{
Edit: "nano {{filename}}",
},
expectedCmdStr: `nano "test"`,
expectedEditInTerminal: true,
},
{
filename: "file/with space",
osConfig: config.OSConfig{
EditPreset: "sublime",
},
expectedCmdStr: `subl -- "file/with space"`,
expectedEditInTerminal: false,
},
}
for _, s := range scenarios {
userConfig := config.GetDefaultConfig()
userConfig.OS = s.osConfig
instance := buildFileCommands(commonDeps{
userConfig: userConfig,
})
cmdStr, editInTerminal := instance.GetEditCmdStr(s.filename)
assert.Equal(t, s.expectedCmdStr, cmdStr)
assert.Equal(t, s.expectedEditInTerminal, editInTerminal)
}
}
func TestEditFileAtLineCmd(t *testing.T) {
type scenario struct {
filename string
lineNumber int
osConfig config.OSConfig
expectedCmdStr string
expectedEditInTerminal bool
}
scenarios := []scenario{
{
filename: "test",
lineNumber: 42,
osConfig: config.OSConfig{},
expectedCmdStr: `vim +42 -- "test"`,
expectedEditInTerminal: true,
},
{
filename: "test",
lineNumber: 35,
osConfig: config.OSConfig{
EditAtLine: "nano +{{line}} {{filename}}",
},
expectedCmdStr: `nano +35 "test"`,
expectedEditInTerminal: true,
},
{
filename: "file/with space",
lineNumber: 12,
osConfig: config.OSConfig{
EditPreset: "sublime",
},
expectedCmdStr: `subl -- "file/with space":12`,
expectedEditInTerminal: false,
},
}
for _, s := range scenarios {
userConfig := config.GetDefaultConfig()
userConfig.OS = s.osConfig
instance := buildFileCommands(commonDeps{
userConfig: userConfig,
})
cmdStr, editInTerminal := instance.GetEditAtLineCmdStr(s.filename, s.lineNumber)
assert.Equal(t, s.expectedCmdStr, cmdStr)
assert.Equal(t, s.expectedEditInTerminal, editInTerminal)
}
}
func TestEditFileAtLineAndWaitCmd(t *testing.T) {
type scenario struct {
filename string
lineNumber int
osConfig config.OSConfig
expectedCmdStr string
}
scenarios := []scenario{
{
filename: "test",
lineNumber: 42,
osConfig: config.OSConfig{},
expectedCmdStr: `vim +42 -- "test"`,
},
{
filename: "file/with space",
lineNumber: 12,
osConfig: config.OSConfig{
EditPreset: "sublime",
},
expectedCmdStr: `subl --wait -- "file/with space":12`,
},
}
for _, s := range scenarios {
userConfig := config.GetDefaultConfig()
userConfig.OS = s.osConfig
instance := buildFileCommands(commonDeps{
userConfig: userConfig,
})
cmdStr := instance.GetEditAtLineAndWaitCmdStr(s.filename, s.lineNumber)
assert.Equal(t, s.expectedCmdStr, cmdStr)
}
}
func TestGuessDefaultEditor(t *testing.T) {
type scenario struct {
gitConfigMockResponses map[string]string
getenv func(string) string
expectedResult string
}
scenarios := []scenario{
{
gitConfigMockResponses: nil,
getenv: func(env string) string {
return ""
},
expectedResult: "",
},
{
gitConfigMockResponses: map[string]string{"core.editor": "nano"},
getenv: func(env string) string {
return ""
},
expectedResult: "nano",
},
{
gitConfigMockResponses: map[string]string{"core.editor": "code -w"},
getenv: func(env string) string {
return ""
},
expectedResult: "code",
},
{
gitConfigMockResponses: nil,
getenv: func(env string) string {
if env == "VISUAL" {
return "emacs"
}
return ""
},
expectedResult: "emacs",
},
{
gitConfigMockResponses: nil,
getenv: func(env string) string {
if env == "EDITOR" {
return "bbedit -w"
}
return ""
},
expectedResult: "bbedit",
},
}
for _, s := range scenarios {
instance := buildFileCommands(commonDeps{
gitConfig: git_config.NewFakeGitConfig(s.gitConfigMockResponses),
getenv: s.getenv,
})
assert.Equal(t, s.expectedResult, instance.guessDefaultEditor())
}
}