mirror of
https://github.com/ollama/ollama.git
synced 2026-10-22 03:22:02 +00:00
cmd: guard empty editor before indexing fields (#17067)
Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
This commit is contained in:
parent
fb30760996
commit
02dc3ea4c3
2 changed files with 39 additions and 4 deletions
|
|
@ -653,9 +653,12 @@ func editInExternalEditor(content string) (string, error) {
|
|||
}
|
||||
|
||||
// Check that the editor binary exists
|
||||
name := strings.Fields(editor)[0]
|
||||
if _, err := exec.LookPath(name); err != nil {
|
||||
return "", fmt.Errorf("editor %q not found, set OLLAMA_EDITOR to the path of your preferred editor", name)
|
||||
args := strings.Fields(editor)
|
||||
if len(args) == 0 {
|
||||
return "", fmt.Errorf("no editor configured, set OLLAMA_EDITOR to the path of your preferred editor")
|
||||
}
|
||||
if _, err := exec.LookPath(args[0]); err != nil {
|
||||
return "", fmt.Errorf("editor %q not found, set OLLAMA_EDITOR to the path of your preferred editor", args[0])
|
||||
}
|
||||
|
||||
tmpFile, err := os.CreateTemp("", "ollama-prompt-*.txt")
|
||||
|
|
@ -672,7 +675,6 @@ func editInExternalEditor(content string) (string, error) {
|
|||
}
|
||||
tmpFile.Close()
|
||||
|
||||
args := strings.Fields(editor)
|
||||
args = append(args, tmpFile.Name())
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Stdin = os.Stdin
|
||||
|
|
|
|||
|
|
@ -85,6 +85,39 @@ func TestExtractFileDataRemovesQuotedFilepath(t *testing.T) {
|
|||
assert.Equal(t, cleaned, "before after")
|
||||
}
|
||||
|
||||
func TestEditInExternalEditorWhitespaceOnly(t *testing.T) {
|
||||
// A whitespace-only VISUAL or EDITOR is non-empty, so it bypasses the
|
||||
// editor == "" fallbacks, but strings.Fields collapses it to an empty
|
||||
// slice. Indexing that slice must not panic; it must return an error.
|
||||
cases := []struct {
|
||||
name string
|
||||
visual string
|
||||
editor string
|
||||
}{
|
||||
{name: "VISUAL whitespace", visual: "\t "},
|
||||
{name: "EDITOR whitespace", editor: "\t "},
|
||||
}
|
||||
for _, tt := range cases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Setenv("OLLAMA_EDITOR", "")
|
||||
t.Setenv("VISUAL", tt.visual)
|
||||
t.Setenv("EDITOR", tt.editor)
|
||||
_, err := editInExternalEditor("content")
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEditInExternalEditorParsesEditorWithArgs(t *testing.T) {
|
||||
// A well-formed editor command with arguments must still be parsed so
|
||||
// its binary is looked up (guards the normal path from regressing).
|
||||
t.Setenv("OLLAMA_EDITOR", "definitely-not-a-real-editor arg1")
|
||||
t.Setenv("VISUAL", "")
|
||||
t.Setenv("EDITOR", "")
|
||||
_, err := editInExternalEditor("content")
|
||||
assert.ErrorContains(t, err, "definitely-not-a-real-editor")
|
||||
}
|
||||
|
||||
func TestExtractFileDataWAV(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
fp := filepath.Join(dir, "sample.wav")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue