-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
155 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,121 @@ | ||
package notes | ||
|
||
import ( | ||
"github.com/kballard/go-shellquote" | ||
"github.com/rhysd/go-fakeio" | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestOpenEditor(t *testing.T) { | ||
fake := fakeio.Stdout() | ||
defer fake.Restore() | ||
|
||
exe, err := exec.LookPath("echo") | ||
echo, err := exec.LookPath("echo") | ||
panicIfErr(err) | ||
|
||
cfg := &Config{ | ||
EditorPath: exe, | ||
HomePath: ".", | ||
} | ||
for _, tc := range []struct { | ||
what string | ||
editor string | ||
want string | ||
}{ | ||
{ | ||
what: "executable", | ||
editor: "echo", | ||
want: "foo bar\n", | ||
}, | ||
{ | ||
what: "executable path", | ||
editor: shellquote.Join(echo), // On Windows, path would contain 'Program Files' | ||
want: "foo bar\n", | ||
}, | ||
{ | ||
what: "multiple args", | ||
editor: "echo 'aaa' bbb", | ||
want: "aaa bbb foo bar\n", | ||
}, | ||
{ | ||
what: "arg contains white space", | ||
editor: "echo 'aaa bbb' 'ccc\tddd'", | ||
want: "aaa bbb ccc\tddd foo bar\n", | ||
}, | ||
{ | ||
what: "arg contains multiple white space", | ||
editor: "echo aaa bbb ", | ||
want: "aaa bbb foo bar\n", | ||
}, | ||
} { | ||
t.Run(tc.what, func(t *testing.T) { | ||
fake := fakeio.Stdout() | ||
defer fake.Restore() | ||
|
||
if err := openEditor(cfg, "foo", "bar"); err != nil { | ||
t.Fatal(err) | ||
} | ||
cfg := &Config{ | ||
EditorCmd: tc.editor, | ||
HomePath: ".", | ||
} | ||
|
||
have, err := fake.String() | ||
if err != nil { | ||
panic(err) | ||
} | ||
if err := openEditor(cfg, "foo", "bar"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if have != "foo bar\n" { | ||
t.Fatal("Arguments are unexpected:", have) | ||
} | ||
} | ||
have, err := fake.String() | ||
panicIfErr(err) | ||
|
||
func TestEditorPathNotSet(t *testing.T) { | ||
err := openEditor(&Config{}) | ||
if err == nil { | ||
t.Fatal("Error did not occur") | ||
} | ||
if !strings.Contains(err.Error(), "Editor is not set") { | ||
t.Fatal("Unexpected error:", err) | ||
if have != tc.want { | ||
t.Fatalf("Output from %q is unexpected. Wanted %q but have %q", tc.editor, tc.want, have) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestEditorExitError(t *testing.T) { | ||
func TestEditorInvalidEditor(t *testing.T) { | ||
exe, err := exec.LookPath("false") | ||
panicIfErr(err) | ||
exe = shellquote.Join(exe) | ||
|
||
err = openEditor(&Config{EditorPath: exe}) | ||
if err == nil { | ||
t.Fatal("Error did not occur") | ||
} | ||
if !strings.Contains(err.Error(), "Editor command did not exit successfully") { | ||
t.Fatal("Unexpected error:", err) | ||
for _, tc := range []struct { | ||
what string | ||
value string | ||
want string | ||
}{ | ||
{ | ||
what: "empty", | ||
value: "", | ||
want: "Editor is not set", | ||
}, | ||
{ | ||
what: "empty command", | ||
value: "''", | ||
want: "did not exit successfully", | ||
}, | ||
{ | ||
what: "unterminated single quote", | ||
value: "vim '-g", | ||
want: "Cannot parse editor command line", | ||
}, | ||
{ | ||
what: "unterminated double quote", | ||
value: "vim \"-g", | ||
want: "Cannot parse editor command line", | ||
}, | ||
{ | ||
what: "executable exit failure", | ||
value: exe, | ||
want: "did not exit successfully", | ||
}, | ||
{ | ||
what: "executable does not exist", | ||
value: "unknown-command-which-does-not-exist", | ||
want: "did not exit successfully", | ||
}, | ||
} { | ||
t.Run(tc.want, func(t *testing.T) { | ||
cfg := &Config{EditorCmd: tc.value} | ||
err := openEditor(cfg) | ||
if err == nil { | ||
t.Fatal("Error did not occur") | ||
} | ||
if !strings.Contains(err.Error(), tc.want) { | ||
t.Fatal("Unexpected error:", err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.