1
1
package plugin_test
2
2
3
3
import (
4
- "context"
5
- "errors"
6
4
"fmt"
7
- "os"
8
- "path/filepath"
9
5
"strings"
10
6
"testing"
11
7
12
8
"github.com/neovim/go-client/nvim"
9
+ "github.com/neovim/go-client/nvim/nvimtest"
13
10
"github.com/neovim/go-client/nvim/plugin"
14
11
)
15
12
16
- func newChildProcess (tb testing.TB ) (p * plugin.Plugin , cleanup func ()) {
17
- tb .Helper ()
18
-
19
- env := []string {}
20
- if v := os .Getenv ("VIM" ); v != "" {
21
- env = append (env , "VIM=" + v )
22
- }
23
-
24
- ctx := context .Background ()
25
- opts := []nvim.ChildProcessOption {
26
- nvim .ChildProcessCommand (nvim .BinaryName ),
27
- nvim .ChildProcessArgs ("-u" , "NONE" , "-n" , "-i" , "NONE" , "--embed" , "--headless" ),
28
- nvim .ChildProcessContext (ctx ),
29
- nvim .ChildProcessLogf (tb .Logf ),
30
- nvim .ChildProcessEnv (env ),
31
- }
32
- v , err := nvim .NewChildProcess (opts ... )
33
- if err != nil {
34
- tb .Fatal (err )
35
- }
36
-
37
- done := make (chan error , 1 )
38
- go func () {
39
- done <- v .Serve ()
40
- }()
41
-
42
- cleanup = func () {
43
- if err := v .Close (); err != nil {
44
- tb .Fatal (err )
45
- }
46
-
47
- err := <- done
48
- if err != nil {
49
- tb .Fatal (err )
50
- }
51
-
52
- const nvimlogFile = ".nvimlog"
53
- wd , err := os .Getwd ()
54
- if err != nil {
55
- tb .Fatal (err )
56
- }
57
- if walkErr := filepath .Walk (wd , func (path string , info os.FileInfo , err error ) error {
58
- if err != nil {
59
- return err
60
- }
61
-
62
- if info .IsDir () {
63
- return nil
64
- }
65
-
66
- if fname := info .Name (); fname == nvimlogFile {
67
- if err := os .RemoveAll (path ); err != nil {
68
- return fmt .Errorf ("failed to remove %s file: %w" , path , err )
69
- }
70
- }
71
-
72
- return nil
73
- }); walkErr != nil && ! os .IsNotExist (err ) {
74
- tb .Fatal (fmt .Errorf ("walkErr: %w" , errors .Unwrap (walkErr )))
75
- }
76
- }
77
-
78
- return plugin .New (v ), cleanup
79
- }
80
-
81
13
func TestRegister (t * testing.T ) {
82
- p , cleanup := newChildProcess (t )
83
- t .Cleanup (func () {
84
- cleanup ()
85
- })
14
+ p := plugin .New (nvimtest .NewChildProcess (t ))
86
15
87
16
p .Handle ("hello" , func (s string ) (string , error ) {
88
17
return "Hello, " + s , nil
89
18
})
19
+
90
20
p .HandleFunction (& plugin.FunctionOptions {Name : "Hello" }, func (args []string ) (string , error ) {
91
21
return "Hello, " + strings .Join (args , " " ), nil
92
22
})
93
23
94
24
if err := p .RegisterForTests (); err != nil {
95
- t .Fatal ( err )
25
+ t .Fatalf ( "register for test: %v" , err )
96
26
}
97
27
98
- {
99
- result , err := p .Nvim .CommandOutput (":echo Hello('John', 'Doe')" )
100
- if err != nil {
101
- t .Error (err )
102
- }
103
- expected := "Hello, John Doe"
104
- if result != expected {
105
- t .Errorf ("Hello returned %q, want %q" , result , expected )
106
- }
28
+ result , err := p .Nvim .Exec (`:echo Hello('John', 'Doe')` , true )
29
+ if err != nil {
30
+ t .Fatalf ("exec echo command: %v" , err )
31
+ }
32
+ expected := `Hello, John Doe`
33
+ if result != expected {
34
+ t .Fatalf ("Hello returned %q, want %q" , result , expected )
107
35
}
108
36
109
- {
110
- cid := p .Nvim .ChannelID ()
111
-
112
- var result string
113
- if err := p .Nvim .Call ("rpcrequest" , & result , cid , "hello" , "world" ); err != nil {
114
- t .Fatal (err )
115
- }
116
-
117
- expected := "Hello, world"
118
- if result != expected {
119
- t .Errorf ("hello returned %q, want %q" , result , expected )
120
- }
37
+ cid := p .Nvim .ChannelID ()
38
+ var result2 string
39
+ if err := p .Nvim .Call ("rpcrequest" , & result2 , cid , "hello" , "world" ); err != nil {
40
+ t .Fatalf ("call rpcrequest(%v, %v, %v, %v): %v" , & result2 , cid , "hello" , "world" , err )
41
+ }
42
+ expected2 := "Hello, world"
43
+ if result2 != expected2 {
44
+ t .Fatalf ("hello returned %q, want %q" , result2 , expected2 )
121
45
}
122
46
}
123
47
124
48
func TestSubscribe (t * testing.T ) {
125
- p , cleanup := newChildProcess (t )
126
- t .Cleanup (func () {
127
- cleanup ()
128
- })
49
+ p := plugin .New (nvimtest .NewChildProcess (t ))
129
50
130
51
const event1 = "event1"
131
52
eventFn1 := func (t * testing.T , v * nvim.Nvim ) error {
@@ -141,6 +62,8 @@ func TestSubscribe(t *testing.T) {
141
62
}
142
63
})
143
64
}
65
+ p .Handle (event1 , func () error { return eventFn1 (t , p .Nvim ) })
66
+
144
67
const event2 = "event2"
145
68
eventFn2 := func (t * testing.T , v * nvim.Nvim ) error {
146
69
return v .RegisterHandler (event1 , func (event ... interface {}) {
@@ -155,60 +78,61 @@ func TestSubscribe(t *testing.T) {
155
78
}
156
79
})
157
80
}
158
- p .Handle (event1 , func () error { return eventFn1 (t , p .Nvim ) })
159
81
p .Handle (event2 , func () error { return eventFn2 (t , p .Nvim ) })
160
82
161
83
if err := p .RegisterForTests (); err != nil {
162
- t .Fatal ( err )
84
+ t .Fatalf ( "register for test: %v" , err )
163
85
}
164
86
165
87
if err := p .Nvim .Subscribe (event1 ); err != nil {
166
- t .Fatal ( err )
88
+ t .Fatalf ( "subscribe(%v): %v" , event1 , err )
167
89
}
90
+
168
91
b := p .Nvim .NewBatch ()
169
92
b .Subscribe (event2 )
170
93
if err := b .Execute (); err != nil {
171
- t .Fatal ( err )
94
+ t .Fatalf ( "batch execute: %v" , err )
172
95
}
173
96
174
97
// warm-up
175
98
var result int
176
99
if err := p .Nvim .Eval (fmt .Sprintf (`rpcnotify(0, %q)` , event1 ), & result ); err != nil {
177
- t .Fatal ( err )
100
+ t .Fatalf ( "eval rpcnotify for warm-up of event1: %v" , err )
178
101
}
179
102
if result != 1 {
180
103
t .Fatalf ("expect 1 but got %d" , result )
181
104
}
182
105
183
106
var result2 int
184
107
if err := p .Nvim .Eval (fmt .Sprintf (`rpcnotify(0, %q, 1, 2, 3)` , event1 ), & result2 ); err != nil {
185
- t .Fatal ( err )
108
+ t .Fatalf ( "eval rpcnotify for event1: %v" , err )
186
109
}
187
110
if result2 != 1 {
188
111
t .Fatalf ("expect 1 but got %d" , result2 )
189
112
}
190
113
191
114
var result3 int
192
115
if err := p .Nvim .Eval (fmt .Sprintf (`rpcnotify(0, %q, 4, 5, 6)` , event2 ), & result3 ); err != nil {
193
- t .Fatal ( err )
116
+ t .Fatalf ( "eval rpcnotify for event2: %v" , err )
194
117
}
195
118
if result3 != 1 {
196
119
t .Fatalf ("expect 1 but got %d" , result3 )
197
120
}
198
121
199
122
if err := p .Nvim .Unsubscribe (event1 ); err != nil {
200
- t .Fatal ( err )
123
+ t .Fatalf ( "unsubscribe event1: %v" , err )
201
124
}
125
+
202
126
b .Unsubscribe (event2 )
203
127
if err := b .Execute (); err != nil {
204
- t .Fatal ( err )
128
+ t .Fatalf ( "unsubscribe event2: %v" , err )
205
129
}
206
130
207
131
if err := p .Nvim .Eval (fmt .Sprintf (`rpcnotify(0, %q, 7, 8, 9)` , event1 ), nil ); err != nil {
208
- t .Fatal ( err )
132
+ t .Fatalf ( "ensure rpcnotify to event1 is no-op: %v" , err )
209
133
}
210
134
211
135
if err := p .Nvim .Eval (fmt .Sprintf (`rpcnotify(0, %q, 10, 11, 12)` , event2 ), nil ); err != nil {
212
- t .Fatal ( err )
136
+ t .Fatalf ( "ensure rpcnotify to event2 is no-op: %v" , err )
213
137
}
214
138
}
0 commit comments