-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditor_test.go
More file actions
140 lines (116 loc) · 3.35 KB
/
editor_test.go
File metadata and controls
140 lines (116 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"os"
"testing"
)
func TestNewEditor_ValidPath(t *testing.T) {
// Use a real executable from the system
editorPath := "/bin/sh"
if _, err := os.Stat(editorPath); os.IsNotExist(err) {
t.Skip("sh not available")
}
editor, err := NewEditor(editorPath)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if editor.path != editorPath {
t.Errorf("expected path=%s, got %s", editorPath, editor.path)
}
}
func TestNewEditor_NonExistentPath(t *testing.T) {
_, err := NewEditor("/nonexistent/path/to/editor")
if err == nil {
t.Error("expected error for non-existent editor, got nil")
}
}
func TestNewEditor_DirectoryPath(t *testing.T) {
_, err := NewEditor("/tmp")
if err == nil {
t.Error("expected error for directory path, got nil")
}
}
func TestNewEditor_NonExecutablePath(t *testing.T) {
// Create a temporary non-executable file
tmpFile, err := os.CreateTemp("", "non-executable-*")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())
tmpFile.Close()
_, err = NewEditor(tmpFile.Name())
if err == nil {
t.Error("expected error for non-executable file, got nil")
}
}
func TestNewEditor_FromEnv(t *testing.T) {
// Create a temporary executable file
tmpFile, err := os.CreateTemp("", "executable-*")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())
tmpFile.Close()
// Make it executable
if err := os.Chmod(tmpFile.Name(), 0o755); err != nil {
t.Fatalf("failed to chmod file: %v", err)
}
// Save and restore original EDITOR
originalEditor := os.Getenv("EDITOR")
defer os.Setenv("EDITOR", originalEditor)
os.Setenv("EDITOR", tmpFile.Name())
editor, err := NewEditor("")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if editor.path != tmpFile.Name() {
t.Errorf("expected path=%s, got %s", tmpFile.Name(), editor.path)
}
}
func TestNewEditor_NoEnv(t *testing.T) {
// Save and restore original EDITOR
originalEditor := os.Getenv("EDITOR")
defer os.Setenv("EDITOR", originalEditor)
os.Setenv("EDITOR", "")
_, err := NewEditor("")
if err == nil {
t.Error("expected error when EDITOR not set, got nil")
}
}
func TestEditorOpen_NonExistentFile(t *testing.T) {
editorPath := "/bin/sh"
if _, err := os.Stat(editorPath); os.IsNotExist(err) {
t.Skip("sh not available")
}
editor, err := NewEditor(editorPath)
if err != nil {
t.Fatalf("failed to create editor: %v", err)
}
// Try to open a non-existent file (sh will fail to open it)
err = editor.Open("/nonexistent/file/path")
if err == nil {
t.Error("expected error when opening non-existent file, got nil")
}
}
func TestEditorOpen_ValidFile(t *testing.T) {
editorPath := "/bin/sh"
if _, err := os.Stat(editorPath); os.IsNotExist(err) {
t.Skip("sh not available")
}
editor, err := NewEditor(editorPath)
if err != nil {
t.Fatalf("failed to create editor: %v", err)
}
// Create a temporary file
tmpFile, err := os.CreateTemp("", "editor-test-*")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())
tmpFile.Close()
// This should execute sh (with no arguments) which will try to read from stdin
// and immediately hit EOF, returning successfully
err = editor.Open(tmpFile.Name())
if err != nil {
t.Logf("editor.Open returned error: %v (this is expected for sh with no args)", err)
}
}