summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git_commands/file_test.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2022-01-08 14:00:36 +1100
committerJesse Duffield <jessedduffield@gmail.com>2022-01-09 14:09:53 +1100
commitc9a0cc6b30dca6ff6c520268c10afff4e99a68e9 (patch)
tree74a03be28aafb5fba5c8391ca611aeb25ca89445 /pkg/commands/git_commands/file_test.go
parent3621854dc79baf2064b00b561ebea0ecc8fcb5df (diff)
refactor
Diffstat (limited to 'pkg/commands/git_commands/file_test.go')
-rw-r--r--pkg/commands/git_commands/file_test.go163
1 files changed, 163 insertions, 0 deletions
diff --git a/pkg/commands/git_commands/file_test.go b/pkg/commands/git_commands/file_test.go
new file mode 100644
index 000000000..a26699b3e
--- /dev/null
+++ b/pkg/commands/git_commands/file_test.go
@@ -0,0 +1,163 @@
+package git_commands
+
+import (
+ "testing"
+
+ "github.com/go-errors/errors"
+ "github.com/jesseduffield/lazygit/pkg/commands/git_config"
+ "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
+ "github.com/jesseduffield/lazygit/pkg/config"
+ "github.com/stretchr/testify/assert"
+)
+
+func TestEditFileCmdStr(t *testing.T) {
+ type scenario struct {
+ filename string
+ configEditCommand string
+ configEditCommandTemplate string
+ runner *oscommands.FakeCmdObjRunner
+ getenv func(string) string
+ gitConfigMockResponses map[string]string
+ test func(string, error)
+ }
+
+ scenarios := []scenario{
+ {
+ filename: "test",
+ configEditCommand: "",
+ configEditCommandTemplate: "{{editor}} {{filename}}",
+ runner: oscommands.NewFakeRunner(t).
+ Expect(`which vi`, "", errors.New("error")),
+ getenv: func(env string) string {
+ return ""
+ },
+ gitConfigMockResponses: nil,
+ test: func(cmdStr string, err error) {
+ assert.EqualError(t, err, "No editor defined in config file, $GIT_EDITOR, $VISUAL, $EDITOR, or git config")
+ },
+ },
+ {
+ filename: "test",
+ configEditCommand: "nano",
+ configEditCommandTemplate: "{{editor}} {{filename}}",
+ 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, `nano "test"`, cmdStr)
+ },
+ },
+ {
+ filename: "test",
+ configEditCommand: "",
+ configEditCommandTemplate: "{{editor}} {{filename}}",
+ runner: oscommands.NewFakeRunner(t),
+ getenv: func(env string) string {
+ return ""
+ },
+ gitConfigMockResponses: map[string]string{"core.editor": "nano"},
+ test: func(cmdStr string, err error) {
+ assert.NoError(t, err)
+ assert.Equal(t, `nano "test"`, cmdStr)
+ },
+ },
+ {
+ filename: "test",
+ configEditCommand: "",
+ configEditCommandTemplate: "{{editor}} {{filename}}",
+ runner: oscommands.NewFakeRunner(t),
+ getenv: func(env string) string {
+ if env == "VISUAL" {
+ return "nano"
+ }
+
+ return ""
+ },
+ gitConfigMockResponses: nil,
+ test: func(cmdStr string, err error) {
+ assert.NoError(t, err)
+ },
+ },
+ {
+ filename: "test",
+ configEditCommand: "",
+ configEditCommandTemplate: "{{editor}} {{filename}}",
+ runner: oscommands.NewFakeRunner(t),
+ getenv: func(env string) string {
+ if env == "EDITOR" {
+ return "emacs"
+ }
+
+ return ""
+ },
+ gitConfigMockResponses: nil,
+ test: func(cmdStr string, err error) {
+ assert.NoError(t, err)
+ assert.Equal(t, `emacs "test"`, cmdStr)
+ },
+ },
+ {
+ filename: "test",
+ configEditCommand: "",
+ configEditCommandTemplate: "{{editor}} {{filename}}",
+ runner: oscommands.NewFakeRunner(t).
+ Expect(`which vi`, "/usr/bin/vi", nil),
+ getenv: func(env string) string {
+ return ""
+ },
+ gitConfigMockResponses: nil,
+ test: func(cmdStr string, err error) {
+ assert.NoError(t, err)
+ assert.Equal(t, `vi "test"`, cmdStr)
+ },
+ },
+ {
+ filename: "file/with space",
+ configEditCommand: "",
+ configEditCommandTemplate: "{{editor}} {{filename}}",
+ runner: oscommands.NewFakeRunner(t).
+ Expect(`which vi`, "/usr/bin/vi", nil),
+ getenv: func(env string) string {
+ return ""
+ },
+ gitConfigMockResponses: nil,
+ test: func(cmdStr string, err error) {
+ assert.NoError(t, err)
+ assert.Equal(t, `vi "file/with space"`, cmdStr)
+ },
+ },
+ {
+ filename: "open file/at line",
+ configEditCommand: "vim",
+ configEditCommandTemplate: "{{editor}} +{{line}} {{filename}}",
+ 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 "open file/at line"`, cmdStr)
+ },
+ },
+ }
+
+ for _, s := range scenarios {
+ 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,
+ })
+
+ s.test(instance.GetEditCmdStr(s.filename, 1))
+ s.runner.CheckForMissingCalls()
+ }
+}