summaryrefslogtreecommitdiff
path: root/internal/editor
diff options
context:
space:
mode:
Diffstat (limited to 'internal/editor')
-rw-r--r--internal/editor/editor.go36
-rw-r--r--internal/editor/editor_test.go56
2 files changed, 50 insertions, 42 deletions
diff --git a/internal/editor/editor.go b/internal/editor/editor.go
index 9a9e737..aad7047 100644
--- a/internal/editor/editor.go
+++ b/internal/editor/editor.go
@@ -21,11 +21,25 @@ func Resolve() (string, error) {
return ed, nil
}
-// RunEditor is the seam that invokes the editor on the given file path.
-// Override in tests to avoid launching a real editor. It uses
-// exec.CommandContext so a cancelled ctx (e.g. process shutdown) kills the
-// editor subprocess instead of leaving it blocking on terminal input.
-var RunEditor = func(ctx context.Context, editor, path string) error {
+type Runner struct {
+ runEditor func(context.Context, string, string) error
+}
+
+func (r Runner) edit(ctx context.Context, editor, path string) error {
+ if r.runEditor != nil {
+ return r.runEditor(ctx, editor, path)
+ }
+ return runEditor(ctx, editor, path)
+}
+
+// RunEditor invokes the editor on the given file path. It uses
+// exec.CommandContext so a cancelled ctx kills the editor subprocess instead
+// of leaving it blocking on terminal input.
+func RunEditor(ctx context.Context, editor, path string) error {
+ return runEditor(ctx, editor, path)
+}
+
+func runEditor(ctx context.Context, editor, path string) error {
cmd := exec.CommandContext(ctx, editor, path)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
@@ -38,6 +52,10 @@ var RunEditor = func(ctx context.Context, editor, path string) error {
// Returns the trimmed content. ctx is forwarded to the editor subprocess so it
// can be cancelled along with the surrounding command.
func OpenTempAndEdit(ctx context.Context, initial []byte) (string, error) {
+ return Runner{}.OpenTempAndEdit(ctx, initial)
+}
+
+func (r Runner) OpenTempAndEdit(ctx context.Context, initial []byte) (string, error) {
ed, err := Resolve()
if err != nil {
return "", err
@@ -63,7 +81,7 @@ func OpenTempAndEdit(ctx context.Context, initial []byte) (string, error) {
if err := f.Close(); err != nil {
return "", err
}
- if err := RunEditor(ctx, ed, path); err != nil {
+ if err := r.edit(ctx, ed, path); err != nil {
return "", err
}
b, err := os.ReadFile(filepath.Clean(path))
@@ -77,6 +95,10 @@ func OpenTempAndEdit(ctx context.Context, initial []byte) (string, error) {
// from Resolve() (HEXAI_EDITOR or EDITOR). ctx is forwarded to the editor
// subprocess so it can be cancelled with the surrounding command.
func OpenFile(ctx context.Context, path string) error {
+ return Runner{}.OpenFile(ctx, path)
+}
+
+func (r Runner) OpenFile(ctx context.Context, path string) error {
ed, err := Resolve()
if err != nil {
return err
@@ -91,5 +113,5 @@ func OpenFile(ctx context.Context, path string) error {
return mkErr
}
}
- return RunEditor(ctx, ed, path)
+ return r.edit(ctx, ed, path)
}
diff --git a/internal/editor/editor_test.go b/internal/editor/editor_test.go
index f2c1e22..543235e 100644
--- a/internal/editor/editor_test.go
+++ b/internal/editor/editor_test.go
@@ -75,17 +75,15 @@ func TestResolve_WhitespaceOnly(t *testing.T) {
}
func TestOpenTempAndEdit_UsesRunEditor(t *testing.T) {
- old := RunEditor
- t.Cleanup(func() { RunEditor = old })
// Ensure Resolve() succeeds
t.Setenv("HEXAI_EDITOR", "dummy")
var capturedPath string
- RunEditor = func(_ context.Context, editor, path string) error {
+ r := Runner{runEditor: func(_ context.Context, editor, path string) error {
capturedPath = path
// simulate user writing content
return os.WriteFile(path, []byte("Hello\nWorld\n"), 0o600)
- }
- out, err := OpenTempAndEdit(context.Background(), []byte("# Start\n\n"))
+ }}
+ out, err := r.OpenTempAndEdit(context.Background(), []byte("# Start\n\n"))
if err != nil {
t.Fatalf("OpenTempAndEdit: %v", err)
}
@@ -109,14 +107,12 @@ func TestOpenTempAndEdit_NoEditor(t *testing.T) {
// TestOpenTempAndEdit_NilInitial verifies that nil initial content works (empty file).
func TestOpenTempAndEdit_NilInitial(t *testing.T) {
- old := RunEditor
- t.Cleanup(func() { RunEditor = old })
t.Setenv("HEXAI_EDITOR", "dummy")
- RunEditor = func(_ context.Context, editor, path string) error {
+ r := Runner{runEditor: func(_ context.Context, editor, path string) error {
// simulate user writing content into a file that started empty
return os.WriteFile(path, []byte("result"), 0o600)
- }
- out, err := OpenTempAndEdit(context.Background(), nil)
+ }}
+ out, err := r.OpenTempAndEdit(context.Background(), nil)
if err != nil {
t.Fatalf("OpenTempAndEdit with nil initial: %v", err)
}
@@ -128,13 +124,11 @@ func TestOpenTempAndEdit_NilInitial(t *testing.T) {
// TestOpenTempAndEdit_EmptyInitial verifies that empty (zero-length) initial content
// skips the write branch but still works end-to-end.
func TestOpenTempAndEdit_EmptyInitial(t *testing.T) {
- old := RunEditor
- t.Cleanup(func() { RunEditor = old })
t.Setenv("HEXAI_EDITOR", "dummy")
- RunEditor = func(_ context.Context, editor, path string) error {
+ r := Runner{runEditor: func(_ context.Context, editor, path string) error {
return os.WriteFile(path, []byte(" trimmed "), 0o600)
- }
- out, err := OpenTempAndEdit(context.Background(), []byte{})
+ }}
+ out, err := r.OpenTempAndEdit(context.Background(), []byte{})
if err != nil {
t.Fatalf("OpenTempAndEdit with empty initial: %v", err)
}
@@ -145,14 +139,12 @@ func TestOpenTempAndEdit_EmptyInitial(t *testing.T) {
// TestOpenTempAndEdit_EditorError verifies that an editor failure propagates the error.
func TestOpenTempAndEdit_EditorError(t *testing.T) {
- old := RunEditor
- t.Cleanup(func() { RunEditor = old })
t.Setenv("HEXAI_EDITOR", "dummy")
editorErr := errors.New("editor crashed")
- RunEditor = func(_ context.Context, editor, path string) error {
+ r := Runner{runEditor: func(_ context.Context, editor, path string) error {
return editorErr
- }
- _, err := OpenTempAndEdit(context.Background(), []byte("some content"))
+ }}
+ _, err := r.OpenTempAndEdit(context.Background(), []byte("some content"))
if err == nil {
t.Fatal("expected error when editor fails")
}
@@ -163,14 +155,12 @@ func TestOpenTempAndEdit_EditorError(t *testing.T) {
// TestOpenTempAndEdit_EditorDeletesFile verifies error when the editor removes the temp file.
func TestOpenTempAndEdit_EditorDeletesFile(t *testing.T) {
- old := RunEditor
- t.Cleanup(func() { RunEditor = old })
t.Setenv("HEXAI_EDITOR", "dummy")
- RunEditor = func(_ context.Context, editor, path string) error {
+ r := Runner{runEditor: func(_ context.Context, editor, path string) error {
// simulate the editor deleting the file
return os.Remove(path)
- }
- _, err := OpenTempAndEdit(context.Background(), []byte("content"))
+ }}
+ _, err := r.OpenTempAndEdit(context.Background(), []byte("content"))
if err == nil {
t.Fatal("expected error when temp file is deleted by editor")
}
@@ -178,15 +168,13 @@ func TestOpenTempAndEdit_EditorDeletesFile(t *testing.T) {
// TestOpenTempAndEdit_TempFileCleanup verifies the temp file is removed after success.
func TestOpenTempAndEdit_TempFileCleanup(t *testing.T) {
- old := RunEditor
- t.Cleanup(func() { RunEditor = old })
t.Setenv("HEXAI_EDITOR", "dummy")
var capturedPath string
- RunEditor = func(_ context.Context, editor, path string) error {
+ r := Runner{runEditor: func(_ context.Context, editor, path string) error {
capturedPath = path
return os.WriteFile(path, []byte("done"), 0o600)
- }
- _, err := OpenTempAndEdit(context.Background(), nil)
+ }}
+ _, err := r.OpenTempAndEdit(context.Background(), nil)
if err != nil {
t.Fatalf("OpenTempAndEdit: %v", err)
}
@@ -197,17 +185,15 @@ func TestOpenTempAndEdit_TempFileCleanup(t *testing.T) {
}
func TestOpenFile_CreatesParentAndInvokesEditor(t *testing.T) {
- old := RunEditor
- t.Cleanup(func() { RunEditor = old })
t.Setenv("HEXAI_EDITOR", "dummy")
target := filepath.Join(t.TempDir(), "nested", "config.toml")
var gotEditor, gotPath string
- RunEditor = func(_ context.Context, editorCmd, path string) error {
+ r := Runner{runEditor: func(_ context.Context, editorCmd, path string) error {
gotEditor = editorCmd
gotPath = path
return nil
- }
- if err := OpenFile(context.Background(), target); err != nil {
+ }}
+ if err := r.OpenFile(context.Background(), target); err != nil {
t.Fatalf("OpenFile: %v", err)
}
if gotEditor != "dummy" {