Skip to content

Commit d3a4558

Browse files
committed
test: TestEditorParsing
(cherry picked from commit e44b53a)
1 parent 9545368 commit d3a4558

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

core/commands/config.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,18 @@ func setConfig(r repo.Repo, key string, value interface{}) (*ConfigField, error)
506506
return getConfig(r, key)
507507
}
508508

509+
// parseEditorCommand parses the EDITOR environment variable into command and arguments
510+
func parseEditorCommand(editor string) ([]string, error) {
511+
return shlex.Split(editor, true)
512+
}
513+
509514
func editConfig(filename string) error {
510515
editor := os.Getenv("EDITOR")
511516
if editor == "" {
512517
return errors.New("ENV variable $EDITOR not set")
513518
}
514519

515-
editorAndArgs, err := shlex.Split(editor, true)
520+
editorAndArgs, err := parseEditorCommand(editor)
516521
if err != nil {
517522
return fmt.Errorf("cannot parse $EDITOR value: %s", err)
518523
}

core/commands/config_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,116 @@ func TestScrubMapInternalDelete(t *testing.T) {
1414
t.Errorf("expecting an empty map, got a non-empty map")
1515
}
1616
}
17+
18+
func TestEditorParsing(t *testing.T) {
19+
testCases := []struct {
20+
name string
21+
input string
22+
expected []string
23+
hasError bool
24+
}{
25+
{
26+
name: "simple editor",
27+
input: "vim",
28+
expected: []string{"vim"},
29+
hasError: false,
30+
},
31+
{
32+
name: "editor with single flag",
33+
input: "emacs -nw",
34+
expected: []string{"emacs", "-nw"},
35+
hasError: false,
36+
},
37+
{
38+
name: "VS Code with wait flag (issue #9375)",
39+
input: "code --wait",
40+
expected: []string{"code", "--wait"},
41+
hasError: false,
42+
},
43+
{
44+
name: "VS Code with full path and wait flag (issue #9375)",
45+
input: "/opt/homebrew/bin/code --wait",
46+
expected: []string{"/opt/homebrew/bin/code", "--wait"},
47+
hasError: false,
48+
},
49+
{
50+
name: "editor with quoted path containing spaces",
51+
input: "\"/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code\" --wait",
52+
expected: []string{"/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code", "--wait"},
53+
hasError: false,
54+
},
55+
{
56+
name: "sublime text with wait flag",
57+
input: "subl -w",
58+
expected: []string{"subl", "-w"},
59+
hasError: false,
60+
},
61+
{
62+
name: "nano editor",
63+
input: "nano",
64+
expected: []string{"nano"},
65+
hasError: false,
66+
},
67+
{
68+
name: "gedit editor",
69+
input: "gedit",
70+
expected: []string{"gedit"},
71+
hasError: false,
72+
},
73+
{
74+
name: "editor with multiple flags",
75+
input: "vim -c 'set number' -c 'set hlsearch'",
76+
expected: []string{"vim", "-c", "set number", "-c", "set hlsearch"},
77+
hasError: false,
78+
},
79+
{
80+
name: "trailing backslash (POSIX edge case)",
81+
input: "editor\\",
82+
expected: nil,
83+
hasError: true,
84+
},
85+
{
86+
name: "double quoted editor name with spaces",
87+
input: "\"code with spaces\" --wait",
88+
expected: []string{"code with spaces", "--wait"},
89+
hasError: false,
90+
},
91+
{
92+
name: "single quoted editor with flags",
93+
input: "'my editor' -flag",
94+
expected: []string{"my editor", "-flag"},
95+
hasError: false,
96+
},
97+
}
98+
99+
for _, tc := range testCases {
100+
t.Run(tc.name, func(t *testing.T) {
101+
result, err := parseEditorCommand(tc.input)
102+
103+
if tc.hasError {
104+
if err == nil {
105+
t.Errorf("Expected error for input '%s', but got none", tc.input)
106+
}
107+
return
108+
}
109+
110+
if err != nil {
111+
t.Errorf("Unexpected error for input '%s': %v", tc.input, err)
112+
return
113+
}
114+
115+
if len(result) != len(tc.expected) {
116+
t.Errorf("Expected %d args, got %d for input '%s'", len(tc.expected), len(result), tc.input)
117+
t.Errorf("Expected: %v", tc.expected)
118+
t.Errorf("Got: %v", result)
119+
return
120+
}
121+
122+
for i, expected := range tc.expected {
123+
if result[i] != expected {
124+
t.Errorf("Expected arg %d to be '%s', got '%s' for input '%s'", i, expected, result[i], tc.input)
125+
}
126+
}
127+
})
128+
}
129+
}

0 commit comments

Comments
 (0)