summaryrefslogtreecommitdiff
path: root/internal/editor/editor_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/editor/editor_test.go')
-rw-r--r--internal/editor/editor_test.go56
1 files changed, 21 insertions, 35 deletions
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" {