mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
Open in editor split $VISUAL and $EDITOR on whitespace, so a Windows path such as "C:\Program Files\Notepad++\notepad++.exe" broke at the space and no editor was found. A user asked to open files in Notepad++, which is not on PATH by default (#538). Double or single quotes now keep a field together. Backslashes stay literal, since Windows paths use them.
116 lines
3.4 KiB
Go
116 lines
3.4 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestHandleOpenInEditor_UsesEchoAsEditor(t *testing.T) {
|
|
// Use auto-detect via $EDITOR so we don't need the binary in the whitelist.
|
|
t.Setenv("VISUAL", "")
|
|
t.Setenv("EDITOR", "true")
|
|
|
|
s, src := newTestServer(t)
|
|
addSkill(t, src, "my-skill")
|
|
|
|
body := openInEditorRequest{Editor: "auto"}
|
|
raw, _ := json.Marshal(body)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/resources/my-skill/open-in-editor", bytes.NewReader(raw))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
var resp openInEditorResponse
|
|
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if resp.PID == 0 {
|
|
t.Errorf("expected pid > 0, got %d", resp.PID)
|
|
}
|
|
if resp.Editor != "true" {
|
|
t.Errorf("expected editor=true, got %s", resp.Editor)
|
|
}
|
|
}
|
|
|
|
func TestHandleOpenInEditor_NotFound(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
raw, _ := json.Marshal(openInEditorRequest{Editor: "true"})
|
|
req := httptest.NewRequest(http.MethodPost, "/api/resources/no-skill/open-in-editor", bytes.NewReader(raw))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusNotFound {
|
|
t.Fatalf("expected 404, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleOpenInEditor_HeadlessRefuses(t *testing.T) {
|
|
t.Setenv("SKILLSHARE_HEADLESS", "1")
|
|
s, src := newTestServer(t)
|
|
addSkill(t, src, "my-skill")
|
|
|
|
raw, _ := json.Marshal(openInEditorRequest{Editor: "true"})
|
|
req := httptest.NewRequest(http.MethodPost, "/api/resources/my-skill/open-in-editor", bytes.NewReader(raw))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusConflict {
|
|
t.Fatalf("expected 409, got %d", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestHandleOpenInEditor_UnknownBinary(t *testing.T) {
|
|
s, src := newTestServer(t)
|
|
addSkill(t, src, "my-skill")
|
|
|
|
raw, _ := json.Marshal(openInEditorRequest{Editor: "this-is-not-a-real-editor-xyz"})
|
|
req := httptest.NewRequest(http.MethodPost, "/api/resources/my-skill/open-in-editor", bytes.NewReader(raw))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusConflict {
|
|
t.Fatalf("expected 409, got %d", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestPickEditor_AutoPrefersEditorEnv(t *testing.T) {
|
|
t.Setenv("VISUAL", "")
|
|
t.Setenv("EDITOR", "true")
|
|
|
|
cand, name, err := pickEditor("auto")
|
|
if err != nil {
|
|
t.Fatalf("pickEditor: %v", err)
|
|
}
|
|
if cand.bin != "true" || name != "true" {
|
|
t.Errorf("expected bin=true, got bin=%s name=%s", cand.bin, name)
|
|
}
|
|
}
|
|
|
|
func TestSplitCommand(t *testing.T) {
|
|
head, rest := splitCommand("code --wait --new-window")
|
|
if head != "code" {
|
|
t.Errorf("expected head=code, got %s", head)
|
|
}
|
|
if len(rest) != 2 || rest[0] != "--wait" || rest[1] != "--new-window" {
|
|
t.Errorf("unexpected rest: %v", rest)
|
|
}
|
|
}
|
|
|
|
func TestSplitCommand_QuotedPath(t *testing.T) {
|
|
for value, want := range map[string][]string{
|
|
`"C:\Program Files\Notepad++\notepad++.exe" -multiInst`: {`C:\Program Files\Notepad++\notepad++.exe`, "-multiInst"},
|
|
`'/Applications/My Editor.app/edit'`: {"/Applications/My Editor.app/edit"},
|
|
} {
|
|
head, rest := splitCommand(value)
|
|
if got := append([]string{head}, rest...); strings.Join(got, "|") != strings.Join(want, "|") {
|
|
t.Errorf("%s: got %q, want %q", value, got, want)
|
|
}
|
|
}
|
|
}
|